From e044563bb0faaf62afaf7b9f358729bc4c70f065 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 7 Mar 2019 15:12:52 +0100 Subject: [PATCH] src,lib: allow running multiple per-context files Create an `lib/internal/per_context/` directory that can host multiple files which we execute for each context. PR-URL: https://github.com/nodejs/node/pull/26497 Reviewed-By: James M Snell --- lib/internal/bootstrap/cache.js | 5 ++- .../context.js => per_context/setup.js} | 0 node.gyp | 2 +- src/api/environment.cc | 42 +++++++++++-------- 4 files changed, 29 insertions(+), 20 deletions(-) rename lib/internal/{bootstrap/context.js => per_context/setup.js} (100%) diff --git a/lib/internal/bootstrap/cache.js b/lib/internal/bootstrap/cache.js index 3dc6650cd3e35b..f1386eaf460cc7 100644 --- a/lib/internal/bootstrap/cache.js +++ b/lib/internal/bootstrap/cache.js @@ -20,10 +20,11 @@ const cannotBeRequired = [ 'internal/test/binding', - 'internal/bootstrap/context', 'internal/bootstrap/primordials', 'internal/bootstrap/loaders', - 'internal/bootstrap/node' + 'internal/bootstrap/node', + + 'internal/per_context/setup', ]; // Skip modules that cannot be required when they are not diff --git a/lib/internal/bootstrap/context.js b/lib/internal/per_context/setup.js similarity index 100% rename from lib/internal/bootstrap/context.js rename to lib/internal/per_context/setup.js diff --git a/node.gyp b/node.gyp index 804da3941c3cc2..e8f89139c316e6 100644 --- a/node.gyp +++ b/node.gyp @@ -27,12 +27,12 @@ 'node_lib_target_name%': 'node_lib', 'node_intermediate_lib_type%': 'static_library', 'library_files': [ - 'lib/internal/bootstrap/context.js', 'lib/internal/bootstrap/primordials.js', 'lib/internal/bootstrap/cache.js', 'lib/internal/bootstrap/loaders.js', 'lib/internal/bootstrap/node.js', 'lib/internal/bootstrap/pre_execution.js', + 'lib/internal/per_context/setup.js', 'lib/async_hooks.js', 'lib/assert.js', 'lib/buffer.js', diff --git a/src/api/environment.cc b/src/api/environment.cc index bbabafe7c96ae6..4df339b2e69c8a 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -259,25 +259,33 @@ Local NewContext(Isolate* isolate, True(isolate)); { - // Run lib/internal/bootstrap/context.js + // Run per-context JS files. Context::Scope context_scope(context); - std::vector> parameters = { - FIXED_ONE_BYTE_STRING(isolate, "global")}; - Local arguments[] = {context->Global()}; - MaybeLocal maybe_fn = - per_process::native_module_loader.LookupAndCompile( - context, "internal/bootstrap/context", ¶meters, nullptr); - if (maybe_fn.IsEmpty()) { - return Local(); - } - Local fn = maybe_fn.ToLocalChecked(); - MaybeLocal result = - fn->Call(context, Undefined(isolate), arraysize(arguments), arguments); - // Execution failed during context creation. - // TODO(joyeecheung): deprecate this signature and return a MaybeLocal. - if (result.IsEmpty()) { - return Local(); + static const char* context_files[] = { + "internal/per_context/setup", + nullptr + }; + + for (const char** module = context_files; *module != nullptr; module++) { + std::vector> parameters = { + FIXED_ONE_BYTE_STRING(isolate, "global")}; + Local arguments[] = {context->Global()}; + MaybeLocal maybe_fn = + per_process::native_module_loader.LookupAndCompile( + context, *module, ¶meters, nullptr); + if (maybe_fn.IsEmpty()) { + return Local(); + } + Local fn = maybe_fn.ToLocalChecked(); + MaybeLocal result = + fn->Call(context, Undefined(isolate), + arraysize(arguments), arguments); + // Execution failed during context creation. + // TODO(joyeecheung): deprecate this signature and return a MaybeLocal. + if (result.IsEmpty()) { + return Local(); + } } }