@@ -1442,7 +1442,8 @@ static MaybeLocal<Function> CompileFunctionForCJSLoader(Environment* env,
1442
1442
Local<Context> context,
1443
1443
Local<String> code,
1444
1444
Local<String> filename,
1445
- bool * cache_rejected) {
1445
+ bool * cache_rejected,
1446
+ bool is_cjs_scope) {
1446
1447
Isolate* isolate = context->GetIsolate ();
1447
1448
EscapableHandleScope scope (isolate);
1448
1449
@@ -1494,7 +1495,10 @@ static MaybeLocal<Function> CompileFunctionForCJSLoader(Environment* env,
1494
1495
options = ScriptCompiler::kConsumeCodeCache ;
1495
1496
}
1496
1497
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
+ }
1498
1502
MaybeLocal<Function> maybe_fn = ScriptCompiler::CompileFunction (
1499
1503
context,
1500
1504
&source,
@@ -1556,7 +1560,7 @@ static void CompileFunctionForCJSLoader(
1556
1560
ShouldNotAbortOnUncaughtScope no_abort_scope (realm->env ());
1557
1561
TryCatchScope try_catch (env);
1558
1562
if (!CompileFunctionForCJSLoader (
1559
- env, context, code, filename, &cache_rejected)
1563
+ env, context, code, filename, &cache_rejected, true )
1560
1564
.ToLocal (&fn)) {
1561
1565
CHECK (try_catch.HasCaught ());
1562
1566
CHECK (!try_catch.HasTerminated ());
@@ -1694,11 +1698,15 @@ static void ContainsModuleSyntax(const FunctionCallbackInfo<Value>& args) {
1694
1698
CHECK (args[1 ]->IsString ());
1695
1699
Local<String> filename = args[1 ].As <String>();
1696
1700
1697
- // Argument 2 : resource name (URL for ES module).
1701
+ // Argument 3 : resource name (URL for ES module).
1698
1702
Local<String> resource_name = filename;
1699
1703
if (args[2 ]->IsString ()) {
1700
1704
resource_name = args[2 ].As <String>();
1701
1705
}
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 ();
1702
1710
1703
1711
bool cache_rejected = false ;
1704
1712
Local<String> message;
@@ -1707,7 +1715,7 @@ static void ContainsModuleSyntax(const FunctionCallbackInfo<Value>& args) {
1707
1715
TryCatchScope try_catch (env);
1708
1716
ShouldNotAbortOnUncaughtScope no_abort_scope (env);
1709
1717
if (CompileFunctionForCJSLoader (
1710
- env, context, code, filename, &cache_rejected)
1718
+ env, context, code, filename, &cache_rejected, cjs_var )
1711
1719
.ToLocal (&fn)) {
1712
1720
args.GetReturnValue ().Set (false );
1713
1721
return ;
0 commit comments