|
| 1 | +// Copyright 2022 the V8 project authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +d8.file.execute('test/mjsunit/regress/regress-crbug-1321899.js'); |
| 6 | + |
| 7 | +// Attached global should have access |
| 8 | +const realm = Realm.createAllowCrossRealmAccess(); |
| 9 | +const globalProxy = Realm.global(realm); |
| 10 | + |
| 11 | +assertThrows(() => B.setField(globalProxy), TypeError, /Cannot write private member #b to an object whose class did not declare it/); |
| 12 | +assertThrows(() => B.getField(globalProxy), TypeError, /Cannot read private member #b from an object whose class did not declare it/); |
| 13 | + |
| 14 | +new B(globalProxy); |
| 15 | +assertEquals(B.getField(globalProxy), 1); |
| 16 | +B.setField(globalProxy); |
| 17 | +assertEquals(B.getField(globalProxy), 'b'); // Fast case |
| 18 | +B.setField(globalProxy); // Fast case |
| 19 | +assertEquals(B.getField(globalProxy), 'b'); // Fast case |
| 20 | +assertThrows(() => new B(globalProxy), TypeError, /Cannot initialize #b twice on the same object/); |
| 21 | + |
| 22 | +assertThrows(() => C.setField(globalProxy), TypeError, /Cannot write private member #c to an object whose class did not declare it/); |
| 23 | +assertThrows(() => C.getField(globalProxy), TypeError, /Cannot read private member #c from an object whose class did not declare it/); |
| 24 | + |
| 25 | +new C(globalProxy); |
| 26 | +assertEquals(C.getField(globalProxy), undefined); |
| 27 | +C.setField(globalProxy); |
| 28 | +assertEquals(C.getField(globalProxy), 'c'); // Fast case |
| 29 | +C.setField(globalProxy); // Fast case |
| 30 | +assertEquals(C.getField(globalProxy), 'c'); // Fast case |
| 31 | +assertThrows(() => new C(globalProxy), TypeError, /Cannot initialize #c twice on the same object/); |
| 32 | + |
| 33 | +assertThrows(() => D.setAccessor(globalProxy), TypeError, /Receiver must be an instance of class D/); |
| 34 | +assertThrows(() => D.getAccessor(globalProxy), TypeError, /Receiver must be an instance of class D/); |
| 35 | + |
| 36 | +new D(globalProxy); |
| 37 | +assertEquals(D.getAccessor(globalProxy), 0); |
| 38 | +D.setAccessor(globalProxy); |
| 39 | +assertEquals(D.getAccessor(globalProxy), 'd'); // Fast case |
| 40 | +D.setAccessor(globalProxy); // Fast case |
| 41 | +assertEquals(D.getAccessor(globalProxy), 'd'); // Fast case |
| 42 | +assertThrows(() => new D(globalProxy), TypeError, /Cannot initialize private methods of class D twice on the same object/); |
| 43 | + |
| 44 | +assertThrows(() => E.setMethod(globalProxy), TypeError, /Receiver must be an instance of class E/); |
| 45 | +assertThrows(() => E.getMethod(globalProxy), TypeError, /Receiver must be an instance of class E/); |
| 46 | + |
| 47 | +new E(globalProxy); |
| 48 | +assertEquals(E.getMethod(globalProxy)(), 0); |
| 49 | +assertThrows(() => E.setMethod(globalProxy), TypeError, /Private method '#e' is not writable/); |
| 50 | +assertEquals(E.getMethod(globalProxy)(), 0); // Fast case |
| 51 | +assertThrows(() => new E(globalProxy), TypeError, /Cannot initialize private methods of class E twice on the same object/); |
| 52 | + |
| 53 | +// Access should fail after detaching |
| 54 | +Realm.detachGlobal(realm); |
| 55 | + |
| 56 | +assertThrows(() => new B(globalProxy), Error, /no access/); |
| 57 | +assertThrows(() => new C(globalProxy), Error, /no access/); |
| 58 | +assertThrows(() => new D(globalProxy), Error, /no access/); |
| 59 | +assertThrows(() => new E(globalProxy), Error, /no access/); |
| 60 | +assertThrows(() => B.setField(globalProxy), Error, /no access/); |
| 61 | +assertThrows(() => C.setField(globalProxy), Error, /no access/); |
| 62 | +assertThrows(() => D.setAccessor(globalProxy), Error, /no access/); |
| 63 | +assertThrows(() => E.setMethod(globalProxy), Error, /no access/); |
| 64 | +assertThrows(() => D.getAccessor(globalProxy), Error, /no access/); |
| 65 | +assertThrows(() => E.getMethod(globalProxy), Error, /no access/); |
0 commit comments