NeilFraser / JS-Interpreter

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

Can we use destructuring? #261

Closed F1LT3R closed 7 months ago

F1LT3R commented 7 months ago

I'm not sure if I'm doing something wrong, or whether this is not supported.

I expected the value of a inside resolve to be the location.

import interpreter from '/lib/shadow/vendor/interpreter.mjs'

window.log = function (result) {
    console.log({result})
}

var myCode = `
    function resolve ({a}) {
        return a;
    };
    log(resolve({a: url}));

`
var initFunc = function (interpreter, globalObject) {
    interpreter.setProperty(globalObject, 'url', String(location))

    var wrapper = function log(text) {
        return window.log(text)
    }
    interpreter.setProperty(globalObject, 'log', interpreter.createNativeFunction(wrapper))
}
var myInterpreter = new interpreter(myCode, initFunc)
myInterpreter.run()

image

NeilFraser commented 7 months ago

Destructuring is a feature of ES6: http://es6-features.org/#ParameterContextMatching

JS-Interpreter is ES5 only: https://github.com/NeilFraser/JS-Interpreter/issues/186

F1LT3R commented 7 months ago

Got it. Thanks Neil.

Congrats on the great code here and all the work that went into it.

Very impressive!