davideicardi / live-plugin-manager

Plugin manager and installer for Node.JS
MIT License
242 stars 44 forks source link

Problem installing mathjs with plugin-manager #31

Open JeffSpies opened 4 years ago

JeffSpies commented 4 years ago

First off, wow: amazing package!

Just discovered it and am testing it out to manage plugins for an electron app, but I ran into problems with one package in particular: mathjs. Here's a chunk of code:

  await manager.install('mathjs')
  const m = manager.require('mathjs')
  console.log(m.evaluate('7 + 12'))

Ther error is actually coming from dependency typed-function, which seems to provide typing for math.js:

 UnhandledPromiseRejectionWarning: .../typed-function/typed-function.js:128
        throw new TypeError('Unknown type "' + typeName + '"' +
        ^

  TypeError: Unknown type "Function". Did you mean "function"?

I looked through your known issues and checked those packages, but I'm not seeing why it wouldn't work when other packages do. Any immediate ideas or debugging tips in general? Thanks!

https://github.com/josdejong/mathjs https://github.com/josdejong/typed-function

davideicardi commented 4 years ago

Thanks for the feedback. Very strange error...

I see that the exception is generated when requiring mathjs. I suspect that mathjs uses some low level feature that is not available inside live-plugin-manager's runtime. But for now I don't have any idea.

I will try to investigate.

taoyuan commented 4 years ago

I did some research and found that there is an inconsistency between the vm environment and the native node environment.

In typed-function

    var _types = [
      { name: 'number',    test: function (x) { return typeof x === 'number' } },
      { name: 'string',    test: function (x) { return typeof x === 'string' } },
      { name: 'boolean',   test: function (x) { return typeof x === 'boolean' } },
      { name: 'Function',  test: function (x) { return typeof x === 'function'} },
      { name: 'Array',     test: Array.isArray },
      { name: 'Date',      test: function (x) { return x instanceof Date } },
      { name: 'RegExp',    test: function (x) { return x instanceof RegExp } },
      { name: 'Object',    test: function (x) {
         // in VM will be false if x = {}, but it should be true
        return typeof x === 'object' && x !== null && x.constructor === Object           
      }},
      { name: 'null',      test: function (x) { return x === null } },
      { name: 'undefined', test: function (x) { return x === undefined } }
    ];

A live-plugin-manager test case:

const {PluginManager} = require('live-plugin-manager');
const manager = new PluginManager();
manager.runScript('console.log({}.constructor === Object)'); // should be true, but is false
// =>
// false
davideicardi commented 4 years ago

@taoyuan Thanks for the feedback. I'm not sure why this happen. I think it is something that I don't do right here: https://github.com/davideicardi/live-plugin-manager/blob/master/src/PluginVm.ts#L385 Maybe related to https://stackoverflow.com/questions/59009214/some-properties-of-the-global-instance-are-not-copied-by-spread-operator-or-by-o

Any other help is appreciated ;-)

Which version of node.js are you using? Which version of live-plugin-manager?

taoyuan commented 4 years ago

@davideicardi Yes, I tried to figure out which properties is missing in PlugVM#L385, but with no lucky.

By the way, I am using Node.js@v14.12.0 and live-plugin-manager@0.15.1