-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
fa84657
commit e760ab7
Showing
7 changed files
with
106 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
|
||
/** | ||
* This file is executed in every realm that is created by Node.js, including | ||
* the context of main thread, worker threads, and ShadowRealms. | ||
* Only per-realm internal states and callbacks should be bootstrapped in this | ||
* file and no globals should be exposed to the user code. | ||
*/ | ||
|
||
setupPrepareStackTrace(); | ||
|
||
function setupPrepareStackTrace() { | ||
const { | ||
setEnhanceStackForFatalException, | ||
setPrepareStackTraceCallback, | ||
} = internalBinding('errors'); | ||
const { | ||
prepareStackTrace, | ||
fatalExceptionStackEnhancers: { | ||
beforeInspector, | ||
afterInspector, | ||
}, | ||
} = require('internal/errors'); | ||
// Tell our PrepareStackTraceCallback passed to the V8 API | ||
// to call prepareStackTrace(). | ||
setPrepareStackTraceCallback(prepareStackTrace); | ||
// Set the function used to enhance the error stack for printing | ||
setEnhanceStackForFatalException(beforeInspector, afterInspector); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Flags: --experimental-shadow-realm | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
|
||
{ | ||
// Validates inner Error.prepareStackTrace can not leak into the outer realm. | ||
const shadowRealm = new ShadowRealm(); | ||
|
||
const stack = shadowRealm.evaluate(` | ||
Error.prepareStackTrace = (error, trace) => { | ||
globalThis.leaked = 'inner'; | ||
return String(error); | ||
}; | ||
try { | ||
throw new Error('boom'); | ||
} catch (e) { | ||
e.stack; | ||
} | ||
`); | ||
assert.strictEqual(stack, 'Error: boom'); | ||
assert.strictEqual('leaked' in globalThis, false); | ||
} | ||
|
||
{ | ||
// Validates stacks can be generated in the ShadowRealm. | ||
const shadowRealm = new ShadowRealm(); | ||
|
||
const stack = shadowRealm.evaluate(` | ||
function myFunc() { | ||
throw new Error('boom'); | ||
} | ||
try { | ||
myFunc(); | ||
} catch (e) { | ||
e.stack; | ||
} | ||
`); | ||
const lines = stack.split('\n'); | ||
assert.strictEqual(lines[0], 'Error: boom'); | ||
assert.match(lines[1], /^ {4}at myFunc \(.*\)/); | ||
} |