Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 99977c5

Browse files
committedMar 20, 2023
fixup!
1 parent e760ab7 commit 99977c5

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed
 

‎src/api/environment.cc

+6-8
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,13 @@ MaybeLocal<Value> PrepareStackTraceCallback(Local<Context> context,
6767
if (env == nullptr) {
6868
return exception->ToString(context).FromMaybe(Local<Value>());
6969
}
70-
Realm* current_realm = Realm::GetCurrent(context);
70+
Realm* realm = Realm::GetCurrent(context);
7171
Local<Function> prepare;
72-
if (current_realm != nullptr &&
73-
current_realm->kind() != Realm::Kind::kPrincipal) {
74-
// If we are in a Realm that is not the principal Realm (e.g. ShadowRealm),
75-
// call the realm specific prepareStackTrace callback to avoid passing the
76-
// JS objects (the exception and trace) across the realm boundary with the
77-
// `Error.prepareStackTrace` override.
78-
prepare = current_realm->prepare_stack_trace_callback();
72+
if (realm != nullptr) {
73+
// If we are in a Realm, call the realm specific prepareStackTrace callback
74+
// to avoid passing the JS objects (the exception and trace) across the
75+
// realm boundary with the `Error.prepareStackTrace` override.
76+
prepare = realm->prepare_stack_trace_callback();
7977
} else {
8078
// The context is created with ContextifyContext, call the principal
8179
// realm's prepareStackTrace callback.

‎test/parallel/test-shadow-realm-prepare-stack-trace.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44
require('../common');
55
const assert = require('assert');
66

7+
let principalRealmPrepareStackTraceCalled = false;
8+
Error.prepareStackTrace = (error, trace) => {
9+
principalRealmPrepareStackTraceCalled = true;
10+
return `${String(error)}\n at ${trace.join('\n at ')}`;
11+
};
12+
713
{
814
// Validates inner Error.prepareStackTrace can not leak into the outer realm.
915
const shadowRealm = new ShadowRealm();
1016

1117
const stack = shadowRealm.evaluate(`
1218
Error.prepareStackTrace = (error, trace) => {
1319
globalThis.leaked = 'inner';
14-
return String(error);
20+
return 'from shadow realm';
1521
};
1622
1723
try {
@@ -20,7 +26,8 @@ try {
2026
e.stack;
2127
}
2228
`);
23-
assert.strictEqual(stack, 'Error: boom');
29+
assert.ok(!principalRealmPrepareStackTraceCalled);
30+
assert.strictEqual(stack, 'from shadow realm');
2431
assert.strictEqual('leaked' in globalThis, false);
2532
}
2633

@@ -39,6 +46,7 @@ try {
3946
e.stack;
4047
}
4148
`);
49+
assert.ok(!principalRealmPrepareStackTraceCalled);
4250
const lines = stack.split('\n');
4351
assert.strictEqual(lines[0], 'Error: boom');
4452
assert.match(lines[1], /^ {4}at myFunc \(.*\)/);

0 commit comments

Comments
 (0)
Please sign in to comment.