1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15var obj = {
16    prop1: 42,
17    prop2: 13,
18    prop3: "foo",
19    prop4: 1
20};
21
22var obj2 = {
23    foo: "foo"
24};
25
26obj[Symbol.unscopables] = {
27    prop1: false,
28    prop2: true,
29    prop3: undefined,
30    prop4: obj2
31};
32
33with (obj)
34{
35    assert(prop1 === 42);
36
37    try {
38      prop2;
39      assert(false);
40    } catch (e) {
41      assert(e instanceof ReferenceError);
42    }
43
44    assert (prop3 === "foo");
45
46    try {
47      prop4;
48      assert(false);
49    } catch (e) {
50      assert(e instanceof ReferenceError);
51    }
52
53    try {
54      prop5;
55      assert(false);
56    } catch (e) {
57      assert(e instanceof ReferenceError);
58    }
59}
60
61var obj2 = {};
62Object.defineProperty(obj2, Symbol.unscopables, { get: function () { throw 42; } });
63
64with (obj2) {
65  try {
66    prop;
67    assert(false);
68  } catch (e) {
69    assert(e instanceof ReferenceError);
70  }
71}
72
73var obj3 = { foo: 12 };
74Object.defineProperty(obj3, Symbol.unscopables, { get: function () { throw 42; } });
75
76with (obj3) {
77  try {
78    typeof foo;
79  } catch (e) {
80    assert(e === 42);
81  }
82}
83
84var symbol_obj = Array.prototype[Symbol.unscopables];
85assert(symbol_obj.copyWithin === true);
86assert(symbol_obj.entries === true);
87assert(symbol_obj.fill === true);
88assert(symbol_obj.find === true);
89assert(symbol_obj.findIndex === true);
90assert(symbol_obj.keys === true);
91assert(symbol_obj.values === true);
92
93assert(Object.getPrototypeOf(Array.prototype[Symbol.unscopables]) === null);
94
95var obj3 = Object.getOwnPropertyDescriptor(Array.prototype[Symbol.unscopables], "find");
96assert(obj3.value === true);
97assert(obj3.writable === true);
98assert(obj3.enumerable == true);
99assert(obj3.configurable == true);
100
101var a = { foo: 1, bar: 2 };
102a[Symbol.unscopables] = { bar: true };
103with (a) {
104  assert(foo === 1);
105  assert(typeof bar === "undefined");
106}
107
108let track = [];
109let proxy = new Proxy({ a : 4, [Symbol.unscopables] : [] }, {
110  has (t, p) {
111    track.push(p);
112    return Reflect.has(...arguments);
113  },
114  get (t, p, r) {
115    track.push(p);
116    return Reflect.get(...arguments);
117  }
118});
119
120with (proxy){
121  a;
122}
123
124assert(track.length == 3);
125assert(track[0] === 'a');
126assert(track[1] === Symbol.unscopables);
127assert(track[2] === 'a');
128