NeilFraser / JS-Interpreter

A sandboxed JavaScript interpreter in JavaScript.
Apache License 2.0
1.96k stars 352 forks source link

Fallback to globalThis in acorn.js if this is not available #258

Closed yorinasub17 closed 4 months ago

yorinasub17 commented 11 months ago

This increases compatibility of the included acorn.js file to support environments that don't support a root this object (e.g., Deno) by falling back to globalThis if this is not defined.

When using the base JS-Interpreter with Deno, the following error happens:

error: TypeError: Cannot read properties of undefined (reading 'acorn')
  mod(root.acorn || (root.acorn = {})); // Plain browser env
           ^
    at https://raw.githubusercontent.com/NeilFraser/JS-Interpreter/10a8cf5e613834b5d36655c1921f818455fc324a/acorn.js:32:12
    at https://raw.githubusercontent.com/NeilFraser/JS-Interpreter/10a8cf5e613834b5d36655c1921f818455fc324a/acorn.js:33:3

This patch resolves the error.

NeilFraser commented 4 months ago

I've committed this change f3e1c38afb8f7f2530d9b5ace18eaef4f0871a7a which resolves the issue using a preference for globalThis. It also brings Acorn in line with JS-Interpreter for global object detection. Thanks!

yorinasub17 commented 4 months ago

Thanks for bringing this in!