NeilFraser / JS-Interpreter

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

Constructor execute fail in the js-interpreter #235

Closed CommanderXL closed 2 years ago

CommanderXL commented 2 years ago

The example code is:

const p = new Interpreter('')

function Aa(handler) {
  this.handler = handler
}

function AdWrapper () {
  return Aa
}

var str = 'console.log(111, new (AdWrapper())(function() {}))'    // Throw Error:  Cannot set property 'handler' of undefined

p.setProperty.(p.globalObject, 'AdWrapper', p.createHostProxy(AdWrapper, true))
p.appendCode(str)
p.keepRun()

Well, If I run the code in the normal js environment, it ok:

function Aa(handler) {
  this.handler = handler
}

function AdWrapper () {
  return Aa
}

console.log(111, new (AdWrapper())(function() {}))   // it's ok