From b28c3d90a60c6855e0efba739844cbd29371d214 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 10 Apr 2019 16:31:52 +0800 Subject: [PATCH 1/2] lib: create primordials in every context This allows us to use primordials in other per-context scripts. --- .../{bootstrap => per_context}/primordials.js | 0 node.gyp | 2 +- src/api/environment.cc | 28 +++++++++++-------- src/node.cc | 26 +++++++---------- 4 files changed, 28 insertions(+), 28 deletions(-) rename lib/internal/{bootstrap => per_context}/primordials.js (100%) diff --git a/lib/internal/bootstrap/primordials.js b/lib/internal/per_context/primordials.js similarity index 100% rename from lib/internal/bootstrap/primordials.js rename to lib/internal/per_context/primordials.js diff --git a/node.gyp b/node.gyp index 9d6d0dd7cbb695..cb063317e60d60 100644 --- a/node.gyp +++ b/node.gyp @@ -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', diff --git a/src/api/environment.cc b/src/api/environment.cc index 62eca337e15627..6c1cad68435040 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -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; @@ -332,24 +333,29 @@ Local NewContext(Isolate* isolate, // Run per-context JS files. Context::Scope context_scope(context); Local exports; - if (!GetPerContextExports(context).ToLocal(&exports)) - return Local(); + Local primordials_string = + FIXED_ONE_BYTE_STRING(isolate, "primordials"); Local global_string = FIXED_ONE_BYTE_STRING(isolate, "global"); Local 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 primordials = Object::New(isolate); + if (!primordials->SetPrototype(context, Null(isolate)).FromJust() || + !GetPerContextExports(context).ToLocal(&exports) || + !exports->Set(context, primordials_string, primordials).FromJust()) { + return Local(); + } + + 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> parameters = { - global_string, - exports_string - }; - Local arguments[] = {context->Global(), exports}; + global_string, exports_string, primordials_string}; + Local arguments[] = {context->Global(), exports, primordials}; MaybeLocal maybe_fn = per_process::native_module_loader.LookupAndCompile( context, *module, ¶meters, nullptr); diff --git a/src/node.cc b/src/node.cc index a5a0cf33e1d548..b6bad7bcb6942f 100644 --- a/src/node.cc +++ b/src/node.cc @@ -267,14 +267,16 @@ MaybeLocal 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> primordials_params = { - env->primordials_string() - }; - std::vector> primordials_args = { - env->primordials() - }; + // Store primordials setup by the per-context script in the environment. + Local per_context_bindings; + Local primordials; + if (!GetPerContextExports(context).ToLocal(&per_context_bindings) || + !per_context_bindings->Get(context, env->primordials_string()) + .ToLocal(&primordials) || + !primordials->IsObject()) { + return MaybeLocal(); + } + env->set_primordials(primordials.As()); #if HAVE_INSPECTOR if (env->options()->debug_options().break_node_first_line) { @@ -282,14 +284,6 @@ MaybeLocal RunBootstrapping(Environment* env) { "Break at bootstrap"); } #endif // HAVE_INSPECTOR - MaybeLocal primordials_ret = - ExecuteBootstrapper(env, - "internal/bootstrap/primordials", - &primordials_params, - &primordials_args); - if (primordials_ret.IsEmpty()) { - return MaybeLocal(); - } // Create binding loaders std::vector> loaders_params = { From 81ee19d2f9cb74284bb96da6b4b0c13b3230cab4 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 10 Apr 2019 16:47:44 +0800 Subject: [PATCH 2/2] lib: use primordials in domexception.js --- lib/internal/per_context/domexception.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/internal/per_context/domexception.js b/lib/internal/per_context/domexception.js index 729fe3757ada5b..83b8d55995201d 100644 --- a/lib/internal/per_context/domexception.js +++ b/lib/internal/per_context/domexception.js @@ -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 { constructor(type) { @@ -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') {