Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5 | // Flags: --allow-natives-syntax |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6 | |
| 7 | function CreateConstructableProxy(handler) { |
| 8 | return new Proxy(function(){}, handler); |
| 9 | } |
| 10 | |
| 11 | (function() { |
| 12 | var prototype = { x: 1 }; |
| 13 | var log = []; |
| 14 | |
| 15 | var proxy = CreateConstructableProxy({ |
| 16 | get(k) { |
| 17 | log.push("get trap"); |
| 18 | return prototype; |
| 19 | }}); |
| 20 | |
| 21 | var o = Reflect.construct(Number, [100], proxy); |
| 22 | assertEquals(["get trap"], log); |
| 23 | assertTrue(Object.getPrototypeOf(o) === prototype); |
| 24 | assertEquals(100, Number.prototype.valueOf.call(o)); |
| 25 | })(); |
| 26 | |
| 27 | (function() { |
| 28 | var prototype = { x: 1 }; |
| 29 | var log = []; |
| 30 | |
| 31 | var proxy = CreateConstructableProxy({ |
| 32 | get(k) { |
| 33 | log.push("get trap"); |
| 34 | return 10; |
| 35 | }}); |
| 36 | |
| 37 | var o = Reflect.construct(Number, [100], proxy); |
| 38 | assertEquals(["get trap"], log); |
| 39 | assertTrue(Object.getPrototypeOf(o) === Number.prototype); |
| 40 | assertEquals(100, Number.prototype.valueOf.call(o)); |
| 41 | })(); |
| 42 | |
| 43 | (function() { |
| 44 | var prototype = { x: 1 }; |
| 45 | var log = []; |
| 46 | |
| 47 | var proxy = CreateConstructableProxy({ |
| 48 | get(k) { |
| 49 | log.push("get trap"); |
| 50 | return prototype; |
| 51 | }}); |
| 52 | |
| 53 | var o = Reflect.construct(Function, ["return 1000"], proxy); |
| 54 | assertEquals(["get trap"], log); |
| 55 | assertTrue(Object.getPrototypeOf(o) === prototype); |
| 56 | assertEquals(1000, o()); |
| 57 | })(); |
| 58 | |
| 59 | (function() { |
| 60 | var prototype = { x: 1 }; |
| 61 | var log = []; |
| 62 | |
| 63 | var proxy = CreateConstructableProxy({ |
| 64 | get(k) { |
| 65 | log.push("get trap"); |
| 66 | return prototype; |
| 67 | }}); |
| 68 | |
| 69 | var o = Reflect.construct(Array, [1, 2, 3], proxy); |
| 70 | assertEquals(["get trap"], log); |
| 71 | assertTrue(Object.getPrototypeOf(o) === prototype); |
| 72 | assertEquals([1, 2, 3], o); |
| 73 | })(); |