For a NodeJS standalone app, I need the ability to parse arbitrary JS code. This app is offline, and does not interact with anything else than the user input: I can use eval
or new Function()
without security risk.
However, a user can create a variable inside its code. I would like this variable to be created not inside a global space, but inside a custom context object I would give.
For example, getting something like this:
let context = {a: 5}
new Function('context', '/*something*/ console.log("a =", a); let b = -5;')(context)
console.log('context is', context)
Would output
a = 5
context is {a: 5, b: -5}
Is there any way to achieve that?