summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/harmony/shadowrealm-evaluate.js
blob: 8288963428138eb03882c8df4a0c94f0f5cd736b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// 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();

// re-throwing with SyntaxError
assertThrows(() => shadowRealm.evaluate('...'), SyntaxError, 'Unexpected end of input')

// builtin
var wrapped = shadowRealm.evaluate('String.prototype.substring');
assertEquals(wrapped.call('123', 1), '23');

// bound function
var wrapped = shadowRealm.evaluate('(function it() { return this.a }).bind({ a: 1 })');
assertEquals(wrapped(), 1);

// nested bound function
var wrapped = shadowRealm.evaluate('(function it() { return this.a }).bind({ a: 1 }).bind().bind()');
assertEquals(wrapped(), 1);

// function with function context
var wrapped = shadowRealm.evaluate(`
(function () {
  var a = 1;
  function it() { return a++; };
  return it;
})()
`);
assertEquals(wrapped(), 1);
assertEquals(wrapped(), 2);

// callable proxy
var wrapped = shadowRealm.evaluate('new Proxy(() => 1, {})');
assertEquals(wrapped(), 1);

// nested callable proxy
var wrapped = shadowRealm.evaluate('new Proxy(new Proxy(new Proxy(() => 1, {}), {}), {})');
assertEquals(wrapped(), 1);

// revocable proxy
var wrapped = shadowRealm.evaluate(`
var revocable = Proxy.revocable(() => 1, {});
globalThis.revoke = () => {
  revocable.revoke();
};

revocable.proxy;
`);
var revoke = shadowRealm.evaluate('globalThis.revoke');
assertEquals(wrapped(), 1);
revoke();
assertThrows(() => wrapped(), TypeError, "Cannot perform 'apply' on a proxy that has been revoked");

// revoked proxy
assertThrows(() => shadowRealm.evaluate(`
var revocable = Proxy.revocable(() => 1, {});
revocable.revoke();
revocable.proxy;
`), TypeError, "Cannot wrap target callable (TypeError: Cannot perform 'getOwnPropertyDescriptor' on a proxy that has been revoked)");

// no-side-effects inspection on thrown error
assertThrows(() => shadowRealm.evaluate(`
throw new Error('foo');
`), TypeError, "ShadowRealm evaluate threw (Error: foo)");

// no-side-effects inspection on thrown error
assertThrows(() => shadowRealm.evaluate(`
globalThis.messageAccessed = false;
const err = new Error('foo');
Object.defineProperty(err, 'message', {
  get: function() {
    globalThis.messageAccessed = true;
    return 'bar';
  },
});
throw err;
`), TypeError, "ShadowRealm evaluate threw (Error)");
assertFalse(shadowRealm.evaluate('globalThis.messageAccessed'));