-
Notifications
You must be signed in to change notification settings - Fork 5
Open
Description
If JsContext
is cloneable, we can easily create one context based on the other context, which makes script executing more convenient.
Currently, if I want to make executed scripts not affecting each other, I could
- create a new jsContext, which may be expensive, given that one jsContext may need to bind many objects and functions at the startup(configuration phase). Though I haven't dive into the C source code of creating a JsContext, it may be expensive too.
- use anonymous function, like:
val encapsulatedScript = """
(function(){
$script
})();
""".trimIndent()
jsContext.evaluate(encapsulatedScript, "script.js")
- compile to bytecode and evaluate it
val bytes = jsContext.compileJsToBytecode(script)
jsContext.evaluateBytecode(bytes)
However, this method is not stable, evaluating twice the script like
let x = "hello"
is no problem, but if evaluating this script twice, it will cause problem with no error prompting(while the second method has nice prompt):
let x = "hello"
console.log(x)
Having jsContext
cloneable can also allow use to choose whether script can use variables created by previous scripts or not, while the second method mentioned above can only separate these scripts
Metadata
Metadata
Assignees
Labels
No labels