Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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`.
  • Loading branch information
joyeecheung committed May 20, 2019
commit 307058556b739a911e5abd20366a18c38844c464
40 changes: 29 additions & 11 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,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 @@ -257,16 +283,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 @@ -338,7 +354,9 @@ Environment::Environment(IsolateData* isolate_data,
async_hooks_.no_force_checks();
}

set_process_object(node::CreateProcessObject(this).ToLocalChecked());
// TODO(joyeecheung): deserialize when the snapshot cover the environment
Comment thread
joyeecheung marked this conversation as resolved.
Outdated
// 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 @@ -794,6 +794,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 @@ -261,17 +261,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