1// Copyright 2013 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28// Flags: --harmony-proxies --harmony-symbols
29
30
31// Helper.
32
33function TestWithProxies(test, x, y, z) {
34  test(Proxy.create, x, y, z)
35  test(function(h) {return Proxy.createFunction(h, function() {})}, x, y, z)
36}
37
38
39// No symbols should leak to proxy traps.
40
41function TestNoSymbolsToTrap(handler) {
42  TestWithProxies(TestNoSymbolsToTrap2, handler)
43}
44
45function TestNoSymbolsToTrap2(create, handler) {
46  var p = create(handler)
47  var o = Object.create(p)
48  var symbol = Symbol("secret")
49
50  assertFalse(symbol in p)
51  assertFalse(symbol in o)
52  assertEquals(undefined, p[symbol])
53  assertEquals(undefined, o[symbol])
54  assertEquals(47, p[symbol] = 47)
55  assertEquals(47, o[symbol] = 47)
56  assertFalse(delete p[symbol])
57  assertTrue(delete o[symbol])
58  assertTrue(delete o[symbol])
59  assertFalse({}.hasOwnProperty.call(p, symbol))
60  assertFalse({}.hasOwnProperty.call(o, symbol))
61  assertEquals(undefined, Object.getOwnPropertyDescriptor(p, symbol))
62  assertEquals(undefined, Object.getOwnPropertyDescriptor(o, symbol))
63}
64
65
66TestNoSymbolsToTrap({
67  has: assertUnreachable,
68  hasOwn: assertUnreachable,
69  get: assertUnreachable,
70  set: assertUnreachable,
71  delete: assertUnreachable,
72  getPropertyDescriptor: assertUnreachable,
73  getOwnPropertyDescriptor: assertUnreachable,
74  defineProperty: assertUnreachable
75})
76
77
78// All symbols returned from proxy traps should be filtered.
79
80function TestNoSymbolsFromTrap(handler) {
81  TestWithProxies(TestNoSymbolsFromTrap2, handler)
82}
83
84function TestNoSymbolsFromTrap2(create, handler) {
85  var p = create(handler)
86  var o = Object.create(p)
87
88  assertEquals(0, Object.keys(p).length)
89  assertEquals(0, Object.keys(o).length)
90  assertEquals(0, Object.getOwnPropertyNames(p).length)
91  assertEquals(0, Object.getOwnPropertyNames(o).length)
92  for (var n in p) assertUnreachable()
93  for (var n in o) assertUnreachable()
94}
95
96
97function MakeSymbolArray() {
98  return [Symbol(), Symbol("a")]
99}
100
101TestNoSymbolsFromTrap({
102  enumerate: MakeSymbolArray,
103  keys: MakeSymbolArray,
104  getPropertyNames: MakeSymbolArray,
105  getOwnPropertyNames: MakeSymbolArray
106})
107