@@ -1451,7 +1451,8 @@ static MaybeLocal<Function> CompileFunctionForCJSLoader(Environment* env,
1451
1451
Local<Context> context,
1452
1452
Local<String> code,
1453
1453
Local<String> filename,
1454
- bool * cache_rejected) {
1454
+ bool * cache_rejected,
1455
+ bool is_cjs_scope) {
1455
1456
Isolate* isolate = context->GetIsolate ();
1456
1457
EscapableHandleScope scope (isolate);
1457
1458
@@ -1504,7 +1505,10 @@ static MaybeLocal<Function> CompileFunctionForCJSLoader(Environment* env,
1504
1505
options = ScriptCompiler::kConsumeCodeCache ;
1505
1506
}
1506
1507
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
+ }
1508
1512
MaybeLocal<Function> maybe_fn = ScriptCompiler::CompileFunction (
1509
1513
context,
1510
1514
&source,
@@ -1566,7 +1570,7 @@ static void CompileFunctionForCJSLoader(
1566
1570
ShouldNotAbortOnUncaughtScope no_abort_scope (realm->env ());
1567
1571
TryCatchScope try_catch (env);
1568
1572
if (!CompileFunctionForCJSLoader (
1569
- env, context, code, filename, &cache_rejected)
1573
+ env, context, code, filename, &cache_rejected, true )
1570
1574
.ToLocal (&fn)) {
1571
1575
CHECK (try_catch.HasCaught ());
1572
1576
CHECK (!try_catch.HasTerminated ());
@@ -1704,11 +1708,15 @@ static void ContainsModuleSyntax(const FunctionCallbackInfo<Value>& args) {
1704
1708
CHECK (args[1 ]->IsString ());
1705
1709
Local<String> filename = args[1 ].As <String>();
1706
1710
1707
- // Argument 2 : resource name (URL for ES module).
1711
+ // Argument 3 : resource name (URL for ES module).
1708
1712
Local<String> resource_name = filename;
1709
1713
if (args[2 ]->IsString ()) {
1710
1714
resource_name = args[2 ].As <String>();
1711
1715
}
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 ();
1712
1720
1713
1721
bool cache_rejected = false ;
1714
1722
Local<String> message;
@@ -1717,7 +1725,7 @@ static void ContainsModuleSyntax(const FunctionCallbackInfo<Value>& args) {
1717
1725
TryCatchScope try_catch (env);
1718
1726
ShouldNotAbortOnUncaughtScope no_abort_scope (env);
1719
1727
if (CompileFunctionForCJSLoader (
1720
- env, context, code, filename, &cache_rejected)
1728
+ env, context, code, filename, &cache_rejected, cjs_var )
1721
1729
.ToLocal (&fn)) {
1722
1730
args.GetReturnValue ().Set (false );
1723
1731
return ;
0 commit comments