summaryrefslogtreecommitdiff
path: root/src/node_contextify.cc
diff options
context:
space:
mode:
authorUjjwal Sharma <usharma1998@gmail.com>2018-11-07 22:17:09 +0530
committerUjjwal Sharma <usharma1998@gmail.com>2019-02-19 20:58:37 +0530
commit5f8ccecaa2e44c4a04db95ccd278a7078c14dd77 (patch)
treee56a5d9bff97dc73c5898f637d19e78627762529 /src/node_contextify.cc
parentd345b0dc128d99afc8476f58ed5546b43d52d30a (diff)
downloadandroid-node-v8-5f8ccecaa2e44c4a04db95ccd278a7078c14dd77.tar.gz
android-node-v8-5f8ccecaa2e44c4a04db95ccd278a7078c14dd77.tar.bz2
android-node-v8-5f8ccecaa2e44c4a04db95ccd278a7078c14dd77.zip
module: revert module._compile to original state if module is patched
PR-URL: https://github.com/nodejs/node/pull/21573 Fixes: https://github.com/nodejs/node/issues/17396 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'src/node_contextify.cc')
-rw-r--r--src/node_contextify.cc55
1 files changed, 47 insertions, 8 deletions
diff --git a/src/node_contextify.cc b/src/node_contextify.cc
index 343342408b..61f60576d2 100644
--- a/src/node_contextify.cc
+++ b/src/node_contextify.cc
@@ -285,6 +285,15 @@ void ContextifyContext::WeakCallback(
delete context;
}
+void ContextifyContext::WeakCallbackCompileFn(
+ const WeakCallbackInfo<CompileFnEntry>& data) {
+ CompileFnEntry* entry = data.GetParameter();
+ if (entry->env->compile_fn_entries.erase(entry) != 0) {
+ entry->env->id_to_function_map.erase(entry->id);
+ delete entry;
+ }
+}
+
// static
ContextifyContext* ContextifyContext::ContextFromContextifiedSandbox(
Environment* env,
@@ -1027,7 +1036,30 @@ void ContextifyContext::CompileFunction(
data + cached_data_buf->ByteOffset(), cached_data_buf->ByteLength());
}
- ScriptOrigin origin(filename, line_offset, column_offset, True(isolate));
+ // Get the function id
+ uint32_t id = env->get_next_function_id();
+
+ // Set host_defined_options
+ Local<PrimitiveArray> host_defined_options =
+ PrimitiveArray::New(isolate, loader::HostDefinedOptions::kLength);
+ host_defined_options->Set(
+ isolate,
+ loader::HostDefinedOptions::kType,
+ Number::New(isolate, loader::ScriptType::kFunction));
+ host_defined_options->Set(
+ isolate, loader::HostDefinedOptions::kID, Number::New(isolate, id));
+
+ ScriptOrigin origin(filename,
+ line_offset, // line offset
+ column_offset, // column offset
+ True(isolate), // is cross origin
+ Local<Integer>(), // script id
+ Local<Value>(), // source map URL
+ False(isolate), // is opaque (?)
+ False(isolate), // is WASM
+ False(isolate), // is ES Module
+ host_defined_options);
+
ScriptCompiler::Source source(code, origin, cached_data);
ScriptCompiler::CompileOptions options;
if (source.GetCachedData() == nullptr) {
@@ -1061,38 +1093,45 @@ void ContextifyContext::CompileFunction(
}
}
- MaybeLocal<Function> maybe_fun = ScriptCompiler::CompileFunctionInContext(
+ MaybeLocal<Function> maybe_fn = ScriptCompiler::CompileFunctionInContext(
parsing_context, &source, params.size(), params.data(),
context_extensions.size(), context_extensions.data(), options);
- Local<Function> fun;
- if (maybe_fun.IsEmpty() || !maybe_fun.ToLocal(&fun)) {
+ if (maybe_fn.IsEmpty()) {
DecorateErrorStack(env, try_catch);
try_catch.ReThrow();
return;
}
+ Local<Function> fn = maybe_fn.ToLocalChecked();
+ env->id_to_function_map.emplace(std::piecewise_construct,
+ std::make_tuple(id),
+ std::make_tuple(isolate, fn));
+ CompileFnEntry* gc_entry = new CompileFnEntry(env, id);
+ env->id_to_function_map[id].SetWeak(gc_entry,
+ WeakCallbackCompileFn,
+ v8::WeakCallbackType::kParameter);
if (produce_cached_data) {
const std::unique_ptr<ScriptCompiler::CachedData> cached_data(
- ScriptCompiler::CreateCodeCacheForFunction(fun));
+ ScriptCompiler::CreateCodeCacheForFunction(fn));
bool cached_data_produced = cached_data != nullptr;
if (cached_data_produced) {
MaybeLocal<Object> buf = Buffer::Copy(
env,
reinterpret_cast<const char*>(cached_data->data),
cached_data->length);
- if (fun->Set(
+ if (fn->Set(
parsing_context,
env->cached_data_string(),
buf.ToLocalChecked()).IsNothing()) return;
}
- if (fun->Set(
+ if (fn->Set(
parsing_context,
env->cached_data_produced_string(),
Boolean::New(isolate, cached_data_produced)).IsNothing()) return;
}
- args.GetReturnValue().Set(fun);
+ args.GetReturnValue().Set(fn);
}