module: do not set CJS variables for Worker eval

PR-URL: https://github.com/nodejs/node/pull/53050
Backport-PR-URL: https://github.com/nodejs/node/pull/56927
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Refs: https://github.com/nodejs/node/issues/52697
This commit is contained in:
Antoine du Hamel authored and Marco Ippolito committed 2025-02-11 11:55:52 +01:00
1 parent 30ed93db12
commit b86f575504
3 files changed
+42 -6

No files matched your search

+1 -1
View File
@@ -83,7 +83,7 @@ function evalScript(name, body, breakFirstLine, print, shouldLoadESM = false) {
if (getOptionValue('--experimental-detect-module') &&
getOptionValue('--input-type') === '' && getOptionValue('--experimental-default-type') === '' &&
containsModuleSyntax(body, name)) {
containsModuleSyntax(body, name, null, 'no CJS variables')) {
return evalModuleEntryPoint(body, print);
}
+13 -5
View File
@@ -1445,7 +1445,8 @@ static MaybeLocal<Function> CompileFunctionForCJSLoader(Environment* env,
Local<Context> context,
Local<String> code,
Local<String> filename,
bool* cache_rejected) {
bool* cache_rejected,
bool is_cjs_scope) {
Isolate* isolate = context->GetIsolate();
EscapableHandleScope scope(isolate);
@@ -1485,7 +1486,10 @@ static MaybeLocal<Function> CompileFunctionForCJSLoader(Environment* env,
options = ScriptCompiler::kConsumeCodeCache;
}
std::vector<Local<String>> params = GetCJSParameters(env->isolate_data());
std::vector<Local<String>> params;
if (is_cjs_scope) {
params = GetCJSParameters(env->isolate_data());
}
MaybeLocal<Function> maybe_fn = ScriptCompiler::CompileFunction(
context,
&source,
@@ -1544,7 +1548,7 @@ static void CompileFunctionForCJSLoader(
ShouldNotAbortOnUncaughtScope no_abort_scope(realm->env());
TryCatchScope try_catch(env);
if (!CompileFunctionForCJSLoader(
env, context, code, filename, &cache_rejected)
env, context, code, filename, &cache_rejected, true)
.ToLocal(&fn)) {
CHECK(try_catch.HasCaught());
CHECK(!try_catch.HasTerminated());
@@ -1682,11 +1686,15 @@ static void ContainsModuleSyntax(const FunctionCallbackInfo<Value>& args) {
CHECK(args[1]->IsString());
Local<String> filename = args[1].As<String>();
// Argument 2: resource name (URL for ES module).
// Argument 3: resource name (URL for ES module).
Local<String> resource_name = filename;
if (args[2]->IsString()) {
resource_name = args[2].As<String>();
}
// Argument 4: flag to indicate if CJS variables should not be in scope
// (they should be for normal CommonJS modules, but not for the
// CommonJS eval scope).
bool cjs_var = !args[3]->IsString();
bool cache_rejected = false;
Local<String> message;
@@ -1695,7 +1703,7 @@ static void ContainsModuleSyntax(const FunctionCallbackInfo<Value>& args) {
TryCatchScope try_catch(env);
ShouldNotAbortOnUncaughtScope no_abort_scope(env);
if (CompileFunctionForCJSLoader(
env, context, code, filename, &cache_rejected)
env, context, code, filename, &cache_rejected, cjs_var)
.ToLocal(&fn)) {
args.GetReturnValue().Set(false);
return;
@@ -44,6 +44,19 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
strictEqual(signal, null);
});
it('should not switch to module if code is parsable as script', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
'--experimental-detect-module',
'--eval',
'let __filename,__dirname,require,module,exports;this.a',
]);
strictEqual(stderr, '');
strictEqual(stdout, '');
strictEqual(code, 0);
strictEqual(signal, null);
});
it('should be overridden by --experimental-default-type', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
'--experimental-detect-module',
@@ -393,3 +406,18 @@ describe('Wrapping a `require` of an ES module while using `--abort-on-uncaught-
strictEqual(signal, null);
});
});
describe('when working with Worker threads', () => {
it('should support sloppy scripts that declare CJS "global-like" variables', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
'--experimental-detect-module',
'--eval',
'new worker_threads.Worker("let __filename,__dirname,require,module,exports;this.a",{eval:true})',
]);
strictEqual(stderr, '');
strictEqual(stdout, '');
strictEqual(code, 0);
strictEqual(signal, null);
});
});