NeilFraser / JS-Interpreter

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

Import of interpreter.js broken with new nativeGlobal property #218

Closed Webifi closed 2 years ago

Webifi commented 2 years ago

Recently, interpreter.js was modified to do the following:

Interpreter.nativeGlobal = this;

The problem is that things like RegExp are now broken if interpreter.js is imported using code like:

import { Interpreter } from '[your path to interpreter.js]'

This could be fixed by changing nativeGlobal to use globalThis if it exists, like:

Interpreter.nativeGlobal = typeof globalThis === 'undefined' ? this : globalThis;

(Since most projects that would use import will likely support globalThis, that seems like the simplest way to solve it. Otherwise, checking for globalThis, self, window, then global would be the more complete solution.)

Interpreter.nativeGlobal = 
    typeof globalThis != 'undefined' ? globalThis :
    typeof self != 'undefined' ? self : 
    typeof window != 'undefined' ? window : 
    typeof global != 'undefined' ? global : this;
NeilFraser commented 2 years ago

Great, thanks!