@@ -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,34 @@ 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)
10931096 .IsNothing ()) {
10941097 return ;
10951098 }
1096-
10971099 if (StoreCodeCacheResult (env,
1098- args. This () ,
1100+ self ,
10991101 compile_options,
11001102 source,
11011103 produce_cached_data,
11021104 std::move (new_cached_data))
11031105 .IsNothing ()) {
11041106 return ;
11051107 }
1106-
1107- if (args.This ()
1108- ->Set (env->context (),
1108+ if (self->Set (env->context (),
11091109 env->source_url_string (),
11101110 v8_script->GetSourceURL ())
1111- .IsNothing ())
1111+ .IsNothing ()) {
11121112 return ;
1113-
1114- if (args.This ()
1115- ->Set (env->context (),
1113+ }
1114+ if (self->Set (env->context (),
11161115 env->source_map_url_string (),
11171116 v8_script->GetSourceMappingURL ())
1118- .IsNothing ())
1117+ .IsNothing ()) {
11191118 return ;
1119+ }
11201120
11211121 TRACE_EVENT_END0 (TRACING_CATEGORY_NODE2 (vm, script), " ContextifyScript::New" );
11221122}
@@ -1566,25 +1566,35 @@ MaybeLocal<Object> ContextifyFunction::CompileFunctionAndCacheResult(
15661566 return {};
15671567 }
15681568
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);
15821581 }
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)) {
15881598 return {};
15891599 }
15901600
@@ -1799,12 +1809,12 @@ static void CompileFunctionForCJSLoader(
17991809 // be reparsed as ESM.
18001810 Utf8Value filename_utf8 (isolate, filename);
18011811 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)) {
18041814 return ;
18051815 }
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>() );
18081818 if (!can_parse_as_esm) {
18091819 // The syntax error is not related to ESM, throw the original error.
18101820 isolate->ThrowException (cjs_exception);
@@ -1827,15 +1837,22 @@ static void CompileFunctionForCJSLoader(
18271837 }
18281838 }
18291839
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+
18301853 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[] = {
18391856 Boolean::New (isolate, cache_rejected),
18401857 fn.IsEmpty () ? undefined : fn->GetScriptOrigin ().SourceMapUrl (),
18411858 // ScriptOrigin::ResourceName() returns SourceURL magic comment content if
@@ -1844,9 +1861,10 @@ static void CompileFunctionForCJSLoader(
18441861 fn.IsEmpty () ? undefined : fn.As <Value>(),
18451862 Boolean::New (isolate, can_parse_as_esm),
18461863 };
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+ }
18501868}
18511869
18521870bool ShouldRetryAsESM (Realm* realm,
0 commit comments