Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: create primordials in every context #27171

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 8 additions & 5 deletions lib/internal/per_context/domexception.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
'use strict';

// `per_context` scripts are executed before creating the primordials so we
// cannot use them here.
/* eslint-disable no-restricted-globals */
const {
SafeWeakMap,
SafeMap,
Object,
Symbol
} = primordials;

class ERR_INVALID_THIS extends TypeError {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a side note, we'd probably want to make the Errors part of primordials too, but I am not yet sure how this would work for e.g. error-serdes, or if how it works with our internal errors (because we do need to assign properties to the instances dynamically, they probably can't be frozen).

constructor(type) {
Expand All @@ -12,9 +15,9 @@ class ERR_INVALID_THIS extends TypeError {
get code() { return 'ERR_INVALID_THIS'; }
}

const internalsMap = new WeakMap();
const internalsMap = new SafeWeakMap();

const nameToCodeMap = new Map();
const nameToCodeMap = new SafeMap();

class DOMException extends Error {
constructor(message = '', name = 'Error') {
Expand Down
2 changes: 1 addition & 1 deletion node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
'node_lib_target_name%': 'node_lib',
'node_intermediate_lib_type%': 'static_library',
'library_files': [
'lib/internal/bootstrap/primordials.js',
'lib/internal/bootstrap/environment.js',
'lib/internal/bootstrap/loaders.js',
'lib/internal/bootstrap/node.js',
'lib/internal/bootstrap/pre_execution.js',
'lib/internal/per_context/primordials.js',
'lib/internal/per_context/setup.js',
'lib/internal/per_context/domexception.js',
'lib/async_hooks.js',
Expand Down
28 changes: 17 additions & 11 deletions src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ using v8::Local;
using v8::MaybeLocal;
using v8::Message;
using v8::MicrotasksPolicy;
using v8::Null;
using v8::Object;
using v8::ObjectTemplate;
using v8::Private;
Expand Down Expand Up @@ -332,24 +333,29 @@ Local<Context> NewContext(Isolate* isolate,
// Run per-context JS files.
Context::Scope context_scope(context);
Local<Object> exports;
if (!GetPerContextExports(context).ToLocal(&exports))
return Local<Context>();

Local<String> primordials_string =
FIXED_ONE_BYTE_STRING(isolate, "primordials");
Local<String> global_string = FIXED_ONE_BYTE_STRING(isolate, "global");
Local<String> exports_string = FIXED_ONE_BYTE_STRING(isolate, "exports");

static const char* context_files[] = {
"internal/per_context/setup",
"internal/per_context/domexception",
nullptr
};
// Create primordials first and make it available to per-context scripts.
Local<Object> primordials = Object::New(isolate);
if (!primordials->SetPrototype(context, Null(isolate)).FromJust() ||
!GetPerContextExports(context).ToLocal(&exports) ||
!exports->Set(context, primordials_string, primordials).FromJust()) {
return Local<Context>();
}

static const char* context_files[] = {"internal/per_context/primordials",
"internal/per_context/setup",
"internal/per_context/domexception",
nullptr};

for (const char** module = context_files; *module != nullptr; module++) {
std::vector<Local<String>> parameters = {
global_string,
exports_string
};
Local<Value> arguments[] = {context->Global(), exports};
global_string, exports_string, primordials_string};
Local<Value> arguments[] = {context->Global(), exports, primordials};
MaybeLocal<Function> maybe_fn =
per_process::native_module_loader.LookupAndCompile(
context, *module, &parameters, nullptr);
Expand Down
26 changes: 10 additions & 16 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,29 +267,23 @@ MaybeLocal<Value> RunBootstrapping(Environment* env) {
global->Set(context, FIXED_ONE_BYTE_STRING(env->isolate(), "global"), global)
.Check();

// Store primordials
env->set_primordials(Object::New(isolate));
std::vector<Local<String>> primordials_params = {
env->primordials_string()
};
std::vector<Local<Value>> primordials_args = {
env->primordials()
};
// 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(
"Break at bootstrap");
}
#endif // HAVE_INSPECTOR
MaybeLocal<Value> primordials_ret =
ExecuteBootstrapper(env,
"internal/bootstrap/primordials",
&primordials_params,
&primordials_args);
if (primordials_ret.IsEmpty()) {
return MaybeLocal<Value>();
}

// Create binding loaders
std::vector<Local<String>> loaders_params = {
Expand Down