@@ -285,6 +285,15 @@ void ContextifyContext::WeakCallback(
285285 delete context;
286286}
287287
288+ void ContextifyContext::WeakCallbackCompileFn (
289+ const WeakCallbackInfo<CompileFnEntry>& data) {
290+ CompileFnEntry* entry = data.GetParameter ();
291+ if (entry->env ->compile_fn_entries .erase (entry) != 0 ) {
292+ entry->env ->id_to_function_map .erase (entry->id );
293+ delete entry;
294+ }
295+ }
296+
288297// static
289298ContextifyContext* ContextifyContext::ContextFromContextifiedSandbox (
290299 Environment* env,
@@ -1027,7 +1036,30 @@ void ContextifyContext::CompileFunction(
10271036 data + cached_data_buf->ByteOffset (), cached_data_buf->ByteLength ());
10281037 }
10291038
1030- ScriptOrigin origin (filename, line_offset, column_offset, True (isolate));
1039+ // Get the function id
1040+ uint32_t id = env->get_next_function_id ();
1041+
1042+ // Set host_defined_options
1043+ Local<PrimitiveArray> host_defined_options =
1044+ PrimitiveArray::New (isolate, loader::HostDefinedOptions::kLength );
1045+ host_defined_options->Set (
1046+ isolate,
1047+ loader::HostDefinedOptions::kType ,
1048+ Number::New (isolate, loader::ScriptType::kFunction ));
1049+ host_defined_options->Set (
1050+ isolate, loader::HostDefinedOptions::kID , Number::New (isolate, id));
1051+
1052+ ScriptOrigin origin (filename,
1053+ line_offset, // line offset
1054+ column_offset, // column offset
1055+ True (isolate), // is cross origin
1056+ Local<Integer>(), // script id
1057+ Local<Value>(), // source map URL
1058+ False (isolate), // is opaque (?)
1059+ False (isolate), // is WASM
1060+ False (isolate), // is ES Module
1061+ host_defined_options);
1062+
10311063 ScriptCompiler::Source source (code, origin, cached_data);
10321064 ScriptCompiler::CompileOptions options;
10331065 if (source.GetCachedData () == nullptr ) {
@@ -1061,38 +1093,45 @@ void ContextifyContext::CompileFunction(
10611093 }
10621094 }
10631095
1064- MaybeLocal<Function> maybe_fun = ScriptCompiler::CompileFunctionInContext (
1096+ MaybeLocal<Function> maybe_fn = ScriptCompiler::CompileFunctionInContext (
10651097 parsing_context, &source, params.size (), params.data (),
10661098 context_extensions.size (), context_extensions.data (), options);
10671099
1068- Local<Function> fun;
1069- if (maybe_fun.IsEmpty () || !maybe_fun.ToLocal (&fun)) {
1100+ if (maybe_fn.IsEmpty ()) {
10701101 DecorateErrorStack (env, try_catch);
10711102 try_catch.ReThrow ();
10721103 return ;
10731104 }
1105+ Local<Function> fn = maybe_fn.ToLocalChecked ();
1106+ env->id_to_function_map .emplace (std::piecewise_construct,
1107+ std::make_tuple (id),
1108+ std::make_tuple (isolate, fn));
1109+ CompileFnEntry* gc_entry = new CompileFnEntry (env, id);
1110+ env->id_to_function_map [id].SetWeak (gc_entry,
1111+ WeakCallbackCompileFn,
1112+ v8::WeakCallbackType::kParameter );
10741113
10751114 if (produce_cached_data) {
10761115 const std::unique_ptr<ScriptCompiler::CachedData> cached_data (
1077- ScriptCompiler::CreateCodeCacheForFunction (fun ));
1116+ ScriptCompiler::CreateCodeCacheForFunction (fn ));
10781117 bool cached_data_produced = cached_data != nullptr ;
10791118 if (cached_data_produced) {
10801119 MaybeLocal<Object> buf = Buffer::Copy (
10811120 env,
10821121 reinterpret_cast <const char *>(cached_data->data ),
10831122 cached_data->length );
1084- if (fun ->Set (
1123+ if (fn ->Set (
10851124 parsing_context,
10861125 env->cached_data_string (),
10871126 buf.ToLocalChecked ()).IsNothing ()) return ;
10881127 }
1089- if (fun ->Set (
1128+ if (fn ->Set (
10901129 parsing_context,
10911130 env->cached_data_produced_string (),
10921131 Boolean::New (isolate, cached_data_produced)).IsNothing ()) return ;
10931132 }
10941133
1095- args.GetReturnValue ().Set (fun );
1134+ args.GetReturnValue ().Set (fn );
10961135}
10971136
10981137
0 commit comments