@@ -45,6 +45,7 @@ using v8::Array;
4545using v8::ArrayBufferView;
4646using v8::Boolean;
4747using v8::Context;
48+ using v8::DictionaryTemplate;
4849using v8::EscapableHandleScope;
4950using v8::Function;
5051using v8::FunctionCallbackInfo;
@@ -1088,35 +1089,28 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
10881089 new_cached_data.reset (ScriptCompiler::CreateCodeCache (v8_script));
10891090 }
10901091
1092+ auto self = args.This ();
1093+
10911094 if (contextify_script->object ()
10921095 ->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,
10991099 compile_options,
11001100 source,
11011101 produce_cached_data,
11021102 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 (),
11091105 env->source_url_string (),
11101106 v8_script->GetSourceURL ())
1111- .IsNothing ())
1112- return ;
1113-
1114- if (args.This ()
1115- ->Set (env->context (),
1107+ .IsNothing () ||
1108+ self->Set (env->context (),
11161109 env->source_map_url_string (),
11171110 v8_script->GetSourceMappingURL ())
1118- .IsNothing ())
1111+ .IsNothing ()) {
11191112 return ;
1113+ }
11201114
11211115 TRACE_EVENT_END0 (TRACING_CATEGORY_NODE2 (vm, script), " ContextifyScript::New" );
11221116}
@@ -1566,25 +1560,35 @@ MaybeLocal<Object> ContextifyFunction::CompileFunctionAndCacheResult(
15661560 return {};
15671561 }
15681562
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);
15821575 }
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)) {
15881592 return {};
15891593 }
15901594
@@ -1800,7 +1804,7 @@ static void CompileFunctionForCJSLoader(
18001804 Utf8Value filename_utf8 (isolate, filename);
18011805 std::string url = url::FromFilePath (filename_utf8.ToStringView ());
18021806 Local<String> url_value;
1803- if (!String::NewFromUtf8 (isolate , url. c_str () ).ToLocal (&url_value)) {
1807+ if (!ToV8Value (context , url).ToLocal (&url_value)) {
18041808 return ;
18051809 }
18061810 can_parse_as_esm =
@@ -1827,15 +1831,22 @@ static void CompileFunctionForCJSLoader(
18271831 }
18281832 }
18291833
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+
18301847 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[] = {
18391850 Boolean::New (isolate, cache_rejected),
18401851 fn.IsEmpty () ? undefined : fn->GetScriptOrigin ().SourceMapUrl (),
18411852 // ScriptOrigin::ResourceName() returns SourceURL magic comment content if
@@ -1844,9 +1855,10 @@ static void CompileFunctionForCJSLoader(
18441855 fn.IsEmpty () ? undefined : fn.As <Value>(),
18451856 Boolean::New (isolate, can_parse_as_esm),
18461857 };
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+ }
18501862}
18511863
18521864bool ShouldRetryAsESM (Realm* realm,
0 commit comments