@@ -45,6 +45,7 @@ using v8::Array;
45
45
using v8::ArrayBufferView;
46
46
using v8::Boolean;
47
47
using v8::Context;
48
+ using v8::DictionaryTemplate;
48
49
using v8::EscapableHandleScope;
49
50
using v8::Function;
50
51
using v8::FunctionCallbackInfo;
@@ -1088,35 +1089,34 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
1088
1089
new_cached_data.reset (ScriptCompiler::CreateCodeCache (v8_script));
1089
1090
}
1090
1091
1092
+ auto self = args.This ();
1093
+
1091
1094
if (contextify_script->object ()
1092
1095
->SetPrivate (context, env->host_defined_option_symbol (), id_symbol)
1093
1096
.IsNothing ()) {
1094
1097
return ;
1095
1098
}
1096
-
1097
1099
if (StoreCodeCacheResult (env,
1098
- args. This () ,
1100
+ self ,
1099
1101
compile_options,
1100
1102
source,
1101
1103
produce_cached_data,
1102
1104
std::move (new_cached_data))
1103
1105
.IsNothing ()) {
1104
1106
return ;
1105
1107
}
1106
-
1107
- if (args.This ()
1108
- ->Set (env->context (),
1108
+ if (self->Set (env->context (),
1109
1109
env->source_url_string (),
1110
1110
v8_script->GetSourceURL ())
1111
- .IsNothing ())
1111
+ .IsNothing ()) {
1112
1112
return ;
1113
-
1114
- if (args.This ()
1115
- ->Set (env->context (),
1113
+ }
1114
+ if (self->Set (env->context (),
1116
1115
env->source_map_url_string (),
1117
1116
v8_script->GetSourceMappingURL ())
1118
- .IsNothing ())
1117
+ .IsNothing ()) {
1119
1118
return ;
1119
+ }
1120
1120
1121
1121
TRACE_EVENT_END0 (TRACING_CATEGORY_NODE2 (vm, script), " ContextifyScript::New" );
1122
1122
}
@@ -1566,25 +1566,35 @@ MaybeLocal<Object> ContextifyFunction::CompileFunctionAndCacheResult(
1566
1566
return {};
1567
1567
}
1568
1568
1569
- Isolate* isolate = env->isolate ();
1570
- Local<Object> result = Object::New (isolate);
1571
- if (result->Set (parsing_context, env->function_string (), fn).IsNothing ())
1572
- return {};
1573
-
1574
- // ScriptOrigin::ResourceName() returns SourceURL magic comment content if
1575
- // present.
1576
- if (result
1577
- ->Set (parsing_context,
1578
- env->source_url_string (),
1579
- fn->GetScriptOrigin ().ResourceName ())
1580
- .IsNothing ()) {
1581
- return {};
1569
+ auto tmpl = env->compiled_function_template ();
1570
+ if (tmpl.IsEmpty ()) {
1571
+ static constexpr std::string_view names[] = {
1572
+ " function" ,
1573
+ " sourceURL" ,
1574
+ " sourceMapURL" ,
1575
+ " cachedDataRejected" ,
1576
+ " cachedDataProduced" ,
1577
+ " cachedData" ,
1578
+ };
1579
+ tmpl = DictionaryTemplate::New (env->isolate (), names);
1580
+ env->set_compiled_function_template (tmpl);
1582
1581
}
1583
- if (result
1584
- ->Set (parsing_context,
1585
- env->source_map_url_string (),
1586
- fn->GetScriptOrigin ().SourceMapUrl ())
1587
- .IsNothing ()) {
1582
+
1583
+ auto scriptOrigin = fn->GetScriptOrigin ();
1584
+ MaybeLocal<Value> values[] = {
1585
+ fn,
1586
+ // ScriptOrigin::ResourceName() returns SourceURL magic comment content if
1587
+ // present.
1588
+ scriptOrigin.ResourceName (),
1589
+ scriptOrigin.SourceMapUrl (),
1590
+ // These are conditionally filled in by StoreCodeCacheResult below.
1591
+ Undefined (env->isolate ()), // cachedDataRejected
1592
+ Undefined (env->isolate ()), // cachedDataProduced
1593
+ Undefined (env->isolate ()), // cachedData
1594
+ };
1595
+
1596
+ Local<Object> result;
1597
+ if (!NewDictionaryInstance (env->context (), tmpl, values).ToLocal (&result)) {
1588
1598
return {};
1589
1599
}
1590
1600
@@ -1799,12 +1809,12 @@ static void CompileFunctionForCJSLoader(
1799
1809
// be reparsed as ESM.
1800
1810
Utf8Value filename_utf8 (isolate, filename);
1801
1811
std::string url = url::FromFilePath (filename_utf8.ToStringView ());
1802
- Local<String > url_value;
1803
- if (!String::NewFromUtf8 (isolate , url. c_str () ).ToLocal (&url_value)) {
1812
+ Local<Value > url_value;
1813
+ if (!ToV8Value (context , url).ToLocal (&url_value)) {
1804
1814
return ;
1805
1815
}
1806
- can_parse_as_esm =
1807
- ShouldRetryAsESM ( realm, cjs_message->Get (), code, url_value);
1816
+ can_parse_as_esm = ShouldRetryAsESM (
1817
+ realm, cjs_message->Get (), code, url_value. As <String>() );
1808
1818
if (!can_parse_as_esm) {
1809
1819
// The syntax error is not related to ESM, throw the original error.
1810
1820
isolate->ThrowException (cjs_exception);
@@ -1827,15 +1837,22 @@ static void CompileFunctionForCJSLoader(
1827
1837
}
1828
1838
}
1829
1839
1840
+ auto tmpl = env->compiled_function_cjs_template ();
1841
+ if (tmpl.IsEmpty ()) {
1842
+ static constexpr std::string_view names[] = {
1843
+ " cachedDataRejected" ,
1844
+ " sourceMapURL" ,
1845
+ " sourceURL" ,
1846
+ " function" ,
1847
+ " canParseAsESM" ,
1848
+ };
1849
+ tmpl = DictionaryTemplate::New (isolate, names);
1850
+ env->set_compiled_function_cjs_template (tmpl);
1851
+ }
1852
+
1830
1853
Local<Value> undefined = v8::Undefined (isolate);
1831
- Local<Name> names[] = {
1832
- env->cached_data_rejected_string (),
1833
- env->source_map_url_string (),
1834
- env->source_url_string (),
1835
- env->function_string (),
1836
- FIXED_ONE_BYTE_STRING (isolate, " canParseAsESM" ),
1837
- };
1838
- Local<Value> values[] = {
1854
+
1855
+ MaybeLocal<Value> values[] = {
1839
1856
Boolean::New (isolate, cache_rejected),
1840
1857
fn.IsEmpty () ? undefined : fn->GetScriptOrigin ().SourceMapUrl (),
1841
1858
// ScriptOrigin::ResourceName() returns SourceURL magic comment content if
@@ -1844,9 +1861,10 @@ static void CompileFunctionForCJSLoader(
1844
1861
fn.IsEmpty () ? undefined : fn.As <Value>(),
1845
1862
Boolean::New (isolate, can_parse_as_esm),
1846
1863
};
1847
- Local<Object> result = Object::New (
1848
- isolate, v8::Null (isolate), &names[0 ], &values[0 ], arraysize (names));
1849
- args.GetReturnValue ().Set (result);
1864
+ Local<Object> result;
1865
+ if (NewDictionaryInstance (env->context (), tmpl, values).ToLocal (&result)) {
1866
+ args.GetReturnValue ().Set (result);
1867
+ }
1850
1868
}
1851
1869
1852
1870
bool ShouldRetryAsESM (Realm* realm,
0 commit comments