Skip to content

Commit c086736

Browse files
joyeecheungBridgeAR
authored andcommitted
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: #27539 Reviewed-By: Anna Henningsen <[email protected]>
1 parent 70f8e71 commit c086736

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

src/env.cc

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,32 @@ uint64_t Environment::AllocateThreadId() {
235235
return next_thread_id++;
236236
}
237237

238+
void Environment::CreateProperties() {
239+
HandleScope handle_scope(isolate_);
240+
Local<Context> ctx = context();
241+
Local<FunctionTemplate> templ = FunctionTemplate::New(isolate());
242+
templ->InstanceTemplate()->SetInternalFieldCount(1);
243+
Local<Object> obj = templ->GetFunction(ctx)
244+
.ToLocalChecked()
245+
->NewInstance(ctx)
246+
.ToLocalChecked();
247+
obj->SetAlignedPointerInInternalField(0, this);
248+
set_as_callback_data(obj);
249+
set_as_callback_data_template(templ);
250+
251+
// Store primordials setup by the per-context script in the environment.
252+
Local<Object> per_context_bindings =
253+
GetPerContextExports(ctx).ToLocalChecked();
254+
Local<Value> primordials =
255+
per_context_bindings->Get(ctx, primordials_string()).ToLocalChecked();
256+
CHECK(primordials->IsObject());
257+
set_primordials(primordials.As<Object>());
258+
259+
Local<Object> process_object =
260+
node::CreateProcessObject(this).FromMaybe(Local<Object>());
261+
set_process_object(process_object);
262+
}
263+
238264
Environment::Environment(IsolateData* isolate_data,
239265
Local<Context> context,
240266
const std::vector<std::string>& args,
@@ -258,16 +284,6 @@ Environment::Environment(IsolateData* isolate_data,
258284
// We'll be creating new objects so make sure we've entered the context.
259285
HandleScope handle_scope(isolate());
260286
Context::Scope context_scope(context);
261-
{
262-
Local<FunctionTemplate> templ = FunctionTemplate::New(isolate());
263-
templ->InstanceTemplate()->SetInternalFieldCount(1);
264-
Local<Object> obj =
265-
templ->GetFunction(context).ToLocalChecked()->NewInstance(
266-
context).ToLocalChecked();
267-
obj->SetAlignedPointerInInternalField(0, this);
268-
set_as_callback_data(obj);
269-
set_as_callback_data_template(templ);
270-
}
271287

272288
set_env_vars(per_process::system_environment);
273289

@@ -339,7 +355,9 @@ Environment::Environment(IsolateData* isolate_data,
339355
async_hooks_.no_force_checks();
340356
}
341357

342-
set_process_object(node::CreateProcessObject(this).ToLocalChecked());
358+
// TODO(joyeecheung): deserialize when the snapshot covers the environment
359+
// properties.
360+
CreateProperties();
343361
}
344362

345363
CompileFnEntry::CompileFnEntry(Environment* env, uint32_t id)

src/env.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,8 @@ class Environment : public MemoryRetainer {
796796
bool IsRootNode() const override { return true; }
797797
void MemoryInfo(MemoryTracker* tracker) const override;
798798

799+
void CreateProperties();
800+
799801
inline size_t async_callback_scope_depth() const;
800802
inline void PushAsyncCallbackScope();
801803
inline void PopAsyncCallbackScope();

src/node.cc

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,6 @@ MaybeLocal<Value> RunBootstrapping(Environment* env) {
249249
global->Set(context, FIXED_ONE_BYTE_STRING(env->isolate(), "global"), global)
250250
.Check();
251251

252-
// Store primordials setup by the per-context script in the environment.
253-
Local<Object> per_context_bindings;
254-
Local<Value> primordials;
255-
if (!GetPerContextExports(context).ToLocal(&per_context_bindings) ||
256-
!per_context_bindings->Get(context, env->primordials_string())
257-
.ToLocal(&primordials) ||
258-
!primordials->IsObject()) {
259-
return MaybeLocal<Value>();
260-
}
261-
env->set_primordials(primordials.As<Object>());
262-
263252
#if HAVE_INSPECTOR
264253
if (env->options()->debug_options().break_node_first_line) {
265254
env->inspector_agent()->PauseOnNextJavascriptStatement(

0 commit comments

Comments
 (0)