NeilFraser / JS-Interpreter

A sandboxed JavaScript interpreter in JavaScript.
Apache License 2.0
1.98k stars 353 forks source link

TypeError: Interpreter.nativeGlobal.Number is not a function #214

Closed shbobur closed 3 years ago

shbobur commented 3 years ago

I get this error when I run this code: moveForward(Number(10));

This happens in my Node React app where I install the Interpreter with npm. But when I tested it by downloading and linking the interpreter manually everything works without any problems. Is this a problem in my environment or something in npm module?

NeilFraser commented 3 years ago

We don't publish an npm module. Be careful of random code you find in npm.

shbobur commented 3 years ago

@NeilFraser Thank you very much for the quick response. Could you direct me on how to use this repo in my Node app? I am new to Node so I am having difficulties even googling it. Any post or some correct Node terminology for googling would be really helpful :).

NeilFraser commented 3 years ago

I'm no expert at Node or npm. But my process is just to download this repo (either a zip, or using git), then go to the demos directory, and execute node node.js. That should print out:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Which is the fibonacci sequence as computed by the JS-Interpreter. See the file demos/node.js for details.

shbobur commented 3 years ago

Thank you @NeilFraser!

aminmarashi commented 3 years ago

@NeilFraser I'm the maintainer of the npm package, I'll do my best to keep things up-to-date, it's really just a wrapper around the repo added as a submodule and packed into a single package using webpack.

Anyway, I can still reproduce this issue with the original package (and the npm package of course). Here's the code to reproduce:

const { Interpreter } = require('./interpreter.js');
const acorn = require('./acorn');
new Interpreter('Number(1)').run();
➜  JS-Interpreter git:(master) node -e "const { Interpreter } = require('./interpreter.js'); const acorn = require('./acorn'); new Interpreter('Number(1)').run()"
/tmp/JS-Interpreter/interpreter.js:352
        throw e;
        ^

TypeError: Interpreter.nativeGlobal.Number is not a function

I think this might be a regression caused by https://github.com/NeilFraser/JS-Interpreter/commit/30dea3e0dd5e0b45cf773e665fae1dc4a26b16cd

cpcallen commented 3 years ago

@myuzio:

I think this might be a regression caused by 30dea3e

So the change at issue is on (line 178)[https://github.com/NeilFraser/JS-Interpreter/blob/30dea3e0dd5e0b45cf773e665fae1dc4a26b16cd/interpreter.js#L178]:

/**
 * The global object (`window` in a browser, `global` in node.js) is `this`.
 */
Interpreter.nativeGlobal = this;

I don't know how you're packaging interpreter.js, but if you're loading it using require() that's not going to work since this will be an empty object {} instead of global.

aminmarashi commented 3 years ago

That makes sense @cpcallen I was completely ignorant of this fact, I guess I'll find a way to make it work with require as well. It is indeed not an issue with the interpreter itself, but the way it's required in node.

ali-hosseini-deriv commented 3 years ago

This error happened to my project after upgrade to node 14.17.1 LTS / npm version 7 from node 12.2 LTS / npm 6 On node 12 everything working as expected.