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
15// Copyright 2015 the V8 project authors. All rights reserved.
16// Use of this source code is governed by a BSD-style license that can be
17// found in the LICENSE file.
18
19var target = {};
20var handler = { get (target) {
21  throw 42;
22}};
23
24var proxy = new Proxy(target, handler);
25
26try {
27  // vm_op_get_value
28  proxy.a
29  assert(false);
30} catch (e) {
31  assert(e === 42);
32}
33
34try {
35  // ecma_op_get_value_object_base
36  proxy[2];
37  assert(false);
38} catch (e) {
39  assert(e === 42);
40}
41
42try {
43  // @@toPrimitive symbol
44  proxy + "foo";
45  assert(false);
46} catch (e) {
47  assert(e === 42);
48}
49
50// test basic funcionality
51var target = {
52  target_one: 1,
53  prop: "value"
54};
55
56var handler = {handler: 1};
57var proxy = new Proxy(target, handler);
58
59assert(proxy.prop === "value");
60assert(proxy.nothing === undefined);
61assert(proxy.handler === undefined);
62
63handler.get = function () {return "value 2"};
64
65assert(proxy.prop === "value 2");
66assert(proxy.nothing === "value 2");
67assert(proxy.handler === "value 2");
68
69var handler2 = new Proxy({get: function() {return "value 3"}}, {});
70var proxy2 = new Proxy(target, handler2);
71
72assert(proxy2.prop === "value 3");
73assert(proxy2.nothing === "value 3");
74assert(proxy2.handler === "value 3");
75
76var get = [];
77var p = new Proxy([0,,2,,4,,], { get: function(o, k) { get.push(k); return o[k]; }});
78Array.prototype.reverse.call(p);
79
80assert(get + '' === "length,0,4,2");
81
82// test when get throws an error
83var handler = new Proxy({}, {get: function() {throw 42;}});
84var proxy = new Proxy ({}, handler);
85
86try {
87  proxy.prop;
88  assert(false);
89} catch (e) {
90  assert(e === 42);
91}
92
93// test when trap is undefined
94var handler = new Proxy({}, {get: function() {return undefined}});
95var target = {prop: "value"};
96var proxy = new Proxy(target, handler);
97assert(proxy.prop === "value");
98assert(proxy.prop2 === undefined);
99
100// test when invariants gets violated
101var target = {};
102var handler = {get: function(r, p){if (p != "key4") return "value"}}
103var proxy = new Proxy(target, handler);
104
105assert(proxy.key === "value");
106assert(proxy.key2 === "value");
107assert(proxy.key3 === "value");
108assert(proxy.key4 === undefined);
109
110Object.defineProperty(target, "key", {
111  configurable: false,
112  writable: false,
113  value: "different value"
114});
115
116try {
117  proxy.key;
118  assert(false);
119} catch (e) {
120  assert(e instanceof TypeError)
121}
122
123Object.defineProperty(target, "key2", {
124  configurable: false,
125  get: function() {return "different value"}
126});
127
128assert(proxy.key2 === "value");
129
130Object.defineProperty(target, "key3", {
131  configurable: false,
132  set: function() {}
133});
134
135try {
136  proxy.key3;
137  assert(false);
138} catch (e) {
139  assert(e instanceof TypeError)
140}
141