diff --git a/lib/internal/process/execution.js b/lib/internal/process/execution.js index e8a1a29efcc8a6..f5b19d5a7e8c9c 100644 --- a/lib/internal/process/execution.js +++ b/lib/internal/process/execution.js @@ -84,38 +84,19 @@ function evalScript(name, body, breakFirstLine, print, shouldLoadESM = false) { evalModuleEntryPoint(body, print); } - const runScript = () => { - // Create wrapper for cache entry - const script = ` - globalThis.module = module; - globalThis.exports = exports; - globalThis.__dirname = __dirname; - globalThis.require = require; - return (main) => main(); - `; - globalThis.__filename = name; - RegExpPrototypeExec(/^/, ''); // Necessary to reset RegExp statics before user code runs. - const result = module._compile(script, `${name}-wrapper`)(() => { - const compiledScript = compileScript(name, body, baseUrl); - return runScriptInThisContext(compiledScript, true, !!breakFirstLine); - }); - if (print) { - const { log } = require('internal/console/global'); - - process.on('exit', () => { - log(result); - }); - } - - if (origModule !== undefined) - globalThis.module = origModule; - }; + const evalFunction = () => runScriptInContext(name, + body, + breakFirstLine, + print, + module, + baseUrl, + undefined, + origModule); if (shouldLoadESM) { - require('internal/modules/run_main').runEntryPointWithESMLoader(runScript); - return; + return require('internal/modules/run_main').runEntryPointWithESMLoader(evalFunction); } - runScript(); + evalFunction(); } const exceptionHandlerState = { @@ -301,19 +282,19 @@ function evalTypeScript(name, source, breakFirstLine, print, shouldLoadESM = fal } } + const evalFunction = () => runScriptInContext(name, + sourceToRun, + breakFirstLine, + print, + module, + baseUrl, + compiledScript, + origModule); + if (shouldLoadESM) { - return require('internal/modules/run_main').runEntryPointWithESMLoader( - () => runScriptInContext(name, - sourceToRun, - breakFirstLine, - print, - module, - baseUrl, - compiledScript, - origModule)); + return require('internal/modules/run_main').runEntryPointWithESMLoader(evalFunction); } - - runScriptInContext(name, sourceToRun, breakFirstLine, print, module, baseUrl, compiledScript, origModule); + evalFunction(); } /** @@ -476,7 +457,7 @@ function runScriptInContext(name, body, breakFirstLine, print, module, baseUrl, const result = module._compile(script, `${name}-wrapper`)(() => { // If the script was already compiled, use it. return runScriptInThisContext( - compiledScript, + compiledScript ?? compileScript(name, body, baseUrl), true, !!breakFirstLine); }); if (print) { diff --git a/test/fixtures/eval/eval_messages.snapshot b/test/fixtures/eval/eval_messages.snapshot index e3976d69fd93b3..4a29e96f9c0973 100644 --- a/test/fixtures/eval/eval_messages.snapshot +++ b/test/fixtures/eval/eval_messages.snapshot @@ -8,16 +8,11 @@ with(this){__filename} : ^^^^ `---- - Caused by: failed to parse SyntaxError: Strict mode code may not include a with statement - - - - Node.js * 42 42 @@ -27,13 +22,6 @@ throw new Error("hello") Error: hello - - - - - - - Node.js * [eval]:1 throw new Error("hello") @@ -41,13 +29,6 @@ throw new Error("hello") Error: hello - - - - - - - Node.js * 100 [eval]:1 @@ -56,13 +37,6 @@ var x = 100; y = x; ReferenceError: y is not defined - - - - - - - Node.js * [eval]:1 diff --git a/test/fixtures/eval/eval_typescript.js b/test/fixtures/eval/eval_typescript.js new file mode 100644 index 00000000000000..2c96b66f70dde1 --- /dev/null +++ b/test/fixtures/eval/eval_typescript.js @@ -0,0 +1,25 @@ +'use strict'; + +require('../../common'); + +const spawnSync = require('child_process').spawnSync; + +const queue = [ + 'enum Foo{};', + 'throw new SyntaxError("hello")', + 'const foo;', + 'let x: number = 100;x;', + 'const foo: string = 10;', + 'function foo(){};foo(1);', + 'interface Foo{};const foo;', + 'function foo(){ await Promise.resolve(1)};', +]; + +for (const cmd of queue) { + const args = ['--disable-warning=ExperimentalWarning', '-p', cmd]; + const result = spawnSync(process.execPath, args, { + stdio: 'pipe' + }); + process.stdout.write(result.stdout); + process.stdout.write(result.stderr); +} diff --git a/test/fixtures/eval/eval_typescript.snapshot b/test/fixtures/eval/eval_typescript.snapshot new file mode 100644 index 00000000000000..b10f8d6a910e4f --- /dev/null +++ b/test/fixtures/eval/eval_typescript.snapshot @@ -0,0 +1,51 @@ +[eval]:1 +enum Foo{}; +^^^^ + x TypeScript enum is not supported in strip-only mode + ,---- + 1 | enum Foo{}; + : ^^^^^^^^^^ + `---- + +SyntaxError: Unexpected reserved word + +Node.js * +[eval]:1 +throw new SyntaxError("hello") +^ + +SyntaxError: hello + +Node.js * +[eval]:1 +const foo; + ^^^ + +SyntaxError: Missing initializer in const declaration + +Node.js * +100 +undefined +false +[eval]:1 + ;const foo; + ^^^ + +SyntaxError: Missing initializer in const declaration + +Node.js * +[eval]:1 +function foo(){ await Promise.resolve(1)}; + ^^^^^ + x await isn't allowed in non-async function + ,---- + 1 | function foo(){ await Promise.resolve(1)}; + : ^^^^^^^ + `---- + +Caused by: + failed to parse + +SyntaxError: await is only valid in async functions and the top level bodies of modules + +Node.js * diff --git a/test/fixtures/eval/stdin_messages.snapshot b/test/fixtures/eval/stdin_messages.snapshot index 27f5851eb94a4f..c2f33ba8475d07 100644 --- a/test/fixtures/eval/stdin_messages.snapshot +++ b/test/fixtures/eval/stdin_messages.snapshot @@ -8,20 +8,11 @@ with(this){__filename} : ^^^^ `---- - Caused by: failed to parse SyntaxError: Strict mode code may not include a with statement - - - - - - - - Node.js * 42 42 @@ -31,16 +22,6 @@ throw new Error("hello") Error: hello - - - - - - - - - - Node.js * [stdin]:1 throw new Error("hello") @@ -48,16 +29,6 @@ throw new Error("hello") Error: hello - - - - - - - - - - Node.js * 100 [stdin]:1 @@ -66,16 +37,6 @@ let x = 100; y = x; ReferenceError: y is not defined - - - - - - - - - - Node.js * [stdin]:1 diff --git a/test/fixtures/eval/stdin_typescript.snapshot b/test/fixtures/eval/stdin_typescript.snapshot index a7c98ac932e8b0..ccae9c38ee75e0 100644 --- a/test/fixtures/eval/stdin_typescript.snapshot +++ b/test/fixtures/eval/stdin_typescript.snapshot @@ -7,17 +7,8 @@ enum Foo{}; : ^^^^^^^^^^ `---- - SyntaxError: Unexpected reserved word - - - - - - - - Node.js * [stdin]:1 enum Foo{}; @@ -28,17 +19,8 @@ enum Foo{}; : ^^^^^^^^^^ `---- - SyntaxError: Unexpected reserved word - - - - - - - - Node.js * [stdin]:1 throw new SyntaxError("hello") @@ -46,16 +28,6 @@ throw new SyntaxError("hello") SyntaxError: hello - - - - - - - - - - Node.js * [stdin]:1 throw new SyntaxError("hello") @@ -63,16 +35,6 @@ throw new SyntaxError("hello") SyntaxError: hello - - - - - - - - - - Node.js * [stdin]:1 const foo; @@ -80,14 +42,6 @@ const foo; SyntaxError: Missing initializer in const declaration - - - - - - - - Node.js * [stdin]:1 const foo; @@ -95,14 +49,6 @@ const foo; SyntaxError: Missing initializer in const declaration - - - - - - - - Node.js * 100 100 @@ -116,14 +62,6 @@ false SyntaxError: Missing initializer in const declaration - - - - - - - - Node.js * [stdin]:1 ;const foo; @@ -131,14 +69,6 @@ Node.js * SyntaxError: Missing initializer in const declaration - - - - - - - - Node.js * [stdin]:1 function foo(){ await Promise.resolve(1)}; @@ -149,20 +79,11 @@ function foo(){ await Promise.resolve(1)}; : ^^^^^^^ `---- - Caused by: failed to parse SyntaxError: await is only valid in async functions and the top level bodies of modules - - - - - - - - Node.js * [stdin]:1 function foo(){ await Promise.resolve(1)}; @@ -173,19 +94,10 @@ function foo(){ await Promise.resolve(1)}; : ^^^^^^^ `---- - Caused by: failed to parse SyntaxError: await is only valid in async functions and the top level bodies of modules - - - - - - - - Node.js * done diff --git a/test/parallel/test-node-output-eval.mjs b/test/parallel/test-node-output-eval.mjs index a8bfd730e92cf4..d8c52176b1c3c3 100644 --- a/test/parallel/test-node-output-eval.mjs +++ b/test/parallel/test-node-output-eval.mjs @@ -10,21 +10,27 @@ describe('eval output', { concurrency: true }, () => { } const defaultTransform = snapshot.transform( - removeStackTraces, normalize, snapshot.replaceWindowsLineEndings, snapshot.replaceWindowsPaths, - snapshot.replaceNodeVersion + snapshot.replaceNodeVersion, + removeStackTraces, + filterEmptyLines, ); function removeStackTraces(output) { return output.replaceAll(/^ *at .+$/gm, ''); } + function filterEmptyLines(output) { + return output.replaceAll(/^\s*$/gm, ''); + } + const tests = [ { name: 'eval/eval_messages.js' }, { name: 'eval/stdin_messages.js' }, { name: 'eval/stdin_typescript.js' }, + { name: 'eval/eval_typescript.js' }, ]; for (const { name } of tests) {