Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse compiled code #20

Open
AStaroverov opened this issue Oct 17, 2023 · 4 comments
Open

Reuse compiled code #20

AStaroverov opened this issue Oct 17, 2023 · 4 comments

Comments

@AStaroverov
Copy link

After attempt use compiled code with different contexts I catching error

ReferenceError: plus is not defined

and my code

function plus(n,u){return n+u};function minus(n,u){return n-u};var a=plus(value,10);return minus(a,5);.

In documentation I didn't see nothing about this restriction.

So its bug on not?

@nyariv
Copy link
Owner

nyariv commented Oct 18, 2023

Please provide sample code on how you are using different contexts

@AStaroverov
Copy link
Author

for example

const body = 'function plus(n,u){return n+u};function minus(n,u){return n-u};var a=plus(value,10);return minus(a,5);'
const s = sandbox.compile(body, true);

console.log(s({value: 1}).run());
console.log(s({value: 2}).run());

@AStaroverov
Copy link
Author

AStaroverov commented Oct 18, 2023

I found that after first run context.tree will be modified, that broke second call.

This workaround help me

    const parsed = Sandbox.parse(body);
    const context = sandbox.createContext(sandbox.context, parsed);
    const tree = context.tree;
    return (scope) => {
        context.tree = structuredClone(tree);
        return sandbox.executeTree(context, [scope]).result;
    };

@gustavotoyota
Copy link

Just bumped into this problem. The code:

function printValue(value) {
  console.log(value)
}

printValue(row)

Executed with: exec({ row }).run()
The above only works in the first execution, and in the others it throws printValue is not defined.
This works normally, though:

const printValue = (value) => {
  console.log(value)
}

printValue(row)

So SandboxJS doesn't work well with hoisted functions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants