NeilFraser / JS-Interpreter

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

Interpreter using acorn_interpreter.js can't be serialized, interpreter.ast is undefined. #232

Closed ChrisAcrobat closed 2 years ago

ChrisAcrobat commented 2 years ago

I have compared against the public demo, but I can't see what I am doing wrong (if any). Property interpreter.ast in function recordAcornConstructons is undefined for me for some reason when I try to serialize seedrandom.js. The code is taken through Babel. Babel.transform(seedrandomSource, {'presets': ['es2015']}).code

ChrisAcrobat commented 2 years ago

I'll investigate some more, but I get different results when I use the combined acorn_interpreter.js or the them separata (acorn.js, interpreter.js).

If I first do:

interpreter = new Interpreter(someSafeDependencies);
interpreter.run(); // Init system dependencies.

And then do:

stepsRemaining = 1000; // someSafeDependencies should not affect stepsRemaining.
interpreter.appendCode(jsCode);
while(interpreter.step() && 0 < stepsRemaining){
    stepsRemaining--;
}

Then it starts to step after someSafeDependencies, right?

ChrisAcrobat commented 2 years ago

For a moment it looked like then do happened twice or something, but it sorted it self out. Apparently global function onmessage caused problem for me when I tried to override it (I was sure it worked last week, but I guess not.), but I fixed it by adding let onmessage = null; to someSafeDependencies.

So now everything works when I use them separata (acorn.js, interpreter.js), but not combined acorn_interpreter.js. Then I still get interpreter.ast is undefined.

ChrisAcrobat commented 2 years ago

Maybe relates to #229?

NeilFraser commented 2 years ago

Yes, the documentation specifically calls out:

Another limitation is that serialization reaches beyond the public API, and thus does not work with the compressed bundle (acorn_interpreter.js).

The .ast property is also not listed in the public API, thus it gets renamed (looking at the source, it appears to be called .ka today, but that may change with every recompression).

If you need access to non-public functions or properties, then you should use the uncompressed version.

ChrisAcrobat commented 2 years ago

🤦‍♂️

Sorry, I interpreted that differently when read it quickly and thought that it wouldn't affect my use case. Thanks for the help!