@@ -285,6 +285,15 @@ void ContextifyContext::WeakCallback(
285
285
delete context;
286
286
}
287
287
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
+
288
297
// static
289
298
ContextifyContext* ContextifyContext::ContextFromContextifiedSandbox (
290
299
Environment* env,
@@ -1027,7 +1036,30 @@ void ContextifyContext::CompileFunction(
1027
1036
data + cached_data_buf->ByteOffset (), cached_data_buf->ByteLength ());
1028
1037
}
1029
1038
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
+
1031
1063
ScriptCompiler::Source source (code, origin, cached_data);
1032
1064
ScriptCompiler::CompileOptions options;
1033
1065
if (source.GetCachedData () == nullptr ) {
@@ -1061,38 +1093,45 @@ void ContextifyContext::CompileFunction(
1061
1093
}
1062
1094
}
1063
1095
1064
- MaybeLocal<Function> maybe_fun = ScriptCompiler::CompileFunctionInContext (
1096
+ MaybeLocal<Function> maybe_fn = ScriptCompiler::CompileFunctionInContext (
1065
1097
parsing_context, &source, params.size (), params.data (),
1066
1098
context_extensions.size (), context_extensions.data (), options);
1067
1099
1068
- Local<Function> fun;
1069
- if (maybe_fun.IsEmpty () || !maybe_fun.ToLocal (&fun)) {
1100
+ if (maybe_fn.IsEmpty ()) {
1070
1101
DecorateErrorStack (env, try_catch);
1071
1102
try_catch.ReThrow ();
1072
1103
return ;
1073
1104
}
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 );
1074
1113
1075
1114
if (produce_cached_data) {
1076
1115
const std::unique_ptr<ScriptCompiler::CachedData> cached_data (
1077
- ScriptCompiler::CreateCodeCacheForFunction (fun ));
1116
+ ScriptCompiler::CreateCodeCacheForFunction (fn ));
1078
1117
bool cached_data_produced = cached_data != nullptr ;
1079
1118
if (cached_data_produced) {
1080
1119
MaybeLocal<Object> buf = Buffer::Copy (
1081
1120
env,
1082
1121
reinterpret_cast <const char *>(cached_data->data ),
1083
1122
cached_data->length );
1084
- if (fun ->Set (
1123
+ if (fn ->Set (
1085
1124
parsing_context,
1086
1125
env->cached_data_string (),
1087
1126
buf.ToLocalChecked ()).IsNothing ()) return ;
1088
1127
}
1089
- if (fun ->Set (
1128
+ if (fn ->Set (
1090
1129
parsing_context,
1091
1130
env->cached_data_produced_string (),
1092
1131
Boolean::New (isolate, cached_data_produced)).IsNothing ()) return ;
1093
1132
}
1094
1133
1095
- args.GetReturnValue ().Set (fun );
1134
+ args.GetReturnValue ().Set (fn );
1096
1135
}
1097
1136
1098
1137
0 commit comments