Skip to content

Commit 698508a

Browse files
legendecasRafaelGSS
authored andcommitted
src: bootstrap prepare stack trace callback in shadow realm
Bootstrap per-realm callbacks like `prepare_stack_trace_callback` in the ShadowRealm. This enables stack trace decoration in the ShadowRealm. PR-URL: #47107 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 1b9e573 commit 698508a

20 files changed

Lines changed: 141 additions & 83 deletions

‎.github/CODEOWNERS‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
/doc/api/module.md@nodejs/modules@nodejs/loaders
8585
/doc/api/modules.md@nodejs/modules@nodejs/loaders
8686
/doc/api/packages.md@nodejs/modules@nodejs/loaders
87-
/lib/internal/bootstrap/loaders.js@nodejs/modules@nodejs/loaders
87+
/lib/internal/bootstrap/realm.js@nodejs/modules@nodejs/loaders
8888
/lib/internal/modules/*@nodejs/modules@nodejs/loaders
8989
/lib/internal/process/esm_loader.js@nodejs/modules@nodejs/loaders
9090
/lib/internal/process/execution.js@nodejs/modules@nodejs/loaders

‎lib/assert.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const { openSync, closeSync, readSync } = require('fs');
6565
const{ inspect }=require('internal/util/inspect');
6666
const{ isPromise, isRegExp }=require('internal/util/types');
6767
const{EOL}=require('internal/constants');
68-
const{ BuiltinModule }=require('internal/bootstrap/loaders');
68+
const{ BuiltinModule }=require('internal/bootstrap/realm');
6969
const{ isError }=require('internal/util');
7070

7171
consterrorCache=newSafeMap();

‎lib/internal/bootstrap/node.js‎

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Hello, and welcome to hacking node.js!
22
//
3-
// This file is invoked by `Realm::BootstrapNode()` in `src/node_realm.cc`,
3+
// This file is invoked by `Realm::BootstrapRealm()` in `src/node_realm.cc`,
44
// and is responsible for setting up Node.js core before main scripts
55
// under `lib/internal/main/` are executed.
66
//
@@ -32,9 +32,10 @@
3232
// `DOMException` class.
3333
// - `lib/internal/per_context/messageport.js`: JS-side components of the
3434
// `MessagePort` implementation.
35-
// - `lib/internal/bootstrap/loaders.js`: this sets up internal binding and
35+
// - `lib/internal/bootstrap/realm.js`: this sets up internal binding and
3636
// module loaders, including `process.binding()`, `process._linkedBinding()`,
37-
// `internalBinding()` and `BuiltinModule`.
37+
// `internalBinding()` and `BuiltinModule`, and per-realm internal states
38+
// and bindings, including `prepare_stack_trace_callback`.
3839
//
3940
// The initialization done in this script is included in both the main thread
4041
// and the worker threads. After this, further initialization is done based
@@ -52,8 +53,6 @@
5253
// passed by `BuiltinLoader::CompileAndCall()`.
5354
/* global process, require, internalBinding, primordials */
5455

55-
setupPrepareStackTrace();
56-
5756
const{
5857
FunctionPrototypeCall,
5958
JSONParse,
@@ -336,25 +335,6 @@ process.emitWarning = emitWarning;
336335
// Note: only after this point are the timers effective
337336
}
338337

339-
functionsetupPrepareStackTrace(){
340-
const{
341-
setEnhanceStackForFatalException,
342-
setPrepareStackTraceCallback,
343-
}=internalBinding('errors');
344-
const{
345-
prepareStackTrace,
346-
fatalExceptionStackEnhancers: {
347-
beforeInspector,
348-
afterInspector,
349-
},
350-
}=require('internal/errors');
351-
// Tell our PrepareStackTraceCallback passed to the V8 API
352-
// to call prepareStackTrace().
353-
setPrepareStackTraceCallback(prepareStackTrace);
354-
// Set the function used to enhance the error stack for printing
355-
setEnhanceStackForFatalException(beforeInspector,afterInspector);
356-
}
357-
358338
functionsetupProcessObject(){
359339
constEventEmitter=require('events');
360340
constorigProcProto=ObjectGetPrototypeOf(process);
Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// This file is executed in every realm that is created by Node.js, including
2+
// the context of main thread, worker threads, and ShadowRealms.
3+
// Only per-realm internal states and bindings should be bootstrapped in this
4+
// file and no globals should be exposed to the user code.
5+
//
16
// This file creates the internal module & binding loaders used by built-in
27
// modules. In contrast, user land modules are loaded using
38
// lib/internal/modules/cjs/loader.js (CommonJS Modules) or
@@ -30,7 +35,7 @@
3035
// so they can be loaded faster without the cost of I/O. This class makes the
3136
// lib/internal/*, deps/internal/* modules and internalBinding() available by
3237
// default to core modules, and lets the core modules require itself via
33-
// require('internal/bootstrap/loaders') even when this file is not written in
38+
// require('internal/bootstrap/realm') even when this file is not written in
3439
// CommonJS style.
3540
//
3641
// Other objects:
@@ -178,7 +183,7 @@ let internalBinding;
178183
};
179184
}
180185

