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 = function () {};
20425bb815Sopenharmony_civar handler = { construct (target) {
21425bb815Sopenharmony_ci  throw 42;
22425bb815Sopenharmony_ci}};
23425bb815Sopenharmony_ci
24425bb815Sopenharmony_civar proxy = new Proxy(target, handler);
25425bb815Sopenharmony_ci
26425bb815Sopenharmony_citry {
27425bb815Sopenharmony_ci  // opfunc_call
28425bb815Sopenharmony_ci  new proxy(5)
29425bb815Sopenharmony_ci  assert(false);
30425bb815Sopenharmony_ci} catch (e) {
31425bb815Sopenharmony_ci  assert(e === 42);
32425bb815Sopenharmony_ci}
33425bb815Sopenharmony_ci
34425bb815Sopenharmony_citry {
35425bb815Sopenharmony_ci  // 22.1.2.3.4.a
36425bb815Sopenharmony_ci  Array.of.call(proxy);
37425bb815Sopenharmony_ci  assert(false);
38425bb815Sopenharmony_ci} catch (e) {
39425bb815Sopenharmony_ci  assert(e === 42);
40425bb815Sopenharmony_ci}
41425bb815Sopenharmony_ci
42425bb815Sopenharmony_ci// test basic functionality
43425bb815Sopenharmony_civar proxy = new Proxy({},{});
44425bb815Sopenharmony_ci
45425bb815Sopenharmony_citry {
46425bb815Sopenharmony_ci  new proxy();
47425bb815Sopenharmony_ci  assert(false);
48425bb815Sopenharmony_ci} catch (e) {
49425bb815Sopenharmony_ci  assert(e instanceof TypeError);
50425bb815Sopenharmony_ci}
51425bb815Sopenharmony_ci
52425bb815Sopenharmony_civar proxy2 = new Proxy(proxy, {});
53425bb815Sopenharmony_ci
54425bb815Sopenharmony_citry {
55425bb815Sopenharmony_ci  new proxy2();
56425bb815Sopenharmony_ci  assert(false);
57425bb815Sopenharmony_ci} catch (e) {
58425bb815Sopenharmony_ci  assert(e instanceof TypeError);
59425bb815Sopenharmony_ci}
60425bb815Sopenharmony_ci
61425bb815Sopenharmony_civar called = false;
62425bb815Sopenharmony_ci
63425bb815Sopenharmony_cifunction Target() {
64425bb815Sopenharmony_ci  called = true;
65425bb815Sopenharmony_ci  this.property1 = 'value1';
66425bb815Sopenharmony_ci};
67425bb815Sopenharmony_ci
68425bb815Sopenharmony_ciTarget.prototype = {};
69425bb815Sopenharmony_civar proxy = new Proxy(Target, {});
70425bb815Sopenharmony_ci
71425bb815Sopenharmony_ciassert(called === false);
72425bb815Sopenharmony_ci
73425bb815Sopenharmony_civar instance = new proxy();
74425bb815Sopenharmony_ci
75425bb815Sopenharmony_ciassert(called === true);
76425bb815Sopenharmony_ciassert('value1' === instance.property1);
77425bb815Sopenharmony_ciassert(Target.prototype === Object.getPrototypeOf(instance));
78425bb815Sopenharmony_ci
79425bb815Sopenharmony_civar proxy2 = new Proxy(proxy, {});
80425bb815Sopenharmony_cicalled = false;
81425bb815Sopenharmony_civar instance2 = new proxy2();
82425bb815Sopenharmony_ci
83425bb815Sopenharmony_ciassert(called === true);
84425bb815Sopenharmony_ciassert('value1' === instance2.property1);
85425bb815Sopenharmony_ciassert(Target.prototype === Object.getPrototypeOf(instance));
86425bb815Sopenharmony_ci
87425bb815Sopenharmony_cifunction Target2(a, b) {
88425bb815Sopenharmony_ci  this.sum = a + b;
89425bb815Sopenharmony_ci};
90425bb815Sopenharmony_civar handler = {
91425bb815Sopenharmony_ci  construct(t, c, args) {
92425bb815Sopenharmony_ci      return { sum: 42 };
93425bb815Sopenharmony_ci  }
94425bb815Sopenharmony_ci};
95425bb815Sopenharmony_civar proxy = new Proxy(Target2, handler);
96425bb815Sopenharmony_ciassert((new proxy(1, 2)).sum === 42);
97425bb815Sopenharmony_ci
98425bb815Sopenharmony_cifunction Target3(arg1, arg2) {
99425bb815Sopenharmony_ci  this.arg1 = arg1;
100425bb815Sopenharmony_ci  this.arg2 = arg2;
101425bb815Sopenharmony_ci}
102425bb815Sopenharmony_civar seen_target, seen_arguments, seen_new_target;
103425bb815Sopenharmony_civar handler = {
104425bb815Sopenharmony_ci  construct(target, args, new_target) {
105425bb815Sopenharmony_ci    seen_target = target;
106425bb815Sopenharmony_ci    seen_arguments = args;
107425bb815Sopenharmony_ci    seen_new_target = new_target;
108425bb815Sopenharmony_ci    return Reflect.construct(target, args, new_target);
109425bb815Sopenharmony_ci  }
110425bb815Sopenharmony_ci}
111425bb815Sopenharmony_civar proxy = new Proxy(Target3, handler);
112425bb815Sopenharmony_civar instance = new proxy('a', 'b');
113425bb815Sopenharmony_ci
114425bb815Sopenharmony_ciassert(Target3 === seen_target);
115425bb815Sopenharmony_ciassert(JSON.stringify(seen_arguments) === '["a","b"]');
116425bb815Sopenharmony_ciassert(proxy === seen_new_target);
117425bb815Sopenharmony_ciassert('a' === instance.arg1);
118425bb815Sopenharmony_ciassert('b' === instance.arg2);
119425bb815Sopenharmony_ci
120425bb815Sopenharmony_civar instance2 = Reflect.construct(proxy, ['a1', 'b1'], Array);
121425bb815Sopenharmony_ciassert(Target3 === seen_target);
122425bb815Sopenharmony_ciassert(JSON.stringify(seen_arguments) === '["a1","b1"]');
123425bb815Sopenharmony_ciassert(Array === seen_new_target);
124425bb815Sopenharmony_ciassert('a1'=== instance2.arg1);
125425bb815Sopenharmony_ciassert('b1' === instance2.arg2);
126425bb815Sopenharmony_ci
127425bb815Sopenharmony_civar p = new Proxy(function() {}, {
128425bb815Sopenharmony_ci  construct: function(target, argumentsList, newTarget) {
129425bb815Sopenharmony_ci    throw 42;
130425bb815Sopenharmony_ci  }
131425bb815Sopenharmony_ci});
132425bb815Sopenharmony_ci
133425bb815Sopenharmony_citry {
134425bb815Sopenharmony_ci  new p();
135425bb815Sopenharmony_ci  assert(false);
136425bb815Sopenharmony_ci} catch (e) {
137425bb815Sopenharmony_ci  assert(e === 42);
138425bb815Sopenharmony_ci}
139425bb815Sopenharmony_ci
140425bb815Sopenharmony_ci// test when invariants gets violated
141425bb815Sopenharmony_civar p = new Proxy(function() {}, {
142425bb815Sopenharmony_ci  construct: function(target, argumentsList, newTarget) {
143425bb815Sopenharmony_ci    return 1;
144425bb815Sopenharmony_ci  }
145425bb815Sopenharmony_ci});
146425bb815Sopenharmony_ci
147425bb815Sopenharmony_citry {
148425bb815Sopenharmony_ci  new p();
149425bb815Sopenharmony_ci  assert(false);
150425bb815Sopenharmony_ci} catch (e) {
151425bb815Sopenharmony_ci  assert(e instanceof TypeError);
152425bb815Sopenharmony_ci}
153425bb815Sopenharmony_ci
154425bb815Sopenharmony_civar p = new Proxy({}, {
155425bb815Sopenharmony_ci  construct: function(target, argumentsList, newTarget) {
156425bb815Sopenharmony_ci    return {};
157425bb815Sopenharmony_ci  }
158425bb815Sopenharmony_ci});
159425bb815Sopenharmony_ci
160425bb815Sopenharmony_citry {
161425bb815Sopenharmony_ci  new p();
162425bb815Sopenharmony_ci  assert(false);
163425bb815Sopenharmony_ci} catch (e) {
164425bb815Sopenharmony_ci  assert(e instanceof TypeError);
165425bb815Sopenharmony_ci}
166