Skip to content

make JsContext cloneable  #7

@Ziqi-Yang

Description

@Ziqi-Yang

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

  1. 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.
  2. use anonymous function, like:
        val encapsulatedScript = """
            (function(){
                $script
            })();
        """.trimIndent()

        jsContext.evaluate(encapsulatedScript, "script.js")
  1. 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions