private-symbols.js 3.05 KB
Newer Older
1 2 3 4
// Copyright 2016 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.

5
// Flags: --allow-natives-syntax
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


var symbol = %CreatePrivateSymbol("private");


// Private symbols must never be listed.

var object = {};
object[symbol] = 42;
for (var key of Object.keys(object)) assertUnreachable();
for (var key of Object.getOwnPropertySymbols(object)) assertUnreachable();
for (var key of Object.getOwnPropertyNames(object)) assertUnreachable();
for (var key of Reflect.ownKeys(object)) assertUnreachable();
for (var key in object) assertUnreachable();

var object2 = {__proto__: object};
for (var key of Object.keys(object2)) assertUnreachable();
for (var key of Object.getOwnPropertySymbols(object2)) assertUnreachable();
for (var key of Object.getOwnPropertyNames(object2)) assertUnreachable();
for (var key of Reflect.ownKeys(object2)) assertUnreachable();
for (var key in object2) assertUnreachable();


// Private symbols must never leak to proxy traps.

31 32
var proxy = new Proxy({}, new Proxy({}, {get() {return () => {
  throw new Error()}}}));
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
var object = {__proto__: proxy};

// [[Set]]
assertEquals(42, proxy[symbol] = 42);
assertThrows(function() { "use strict"; proxy[symbol] = 42 }, TypeError);
assertEquals(false, Reflect.set(proxy, symbol, 42));
assertEquals(42, object[symbol] = 42);
assertEquals(43, (function() {"use strict"; return object[symbol] = 43})());
assertEquals(true, Reflect.set(object, symbol, 44));

// [[DefineOwnProperty]]
assertEquals(false, Reflect.defineProperty(proxy, symbol, {}));
assertThrows(() => Object.defineProperty(proxy, symbol, {}), TypeError);
assertEquals(true, Reflect.defineProperty(object, symbol, {}));
assertEquals(object, Object.defineProperty(object, symbol, {}));

// [[Delete]]
assertEquals(true, delete proxy[symbol]);
assertEquals(true, (function() {"use strict"; return delete proxy[symbol]})());
assertEquals(true, Reflect.deleteProperty(proxy, symbol));
assertEquals(true, delete object[symbol]);
assertEquals(true, (function() {"use strict"; return delete object[symbol]})());
assertEquals(true, Reflect.deleteProperty(object, symbol));

// [[GetOwnPropertyDescriptor]]
assertEquals(undefined, Object.getOwnPropertyDescriptor(proxy, symbol));
assertEquals(undefined, Reflect.getOwnPropertyDescriptor(proxy, symbol));
assertFalse(Object.prototype.hasOwnProperty.call(proxy, symbol));
assertEquals(undefined, Object.getOwnPropertyDescriptor(object, symbol));
assertEquals(undefined, Reflect.getOwnPropertyDescriptor(object, symbol));
assertFalse(Object.prototype.hasOwnProperty.call(object, symbol));

// [[Has]]
assertFalse(symbol in proxy);
assertFalse(Reflect.has(proxy, symbol));
assertFalse(symbol in object);
assertFalse(Reflect.has(object, symbol));

// [[Get]]
assertEquals(undefined, proxy[symbol]);
assertEquals(undefined, Reflect.get(proxy, symbol));
assertEquals(undefined, Reflect.get(proxy, symbol, 42));
assertEquals(undefined, object[symbol]);
assertEquals(undefined, Reflect.get(object, symbol));
assertEquals(undefined, Reflect.get(object, symbol, 42));