Skip to content

Commit e044563

Browse files
addaleaxtargos
authored andcommitted
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: #26497 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f2a07df commit e044563

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

lib/internal/bootstrap/cache.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ const cannotBeRequired = [
2020

2121
'internal/test/binding',
2222

23-
'internal/bootstrap/context',
2423
'internal/bootstrap/primordials',
2524
'internal/bootstrap/loaders',
26-
'internal/bootstrap/node'
25+
'internal/bootstrap/node',
26+
27+
'internal/per_context/setup',
2728
];
2829

2930
// Skip modules that cannot be required when they are not
File renamed without changes.

node.gyp

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
'node_lib_target_name%': 'node_lib',
2828
'node_intermediate_lib_type%': 'static_library',
2929
'library_files': [
30-
'lib/internal/bootstrap/context.js',
3130
'lib/internal/bootstrap/primordials.js',
3231
'lib/internal/bootstrap/cache.js',
3332
'lib/internal/bootstrap/loaders.js',
3433
'lib/internal/bootstrap/node.js',
3534
'lib/internal/bootstrap/pre_execution.js',
35+
'lib/internal/per_context/setup.js',
3636
'lib/async_hooks.js',
3737
'lib/assert.js',
3838
'lib/buffer.js',

src/api/environment.cc

+25-17
Original file line numberDiff line numberDiff line change
@@ -259,25 +259,33 @@ Local<Context> NewContext(Isolate* isolate,
259259
True(isolate));
260260

261261
{
262-
// Run lib/internal/bootstrap/context.js
262+
// Run per-context JS files.
263263
Context::Scope context_scope(context);
264264

265-
std::vector<Local<String>> parameters = {
266-
FIXED_ONE_BYTE_STRING(isolate, "global")};
267-
Local<Value> arguments[] = {context->Global()};
268-
MaybeLocal<Function> maybe_fn =
269-
per_process::native_module_loader.LookupAndCompile(
270-
context, "internal/bootstrap/context", &parameters, nullptr);
271-
if (maybe_fn.IsEmpty()) {
272-
return Local<Context>();
273-
}
274-
Local<Function> fn = maybe_fn.ToLocalChecked();
275-
MaybeLocal<Value> result =
276-
fn->Call(context, Undefined(isolate), arraysize(arguments), arguments);
277-
// Execution failed during context creation.
278-
// TODO(joyeecheung): deprecate this signature and return a MaybeLocal.
279-
if (result.IsEmpty()) {
280-
return Local<Context>();
265+
static const char* context_files[] = {
266+
"internal/per_context/setup",
267+
nullptr
268+
};
269+
270+
for (const char** module = context_files; *module != nullptr; module++) {
271+
std::vector<Local<String>> parameters = {
272+
FIXED_ONE_BYTE_STRING(isolate, "global")};
273+
Local<Value> arguments[] = {context->Global()};
274+
MaybeLocal<Function> maybe_fn =
275+
per_process::native_module_loader.LookupAndCompile(
276+
context, *module, &parameters, nullptr);
277+
if (maybe_fn.IsEmpty()) {
278+
return Local<Context>();
279+
}
280+
Local<Function> fn = maybe_fn.ToLocalChecked();
281+
MaybeLocal<Value> result =
282+
fn->Call(context, Undefined(isolate),
283+
arraysize(arguments), arguments);
284+
// Execution failed during context creation.
285+
// TODO(joyeecheung): deprecate this signature and return a MaybeLocal.
286+
if (result.IsEmpty()) {
287+
return Local<Context>();
288+
}
281289
}
282290
}
283291

0 commit comments

Comments
 (0)