@@ -281,6 +281,15 @@ void ContextifyContext::WeakCallback(
281
281
delete context;
282
282
}
283
283
284
+ void ContextifyContext::WeakCallbackCompileFn (
285
+ const WeakCallbackInfo<CompileFnEntry>& data) {
286
+ CompileFnEntry* entry = data.GetParameter ();
287
+ if (entry->env ->compile_fn_entries .erase (entry) != 0 ) {
288
+ entry->env ->id_to_function_map .erase (entry->id );
289
+ delete entry;
290
+ }
291
+ }
292
+
284
293
// static
285
294
ContextifyContext* ContextifyContext::ContextFromContextifiedSandbox (
286
295
Environment* env,
@@ -1039,7 +1048,30 @@ void ContextifyContext::CompileFunction(
1039
1048
data + cached_data_buf->ByteOffset (), cached_data_buf->ByteLength ());
1040
1049
}
1041
1050
1042
- ScriptOrigin origin (filename, line_offset, column_offset);
1051
+ // Get the function id
1052
+ uint32_t id = env->get_next_function_id ();
1053
+
1054
+ // Set host_defined_options
1055
+ Local<PrimitiveArray> host_defined_options =
1056
+ PrimitiveArray::New (isolate, loader::HostDefinedOptions::kLength );
1057
+ host_defined_options->Set (
1058
+ isolate,
1059
+ loader::HostDefinedOptions::kType ,
1060
+ Number::New (isolate, loader::ScriptType::kFunction ));
1061
+ host_defined_options->Set (
1062
+ isolate, loader::HostDefinedOptions::kID , Number::New (isolate, id));
1063
+
1064
+ ScriptOrigin origin (filename,
1065
+ line_offset, // line offset
1066
+ column_offset, // column offset
1067
+ True (isolate), // is cross origin
1068
+ Local<Integer>(), // script id
1069
+ Local<Value>(), // source map URL
1070
+ False (isolate), // is opaque (?)
1071
+ False (isolate), // is WASM
1072
+ False (isolate), // is ES Module
1073
+ host_defined_options);
1074
+
1043
1075
ScriptCompiler::Source source (code, origin, cached_data);
1044
1076
ScriptCompiler::CompileOptions options;
1045
1077
if (source.GetCachedData () == nullptr ) {
@@ -1073,38 +1105,45 @@ void ContextifyContext::CompileFunction(
1073
1105
}
1074
1106
}
1075
1107
1076
- MaybeLocal<Function> maybe_fun = ScriptCompiler::CompileFunctionInContext (
1108
+ MaybeLocal<Function> maybe_fn = ScriptCompiler::CompileFunctionInContext (
1077
1109
parsing_context, &source, params.size (), params.data (),
1078
1110
context_extensions.size (), context_extensions.data (), options);
1079
1111
1080
- Local<Function> fun;
1081
- if (maybe_fun.IsEmpty () || !maybe_fun.ToLocal (&fun)) {
1112
+ if (maybe_fn.IsEmpty ()) {
1082
1113
ContextifyScript::DecorateErrorStack (env, try_catch);
1083
1114
try_catch.ReThrow ();
1084
1115
return ;
1085
1116
}
1117
+ Local<Function> fn = maybe_fn.ToLocalChecked ();
1118
+ env->id_to_function_map .emplace (std::piecewise_construct,
1119
+ std::make_tuple (id),
1120
+ std::make_tuple (isolate, fn));
1121
+ CompileFnEntry* gc_entry = new CompileFnEntry (env, id);
1122
+ env->id_to_function_map [id].SetWeak (gc_entry,
1123
+ WeakCallbackCompileFn,
1124
+ v8::WeakCallbackType::kParameter );
1086
1125
1087
1126
if (produce_cached_data) {
1088
1127
const std::unique_ptr<ScriptCompiler::CachedData> cached_data (
1089
- ScriptCompiler::CreateCodeCacheForFunction (fun ));
1128
+ ScriptCompiler::CreateCodeCacheForFunction (fn ));
1090
1129
bool cached_data_produced = cached_data != nullptr ;
1091
1130
if (cached_data_produced) {
1092
1131
MaybeLocal<Object> buf = Buffer::Copy (
1093
1132
env,
1094
1133
reinterpret_cast <const char *>(cached_data->data ),
1095
1134
cached_data->length );
1096
- if (fun ->Set (
1135
+ if (fn ->Set (
1097
1136
parsing_context,
1098
1137
env->cached_data_string (),
1099
1138
buf.ToLocalChecked ()).IsNothing ()) return ;
1100
1139
}
1101
- if (fun ->Set (
1140
+ if (fn ->Set (
1102
1141
parsing_context,
1103
1142
env->cached_data_produced_string (),
1104
1143
Boolean::New (isolate, cached_data_produced)).IsNothing ()) return ;
1105
1144
}
1106
1145
1107
- args.GetReturnValue ().Set (fun );
1146
+ args.GetReturnValue ().Set (fn );
1108
1147
}
1109
1148
1110
1149
0 commit comments