@@ -85,21 +85,14 @@ const kTypes = [
8585
8686const MainContextError = Error ;
8787const overrideStackTrace = new SafeWeakMap ( ) ;
88- const kNoOverride = Symbol ( 'kNoOverride' ) ;
89-
90- const prepareStackTrace = ( globalThis , error , trace ) => {
91- // API for node internals to override error stack formatting
92- // without interfering with userland code.
93- if ( overrideStackTrace . has ( error ) ) {
94- const f = overrideStackTrace . get ( error ) ;
95- overrideStackTrace . delete ( error ) ;
96- return f ( error , trace ) ;
97- }
98-
99- const globalOverride =
100- maybeOverridePrepareStackTrace ( globalThis , error , trace ) ;
101- if ( globalOverride !== kNoOverride ) return globalOverride ;
88+ let internalPrepareStackTrace = defaultPrepareStackTrace ;
10289
90+ /**
91+ * The default implementation of `Error.prepareStackTrace` with simple
92+ * concatenation of stack frames.
93+ * Read more about `Error.prepareStackTrace` at https://v8.dev/docs/stack-trace-api#customizing-stack-traces.
94+ */
95+ function defaultPrepareStackTrace ( error , trace ) {
10396 // Normal error formatting:
10497 //
10598 // Error: Message
@@ -115,9 +108,35 @@ const prepareStackTrace = (globalThis, error, trace) => {
115108 return errorString ;
116109 }
117110 return `${ errorString } \n at ${ ArrayPrototypeJoin ( trace , '\n at ' ) } ` ;
118- } ;
111+ }
112+
113+ function setInternalPrepareStackTrace ( callback ) {
114+ internalPrepareStackTrace = callback ;
115+ }
116+
117+ /**
118+ * Every realm has its own prepareStackTraceCallback. When `error.stack` is
119+ * accessed, if the error is created in a shadow realm, the shadow realm's
120+ * prepareStackTraceCallback is invoked. Otherwise, the principal realm's
121+ * prepareStackTraceCallback is invoked. Note that accessing `error.stack`
122+ * of error objects created in a VM Context will always invoke the
123+ * prepareStackTraceCallback of the principal realm.
124+ * @param {object } globalThis The global object of the realm that the error was
125+ * created in. When the error object is created in a VM Context, this is the
126+ * global object of that VM Context.
127+ * @param {object } error The error object.
128+ * @param {CallSite[] } trace An array of CallSite objects, read more at https://v8.dev/docs/stack-trace-api#customizing-stack-traces.
129+ * @returns {string }
130+ */
131+ function prepareStackTraceCallback ( globalThis , error , trace ) {
132+ // API for node internals to override error stack formatting
133+ // without interfering with userland code.
134+ if ( overrideStackTrace . has ( error ) ) {
135+ const f = overrideStackTrace . get ( error ) ;
136+ overrideStackTrace . delete ( error ) ;
137+ return f ( error , trace ) ;
138+ }
119139
120- const maybeOverridePrepareStackTrace = ( globalThis , error , trace ) => {
121140 // Polyfill of V8's Error.prepareStackTrace API.
122141 // https://crbug.com/v8/7848
123142 // `globalThis` is the global that contains the constructor which
@@ -132,8 +151,17 @@ const maybeOverridePrepareStackTrace = (globalThis, error, trace) => {
132151 return MainContextError . prepareStackTrace ( error , trace ) ;
133152 }
134153
135- return kNoOverride ;
136- } ;
154+ // If the Error.prepareStackTrace was not a function, fallback to the
155+ // internal implementation.
156+ return internalPrepareStackTrace ( error , trace ) ;
157+ }
158+
159+ /**
160+ * The default Error.prepareStackTrace implementation.
161+ */
162+ function ErrorPrepareStackTrace ( error , trace ) {
163+ return internalPrepareStackTrace ( error , trace ) ;
164+ }
137165
138166const aggregateTwoErrors = ( innerError , outerError ) => {
139167 if ( innerError && outerError && innerError !== outerError ) {
@@ -1055,10 +1083,11 @@ module.exports = {
10551083 isStackOverflowError,
10561084 kEnhanceStackBeforeInspector,
10571085 kIsNodeError,
1058- kNoOverride ,
1059- maybeOverridePrepareStackTrace ,
1086+ defaultPrepareStackTrace ,
1087+ setInternalPrepareStackTrace ,
10601088 overrideStackTrace,
1061- prepareStackTrace,
1089+ prepareStackTraceCallback,
1090+ ErrorPrepareStackTrace,
10621091 setArrowMessage,
10631092 SystemError,
10641093 uvErrmapGet,
0 commit comments