module: do not set CJS variables for Worker eval · nodejs/node@4cdb05a · GitHub
Skip to content

Commit 4cdb05a

Browse files
aduh95targos
authored andcommitted
module: do not set CJS variables for Worker eval
PR-URL: #53050 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 6022346 commit 4cdb05a

3 files changed

Lines changed: 42 additions & 6 deletions

File tree

lib/internal/process/execution.js

Lines changed: 1 addition & 1 deletion

src/node_contextify.cc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,8 @@ static MaybeLocal<Function> CompileFunctionForCJSLoader(Environment* env,
14421442
Local<Context> context,
14431443
Local<String> code,
14441444
Local<String> filename,
1445-
bool* cache_rejected) {
1445+
bool* cache_rejected,
1446+
bool is_cjs_scope) {
14461447
Isolate* isolate = context->GetIsolate();
14471448
EscapableHandleScope scope(isolate);
14481449

@@ -1494,7 +1495,10 @@ static MaybeLocal<Function> CompileFunctionForCJSLoader(Environment* env,
14941495
options = ScriptCompiler::kConsumeCodeCache;
14951496
}
14961497

1497-
std::vector<Local<String>> params = GetCJSParameters(env->isolate_data());
1498+
std::vector<Local<String>> params;
1499+
if (is_cjs_scope) {
1500+
params = GetCJSParameters(env->isolate_data());
1501+
}
14981502
MaybeLocal<Function> maybe_fn = ScriptCompiler::CompileFunction(
14991503
context,
15001504
&source,
@@ -1556,7 +1560,7 @@ static void CompileFunctionForCJSLoader(
15561560
ShouldNotAbortOnUncaughtScope no_abort_scope(realm->env());
15571561
TryCatchScope try_catch(env);
15581562
if (!CompileFunctionForCJSLoader(
1559-
env, context, code, filename, &cache_rejected)
1563+
env, context, code, filename, &cache_rejected, true)
15601564
.ToLocal(&fn)) {
15611565
CHECK(try_catch.HasCaught());
15621566
CHECK(!try_catch.HasTerminated());
@@ -1694,11 +1698,15 @@ static void ContainsModuleSyntax(const FunctionCallbackInfo<Value>& args) {
16941698
CHECK(args[1]->IsString());
16951699
Local<String> filename = args[1].As<String>();
16961700

1697-
// Argument 2: resource name (URL for ES module).
1701+
// Argument 3: resource name (URL for ES module).
16981702
Local<String> resource_name = filename;
16991703
if (args[2]->IsString()) {
17001704
resource_name = args[2].As<String>();
17011705
}
1706+
// Argument 4: flag to indicate if CJS variables should not be in scope
1707+
// (they should be for normal CommonJS modules, but not for the
1708+
// CommonJS eval scope).
1709+
bool cjs_var = !args[3]->IsString();
17021710

17031711
bool cache_rejected = false;
17041712
Local<String> message;
@@ -1707,7 +1715,7 @@ static void ContainsModuleSyntax(const FunctionCallbackInfo<Value>& args) {
17071715
TryCatchScope try_catch(env);
17081716
ShouldNotAbortOnUncaughtScope no_abort_scope(env);
17091717
if (CompileFunctionForCJSLoader(
1710-
env, context, code, filename, &cache_rejected)
1718+
env, context, code, filename, &cache_rejected, cjs_var)
17111719
.ToLocal(&fn)) {
17121720
args.GetReturnValue().Set(false);
17131721
return;

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

Lines changed: 28 additions & 0 deletions

0 commit comments

Comments
 (0)