181-
constloaderId='internal/bootstrap/loaders';
186+
constselfId='internal/bootstrap/realm';
182187
const{
183188
builtinIds,
184189
compileFunction,
@@ -235,7 +240,7 @@ class BuiltinModule {
235240
staticexposeInternals(){
236241
for(const{0: id,1: mod}ofBuiltinModule.map){
237242
// Do not expose this to user land even with --expose-internals.
238-
if(id!==loaderId){
243+
if(id!==selfId){
239244
mod.canBeRequiredByUsers=true;
240245
}
241246
}
@@ -354,7 +359,7 @@ const loaderExports = {
354359
};
355360

356361
functionrequireBuiltin(id){
357-
if(id===loaderId){
362+
if(id===selfId){
358363
returnloaderExports;
359364
}
360365

@@ -374,5 +379,27 @@ function requireWithFallbackInDeps(request) {
374379
returnrequireBuiltin(request);
375380
}
376381

382+
functionsetupPrepareStackTrace(){
383+
const{
384+
setEnhanceStackForFatalException,
385+
setPrepareStackTraceCallback,
386+
}=internalBinding('errors');
387+
const{
388+
prepareStackTrace,
389+
fatalExceptionStackEnhancers: {
390+
beforeInspector,
391+
afterInspector,
392+
},
393+
}=requireBuiltin('internal/errors');
394+
// Tell our PrepareStackTraceCallback passed to the V8 API
395+
// to call prepareStackTrace().
396+
setPrepareStackTraceCallback(prepareStackTrace);
397+
// Set the function used to enhance the error stack for printing
398+
setEnhanceStackForFatalException(beforeInspector,afterInspector);
399+
}
400+
377401
// Store the internal loaders in C++.
378402
setInternalLoaders(internalBinding,requireBuiltin);
403+
404+
// Setup per-realm bindings.
405+
setupPrepareStackTrace();

‎lib/internal/main/mksnapshot.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const {
1010
}=primordials;
1111

1212
constbinding=internalBinding('mksnapshot');
13-
const{ BuiltinModule }=require('internal/bootstrap/loaders');
13+
const{ BuiltinModule }=require('internal/bootstrap/realm');
1414
const{
1515
getEmbedderEntryFunction,
1616
compileSerializeMain,

‎lib/internal/modules/cjs/loader.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ module.exports = {
7575
initializeCJS,
7676
};
7777

78-
const{ BuiltinModule }=require('internal/bootstrap/loaders');
78+
const{ BuiltinModule }=require('internal/bootstrap/realm');
7979
const{
8080
maybeCacheSourceMap,
8181
}=require('internal/source_map/source_map_cache');

‎lib/internal/modules/esm/hooks.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class Hooks {
190190
filename: '<preload>',
191191
},
192192
);
193-
const{ BuiltinModule }=require('internal/bootstrap/loaders');
193+
const{ BuiltinModule }=require('internal/bootstrap/realm');
194194
// We only allow replacing the importMetaInitializer during preload;
195195
// after preload is finished, we disable the ability to replace it.
196196
//

‎lib/internal/modules/esm/resolve.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const {
2424
StringPrototypeStartsWith,
2525
}=primordials;
2626
constinternalFS=require('internal/fs/utils');
27-
const{ BuiltinModule }=require('internal/bootstrap/loaders');
27+
const{ BuiltinModule }=require('internal/bootstrap/realm');
2828
const{
2929
realpathSync,
3030
statSync,

‎lib/internal/modules/helpers.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const {
1717
ERR_MANIFEST_DEPENDENCY_MISSING,
1818
ERR_UNKNOWN_BUILTIN_MODULE,
1919
}=require('internal/errors').codes;
20-
const{ BuiltinModule }=require('internal/bootstrap/loaders');
20+
const{ BuiltinModule }=require('internal/bootstrap/realm');
2121

2222
const{ validateString }=require('internal/validators');
2323
constpath=require('path');

‎lib/internal/process/pre_execution.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ function initializeReport() {
351351
functionsetupDebugEnv(){
352352
require('internal/util/debuglog').initializeDebugEnv(process.env.NODE_DEBUG);
353353
if(getOptionValue('--expose-internals')){
354-
require('internal/bootstrap/loaders').BuiltinModule.exposeInternals();
354+
require('internal/bootstrap/realm').BuiltinModule.exposeInternals();
355355
}
356356
}
357357

0 commit comments

Comments
 (0)