NeilFraser / JS-Interpreter

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

Immediate anonymous function Invocation fails in some cases. #203

Closed Webifi closed 3 years ago

Webifi commented 3 years ago

Immediate anonymous function Invocation seems to fail after async function.

Instantiate new interpreter with:

const interpreter = new Interpreter('', (interpreter, globalObject) => {
  // Add async function as test
  interpreter.setProperty(
    globalObject,
    'testAsync',
    interpreter.createAsyncFunction((value, time, callback) => {
      // Return value after delay
      setTimeout(() => {
        callback(value)
        interpreter.run()
      }, time)
    }),
    Interpreter.NONENUMERABLE_DESCRIPTOR
  )
})

This will fail with "Uncaught TypeError: Test delay 1 is not a function"

interpreter.appendCode(`
testAsync('Test delay 1', 1000)
(function () {
  testAsync('Test delay 2', 1000)
}())
`)
interpreter.run()

This will work:

interpreter.appendCode(`
testAsync('Test delay 1', 1000)
var testFunc = function () {
  testAsync('Test delay 2', 1000)
}
testFunc()
`)
interpreter.run()

This will work:

interpreter.appendCode(`
(function () {
  testAsync('Test delay 2', 1000)
}())
`)
interpreter.run()
Webifi commented 3 years ago

Never mind. Simply missing an ";"