Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 ownKeys: function() { return this.enumerate() }, | 118 ownKeys: function() { return this.enumerate() }, |
| 119 enumerate: function() { throw "myexn" } | 119 enumerate: function() { throw "myexn" } |
| 120 }) | 120 }) |
| 121 | 121 |
| 122 TestForInThrow(new Proxy({}, { | 122 TestForInThrow(new Proxy({}, { |
| 123 get: function(pr, pk) { | 123 get: function(pr, pk) { |
| 124 return function() { throw "myexn" } | 124 return function() { throw "myexn" } |
| 125 } | 125 } |
| 126 })); | 126 })); |
| 127 | 127 |
| 128 (function() { | 128 |
| 129 var p = new Proxy({}, {ownKeys:function() { return ["0"]; }}); | 129 function keys(object) { |
| 130 var keys = []; | |
| 131 for (var k in object) { | |
| 132 keys.push(k); | |
| 133 } | |
| 134 return keys; | |
| 135 } | |
| 136 | |
| 137 (function testKeysProxyOnProtoEmpty() { | |
| 138 var p = new Proxy({}, { | |
| 139 ownKeys() { return []; }, | |
|
Jakob Kummerow
2016/03/18 14:12:00
whaaa, fancy ES6 syntax eh?
Camillo Bruni
2016/03/18 17:46:34
if we suffered so much implementing we better make
| |
| 140 }); | |
| 130 var o = [0]; | 141 var o = [0]; |
| 131 o.__proto__ = p; | 142 o.__proto__ = p; |
| 132 var keys = []; | 143 assertEquals(["0"], keys(o)); |
| 133 for (var k in o) { keys.push(k); }; | 144 |
| 134 assertEquals(["0"], keys); | 145 delete o[0]; |
| 146 assertEquals([], keys(o)); | |
| 135 })(); | 147 })(); |
| 136 | 148 |
| 149 (function testKeysProxyOnProto() { | |
| 150 var handler = {ownKeys() { return ["0"]; }}; | |
| 151 var proxy = new Proxy({}, handler); | |
| 152 var object = [0]; | |
| 153 object.__proto__ = proxy; | |
| 154 assertEquals(["0"], keys(object)); | |
| 155 | |
| 156 // The Proxy doesn't set his ownKeys enumerable. | |
| 157 delete object[0]; | |
| 158 assertEquals([], keys(object)); | |
| 159 | |
| 160 // The [[Has]] trap has no influence on which are enumerable properties are | |
|
Jakob Kummerow
2016/03/18 14:12:00
nit: s/which are/which/
| |
| 161 // shown in for-in. | |
| 162 handler.has = function() { return true }; | |
| 163 assertEquals([], keys(object)); | |
| 164 | |
| 165 handler.getOwnPropertyDescriptor = function() { | |
| 166 return {enumerable: true, configurable: true} | |
| 167 } | |
| 168 assertEquals(["0"], keys(object)); | |
| 169 })(); | |
| 170 | |
| 171 (function testKeysProxyProto() { | |
| 172 var target = {t1:true, t2:true}; | |
| 173 var handler = {}; | |
| 174 var proxy = new Proxy(target, handler); | |
| 175 | |
| 176 assertEquals(["t1", "t2"], keys(proxy)); | |
| 177 | |
| 178 target.__proto__ = {p1:true, p2:true}; | |
| 179 assertEquals(["t1", "t2", "p1", "p2"], keys(proxy)); | |
| 180 | |
| 181 handler.getPrototypeOf = function(target) { | |
| 182 return {p3:true, p4:true}; | |
| 183 }; | |
| 184 // for-in walks the prototype chain for the [[Has]] / Enumerable check. | |
| 185 assertEquals(["t1", "t2", "p3", "p4"], keys(proxy)); | |
| 186 | |
| 187 // [[Has]] is not used in for-in. | |
| 188 handler.has = function() { return false }; | |
| 189 assertEquals(["t1", "t2", "p3", "p4"], keys(proxy)); | |
| 190 | |
| 191 // Proxy intercepts enumerability check. | |
| 192 handler.getOwnPropertyDescriptor = function() { | |
| 193 return {enumerable: false, configurable: true} | |
| 194 } | |
| 195 assertEquals([], keys(proxy)); | |
| 196 | |
| 197 handler.getOwnPropertyDescriptor = function() { | |
| 198 return {enumerable: true, configurable: true} | |
| 199 } | |
| 200 assertEquals(["t1", "t2", "p3", "p4"], keys(proxy)); | |
| 201 | |
| 202 handler.getOwnPropertyDescriptor = function(target, key) { | |
| 203 return { | |
| 204 enumerable: key in target, | |
| 205 configurable: true | |
| 206 } | |
| 207 } | |
| 208 assertEquals(["t1", "t2"], keys(proxy)); | |
| 209 | |
| 210 handler.getPrototypeOf = function() { throw "error" }; | |
| 211 assertThrowsEquals(() => {keys(proxy)}, "error"); | |
| 212 })(); | |
| 213 | |
| 214 | |
| 137 (function () { | 215 (function () { |
| 138 var p = new Proxy({}, {ownKeys: function() { return ["1", Symbol(), "2"] }}); | 216 var symbol = Symbol(); |
| 217 var p = new Proxy({}, {ownKeys() { return ["1", symbol, "2"] }}); | |
| 139 assertEquals(["1","2"], Object.getOwnPropertyNames(p)); | 218 assertEquals(["1","2"], Object.getOwnPropertyNames(p)); |
| 219 assertEquals([symbol], Object.getOwnPropertySymbols(p)); | |
| 140 })(); | 220 })(); |
| OLD | NEW |