0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-shadow-realm-prepare-stack-trace.js
Chengzhong Wu 3a014dc38a
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: https://github.com/nodejs/node/pull/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>
2023-04-04 01:13:37 +00:00

54 lines
1.2 KiB
JavaScript

// Flags: --experimental-shadow-realm
'use strict';
require('../common');
const assert = require('assert');
let principalRealmPrepareStackTraceCalled = false;
Error.prepareStackTrace = (error, trace) => {
principalRealmPrepareStackTraceCalled = true;
return `${String(error)}\n at ${trace.join('\n at ')}`;
};
{
// 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 'from shadow realm';
};
try {
throw new Error('boom');
} catch (e) {
e.stack;
}
`);
assert.ok(!principalRealmPrepareStackTraceCalled);
assert.strictEqual(stack, 'from shadow realm');
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;
}
`);
assert.ok(!principalRealmPrepareStackTraceCalled);
const lines = stack.split('\n');
assert.strictEqual(lines[0], 'Error: boom');
assert.match(lines[1], /^ {4}at myFunc \(.*\)/);
}