Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | // Flags: --harmony-proxies |
| 29 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 30 | // Helper. |
| 31 | |
| 32 | function TestWithProxies(test, x, y, z) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 33 | test(function(h){ return new Proxy({}, h) }, x, y, z) |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | |
| 37 | // Iterate over a proxy. |
| 38 | |
| 39 | function TestForIn(properties, handler) { |
| 40 | TestWithProxies(TestForIn2, properties, handler) |
| 41 | } |
| 42 | |
| 43 | function TestForIn2(create, properties, handler) { |
| 44 | var p = create(handler) |
| 45 | var found = [] |
| 46 | for (var x in p) found.push(x) |
| 47 | assertArrayEquals(properties, found) |
| 48 | } |
| 49 | |
| 50 | TestForIn(["0", "a"], { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 51 | ownKeys() { return ["0", "a"] }, |
| 52 | has(target, property) { return true }, |
| 53 | getOwnPropertyDescriptor() { return { enumerable: true, configurable: true }} |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 54 | }) |
| 55 | |
| 56 | TestForIn(["null", "a"], { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 57 | ownKeys() { return this.enumerate() }, |
| 58 | enumerate() { return ["null", "a"] }, |
| 59 | has(target, property) { return true }, |
| 60 | getOwnPropertyDescriptor() { return { enumerable: true, configurable: true }} |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 61 | }) |
| 62 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 63 | |
| 64 | // Iterate over an object with a proxy prototype. |
| 65 | |
| 66 | function TestForInDerived(properties, handler) { |
| 67 | TestWithProxies(TestForInDerived2, properties, handler) |
| 68 | } |
| 69 | |
| 70 | function TestForInDerived2(create, properties, handler) { |
| 71 | var p = create(handler) |
| 72 | var o = Object.create(p) |
| 73 | o.z = 0 |
| 74 | var found = [] |
| 75 | for (var x in o) found.push(x) |
| 76 | assertArrayEquals(["z"].concat(properties), found) |
| 77 | |
| 78 | var oo = Object.create(o) |
| 79 | oo.y = 0 |
| 80 | var found = [] |
| 81 | for (var x in oo) found.push(x) |
| 82 | assertArrayEquals(["y", "z"].concat(properties), found) |
| 83 | } |
| 84 | |
| 85 | TestForInDerived(["0", "a"], { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 86 | ownKeys: function() { return ["0", "a"] }, |
| 87 | has: function(t, k) { return k == "0" || k == "a" }, |
| 88 | getOwnPropertyDescriptor() { return { enumerable: true, configurable: true }} |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 89 | }) |
| 90 | |
| 91 | TestForInDerived(["null", "a"], { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 92 | ownKeys: function() { return this.enumerate() }, |
| 93 | enumerate: function() { return ["null", "a"] }, |
| 94 | has: function(t, k) { return k == "null" || k == "a" }, |
| 95 | getOwnPropertyDescriptor() { return { enumerable: true, configurable: true }} |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 96 | }) |
| 97 | |
| 98 | |
| 99 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 100 | // Throw exception in ownKeys trap. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 101 | |
| 102 | function TestForInThrow(handler) { |
| 103 | TestWithProxies(TestForInThrow2, handler) |
| 104 | } |
| 105 | |
| 106 | function TestForInThrow2(create, handler) { |
| 107 | var p = create(handler) |
| 108 | var o = Object.create(p) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 109 | assertThrowsEquals(function(){ for (var x in p) {} }, "myexn") |
| 110 | assertThrowsEquals(function(){ for (var x in o) {} }, "myexn") |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | TestForInThrow({ |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 114 | ownKeys: function() { throw "myexn" } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 115 | }) |
| 116 | |
| 117 | TestForInThrow({ |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 118 | ownKeys: function() { return this.enumerate() }, |
| 119 | enumerate: function() { throw "myexn" } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 120 | }) |
| 121 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 122 | TestForInThrow(new Proxy({}, { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 123 | get: function(pr, pk) { |
| 124 | return function() { throw "myexn" } |
| 125 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 126 | })); |
| 127 | |
| 128 | (function() { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 129 | var p = new Proxy({}, {ownKeys:function() { return ["0"]; }}); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 130 | var o = [0]; |
| 131 | o.__proto__ = p; |
| 132 | var keys = []; |
| 133 | for (var k in o) { keys.push(k); }; |
| 134 | assertEquals(["0"], keys); |
| 135 | })(); |
| 136 | |
| 137 | (function () { |
| 138 | var p = new Proxy({}, {ownKeys: function() { return ["1", Symbol(), "2"] }}); |
| 139 | assertEquals(["1","2"], Object.getOwnPropertyNames(p)); |
| 140 | })(); |