|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -const { |
4 | | - RegExpPrototypeExec, |
5 | | - globalThis, |
6 | | -} = primordials; |
| 3 | +const { RegExpPrototypeExec, globalThis } = primordials; |
7 | 4 |
|
8 | 5 | const path = require('path'); |
9 | 6 |
|
@@ -50,48 +47,60 @@ function evalModule(source, print) { |
50 | 47 | return handleMainPromise(loadESM((loader) => loader.eval(source))); |
51 | 48 | } |
52 | 49 |
|
53 | | -function evalScript(name, body, breakFirstLine, print) { |
| 50 | +function evalScript(name, body, breakFirstLine, print, shouldLoadESM = false) { |
54 | 51 | const CJSModule = require('internal/modules/cjs/loader').Module; |
55 | 52 | const { kVmBreakFirstLineSymbol } = require('internal/util'); |
56 | 53 | const { pathToFileURL } = require('url'); |
57 | 54 |
|
58 | 55 | const cwd = tryGetCwd(); |
59 | | - const origModule = globalThis.module; // Set e.g. when called from the REPL. |
| 56 | + const origModule = globalThis.module; // Set e.g. when called from the REPL. |
60 | 57 |
|
61 | 58 | const module = new CJSModule(name); |
62 | 59 | module.filename = path.join(cwd, name); |
63 | 60 | module.paths = CJSModule._nodeModulePaths(cwd); |
64 | 61 |
|
| 62 | + const { handleMainPromise } = require('internal/modules/run_main'); |
65 | 63 | const asyncESM = require('internal/process/esm_loader'); |
66 | 64 | const baseUrl = pathToFileURL(module.filename).href; |
| 65 | + const { loadESM } = asyncESM; |
| 66 | + |
| 67 | + const runScript = () => { |
| 68 | + // Create wrapper for cache entry |
| 69 | + const script = ` |
| 70 | + globalThis.module = module; |
| 71 | + globalThis.exports = exports; |
| 72 | + globalThis.__dirname = __dirname; |
| 73 | + globalThis.require = require; |
| 74 | + return (main) => main(); |
| 75 | + `; |
| 76 | + globalThis.__filename = name; |
| 77 | + RegExpPrototypeExec(/^/, ''); // Necessary to reset RegExp statics before user code runs. |
| 78 | + const result = module._compile( |
| 79 | + script, |
| 80 | + `${name}-wrapper` |
| 81 | + )(() => |
| 82 | + require('vm').runInThisContext(body, { |
| 83 | + filename: name, |
| 84 | + displayErrors: true, |
| 85 | + [kVmBreakFirstLineSymbol]: !!breakFirstLine, |
| 86 | + importModuleDynamically(specifier, _, importAssertions) { |
| 87 | + const loader = asyncESM.esmLoader; |
| 88 | + return loader.import(specifier, baseUrl, importAssertions); |
| 89 | + }, |
| 90 | + }) |
| 91 | + ); |
| 92 | + if (print) { |
| 93 | + const { log } = require('internal/console/global'); |
| 94 | + log(result); |
| 95 | + } |
67 | 96 |
|
68 | | - // Create wrapper for cache entry |
69 | | - const script = ` |
70 | | - globalThis.module = module; |
71 | | - globalThis.exports = exports; |
72 | | - globalThis.__dirname = __dirname; |
73 | | - globalThis.require = require; |
74 | | - return (main) => main(); |
75 | | - `; |
76 | | - globalThis.__filename = name; |
77 | | - RegExpPrototypeExec(/^/, ''); // Necessary to reset RegExp statics before user code runs. |
78 | | - const result = module._compile(script, `${name}-wrapper`)(() => |
79 | | - require('vm').runInThisContext(body, { |
80 | | - filename: name, |
81 | | - displayErrors: true, |
82 | | - [kVmBreakFirstLineSymbol]: !!breakFirstLine, |
83 | | - importModuleDynamically(specifier, _, importAssertions) { |
84 | | - const loader = asyncESM.esmLoader; |
85 | | - return loader.import(specifier, baseUrl, importAssertions); |
86 | | - }, |
87 | | - })); |
88 | | - if (print) { |
89 | | - const { log } = require('internal/console/global'); |
90 | | - log(result); |
91 | | - } |
| 97 | + if (origModule !== undefined) globalThis.module = origModule; |
| 98 | + }; |
92 | 99 |
|
93 | | - if (origModule !== undefined) |
94 | | - globalThis.module = origModule; |
| 100 | + if (shouldLoadESM) { |
| 101 | + return handleMainPromise(loadESM(runScript)); |
| 102 | + } |
| 103 | + return runScript(); |
95 | 104 | } |
96 | 105 |
|
97 | 106 | const exceptionHandlerState = { |
@@ -169,10 +178,8 @@ function createOnGlobalUncaughtException() { |
169 | 178 | if (afterHooksExist()) { |
170 | 179 | do { |
171 | 180 | const asyncId = executionAsyncId(); |
172 | | - if (asyncId === 0) |
173 | | - popAsyncContext(0); |
174 | | - else |
175 | | - emitAfter(asyncId); |
| 181 | + if (asyncId === 0) popAsyncContext(0); |
| 182 | + else emitAfter(asyncId); |
176 | 183 | } while (hasAsyncIdStack()); |
177 | 184 | } |
178 | 185 | // And completely empty the id stack, including anything that may be |
|
0 commit comments