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

Incorrect execution result #25

Open
qbx2 opened this issue May 19, 2024 · 0 comments
Open

Incorrect execution result #25

qbx2 opened this issue May 19, 2024 · 0 comments

Comments

@qbx2
Copy link

qbx2 commented May 19, 2024

Hello! First of all, I want to thank you for open-sourcing this nice project!
I am trying to combine SandboxJS with typescript to evaluate typescript code. However, the evaluation undesirably fails when I use optional chaining operators like below (I'm not targeting ES2022 because SandboxJS does not support nullish coalescing operator (??) at the moment):

import ts from 'typescript';

const scope = { myTest: null };
const code2 = ts.transpileModule(`return myTest?.myMethod;`, {
    compilerOptions: { target: ts.ScriptTarget.ES5 },
});
console.log('transpiled', code2.outputText);
const exec2 = sandbox.compile(code2.outputText);
const result2 = exec2(scope).run();
console.log(result2);

Result:

transpiled return myTest === null || myTest === void 0 ? void 0 : myTest.myMethod;

/Users/qbx2/test/node_modules/.pnpm/@[email protected]/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:1929
        throw new TypeError(`Cannot get property ${b} of null`);
              ^

TypeError: Cannot get property myMethod of null
    at /Users/qbx2/test/node_modules/.pnpm/@[email protected]/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:1929:15
    at execSync (/Users/qbx2/test/node_modules/.pnpm/@[email protected]/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2758:28)
    at /Users/qbx2/test/node_modules/.pnpm/@[email protected]/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2447:5
    at execSync (/Users/qbx2/test/node_modules/.pnpm/@[email protected]/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2758:28)
    at /Users/qbx2/test/node_modules/.pnpm/@[email protected]/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2739:36
    at syncDone (/Users/qbx2/test/node_modules/.pnpm/@[email protected]/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2615:5)
    at execSync (/Users/qbx2/test/node_modules/.pnpm/@[email protected]/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2739:20)
    at /Users/qbx2/test/node_modules/.pnpm/@[email protected]/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2739:36
    at syncDone (/Users/qbx2/test/node_modules/.pnpm/@[email protected]/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2615:5)
    at execSync (/Users/qbx2/test/node_modules/.pnpm/@[email protected]/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2739:20)

Node.js v18.17.0

NOTE: After a small amount of digging, it seems that there is a priority issue because the evaluation succeeds when I add parentheses around the condition like return (myTest === null || myTest === void 0) ? void 0 : myTest.myMethod.

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

1 participant