Skip to content

Commit 3fbeb43

Browse files
GeoffreyBoothaduh95
andcommitted
use plain async function wrapper instead of CommonJS module wrapper around async function wrapper; add test
Co-authored-by: Antoine du Hamel <[email protected]>
1 parent c117865 commit 3fbeb43

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/node_contextify.cc

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,38 +1510,37 @@ void ContextifyContext::ContainsModuleSyntax(
15101510
if (!should_retry_as_esm) {
15111511
for (const auto& error_message : throws_only_in_cjs_error_messages) {
15121512
if (message.find(error_message) != std::string_view::npos) {
1513-
// Try parsing again where the user's code is wrapped within an async
1514-
// function. If the new parse succeeds, then the error was caused by
1515-
// either a top-level declaration of one of the CommonJS module
1516-
// variables, or a top-level `await`.
1513+
// Try parsing again where the CommonJS wrapper is replaced by an
1514+
// async function wrapper. If the new parse succeeds, then the error
1515+
// was caused by either a top-level declaration of one of the CommonJS
1516+
// module variables, or a top-level `await`.
15171517
TryCatchScope second_parse_try_catch(env);
1518-
Local<String> wrapped_code =
1518+
code =
15191519
String::Concat(isolate,
15201520
String::NewFromUtf8(isolate, "(async function() {")
15211521
.ToLocalChecked(),
15221522
code);
1523-
wrapped_code = String::Concat(
1523+
code = String::Concat(
15241524
isolate,
1525-
wrapped_code,
1525+
code,
15261526
String::NewFromUtf8(isolate, "})();").ToLocalChecked());
15271527
ScriptCompiler::Source wrapped_source =
15281528
GetCommonJSSourceInstance(isolate,
1529-
wrapped_code,
1529+
code,
15301530
filename,
15311531
0,
15321532
0,
15331533
host_defined_options,
15341534
nullptr);
1535-
ContextifyContext::CompileFunctionAndCacheResult(
1536-
env,
1535+
std::ignore = ScriptCompiler::CompileFunction(
15371536
context,
15381537
&wrapped_source,
1539-
std::move(params),
1540-
std::vector<Local<Object>>(),
1538+
params.size(),
1539+
params.data(),
1540+
0,
1541+
nullptr,
15411542
options,
1542-
true,
1543-
id_symbol,
1544-
second_parse_try_catch);
1543+
v8::ScriptCompiler::NoCacheReason::kNoCacheNoReason);
15451544
if (!second_parse_try_catch.HasTerminated()) {
15461545
if (second_parse_try_catch.HasCaught()) {
15471546
// If on the second parse an error is thrown by ESM syntax, then

test/es-module/test-esm-detect-ambiguous.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,19 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
276276
strictEqual(signal, null);
277277
});
278278

279+
it('throws on undefined `require` when top-level `await` triggers ESM parsing', async () => {
280+
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
281+
'--experimental-detect-module',
282+
'--eval',
283+
'const fs = require("node:fs"); await Promise.resolve();',
284+
]);
285+
286+
match(stderr, /ReferenceError: require is not defined in ES module scope/);
287+
strictEqual(stdout, '');
288+
strictEqual(code, 1);
289+
strictEqual(signal, null);
290+
});
291+
279292
it('permits declaration of CommonJS module variables', async () => {
280293
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
281294
'--experimental-detect-module',

0 commit comments

Comments
 (0)