v8/test/mjsunit/harmony/shadowrealm-wrapped-function.js
legendecas 804be91ab9 [ShadowRealm] side-effect-free inspection on cross-realm exceptions
The spec does not allow side effects on wrapping the exceptions
crossing the realm boundaries. We need to provide an easy way to inspect the exception-thrown cross-realms according to the last TC39 meeting
consensus.

Related spec change: https://github.com/tc39/proposal-shadowrealm/pull/382.

Bug: v8:11989
Change-Id: Ia78d94fd33cba689267aeacd028d662bd4a37fe9
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3618759
Commit-Queue: Chengzhong Wu (legendecas) <legendecas@gmail.com>
Reviewed-by: Shu-yu Guo <syg@chromium.org>
Cr-Commit-Position: refs/heads/main@{#84961}
2022-12-20 17:25:10 +00:00

54 lines
1.5 KiB
JavaScript

// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-shadow-realm
var shadowRealm = new ShadowRealm();
// create a proxy on the wrapped value
var wrapped = shadowRealm.evaluate('() => 1');
assertEquals(wrapped(), 1);
var proxy = new Proxy(wrapped, {
call: function(target, thisArg, args) {
assertEquals(target, wrapped);
return target();
},
});
assertEquals(proxy(), 1);
// create a revocable proxy on the wrapped value
var revocable = Proxy.revocable(wrapped, {
call: function(target, thisArg, args) {
assertEquals(target, wrapped);
return target();
},
});
var proxy = revocable.proxy;
assertEquals(proxy(), 1);
revocable.revoke();
assertThrows(() => proxy(), TypeError, "Cannot perform 'apply' on a proxy that has been revoked");
// no-side-effects inspection on thrown error
var wrapped = shadowRealm.evaluate(`() => {
throw new Error('foo');
}`);
assertThrows(() => wrapped(), TypeError, "WrappedFunction threw (Error: foo)");
// no-side-effects inspection on thrown error
var wrapped = shadowRealm.evaluate(`
globalThis.messageAccessed = false;
() => {
const err = new Error('foo');
Object.defineProperty(err, 'message', {
get: function() {
globalThis.messageAccessed = true;
return 'bar';
},
});
throw err;
}
`);
assertThrows(() => wrapped(), TypeError, "WrappedFunction threw (Error)");
assertFalse(shadowRealm.evaluate('globalThis.messageAccessed'));