src: create Environment properties in Environment::CreateProperties()

Move creation of `env->as_callback_data()`, `env->primordials()`
and `env->process()` into `Environment::CreateProperties()` and
call it in the `Environment` constructor - this can be replaced with
deserialization when we snapshot the per-environment properties
after the instantiation of `Environment`.

PR-URL: https://github.com/nodejs/node/pull/27539
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Joyee Cheung 2019-05-20 13:14:21 +02:00
parent bda7da34c6
commit a1a690e07c
No known key found for this signature in database
GPG Key ID: 92B78A53C8303B8D
3 changed files with 31 additions and 22 deletions

View File

@ -235,6 +235,32 @@ uint64_t Environment::AllocateThreadId() {
return next_thread_id++;
}
void Environment::CreateProperties() {
HandleScope handle_scope(isolate_);
Local<Context> ctx = context();
Local<FunctionTemplate> templ = FunctionTemplate::New(isolate());
templ->InstanceTemplate()->SetInternalFieldCount(1);
Local<Object> obj = templ->GetFunction(ctx)
.ToLocalChecked()
->NewInstance(ctx)
.ToLocalChecked();
obj->SetAlignedPointerInInternalField(0, this);
set_as_callback_data(obj);
set_as_callback_data_template(templ);
// Store primordials setup by the per-context script in the environment.
Local<Object> per_context_bindings =
GetPerContextExports(ctx).ToLocalChecked();
Local<Value> primordials =
per_context_bindings->Get(ctx, primordials_string()).ToLocalChecked();
CHECK(primordials->IsObject());
set_primordials(primordials.As<Object>());
Local<Object> process_object =
node::CreateProcessObject(this).FromMaybe(Local<Object>());
set_process_object(process_object);
}
Environment::Environment(IsolateData* isolate_data,
Local<Context> context,
const std::vector<std::string>& args,
@ -258,16 +284,6 @@ Environment::Environment(IsolateData* isolate_data,
// We'll be creating new objects so make sure we've entered the context.
HandleScope handle_scope(isolate());
Context::Scope context_scope(context);
{
Local<FunctionTemplate> templ = FunctionTemplate::New(isolate());
templ->InstanceTemplate()->SetInternalFieldCount(1);
Local<Object> obj =
templ->GetFunction(context).ToLocalChecked()->NewInstance(
context).ToLocalChecked();
obj->SetAlignedPointerInInternalField(0, this);
set_as_callback_data(obj);
set_as_callback_data_template(templ);
}
set_env_vars(per_process::system_environment);
@ -339,7 +355,9 @@ Environment::Environment(IsolateData* isolate_data,
async_hooks_.no_force_checks();
}
set_process_object(node::CreateProcessObject(this).ToLocalChecked());
// TODO(joyeecheung): deserialize when the snapshot covers the environment
// properties.
CreateProperties();
}
CompileFnEntry::CompileFnEntry(Environment* env, uint32_t id)

View File

@ -796,6 +796,8 @@ class Environment : public MemoryRetainer {
bool IsRootNode() const override { return true; }
void MemoryInfo(MemoryTracker* tracker) const override;
void CreateProperties();
inline size_t async_callback_scope_depth() const;
inline void PushAsyncCallbackScope();
inline void PopAsyncCallbackScope();

View File

@ -255,17 +255,6 @@ MaybeLocal<Value> RunBootstrapping(Environment* env) {
global->Set(context, FIXED_ONE_BYTE_STRING(env->isolate(), "global"), global)
.Check();
// Store primordials setup by the per-context script in the environment.
Local<Object> per_context_bindings;
Local<Value> primordials;
if (!GetPerContextExports(context).ToLocal(&per_context_bindings) ||
!per_context_bindings->Get(context, env->primordials_string())
.ToLocal(&primordials) ||
!primordials->IsObject()) {
return MaybeLocal<Value>();
}
env->set_primordials(primordials.As<Object>());
#if HAVE_INSPECTOR
if (env->options()->debug_options().break_node_first_line) {
env->inspector_agent()->PauseOnNextJavascriptStatement(