OLD | NEW |
1 // Flags: --harmony-proxies | 1 // Flags: --harmony-proxies |
2 | 2 |
3 // Copyright 2008 the V8 project authors. All rights reserved. | 3 // Copyright 2008 the V8 project authors. All rights reserved. |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 17 matching lines...) Expand all Loading... |
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | 29 |
30 | 30 |
31 | 31 |
32 // Getters. | 32 // Getters. |
33 | 33 |
34 function TestGet(handler) { | 34 function TestGet(handler) { |
35 var o = Proxy.create(handler) | 35 var o = Proxy.create(handler) |
36 assertEquals(42, o.a) | 36 assertEquals(42, o.a) |
37 assertEquals(42, o["b"]) | 37 assertEquals(42, o["b"]) |
38 // assertEquals(Object.getOwnPropertyDescriptor(o, "b").value, 42) | |
39 } | 38 } |
40 | 39 |
41 TestGet({ | 40 TestGet({ |
42 get: function(r, k) { return 42 } | 41 get: function(r, k) { return 42 } |
43 }) | 42 }) |
44 TestGet({ | 43 TestGet({ |
45 get: function(r, k) { return this.get2(r, k) }, | 44 get: function(r, k) { return this.get2(r, k) }, |
46 get2: function(r, k) { return 42 } | 45 get2: function(r, k) { return 42 } |
47 }) | 46 }) |
48 TestGet({ | 47 TestGet({ |
(...skipping 13 matching lines...) Expand all Loading... |
62 getPropertyDescriptor: function(k) { return {value: 42} } | 61 getPropertyDescriptor: function(k) { return {value: 42} } |
63 }) | 62 }) |
64 | 63 |
65 TestGet(Proxy.create({ | 64 TestGet(Proxy.create({ |
66 get: function(pr, pk) { | 65 get: function(pr, pk) { |
67 return function(r, k) { return 42 } | 66 return function(r, k) { return 42 } |
68 } | 67 } |
69 })) | 68 })) |
70 | 69 |
71 | 70 |
| 71 function TestGetCall(handler) { |
| 72 var p = Proxy.create(handler) |
| 73 assertEquals(55, p.f()) |
| 74 assertEquals(55, p.f("unused", "arguments")) |
| 75 assertEquals(55, p.f.call(p)) |
| 76 assertEquals(55, p.withargs(45, 5)) |
| 77 assertEquals(55, p.withargs.call(p, 11, 22)) |
| 78 assertEquals("6655", "66" + p) // calls p.toString |
| 79 } |
| 80 |
| 81 TestGetCall({ |
| 82 get: function(r, k) { return function() { return 55 } } |
| 83 }) |
| 84 TestGetCall({ |
| 85 get: function(r, k) { return this.get2(r, k) }, |
| 86 get2: function(r, k) { return function() { return 55 } } |
| 87 }) |
| 88 TestGetCall({ |
| 89 getPropertyDescriptor: function(k) { |
| 90 return {value: function() { return 55 }} |
| 91 } |
| 92 }) |
| 93 TestGetCall({ |
| 94 getPropertyDescriptor: function(k) { return this.getPropertyDescriptor2(k) }, |
| 95 getPropertyDescriptor2: function(k) { |
| 96 return {value: function() { return 55 }} |
| 97 } |
| 98 }) |
| 99 TestGetCall({ |
| 100 getPropertyDescriptor: function(k) { |
| 101 return {get value() { return function() { return 55 } }} |
| 102 } |
| 103 }) |
| 104 TestGetCall({ |
| 105 get: undefined, |
| 106 getPropertyDescriptor: function(k) { |
| 107 return {value: function() { return 55 }} |
| 108 } |
| 109 }) |
| 110 TestGetCall({ |
| 111 get: function(r, k) { |
| 112 if (k == "gg") { |
| 113 return function() { return 55 } |
| 114 } else if (k == "withargs") { |
| 115 return function(n, m) { return n + m * 2 } |
| 116 } else { |
| 117 return function() { return this.gg() } |
| 118 } |
| 119 } |
| 120 }) |
| 121 |
| 122 TestGetCall(Proxy.create({ |
| 123 get: function(pr, pk) { |
| 124 return function(r, k) { return function() { return 55 } } |
| 125 } |
| 126 })) |
| 127 |
| 128 |
72 | 129 |
73 // Setters. | 130 // Setters. |
74 | 131 |
75 var key | 132 var key |
76 var val | 133 var val |
77 function TestSet(handler) { | 134 function TestSet(handler) { |
78 var o = Proxy.create(handler) | 135 var o = Proxy.create(handler) |
79 assertEquals(42, o.a = 42) | 136 assertEquals(42, o.a = 42) |
80 assertEquals("a", key) | 137 assertEquals("a", key) |
81 assertEquals(42, val) | 138 assertEquals(42, val) |
82 assertEquals(43, o["b"] = 43) | 139 assertEquals(43, o["b"] = 43) |
83 assertEquals("b", key) | 140 assertEquals("b", key) |
84 assertEquals(43, val) | 141 assertEquals(43, val) |
85 // assertTrue(Object.defineProperty(o, "c", {value: 44})) | |
86 // assertEquals("c", key) | |
87 // assertEquals(44, val) | |
88 } | 142 } |
89 | 143 |
90 TestSet({ | 144 TestSet({ |
91 set: function(r, k, v) { key = k; val = v; return true } | 145 set: function(r, k, v) { key = k; val = v; return true } |
92 }) | 146 }) |
93 TestSet({ | 147 TestSet({ |
94 set: function(r, k, v) { return this.set2(r, k, v) }, | 148 set: function(r, k, v) { return this.set2(r, k, v) }, |
95 set2: function(r, k, v) { key = k; val = v; return true } | 149 set2: function(r, k, v) { key = k; val = v; return true } |
96 }) | 150 }) |
97 TestSet({ | 151 TestSet({ |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 }) | 196 }) |
143 | 197 |
144 TestSet(Proxy.create({ | 198 TestSet(Proxy.create({ |
145 get: function(pr, pk) { | 199 get: function(pr, pk) { |
146 return function(r, k, v) { key = k; val = v; return true } | 200 return function(r, k, v) { key = k; val = v; return true } |
147 } | 201 } |
148 })) | 202 })) |
149 | 203 |
150 | 204 |
151 | 205 |
152 // Property definition (Object.defineProperty). | 206 // Property definition (Object.defineProperty and Object.defineProperties). |
153 | 207 |
154 var key | 208 var key |
155 var desc | 209 var desc |
156 function TestDefine(handler) { | 210 function TestDefine(handler) { |
157 var o = Proxy.create(handler) | 211 var o = Proxy.create(handler) |
158 assertEquals(o, Object.defineProperty(o, "a", {value: 44})) | 212 assertEquals(o, Object.defineProperty(o, "a", {value: 44})) |
159 assertEquals("a", key) | 213 assertEquals("a", key) |
160 assertEquals(1, Object.getOwnPropertyNames(desc).length) | 214 assertEquals(1, Object.getOwnPropertyNames(desc).length) |
161 assertEquals(44, desc.value) | 215 assertEquals(44, desc.value) |
162 | 216 |
(...skipping 23 matching lines...) Expand all Loading... |
186 | 240 |
187 assertEquals(o, Object.defineProperty(o, "e", {get: function(){ return 5 }})) | 241 assertEquals(o, Object.defineProperty(o, "e", {get: function(){ return 5 }})) |
188 assertEquals("e", key) | 242 assertEquals("e", key) |
189 assertEquals(1, Object.getOwnPropertyNames(desc).length) | 243 assertEquals(1, Object.getOwnPropertyNames(desc).length) |
190 assertEquals(5, desc.get()) | 244 assertEquals(5, desc.get()) |
191 | 245 |
192 assertEquals(o, Object.defineProperty(o, "zzz", {})) | 246 assertEquals(o, Object.defineProperty(o, "zzz", {})) |
193 assertEquals("zzz", key) | 247 assertEquals("zzz", key) |
194 assertEquals(0, Object.getOwnPropertyNames(desc).length) | 248 assertEquals(0, Object.getOwnPropertyNames(desc).length) |
195 | 249 |
196 // This test requires [s in proxy] to be implemented first. | 250 // TODO(rossberg): This test requires [s in proxy] to be implemented first. |
197 // var d = Proxy.create({ | 251 // var d = Proxy.create({ |
198 // get: function(r, k) { return (k === "value") ? 77 : void 0 }, | 252 // get: function(r, k) { return (k === "value") ? 77 : void 0 }, |
199 // getOwnPropertyNames: function() { return ["value"] } | 253 // getOwnPropertyNames: function() { return ["value"] } |
200 // }) | 254 // }) |
201 // assertEquals(1, Object.getOwnPropertyNames(d).length) | 255 // assertEquals(1, Object.getOwnPropertyNames(d).length) |
202 // assertEquals(77, d.value) | 256 // assertEquals(77, d.value) |
203 // assertEquals(o, Object.defineProperty(o, "p", d)) | 257 // assertEquals(o, Object.defineProperty(o, "p", d)) |
204 // assertEquals("p", key) | 258 // assertEquals("p", key) |
205 // assertEquals(1, Object.getOwnPropertyNames(desc).length) | 259 // assertEquals(1, Object.getOwnPropertyNames(desc).length) |
206 // assertEquals(77, desc.value) | 260 // assertEquals(77, desc.value) |
| 261 |
| 262 var props = { |
| 263 'bla': {}, |
| 264 blub: {get: function() { return true }}, |
| 265 '': {get value() { return 20 }}, |
| 266 last: {value: 21, configurable: true, mine: "eyes"} |
| 267 } |
| 268 Object.defineProperty(props, "hidden", {value: "hidden", enumerable: false}) |
| 269 assertEquals(o, Object.defineProperties(o, props)) |
| 270 assertEquals("last", key) |
| 271 assertEquals(2, Object.getOwnPropertyNames(desc).length) |
| 272 assertEquals(21, desc.value) |
| 273 assertEquals(true, desc.configurable) |
| 274 assertEquals(undefined, desc.mine) // Arguably a bug in the spec... |
207 } | 275 } |
208 | 276 |
209 TestDefine({ | 277 TestDefine({ |
210 defineProperty: function(k, d) { key = k; desc = d; return true } | 278 defineProperty: function(k, d) { key = k; desc = d; return true } |
211 }) | 279 }) |
212 TestDefine({ | 280 TestDefine({ |
213 defineProperty: function(k, d) { return this.defineProperty2(k, d) }, | 281 defineProperty: function(k, d) { return this.defineProperty2(k, d) }, |
214 defineProperty2: function(k, d) { key = k; desc = d; return true } | 282 defineProperty2: function(k, d) { key = k; desc = d; return true } |
215 }) | 283 }) |
216 TestDefine(Proxy.create({ | 284 TestDefine(Proxy.create({ |
217 get: function(pr, pk) { | 285 get: function(pr, pk) { |
218 return function(k, d) { key = k; desc = d; return true } | 286 return function(k, d) { key = k; desc = d; return true } |
219 } | 287 } |
220 })) | 288 })) |
221 | 289 |
222 | 290 |
223 | 291 |
| 292 // Property descriptors (Object.getOwnPropertyDescriptor). |
| 293 |
| 294 function TestDescriptor(handler) { |
| 295 var o = Proxy.create(handler) |
| 296 var descs = [ |
| 297 {configurable: true}, |
| 298 {value: 34, enumerable: true, configurable: true}, |
| 299 {value: 3, writable: false, mine: "eyes", configurable: true}, |
| 300 {get value() { return 20 }, get configurable() { return true }}, |
| 301 {get: function() { "get" }, set: function() { "set" }, configurable: true} |
| 302 ] |
| 303 for (var i = 0; i < descs.length; ++i) { |
| 304 assertEquals(o, Object.defineProperty(o, i, descs[i])) |
| 305 var desc = Object.getOwnPropertyDescriptor(o, i) |
| 306 for (p in descs[i]) { |
| 307 // TODO(rossberg): Ignore user attributes as long as the spec isn't |
| 308 // fixed suitably. |
| 309 if (p != "mine") assertEquals(descs[i][p], desc[p]) |
| 310 } |
| 311 assertEquals(undefined, Object.getOwnPropertyDescriptor(o, "absent")) |
| 312 } |
| 313 } |
| 314 |
| 315 |
| 316 TestDescriptor({ |
| 317 defineProperty: function(k, d) { this["__" + k] = d; return true }, |
| 318 getOwnPropertyDescriptor: function(k) { return this["__" + k] } |
| 319 }) |
| 320 TestDescriptor({ |
| 321 defineProperty: function(k, d) { this["__" + k] = d; return true }, |
| 322 getOwnPropertyDescriptor: function(k) { |
| 323 return this.getOwnPropertyDescriptor2(k) |
| 324 }, |
| 325 getOwnPropertyDescriptor2: function(k) { return this["__" + k] } |
| 326 }) |
| 327 |
| 328 |
| 329 |
224 // Comparison. | 330 // Comparison. |
225 | 331 |
226 function TestComparison(eq) { | 332 function TestComparison(eq) { |
227 var o1 = Proxy.create({}) | 333 var o1 = Proxy.create({}) |
228 var o2 = Proxy.create({}) | 334 var o2 = Proxy.create({}) |
229 | 335 |
230 assertTrue(eq(o1, o1)) | 336 assertTrue(eq(o1, o1)) |
231 assertTrue(eq(o2, o2)) | 337 assertTrue(eq(o2, o2)) |
232 assertTrue(!eq(o1, o2)) | 338 assertTrue(!eq(o1, o2)) |
233 assertTrue(!eq(o1, {})) | 339 assertTrue(!eq(o1, {})) |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 assertSame(Object.getPrototypeOf(p1), null) | 408 assertSame(Object.getPrototypeOf(p1), null) |
303 assertSame(Object.getPrototypeOf(p2), o) | 409 assertSame(Object.getPrototypeOf(p2), o) |
304 assertSame(Object.getPrototypeOf(p3), p2) | 410 assertSame(Object.getPrototypeOf(p3), p2) |
305 assertSame(Object.getPrototypeOf(p4), null) | 411 assertSame(Object.getPrototypeOf(p4), null) |
306 } | 412 } |
307 | 413 |
308 TestPrototype() | 414 TestPrototype() |
309 | 415 |
310 | 416 |
311 | 417 |
312 // Property names (Object.getOwnPropertyNames). | 418 // Property names (Object.getOwnPropertyNames, Object.keys). |
313 | 419 |
314 function TestPropertyNames(names, handler) { | 420 function TestPropertyNames(names, handler) { |
315 var p = Proxy.create(handler) | 421 var p = Proxy.create(handler) |
316 assertArrayEquals(names, Object.getOwnPropertyNames(p)) | 422 assertArrayEquals(names, Object.getOwnPropertyNames(p)) |
317 } | 423 } |
318 | 424 |
319 TestPropertyNames([], { | 425 TestPropertyNames([], { |
320 getOwnPropertyNames: function() { return [] } | 426 getOwnPropertyNames: function() { return [] } |
321 }) | 427 }) |
322 TestPropertyNames(["a", "zz", " ", "0"], { | 428 TestPropertyNames(["a", "zz", " ", "0"], { |
323 getOwnPropertyNames: function() { return ["a", "zz", " ", 0] } | 429 getOwnPropertyNames: function() { return ["a", "zz", " ", 0] } |
324 }) | 430 }) |
325 TestPropertyNames(["throw", "function "], { | 431 TestPropertyNames(["throw", "function "], { |
326 getOwnPropertyNames: function() { return this.getOwnPropertyNames2() }, | 432 getOwnPropertyNames: function() { return this.getOwnPropertyNames2() }, |
327 getOwnPropertyNames2: function() { return ["throw", "function "] } | 433 getOwnPropertyNames2: function() { return ["throw", "function "] } |
328 }) | 434 }) |
329 TestPropertyNames(["[object Object]"], { | 435 TestPropertyNames(["[object Object]"], { |
330 get getOwnPropertyNames() { | 436 get getOwnPropertyNames() { |
331 return function() { return [{}] } | 437 return function() { return [{}] } |
332 } | 438 } |
333 }) | 439 }) |
| 440 |
| 441 |
| 442 function TestKeys(names, handler) { |
| 443 var p = Proxy.create(handler) |
| 444 assertArrayEquals(names, Object.keys(p)) |
| 445 } |
| 446 |
| 447 TestKeys([], { |
| 448 keys: function() { return [] } |
| 449 }) |
| 450 TestKeys(["a", "zz", " ", "0"], { |
| 451 keys: function() { return ["a", "zz", " ", 0] } |
| 452 }) |
| 453 TestKeys(["throw", "function "], { |
| 454 keys: function() { return this.keys2() }, |
| 455 keys2: function() { return ["throw", "function "] } |
| 456 }) |
| 457 TestKeys(["[object Object]"], { |
| 458 get keys() { |
| 459 return function() { return [{}] } |
| 460 } |
| 461 }) |
| 462 TestKeys(["a", "0"], { |
| 463 getOwnPropertyNames: function() { return ["a", 23, "zz", "", 0] }, |
| 464 getOwnPropertyDescriptor: function(k) { return {enumerable: k.length == 1} } |
| 465 }) |
| 466 TestKeys(["23", "zz", ""], { |
| 467 getOwnPropertyNames: function() { return this.getOwnPropertyNames2() }, |
| 468 getOwnPropertyNames2: function() { return ["a", 23, "zz", "", 0] }, |
| 469 getOwnPropertyDescriptor: function(k) { |
| 470 return this.getOwnPropertyDescriptor2(k) |
| 471 }, |
| 472 getOwnPropertyDescriptor2: function(k) { return {enumerable: k.length != 1} } |
| 473 }) |
| 474 TestKeys(["a", "b", "c", "5"], { |
| 475 get getOwnPropertyNames() { |
| 476 return function() { return ["0", 4, "a", "b", "c", 5] } |
| 477 }, |
| 478 get getOwnPropertyDescriptor() { |
| 479 return function(k) { return {enumerable: k >= "44"} } |
| 480 } |
| 481 }) |
| 482 TestKeys([], { |
| 483 get getOwnPropertyNames() { |
| 484 return function() { return ["a", "b", "c"] } |
| 485 }, |
| 486 getOwnPropertyDescriptor: function(k) { return {} } |
| 487 }) |
OLD | NEW |