Skip to content

Commit 988f8b6

Browse files
aduh95joyeecheung
authored andcommitted
module: do not set CJS variables for Worker eval
PR-URL: nodejs#53050 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 35e1827 commit 988f8b6

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

lib/internal/process/execution.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function evalScript(name, body, breakFirstLine, print, shouldLoadESM = false) {
8383

8484
if (getOptionValue('--experimental-detect-module') &&
8585
getOptionValue('--input-type') === '' && getOptionValue('--experimental-default-type') === '' &&
86-
containsModuleSyntax(body, name)) {
86+
containsModuleSyntax(body, name, null, 'no CJS variables')) {
8787
return evalModuleEntryPoint(body, print);
8888
}
8989

src/node_contextify.cc

+13-5
Original file line numberDiff line numberDiff line change
@@ -1451,7 +1451,8 @@ static MaybeLocal<Function> CompileFunctionForCJSLoader(Environment* env,
14511451
Local<Context> context,
14521452
Local<String> code,
14531453
Local<String> filename,
1454-
bool* cache_rejected) {
1454+
bool* cache_rejected,
1455+
bool is_cjs_scope) {
14551456
Isolate* isolate = context->GetIsolate();
14561457
EscapableHandleScope scope(isolate);
14571458

@@ -1504,7 +1505,10 @@ static MaybeLocal<Function> CompileFunctionForCJSLoader(Environment* env,
15041505
options = ScriptCompiler::kConsumeCodeCache;
15051506
}
15061507

1507-
std::vector<Local<String>> params = GetCJSParameters(env->isolate_data());
1508+
std::vector<Local<String>> params;
1509+
if (is_cjs_scope) {
1510+
params = GetCJSParameters(env->isolate_data());
1511+
}
15081512
MaybeLocal<Function> maybe_fn = ScriptCompiler::CompileFunction(
15091513
context,
15101514
&source,
@@ -1566,7 +1570,7 @@ static void CompileFunctionForCJSLoader(
15661570
ShouldNotAbortOnUncaughtScope no_abort_scope(realm->env());
15671571
TryCatchScope try_catch(env);
15681572
if (!CompileFunctionForCJSLoader(
1569-
env, context, code, filename, &cache_rejected)
1573+
env, context, code, filename, &cache_rejected, true)
15701574
.ToLocal(&fn)) {
15711575
CHECK(try_catch.HasCaught());
15721576
CHECK(!try_catch.HasTerminated());
@@ -1704,11 +1708,15 @@ static void ContainsModuleSyntax(const FunctionCallbackInfo<Value>& args) {
17041708
CHECK(args[1]->IsString());
17051709
Local<String> filename = args[1].As<String>();
17061710

1707-
// Argument 2: resource name (URL for ES module).
1711+
// Argument 3: resource name (URL for ES module).
17081712
Local<String> resource_name = filename;
17091713
if (args[2]->IsString()) {
17101714
resource_name = args[2].As<String>();
17111715
}
1716+
// Argument 4: flag to indicate if CJS variables should not be in scope
1717+
// (they should be for normal CommonJS modules, but not for the
1718+
// CommonJS eval scope).
1719+
bool cjs_var = !args[3]->IsString();
17121720

17131721
bool cache_rejected = false;
17141722
Local<String> message;
@@ -1717,7 +1725,7 @@ static void ContainsModuleSyntax(const FunctionCallbackInfo<Value>& args) {
17171725
TryCatchScope try_catch(env);
17181726
ShouldNotAbortOnUncaughtScope no_abort_scope(env);
17191727
if (CompileFunctionForCJSLoader(
1720-
env, context, code, filename, &cache_rejected)
1728+
env, context, code, filename, &cache_rejected, cjs_var)
17211729
.ToLocal(&fn)) {
17221730
args.GetReturnValue().Set(false);
17231731
return;

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

+28
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
4444
strictEqual(signal, null);
4545
});
4646

47+
it('should not switch to module if code is parsable as script', async () => {
48+
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
49+
'--experimental-detect-module',
50+
'--eval',
51+
'let __filename,__dirname,require,module,exports;this.a',
52+
]);
53+
54+
strictEqual(stderr, '');
55+
strictEqual(stdout, '');
56+
strictEqual(code, 0);
57+
strictEqual(signal, null);
58+
});
59+
4760
it('should be overridden by --experimental-default-type', async () => {
4861
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
4962
'--experimental-detect-module',
@@ -393,3 +406,18 @@ describe('Wrapping a `require` of an ES module while using `--abort-on-uncaught-
393406
strictEqual(signal, null);
394407
});
395408
});
409+
410+
describe('when working with Worker threads', () => {
411+
it('should support sloppy scripts that declare CJS "global-like" variables', async () => {
412+
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
413+
'--experimental-detect-module',
414+
'--eval',
415+
'new worker_threads.Worker("let __filename,__dirname,require,module,exports;this.a",{eval:true})',
416+
]);
417+
418+
strictEqual(stderr, '');
419+
strictEqual(stdout, '');
420+
strictEqual(code, 0);
421+
strictEqual(signal, null);
422+
});
423+
});

0 commit comments

Comments
 (0)