diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2019-05-20 14:14:21 +0300 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2019-06-03 18:27:48 +0300 |
commit | a1a690e07c92d209cfecb2e7725ce649287edf26 (patch) | |
tree | 87dc42011acaab55a820ef378c0bd75cf1c20388 | |
parent | bda7da34c6f8edabf45623acb21a410c76a33bff (diff) |
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>
-rw-r--r-- | src/env.cc | 40 | ||||
-rw-r--r-- | src/env.h | 2 | ||||
-rw-r--r-- | src/node.cc | 11 |
3 files changed, 31 insertions, 22 deletions
diff --git a/src/env.cc b/src/env.cc index 7e420548052..85239c02a91 100644 --- a/src/env.cc +++ b/src/env.cc @@ -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) diff --git a/src/env.h b/src/env.h index d067d67e1c6..778f0aa1e2f 100644 --- a/src/env.h +++ b/src/env.h @@ -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(); diff --git a/src/node.cc b/src/node.cc index 0edc6fd4ce0..d5c50bac88e 100644 --- a/src/node.cc +++ b/src/node.cc @@ -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( |