NeilFraser / JS-Interpreter

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

How to return a value? #190

Closed cosbgn closed 4 years ago

cosbgn commented 4 years ago

By default the library returns the last value, however I would like to return at a specific point, something like this:

var sunny = true
if (sunny){
return "It's sunny today"
}
return "today is not sunny"

This will fail, because the returns are not valid, so I tried to wrap them in a function:

function x(){
 var sunny....... 
}
x()

but also this fails. Any ideas on how this could be done?

cosbgn commented 4 years ago

Never mind, my syntax was wrong. It all works.

rrrhys commented 3 years ago

Contrived example for future weary google travelers:

        const expression = "5 + 2";
        const interpreter = new Interpreter(expression);
        interpreter.run()
        const result = interpreter.value;