@@ -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,28 @@ 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
- .IsNothing ()) {
1094
- return ;
1095
- }
1096
-
1097
- if (StoreCodeCacheResult (env,
1098
- args.This (),
1096
+ .IsNothing () ||
1097
+ StoreCodeCacheResult (env,
1098
+ self,
1099
1099
compile_options,
1100
1100
source,
1101
1101
produce_cached_data,
1102
1102
std::move (new_cached_data))
1103
- .IsNothing ()) {
1104
- return ;
1105
- }
1106
-
1107
- if (args.This ()
1108
- ->Set (env->context (),
1103
+ .IsNothing () ||
1104
+ self->Set (env->context (),
1109
1105
env->source_url_string (),
1110
1106
v8_script->GetSourceURL ())
1111
- .IsNothing ())
1112
- return ;
1113
-
1114
- if (args.This ()
1115
- ->Set (env->context (),
1107
+ .IsNothing () ||
1108
+ self->Set (env->context (),
1116
1109
env->source_map_url_string (),
1117
1110
v8_script->GetSourceMappingURL ())
1118
- .IsNothing ())
1111
+ .IsNothing ()) {
1119
1112
return ;
1113
+ }
1120
1114
1121
1115
TRACE_EVENT_END0 (TRACING_CATEGORY_NODE2 (vm, script), " ContextifyScript::New" );
1122
1116
}
@@ -1566,25 +1560,35 @@ MaybeLocal<Object> ContextifyFunction::CompileFunctionAndCacheResult(
1566
1560
return {};
1567
1561
}
1568
1562
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 {};
1563
+ auto tmpl = env->compiled_function_template ();
1564
+ if (tmpl.IsEmpty ()) {
1565
+ static constexpr std::string_view names[] = {
1566
+ " function" ,
1567
+ " sourceURL" ,
1568
+ " sourceMapURL" ,
1569
+ " cachedDataRejected" ,
1570
+ " cachedDataProduced" ,
1571
+ " cachedData" ,
1572
+ };
1573
+ tmpl = DictionaryTemplate::New (env->isolate (), names);
1574
+ env->set_compiled_function_template (tmpl);
1582
1575
}
1583
- if (result
1584
- ->Set (parsing_context,
1585
- env->source_map_url_string (),
1586
- fn->GetScriptOrigin ().SourceMapUrl ())
1587
- .IsNothing ()) {
1576
+
1577
+ auto scriptOrigin = fn->GetScriptOrigin ();
1578
+ MaybeLocal<Value> values[] = {
1579
+ fn,
1580
+ // ScriptOrigin::ResourceName() returns SourceURL magic comment content if
1581
+ // present.
1582
+ scriptOrigin.ResourceName (),
1583
+ scriptOrigin.SourceMapUrl (),
1584
+ // These are conditionally filled in by StoreCodeCacheResult below.
1585
+ Undefined (env->isolate ()), // cachedDataRejected
1586
+ Undefined (env->isolate ()), // cachedDataProduced
1587
+ Undefined (env->isolate ()), // cachedData
1588
+ };
1589
+
1590
+ Local<Object> result;
1591
+ if (!NewDictionaryInstance (env->context (), tmpl, values).ToLocal (&result)) {
1588
1592
return {};
1589
1593
}
1590
1594
@@ -1800,7 +1804,7 @@ static void CompileFunctionForCJSLoader(
1800
1804
Utf8Value filename_utf8 (isolate, filename);
1801
1805
std::string url = url::FromFilePath (filename_utf8.ToStringView ());
1802
1806
Local<String> url_value;
1803
- if (!String::NewFromUtf8 (isolate , url. c_str () ).ToLocal (&url_value)) {
1807
+ if (!ToV8Value (context , url).ToLocal (&url_value)) {
1804
1808
return ;
1805
1809
}
1806
1810
can_parse_as_esm =
@@ -1827,15 +1831,22 @@ static void CompileFunctionForCJSLoader(
1827
1831
}
1828
1832
}
1829
1833
1834
+ auto tmpl = env->compiled_function_cjs_template ();
1835
+ if (tmpl.IsEmpty ()) {
1836
+ static constexpr std::string_view names[] = {
1837
+ " cachedDataRejected" ,
1838
+ " sourceMapURL" ,
1839
+ " sourceURL" ,
1840
+ " function" ,
1841
+ " canParseAsESM" ,
1842
+ };
1843
+ tmpl = DictionaryTemplate::New (isolate, names);
1844
+ env->set_compiled_function_cjs_template (tmpl);
1845
+ }
1846
+
1830
1847
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[] = {
1848
+
1849
+ MaybeLocal<Value> values[] = {
1839
1850
Boolean::New (isolate, cache_rejected),
1840
1851
fn.IsEmpty () ? undefined : fn->GetScriptOrigin ().SourceMapUrl (),
1841
1852
// ScriptOrigin::ResourceName() returns SourceURL magic comment content if
@@ -1844,9 +1855,10 @@ static void CompileFunctionForCJSLoader(
1844
1855
fn.IsEmpty () ? undefined : fn.As <Value>(),
1845
1856
Boolean::New (isolate, can_parse_as_esm),
1846
1857
};
1847
- Local<Object> result = Object::New (
1848
- isolate, v8::Null (isolate), &names[0 ], &values[0 ], arraysize (names));
1849
- args.GetReturnValue ().Set (result);
1858
+ Local<Object> result;
1859
+ if (NewDictionaryInstance (env->context (), tmpl, values).ToLocal (&result)) {
1860
+ args.GetReturnValue ().Set (result);
1861
+ }
1850
1862
}
1851
1863
1852
1864
bool ShouldRetryAsESM (Realm* realm,
0 commit comments