1425bb815Sopenharmony_ci// Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci//
3425bb815Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci// you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci// You may obtain a copy of the License at
6425bb815Sopenharmony_ci//
7425bb815Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci//
9425bb815Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci// See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci// limitations under the License.
14425bb815Sopenharmony_ci
15425bb815Sopenharmony_ci// Copyright 2015 the V8 project authors. All rights reserved.
16425bb815Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
17425bb815Sopenharmony_ci// found in the LICENSE file.
18425bb815Sopenharmony_ci
19425bb815Sopenharmony_civar target = {};
20425bb815Sopenharmony_civar handler = { get (target) {
21425bb815Sopenharmony_ci  throw 42;
22425bb815Sopenharmony_ci}};
23425bb815Sopenharmony_ci
24425bb815Sopenharmony_civar proxy = new Proxy(target, handler);
25425bb815Sopenharmony_ci
26425bb815Sopenharmony_citry {
27425bb815Sopenharmony_ci  // vm_op_get_value
28425bb815Sopenharmony_ci  proxy.a
29425bb815Sopenharmony_ci  assert(false);
30425bb815Sopenharmony_ci} catch (e) {
31425bb815Sopenharmony_ci  assert(e === 42);
32425bb815Sopenharmony_ci}
33425bb815Sopenharmony_ci
34425bb815Sopenharmony_citry {
35425bb815Sopenharmony_ci  // ecma_op_get_value_object_base
36425bb815Sopenharmony_ci  proxy[2];
37425bb815Sopenharmony_ci  assert(false);
38425bb815Sopenharmony_ci} catch (e) {
39425bb815Sopenharmony_ci  assert(e === 42);
40425bb815Sopenharmony_ci}
41425bb815Sopenharmony_ci
42425bb815Sopenharmony_citry {
43425bb815Sopenharmony_ci  // @@toPrimitive symbol
44425bb815Sopenharmony_ci  proxy + "foo";
45425bb815Sopenharmony_ci  assert(false);
46425bb815Sopenharmony_ci} catch (e) {
47425bb815Sopenharmony_ci  assert(e === 42);
48425bb815Sopenharmony_ci}
49425bb815Sopenharmony_ci
50425bb815Sopenharmony_ci// test basic funcionality
51425bb815Sopenharmony_civar target = {
52425bb815Sopenharmony_ci  target_one: 1,
53425bb815Sopenharmony_ci  prop: "value"
54425bb815Sopenharmony_ci};
55425bb815Sopenharmony_ci
56425bb815Sopenharmony_civar handler = {handler: 1};
57425bb815Sopenharmony_civar proxy = new Proxy(target, handler);
58425bb815Sopenharmony_ci
59425bb815Sopenharmony_ciassert(proxy.prop === "value");
60425bb815Sopenharmony_ciassert(proxy.nothing === undefined);
61425bb815Sopenharmony_ciassert(proxy.handler === undefined);
62425bb815Sopenharmony_ci
63425bb815Sopenharmony_cihandler.get = function () {return "value 2"};
64425bb815Sopenharmony_ci
65425bb815Sopenharmony_ciassert(proxy.prop === "value 2");
66425bb815Sopenharmony_ciassert(proxy.nothing === "value 2");
67425bb815Sopenharmony_ciassert(proxy.handler === "value 2");
68425bb815Sopenharmony_ci
69425bb815Sopenharmony_civar handler2 = new Proxy({get: function() {return "value 3"}}, {});
70425bb815Sopenharmony_civar proxy2 = new Proxy(target, handler2);
71425bb815Sopenharmony_ci
72425bb815Sopenharmony_ciassert(proxy2.prop === "value 3");
73425bb815Sopenharmony_ciassert(proxy2.nothing === "value 3");
74425bb815Sopenharmony_ciassert(proxy2.handler === "value 3");
75425bb815Sopenharmony_ci
76425bb815Sopenharmony_civar get = [];
77425bb815Sopenharmony_civar p = new Proxy([0,,2,,4,,], { get: function(o, k) { get.push(k); return o[k]; }});
78425bb815Sopenharmony_ciArray.prototype.reverse.call(p);
79425bb815Sopenharmony_ci
80425bb815Sopenharmony_ciassert(get + '' === "length,0,4,2");
81425bb815Sopenharmony_ci
82425bb815Sopenharmony_ci// test when get throws an error
83425bb815Sopenharmony_civar handler = new Proxy({}, {get: function() {throw 42;}});
84425bb815Sopenharmony_civar proxy = new Proxy ({}, handler);
85425bb815Sopenharmony_ci
86425bb815Sopenharmony_citry {
87425bb815Sopenharmony_ci  proxy.prop;
88425bb815Sopenharmony_ci  assert(false);
89425bb815Sopenharmony_ci} catch (e) {
90425bb815Sopenharmony_ci  assert(e === 42);
91425bb815Sopenharmony_ci}
92425bb815Sopenharmony_ci
93425bb815Sopenharmony_ci// test when trap is undefined
94425bb815Sopenharmony_civar handler = new Proxy({}, {get: function() {return undefined}});
95425bb815Sopenharmony_civar target = {prop: "value"};
96425bb815Sopenharmony_civar proxy = new Proxy(target, handler);
97425bb815Sopenharmony_ciassert(proxy.prop === "value");
98425bb815Sopenharmony_ciassert(proxy.prop2 === undefined);
99425bb815Sopenharmony_ci
100425bb815Sopenharmony_ci// test when invariants gets violated
101425bb815Sopenharmony_civar target = {};
102425bb815Sopenharmony_civar handler = {get: function(r, p){if (p != "key4") return "value"}}
103425bb815Sopenharmony_civar proxy = new Proxy(target, handler);
104425bb815Sopenharmony_ci
105425bb815Sopenharmony_ciassert(proxy.key === "value");
106425bb815Sopenharmony_ciassert(proxy.key2 === "value");
107425bb815Sopenharmony_ciassert(proxy.key3 === "value");
108425bb815Sopenharmony_ciassert(proxy.key4 === undefined);
109425bb815Sopenharmony_ci
110425bb815Sopenharmony_ciObject.defineProperty(target, "key", {
111425bb815Sopenharmony_ci  configurable: false,
112425bb815Sopenharmony_ci  writable: false,
113425bb815Sopenharmony_ci  value: "different value"
114425bb815Sopenharmony_ci});
115425bb815Sopenharmony_ci
116425bb815Sopenharmony_citry {
117425bb815Sopenharmony_ci  proxy.key;
118425bb815Sopenharmony_ci  assert(false);
119425bb815Sopenharmony_ci} catch (e) {
120425bb815Sopenharmony_ci  assert(e instanceof TypeError)
121425bb815Sopenharmony_ci}
122425bb815Sopenharmony_ci
123425bb815Sopenharmony_ciObject.defineProperty(target, "key2", {
124425bb815Sopenharmony_ci  configurable: false,
125425bb815Sopenharmony_ci  get: function() {return "different value"}
126425bb815Sopenharmony_ci});
127425bb815Sopenharmony_ci
128425bb815Sopenharmony_ciassert(proxy.key2 === "value");
129425bb815Sopenharmony_ci
130425bb815Sopenharmony_ciObject.defineProperty(target, "key3", {
131425bb815Sopenharmony_ci  configurable: false,
132425bb815Sopenharmony_ci  set: function() {}
133425bb815Sopenharmony_ci});
134425bb815Sopenharmony_ci
135425bb815Sopenharmony_citry {
136425bb815Sopenharmony_ci  proxy.key3;
137425bb815Sopenharmony_ci  assert(false);
138425bb815Sopenharmony_ci} catch (e) {
139425bb815Sopenharmony_ci  assert(e instanceof TypeError)
140425bb815Sopenharmony_ci}
141