src: define per-isolate internal bindings registration callback

Bindings that need to be loaded in distinct contexts of node::Realm in
the same node::Environment instance needs to share the per-isolate
templates.

Add a new binding registration callback, which is called with
per-IsolateData. This allows bindings to define templates and
share them across realms eagerly, and avoid accidental modification on
the templates when the per-context callback is called multiple times.

This also tracks loaded bindings and built-in modules with node::Realm.

PR-URL: https://github.com/nodejs/node/pull/45547
Refs: https://github.com/nodejs/node/issues/42528
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
legendecas authored and RafaelGSS committed 2023-01-05 13:11:40 -03:00
1 parent 0552b13232
commit db535b6caa
13 files changed
+236 -138

No files matched your search

+3
View File
@@ -821,6 +821,7 @@ void Environment::set_process_exit_handler(
#undef VY
#undef VP
#define VM(PropertyName) V(PropertyName##_binding, v8::FunctionTemplate)
#define V(PropertyName, TypeName) \
inline v8::Local<TypeName> IsolateData::PropertyName() const { \
return PropertyName##_.Get(isolate_); \
@@ -829,7 +830,9 @@ void Environment::set_process_exit_handler(
PropertyName##_.Set(isolate_, value); \
}
PER_ISOLATE_TEMPLATE_PROPERTIES(V)
NODE_BINDINGS_WITH_PER_ISOLATE_INIT(VM)
#undef V
#undef VM
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
+5 -21
View File
@@ -314,6 +314,7 @@ IsolateDataSerializeInfo IsolateData::Serialize(SnapshotCreator* creator) {
info.primitive_values.push_back(creator->AddData(async_wrap_provider(i)));
uint32_t id = 0;
#define VM(PropertyName) V(PropertyName##_binding, FunctionTemplate)
#define V(PropertyName, TypeName) \
do { \
Local<TypeName> field = PropertyName(); \
@@ -324,6 +325,7 @@ IsolateDataSerializeInfo IsolateData::Serialize(SnapshotCreator* creator) {
id++; \
} while (0);
PER_ISOLATE_TEMPLATE_PROPERTIES(V)
NODE_BINDINGS_WITH_PER_ISOLATE_INIT(VM)
#undef V
return info;
@@ -368,6 +370,7 @@ void IsolateData::DeserializeProperties(const IsolateDataSerializeInfo* info) {
const std::vector<PropInfo>& values = info->template_values;
i = 0; // index to the array
uint32_t id = 0;
#define VM(PropertyName) V(PropertyName##_binding, FunctionTemplate)
#define V(PropertyName, TypeName) \
do { \
if (values.size() > i && id == values[i].id) { \
@@ -388,6 +391,7 @@ void IsolateData::DeserializeProperties(const IsolateDataSerializeInfo* info) {
} while (0);
PER_ISOLATE_TEMPLATE_PROPERTIES(V);
NODE_BINDINGS_WITH_PER_ISOLATE_INIT(VM);
#undef V
}
@@ -454,12 +458,12 @@ void IsolateData::CreateProperties() {
NODE_ASYNC_PROVIDER_TYPES(V)
#undef V
// TODO(legendecas): eagerly create per isolate templates.
Local<FunctionTemplate> templ = FunctionTemplate::New(isolate());
templ->InstanceTemplate()->SetInternalFieldCount(
BaseObject::kInternalFieldCount);
templ->Inherit(BaseObject::GetConstructorTemplate(this));
set_binding_data_ctor_template(templ);
binding::CreateInternalBindingTemplates(this);
contextify::ContextifyContext::InitializeGlobalTemplates(this);
}
@@ -1580,18 +1584,6 @@ void Environment::PrintInfoForSnapshotIfDebug() {
if (enabled_debug_list()->enabled(DebugCategory::MKSNAPSHOT)) {
fprintf(stderr, "At the exit of the Environment:\n");
principal_realm()->PrintInfoForSnapshot();
fprintf(stderr, "\nBuiltins without cache:\n");
for (const auto& s : builtins_without_cache) {
fprintf(stderr, "%s\n", s.c_str());
}
fprintf(stderr, "\nBuiltins with cache:\n");
for (const auto& s : builtins_with_cache) {
fprintf(stderr, "%s\n", s.c_str());
}
fprintf(stderr, "\nStatic bindings (need to be registered):\n");
for (const auto mod : internal_bindings) {
fprintf(stderr, "%s:%s\n", mod->nm_filename, mod->nm_modname);
}
}
}
@@ -1599,11 +1591,6 @@ EnvSerializeInfo Environment::Serialize(SnapshotCreator* creator) {
EnvSerializeInfo info;
Local<Context> ctx = context();
// Currently all modules are compiled without cache in builtin snapshot
// builder.
info.builtins = std::vector<std::string>(builtins_without_cache.begin(),
builtins_without_cache.end());
info.async_hooks = async_hooks_.Serialize(ctx, creator);
info.immediate_info = immediate_info_.Serialize(ctx, creator);
info.tick_info = tick_info_.Serialize(ctx, creator);
@@ -1649,7 +1636,6 @@ void Environment::DeserializeProperties(const EnvSerializeInfo* info) {
RunDeserializeRequests();
builtins_in_snapshot = info->builtins;
async_hooks_.Deserialize(ctx);
immediate_info_.Deserialize(ctx);
tick_info_.Deserialize(ctx);
@@ -1840,8 +1826,6 @@ void Environment::MemoryInfo(MemoryTracker* tracker) const {
// Iteratable STLs have their own sizes subtracted from the parent
// by default.
tracker->TrackField("isolate_data", isolate_data_);
tracker->TrackField("builtins_with_cache", builtins_with_cache);
tracker->TrackField("builtins_without_cache", builtins_without_cache);
tracker->TrackField("destroy_async_id_list", destroy_async_id_list_);
tracker->TrackField("exec_argv", exec_argv_);
tracker->TrackField("exiting", exiting_);
+6 -9
View File
@@ -155,11 +155,14 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
#undef VS
#undef VP
#define VM(PropertyName) V(PropertyName##_binding, v8::FunctionTemplate)
#define V(PropertyName, TypeName) \
inline v8::Local<TypeName> PropertyName() const; \
inline void set_##PropertyName(v8::Local<TypeName> value);
PER_ISOLATE_TEMPLATE_PROPERTIES(V)
NODE_BINDINGS_WITH_PER_ISOLATE_INIT(VM)
#undef V
#undef VM
inline v8::Local<v8::String> async_wrap_provider(int index) const;
@@ -179,6 +182,7 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
#define VM(PropertyName) V(v8::FunctionTemplate, PropertyName##_binding)
#define VT(PropertyName, TypeName) V(TypeName, PropertyName)
#define V(TypeName, PropertyName) \
v8::Eternal<TypeName> PropertyName ## _;
@@ -186,8 +190,9 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
PER_ISOLATE_SYMBOL_PROPERTIES(VY)
PER_ISOLATE_STRING_PROPERTIES(VS)
PER_ISOLATE_TEMPLATE_PROPERTIES(VT)
NODE_BINDINGS_WITH_PER_ISOLATE_INIT(VM)
#undef V
#undef V
#undef VM
#undef VT
#undef VS
#undef VY
@@ -457,7 +462,6 @@ struct DeserializeRequest {
};
struct EnvSerializeInfo {
std::vector<std::string> builtins;
AsyncHooks::SerializeInfo async_hooks;
TickInfo::SerializeInfo tick_info;
ImmediateInfo::SerializeInfo immediate_info;
@@ -713,13 +717,6 @@ class Environment : public MemoryRetainer {
// List of id's that have been destroyed and need the destroy() cb called.
inline std::vector<double>* destroy_async_id_list();
std::set<struct node_module*> internal_bindings;
std::set<std::string> builtins_with_cache;
std::set<std::string> builtins_without_cache;
// This is only filled during deserialization. We use a vector since
// it's only used for tests.
std::vector<std::string> builtins_in_snapshot;
std::unordered_multimap<int, loader::ModuleWrap*> hash_to_module_map;
std::unordered_map<uint32_t, loader::ModuleWrap*> id_to_module_map;
std::unordered_map<uint32_t, contextify::ContextifyScript*>
+70 -25
View File
@@ -102,6 +102,12 @@
NODE_BUILTIN_BINDINGS(V)
#undef V
#define V(modname) \
void _register_isolate_##modname(node::IsolateData* isolate_data, \
v8::Local<v8::FunctionTemplate> target);
NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V)
#undef V
#ifdef _AIX
// On AIX, dlopen() behaves differently from other operating systems, in that
// it returns unique values from each call, rather than identical values, when
@@ -229,9 +235,12 @@ static bool libc_may_be_musl() { return false; }
namespace node {
using v8::Context;
using v8::EscapableHandleScope;
using v8::Exception;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
@@ -552,50 +561,86 @@ inline struct node_module* FindModule(struct node_module* list,
return mp;
}
static Local<Object> InitInternalBinding(Environment* env,
node_module* mod,
Local<String> module) {
// Internal bindings don't have a "module" object, only exports.
Local<Function> ctor = env->binding_data_ctor_template()
->GetFunction(env->context())
.ToLocalChecked();
Local<Object> exports = ctor->NewInstance(env->context()).ToLocalChecked();
void CreateInternalBindingTemplates(IsolateData* isolate_data) {
#define V(modname) \
do { \
Local<FunctionTemplate> templ = \
FunctionTemplate::New(isolate_data->isolate()); \
templ->InstanceTemplate()->SetInternalFieldCount( \
BaseObject::kInternalFieldCount); \
templ->Inherit(BaseObject::GetConstructorTemplate(isolate_data)); \
_register_isolate_##modname(isolate_data, templ); \
isolate_data->set_##modname##_binding(templ); \
} while (0);
NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V)
#undef V
}
static Local<Object> GetInternalBindingExportObject(IsolateData* isolate_data,
const char* mod_name,
Local<Context> context) {
Local<FunctionTemplate> ctor;
#define V(name) \
if (strcmp(mod_name, #name) == 0) { \
ctor = isolate_data->name##_binding(); \
} else // NOLINT(readability/braces)
NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V)
#undef V
{
ctor = isolate_data->binding_data_ctor_template();
}
Local<Object> obj = ctor->GetFunction(context)
.ToLocalChecked()
->NewInstance(context)
.ToLocalChecked();
return obj;
}
static Local<Object> InitInternalBinding(Realm* realm, node_module* mod) {
EscapableHandleScope scope(realm->isolate());
Local<Context> context = realm->context();
Local<Object> exports = GetInternalBindingExportObject(
realm->isolate_data(), mod->nm_modname, context);
CHECK_NULL(mod->nm_register_func);
CHECK_NOT_NULL(mod->nm_context_register_func);
Local<Value> unused = Undefined(env->isolate());
mod->nm_context_register_func(exports, unused, env->context(), mod->nm_priv);
return exports;
Local<Value> unused = Undefined(realm->isolate());
// Internal bindings don't have a "module" object, only exports.
mod->nm_context_register_func(exports, unused, context, mod->nm_priv);
return scope.Escape(exports);
}
void GetInternalBinding(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Realm* realm = Realm::GetCurrent(args);
Isolate* isolate = realm->isolate();
HandleScope scope(isolate);
Local<Context> context = realm->context();
CHECK(args[0]->IsString());
Local<String> module = args[0].As<String>();
node::Utf8Value module_v(env->isolate(), module);
node::Utf8Value module_v(isolate, module);
Local<Object> exports;
node_module* mod = FindModule(modlist_internal, *module_v, NM_F_INTERNAL);
if (mod != nullptr) {
exports = InitInternalBinding(env, mod, module);
env->internal_bindings.insert(mod);
exports = InitInternalBinding(realm, mod);
realm->internal_bindings.insert(mod);
} else if (!strcmp(*module_v, "constants")) {
exports = Object::New(env->isolate());
CHECK(
exports->SetPrototype(env->context(), Null(env->isolate())).FromJust());
DefineConstants(env->isolate(), exports);
exports = Object::New(isolate);
CHECK(exports->SetPrototype(context, Null(isolate)).FromJust());
DefineConstants(isolate, exports);
} else if (!strcmp(*module_v, "natives")) {
exports = builtins::BuiltinLoader::GetSourceObject(env->context());
exports = builtins::BuiltinLoader::GetSourceObject(context);
// Legacy feature: process.binding('natives').config contains stringified
// config.gypi
CHECK(exports
->Set(env->context(),
env->config_string(),
builtins::BuiltinLoader::GetConfigString(env->isolate()))
->Set(context,
realm->isolate_data()->config_string(),
builtins::BuiltinLoader::GetConfigString(isolate))
.FromJust());
} else {
return THROW_ERR_INVALID_MODULE(env, "No such binding: %s", *module_v);
return THROW_ERR_INVALID_MODULE(isolate, "No such binding: %s", *module_v);
}
args.GetReturnValue().Set(exports);
+15
View File
@@ -24,6 +24,8 @@ static_assert(static_cast<int>(NM_F_LINKED) ==
static_cast<int>(node::ModuleFlags::kLinked),
"NM_F_LINKED != node::ModuleFlags::kLinked");
#define NODE_BINDINGS_WITH_PER_ISOLATE_INIT(V) V(builtins)
#define NODE_BINDING_CONTEXT_AWARE_CPP(modname, regfunc, priv, flags) \
static node::node_module _module = { \
NODE_MODULE_VERSION, \
@@ -44,9 +46,20 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> exports,
namespace node {
// Define a node internal binding that may be loaded in a context of
// a node::Environment.
// If an internal binding needs initializing per-isolate templates, define
// with NODE_BINDING_PER_ISOLATE_INIT too.
#define NODE_BINDING_CONTEXT_AWARE_INTERNAL(modname, regfunc) \
NODE_BINDING_CONTEXT_AWARE_CPP(modname, regfunc, nullptr, NM_F_INTERNAL)
// Define a per-isolate initialization function for a node internal binding.
#define NODE_BINDING_PER_ISOLATE_INIT(modname, per_isolate_func) \
void _register_isolate_##modname(node::IsolateData* isolate_data, \
v8::Local<v8::FunctionTemplate> target) { \
per_isolate_func(isolate_data, target); \
}
// Globals per process
// This is set by node::Init() which is used by embedders
extern bool node_is_initialized;
@@ -87,6 +100,8 @@ class DLib {
// use the __attribute__((constructor)). Need to
// explicitly call the _register* functions.
void RegisterBuiltinBindings();
// Create per-isolate templates for the internal bindings.
void CreateInternalBindingTemplates(IsolateData* isolate_data);
void GetInternalBinding(const v8::FunctionCallbackInfo<v8::Value>& args);
void GetLinkedBinding(const v8::FunctionCallbackInfo<v8::Value>& args);
void DLOpen(const v8::FunctionCallbackInfo<v8::Value>& args);
+64 -67
View File
@@ -13,6 +13,7 @@ using v8::DEFAULT;
using v8::EscapableHandleScope;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::IntegrityLevel;
using v8::Isolate;
using v8::Local;
@@ -20,6 +21,7 @@ using v8::MaybeLocal;
using v8::Name;
using v8::None;
using v8::Object;
using v8::ObjectTemplate;
using v8::PropertyCallbackInfo;
using v8::ScriptCompiler;
using v8::ScriptOrigin;
@@ -371,8 +373,9 @@ MaybeLocal<Function> BuiltinLoader::LookupAndCompileInternal(
// Returns Local<Function> of the compiled module if return_code_cache
// is false (we are only compiling the function).
// Otherwise return a Local<Object> containing the cache.
MaybeLocal<Function> BuiltinLoader::LookupAndCompile(
Local<Context> context, const char* id, Environment* optional_env) {
MaybeLocal<Function> BuiltinLoader::LookupAndCompile(Local<Context> context,
const char* id,
Realm* optional_realm) {
Result result;
std::vector<Local<String>> parameters;
Isolate* isolate = context->GetIsolate();
@@ -426,8 +429,8 @@ MaybeLocal<Function> BuiltinLoader::LookupAndCompile(
MaybeLocal<Function> maybe = GetInstance()->LookupAndCompileInternal(
context, id, &parameters, &result);
if (optional_env != nullptr) {
RecordResult(id, result, optional_env);
if (optional_realm != nullptr) {
RecordResult(id, result, optional_realm);
}
return maybe;
}
@@ -482,18 +485,17 @@ MaybeLocal<Value> BuiltinLoader::CompileAndCall(Local<Context> context,
// all the other cases: the arguments are generated in the JS-land loader.
UNREACHABLE();
}
return CompileAndCall(
context, id, arguments.size(), arguments.data(), realm->env());
return CompileAndCall(context, id, arguments.size(), arguments.data(), realm);
}
MaybeLocal<Value> BuiltinLoader::CompileAndCall(Local<Context> context,
const char* id,
int argc,
Local<Value> argv[],
Environment* optional_env) {
Realm* optional_realm) {
// Arguments must match the parameters specified in
// BuiltinLoader::LookupAndCompile().
MaybeLocal<Function> maybe_fn = LookupAndCompile(context, id, optional_env);
MaybeLocal<Function> maybe_fn = LookupAndCompile(context, id, optional_realm);
Local<Function> fn;
if (!maybe_fn.ToLocal(&fn)) {
return MaybeLocal<Value>();
@@ -596,44 +598,44 @@ void BuiltinLoader::GetBuiltinCategories(
}
void BuiltinLoader::GetCacheUsage(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
Local<Context> context = env->context();
Realm* realm = Realm::GetCurrent(args);
Isolate* isolate = realm->isolate();
Local<Context> context = realm->context();
Local<Object> result = Object::New(isolate);
Local<Value> builtins_with_cache_js;
Local<Value> builtins_without_cache_js;
Local<Value> builtins_in_snapshot_js;
if (!ToV8Value(context, env->builtins_with_cache)
if (!ToV8Value(context, realm->builtins_with_cache)
.ToLocal(&builtins_with_cache_js)) {
return;
}
if (result
->Set(env->context(),
->Set(context,
OneByteString(isolate, "compiledWithCache"),
builtins_with_cache_js)
.IsNothing()) {
return;
}
if (!ToV8Value(context, env->builtins_without_cache)
if (!ToV8Value(context, realm->builtins_without_cache)
.ToLocal(&builtins_without_cache_js)) {
return;
}
if (result
->Set(env->context(),
->Set(context,
OneByteString(isolate, "compiledWithoutCache"),
builtins_without_cache_js)
.IsNothing()) {
return;
}
if (!ToV8Value(context, env->builtins_in_snapshot)
if (!ToV8Value(context, realm->builtins_in_snapshot)
.ToLocal(&builtins_without_cache_js)) {
return;
}
if (result
->Set(env->context(),
->Set(context,
OneByteString(isolate, "compiledInSnapshot"),
builtins_without_cache_js)
.IsNothing()) {
@@ -659,21 +661,21 @@ void BuiltinLoader::ConfigStringGetter(
void BuiltinLoader::RecordResult(const char* id,
BuiltinLoader::Result result,
Environment* env) {
Realm* realm) {
if (result == BuiltinLoader::Result::kWithCache) {
env->builtins_with_cache.insert(id);
realm->builtins_with_cache.insert(id);
} else {
env->builtins_without_cache.insert(id);
realm->builtins_without_cache.insert(id);
}
}
void BuiltinLoader::CompileFunction(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Realm* realm = Realm::GetCurrent(args);
CHECK(args[0]->IsString());
node::Utf8Value id_v(env->isolate(), args[0].As<String>());
node::Utf8Value id_v(realm->isolate(), args[0].As<String>());
const char* id = *id_v;
MaybeLocal<Function> maybe =
GetInstance()->LookupAndCompile(env->context(), id, env);
GetInstance()->LookupAndCompile(realm->context(), id, realm);
Local<Function> fn;
if (maybe.ToLocal(&fn)) {
args.GetReturnValue().Set(fn);
@@ -685,51 +687,44 @@ void BuiltinLoader::HasCachedBuiltins(const FunctionCallbackInfo<Value>& args) {
v8::Boolean::New(args.GetIsolate(), GetInstance()->has_code_cache_));
}
// TODO(joyeecheung): It is somewhat confusing that Class::Initialize
// is used to initialize to the binding, but it is the current convention.
// Rename this across the code base to something that makes more sense.
void BuiltinLoader::Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv) {
Environment* env = Environment::GetCurrent(context);
Isolate* isolate = env->isolate();
void BuiltinLoader::CreatePerIsolateProperties(IsolateData* isolate_data,
Local<FunctionTemplate> target) {
Isolate* isolate = isolate_data->isolate();
Local<ObjectTemplate> proto = target->PrototypeTemplate();
target
->SetAccessor(context,
env->config_string(),
ConfigStringGetter,
nullptr,
MaybeLocal<Value>(),
DEFAULT,
None,
SideEffectType::kHasNoSideEffect)
.Check();
target
->SetAccessor(context,
FIXED_ONE_BYTE_STRING(isolate, "builtinIds"),
BuiltinIdsGetter,
nullptr,
MaybeLocal<Value>(),
DEFAULT,
None,
SideEffectType::kHasNoSideEffect)
.Check();
proto->SetAccessor(isolate_data->config_string(),
ConfigStringGetter,
nullptr,
Local<Value>(),
DEFAULT,
None,
SideEffectType::kHasNoSideEffect);
target
->SetAccessor(context,
FIXED_ONE_BYTE_STRING(isolate, "builtinCategories"),
GetBuiltinCategories,
nullptr,
Local<Value>(),
DEFAULT,
None,
SideEffectType::kHasNoSideEffect)
.Check();
proto->SetAccessor(FIXED_ONE_BYTE_STRING(isolate, "builtinIds"),
BuiltinIdsGetter,
nullptr,
Local<Value>(),
DEFAULT,
None,
SideEffectType::kHasNoSideEffect);
SetMethod(context, target, "getCacheUsage", BuiltinLoader::GetCacheUsage);
SetMethod(context, target, "compileFunction", BuiltinLoader::CompileFunction);
SetMethod(context, target, "hasCachedBuiltins", HasCachedBuiltins);
proto->SetAccessor(FIXED_ONE_BYTE_STRING(isolate, "builtinCategories"),
GetBuiltinCategories,
nullptr,
Local<Value>(),
DEFAULT,
None,
SideEffectType::kHasNoSideEffect);
SetMethod(isolate, proto, "getCacheUsage", BuiltinLoader::GetCacheUsage);
SetMethod(isolate, proto, "compileFunction", BuiltinLoader::CompileFunction);
SetMethod(isolate, proto, "hasCachedBuiltins", HasCachedBuiltins);
}
void BuiltinLoader::CreatePerContextProperties(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv) {
// internalBinding('builtins') should be frozen
target->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromJust();
}
@@ -747,7 +742,9 @@ void BuiltinLoader::RegisterExternalReferences(
} // namespace builtins
} // namespace node
NODE_BINDING_CONTEXT_AWARE_INTERNAL(builtins,
node::builtins::BuiltinLoader::Initialize)
NODE_BINDING_PER_ISOLATE_INIT(
builtins, node::builtins::BuiltinLoader::CreatePerIsolateProperties)
NODE_BINDING_CONTEXT_AWARE_INTERNAL(
builtins, node::builtins::BuiltinLoader::CreatePerContextProperties)
NODE_BINDING_EXTERNAL_REFERENCE(
builtins, node::builtins::BuiltinLoader::RegisterExternalReferences)
+9 -9
View File
@@ -44,24 +44,24 @@ class NODE_EXTERN_PRIVATE BuiltinLoader {
BuiltinLoader& operator=(const BuiltinLoader&) = delete;
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
static void Initialize(v8::Local<v8::Object> target,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv);
static void CreatePerIsolateProperties(
IsolateData* isolate_data, v8::Local<v8::FunctionTemplate> target);
static void CreatePerContextProperties(v8::Local<v8::Object> target,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv);
// The parameters used to compile the scripts are detected based on
// the pattern of the id.
static v8::MaybeLocal<v8::Function> LookupAndCompile(
v8::Local<v8::Context> context,
const char* id,
Environment* optional_env);
v8::Local<v8::Context> context, const char* id, Realm* optional_realm);
static v8::MaybeLocal<v8::Value> CompileAndCall(
v8::Local<v8::Context> context,
const char* id,
int argc,
v8::Local<v8::Value> argv[],
Environment* optional_env);
Realm* optional_realm);
static v8::MaybeLocal<v8::Value> CompileAndCall(
v8::Local<v8::Context> context, const char* id, Realm* realm);
@@ -118,7 +118,7 @@ class NODE_EXTERN_PRIVATE BuiltinLoader {
static void RecordResult(const char* id,
BuiltinLoader::Result result,
Environment* env);
Realm* realm);
static void GetBuiltinCategories(
v8::Local<v8::Name> property,
const v8::PropertyCallbackInfo<v8::Value>& info);
+4
View File
@@ -30,6 +30,10 @@ inline Realm* Realm::GetCurrent(const v8::PropertyCallbackInfo<T>& info) {
return GetCurrent(info.GetIsolate()->GetCurrentContext());
}
inline IsolateData* Realm::isolate_data() const {
return env_->isolate_data();
}
inline Environment* Realm::env() const {
return env_;
}
+23
View File
@@ -46,6 +46,8 @@ void Realm::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackField("env", env_);
tracker->TrackField("cleanup_queue", cleanup_queue_);
tracker->TrackField("builtins_with_cache", builtins_with_cache);
tracker->TrackField("builtins_without_cache", builtins_without_cache);
ForEachBaseObject([&](BaseObject* obj) {
if (obj->IsDoneInitializing()) {
@@ -102,6 +104,11 @@ RealmSerializeInfo Realm::Serialize(SnapshotCreator* creator) {
RealmSerializeInfo info;
Local<Context> ctx = context();
// Currently all modules are compiled without cache in builtin snapshot
// builder.
info.builtins = std::vector<std::string>(builtins_without_cache.begin(),
builtins_without_cache.end());
uint32_t id = 0;
#define V(PropertyName, TypeName) \
do { \
@@ -126,6 +133,8 @@ RealmSerializeInfo Realm::Serialize(SnapshotCreator* creator) {
void Realm::DeserializeProperties(const RealmSerializeInfo* info) {
Local<Context> ctx = context();
builtins_in_snapshot = info->builtins;
const std::vector<PropInfo>& values = info->persistent_values;
size_t i = 0; // index to the array
uint32_t id = 0;
@@ -305,6 +314,20 @@ void Realm::PrintInfoForSnapshot() {
std::cout << "#" << i++ << " " << obj << ": " << obj->MemoryInfoName()
<< "\n";
});
fprintf(stderr, "\nnBuiltins without cache:\n");
for (const auto& s : builtins_without_cache) {
fprintf(stderr, "%s\n", s.c_str());
}
fprintf(stderr, "\nBuiltins with cache:\n");
for (const auto& s : builtins_with_cache) {
fprintf(stderr, "%s\n", s.c_str());
}
fprintf(stderr, "\nStatic bindings (need to be registered):\n");
for (const auto mod : internal_bindings) {
fprintf(stderr, "%s:%s\n", mod->nm_filename, mod->nm_modname);
}
fprintf(stderr, "End of the Realm.\n");
}
+8
View File
@@ -12,6 +12,7 @@
namespace node {
struct RealmSerializeInfo {
std::vector<std::string> builtins;
std::vector<PropInfo> persistent_values;
std::vector<PropInfo> native_objects;
@@ -99,6 +100,13 @@ class Realm : public MemoryRetainer {
PER_REALM_STRONG_PERSISTENT_VALUES(V)
#undef V
std::set<struct node_module*> internal_bindings;
std::set<std::string> builtins_with_cache;
std::set<std::string> builtins_without_cache;
// This is only filled during deserialization. We use a vector since
// it's only used for tests.
std::vector<std::string> builtins_in_snapshot;
private:
void InitializeContext(v8::Local<v8::Context> context,
const RealmSerializeInfo* realm_info);
+7 -7
View File
@@ -99,6 +99,9 @@ std::ostream& operator<<(std::ostream& output,
std::ostream& operator<<(std::ostream& output, const RealmSerializeInfo& i) {
output << "{\n"
<< "// -- builtins begins --\n"
<< i.builtins << ",\n"
<< "// -- builtins ends --\n"
<< "// -- persistent_values begins --\n"
<< i.persistent_values << ",\n"
<< "// -- persistent_values ends --\n"
@@ -112,9 +115,6 @@ std::ostream& operator<<(std::ostream& output, const RealmSerializeInfo& i) {
std::ostream& operator<<(std::ostream& output, const EnvSerializeInfo& i) {
output << "{\n"
<< "// -- builtins begins --\n"
<< i.builtins << ",\n"
<< "// -- builtins ends --\n"
<< "// -- async_hooks begins --\n"
<< i.async_hooks << ",\n"
<< "// -- async_hooks ends --\n"
@@ -704,6 +704,7 @@ template <>
RealmSerializeInfo FileReader::Read() {
per_process::Debug(DebugCategory::MKSNAPSHOT, "Read<RealmSerializeInfo>()\n");
RealmSerializeInfo result;
result.builtins = ReadVector<std::string>();
result.persistent_values = ReadVector<PropInfo>();
result.native_objects = ReadVector<PropInfo>();
result.context = Read<SnapshotIndex>();
@@ -718,7 +719,8 @@ size_t FileWriter::Write(const RealmSerializeInfo& data) {
}
// Use += here to ensure order of evaluation.
size_t written_total = WriteVector<PropInfo>(data.persistent_values);
size_t written_total = WriteVector<std::string>(data.builtins);
written_total += WriteVector<PropInfo>(data.persistent_values);
written_total += WriteVector<PropInfo>(data.native_objects);
written_total += Write<SnapshotIndex>(data.context);
@@ -730,7 +732,6 @@ template <>
EnvSerializeInfo FileReader::Read() {
per_process::Debug(DebugCategory::MKSNAPSHOT, "Read<EnvSerializeInfo>()\n");
EnvSerializeInfo result;
result.builtins = ReadVector<std::string>();
result.async_hooks = Read<AsyncHooks::SerializeInfo>();
result.tick_info = Read<TickInfo::SerializeInfo>();
result.immediate_info = Read<ImmediateInfo::SerializeInfo>();
@@ -751,8 +752,7 @@ size_t FileWriter::Write(const EnvSerializeInfo& data) {
}
// Use += here to ensure order of evaluation.
size_t written_total = WriteVector<std::string>(data.builtins);
written_total += Write<AsyncHooks::SerializeInfo>(data.async_hooks);
size_t written_total = Write<AsyncHooks::SerializeInfo>(data.async_hooks);
written_total += Write<TickInfo::SerializeInfo>(data.tick_info);
written_total += Write<ImmediateInfo::SerializeInfo>(data.immediate_info);
written_total += Write<performance::PerformanceState::SerializeInfo>(
+17
View File
@@ -356,6 +356,23 @@ void SetMethod(Local<v8::Context> context,
function->SetName(name_string); // NODE_SET_METHOD() compatibility.
}
void SetMethod(v8::Isolate* isolate,
v8::Local<v8::Template> that,
const char* name,
v8::FunctionCallback callback) {
Local<v8::FunctionTemplate> t =
NewFunctionTemplate(isolate,
callback,
Local<v8::Signature>(),
v8::ConstructorBehavior::kThrow,
v8::SideEffectType::kHasSideEffect);
// kInternalized strings are created in the old space.
const v8::NewStringType type = v8::NewStringType::kInternalized;
Local<v8::String> name_string =
v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked();
that->Set(name_string, t);
}
void SetFastMethod(Local<v8::Context> context,
Local<v8::Object> that,
const char* name,
+5
View File
@@ -881,6 +881,11 @@ void SetMethod(v8::Local<v8::Context> context,
v8::Local<v8::Object> that,
const char* name,
v8::FunctionCallback callback);
// Similar to SetProtoMethod but without receiver signature checks.
void SetMethod(v8::Isolate* isolate,
v8::Local<v8::Template> that,
const char* name,
v8::FunctionCallback callback);
void SetFastMethod(v8::Local<v8::Context> context,
v8::Local<v8::Object> that,