Skip to content

Commit

Permalink
src: create Environment properties in Environment::CreateProperties()
Browse files Browse the repository at this point in the history
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: #27539
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
joyeecheung authored and BridgeAR committed Jun 17, 2019
1 parent 70f8e71 commit c086736
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
40 changes: 29 additions & 11 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
11 changes: 0 additions & 11 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,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(
Expand Down

0 comments on commit c086736

Please sign in to comment.