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 24 matching lines...) Expand all Loading... |
35 | 35 |
36 function TestGet(handler) { | 36 function TestGet(handler) { |
37 var o = Proxy.create(handler) | 37 var o = Proxy.create(handler) |
38 assertEquals(42, o.a) | 38 assertEquals(42, o.a) |
39 assertEquals(42, o["b"]) | 39 assertEquals(42, o["b"]) |
40 } | 40 } |
41 | 41 |
42 TestGet({ | 42 TestGet({ |
43 get: function(r, k) { return 42 } | 43 get: function(r, k) { return 42 } |
44 }) | 44 }) |
| 45 |
45 TestGet({ | 46 TestGet({ |
46 get: function(r, k) { return this.get2(r, k) }, | 47 get: function(r, k) { return this.get2(r, k) }, |
47 get2: function(r, k) { return 42 } | 48 get2: function(r, k) { return 42 } |
48 }) | 49 }) |
| 50 |
49 TestGet({ | 51 TestGet({ |
50 getPropertyDescriptor: function(k) { return {value: 42} } | 52 getPropertyDescriptor: function(k) { return {value: 42} } |
51 }) | 53 }) |
| 54 |
52 TestGet({ | 55 TestGet({ |
53 getPropertyDescriptor: function(k) { return this.getPropertyDescriptor2(k) }, | 56 getPropertyDescriptor: function(k) { return this.getPropertyDescriptor2(k) }, |
54 getPropertyDescriptor2: function(k) { return {value: 42} } | 57 getPropertyDescriptor2: function(k) { return {value: 42} } |
55 }) | 58 }) |
| 59 |
56 TestGet({ | 60 TestGet({ |
57 getPropertyDescriptor: function(k) { | 61 getPropertyDescriptor: function(k) { |
58 return {get value() { return 42 }} | 62 return {get value() { return 42 }} |
59 } | 63 } |
60 }) | 64 }) |
| 65 |
61 TestGet({ | 66 TestGet({ |
62 get: undefined, | 67 get: undefined, |
63 getPropertyDescriptor: function(k) { return {value: 42} } | 68 getPropertyDescriptor: function(k) { return {value: 42} } |
64 }) | 69 }) |
65 | 70 |
66 TestGet(Proxy.create({ | 71 TestGet(Proxy.create({ |
67 get: function(pr, pk) { | 72 get: function(pr, pk) { |
68 return function(r, k) { return 42 } | 73 return function(r, k) { return 42 } |
69 } | 74 } |
70 })) | 75 })) |
71 | 76 |
72 | 77 |
73 function TestGetCall(handler) { | 78 function TestGetCall(handler) { |
74 var p = Proxy.create(handler) | 79 var p = Proxy.create(handler) |
75 assertEquals(55, p.f()) | 80 assertEquals(55, p.f()) |
76 assertEquals(55, p.f("unused", "arguments")) | 81 assertEquals(55, p.f("unused", "arguments")) |
77 assertEquals(55, p.f.call(p)) | 82 assertEquals(55, p.f.call(p)) |
78 assertEquals(55, p.withargs(45, 5)) | 83 assertEquals(55, p.withargs(45, 5)) |
79 assertEquals(55, p.withargs.call(p, 11, 22)) | 84 assertEquals(55, p.withargs.call(p, 11, 22)) |
80 assertEquals("6655", "66" + p) // calls p.toString | 85 assertEquals("6655", "66" + p) // calls p.toString |
81 } | 86 } |
82 | 87 |
83 TestGetCall({ | 88 TestGetCall({ |
84 get: function(r, k) { return function() { return 55 } } | 89 get: function(r, k) { return function() { return 55 } } |
85 }) | 90 }) |
| 91 |
86 TestGetCall({ | 92 TestGetCall({ |
87 get: function(r, k) { return this.get2(r, k) }, | 93 get: function(r, k) { return this.get2(r, k) }, |
88 get2: function(r, k) { return function() { return 55 } } | 94 get2: function(r, k) { return function() { return 55 } } |
89 }) | 95 }) |
| 96 |
90 TestGetCall({ | 97 TestGetCall({ |
91 getPropertyDescriptor: function(k) { | 98 getPropertyDescriptor: function(k) { |
92 return {value: function() { return 55 }} | 99 return {value: function() { return 55 }} |
93 } | 100 } |
94 }) | 101 }) |
| 102 |
95 TestGetCall({ | 103 TestGetCall({ |
96 getPropertyDescriptor: function(k) { return this.getPropertyDescriptor2(k) }, | 104 getPropertyDescriptor: function(k) { return this.getPropertyDescriptor2(k) }, |
97 getPropertyDescriptor2: function(k) { | 105 getPropertyDescriptor2: function(k) { |
98 return {value: function() { return 55 }} | 106 return {value: function() { return 55 }} |
99 } | 107 } |
100 }) | 108 }) |
| 109 |
101 TestGetCall({ | 110 TestGetCall({ |
102 getPropertyDescriptor: function(k) { | 111 getPropertyDescriptor: function(k) { |
103 return {get value() { return function() { return 55 } }} | 112 return {get value() { return function() { return 55 } }} |
104 } | 113 } |
105 }) | 114 }) |
| 115 |
106 TestGetCall({ | 116 TestGetCall({ |
107 get: undefined, | 117 get: undefined, |
108 getPropertyDescriptor: function(k) { | 118 getPropertyDescriptor: function(k) { |
109 return {value: function() { return 55 }} | 119 return {value: function() { return 55 }} |
110 } | 120 } |
111 }) | 121 }) |
| 122 |
112 TestGetCall({ | 123 TestGetCall({ |
113 get: function(r, k) { | 124 get: function(r, k) { |
114 if (k == "gg") { | 125 if (k == "gg") { |
115 return function() { return 55 } | 126 return function() { return 55 } |
116 } else if (k == "withargs") { | 127 } else if (k == "withargs") { |
117 return function(n, m) { return n + m * 2 } | 128 return function(n, m) { return n + m * 2 } |
118 } else { | 129 } else { |
119 return function() { return this.gg() } | 130 return function() { return this.gg() } |
120 } | 131 } |
121 } | 132 } |
(...skipping 17 matching lines...) Expand all Loading... |
139 assertEquals("a", key) | 150 assertEquals("a", key) |
140 assertEquals(42, val) | 151 assertEquals(42, val) |
141 assertEquals(43, o["b"] = 43) | 152 assertEquals(43, o["b"] = 43) |
142 assertEquals("b", key) | 153 assertEquals("b", key) |
143 assertEquals(43, val) | 154 assertEquals(43, val) |
144 } | 155 } |
145 | 156 |
146 TestSet({ | 157 TestSet({ |
147 set: function(r, k, v) { key = k; val = v; return true } | 158 set: function(r, k, v) { key = k; val = v; return true } |
148 }) | 159 }) |
| 160 |
149 TestSet({ | 161 TestSet({ |
150 set: function(r, k, v) { return this.set2(r, k, v) }, | 162 set: function(r, k, v) { return this.set2(r, k, v) }, |
151 set2: function(r, k, v) { key = k; val = v; return true } | 163 set2: function(r, k, v) { key = k; val = v; return true } |
152 }) | 164 }) |
| 165 |
153 TestSet({ | 166 TestSet({ |
154 getOwnPropertyDescriptor: function(k) { return {writable: true} }, | 167 getOwnPropertyDescriptor: function(k) { return {writable: true} }, |
155 defineProperty: function(k, desc) { key = k; val = desc.value } | 168 defineProperty: function(k, desc) { key = k; val = desc.value } |
156 }) | 169 }) |
| 170 |
157 TestSet({ | 171 TestSet({ |
158 getOwnPropertyDescriptor: function(k) { | 172 getOwnPropertyDescriptor: function(k) { |
159 return this.getOwnPropertyDescriptor2(k) | 173 return this.getOwnPropertyDescriptor2(k) |
160 }, | 174 }, |
161 getOwnPropertyDescriptor2: function(k) { return {writable: true} }, | 175 getOwnPropertyDescriptor2: function(k) { return {writable: true} }, |
162 defineProperty: function(k, desc) { this.defineProperty2(k, desc) }, | 176 defineProperty: function(k, desc) { this.defineProperty2(k, desc) }, |
163 defineProperty2: function(k, desc) { key = k; val = desc.value } | 177 defineProperty2: function(k, desc) { key = k; val = desc.value } |
164 }) | 178 }) |
| 179 |
165 TestSet({ | 180 TestSet({ |
166 getOwnPropertyDescriptor: function(k) { | 181 getOwnPropertyDescriptor: function(k) { |
167 return {get writable() { return true }} | 182 return {get writable() { return true }} |
168 }, | 183 }, |
169 defineProperty: function(k, desc) { key = k; val = desc.value } | 184 defineProperty: function(k, desc) { key = k; val = desc.value } |
170 }) | 185 }) |
| 186 |
171 TestSet({ | 187 TestSet({ |
172 getOwnPropertyDescriptor: function(k) { | 188 getOwnPropertyDescriptor: function(k) { |
173 return {set: function(v) { key = k; val = v }} | 189 return {set: function(v) { key = k; val = v }} |
174 } | 190 } |
175 }) | 191 }) |
| 192 |
176 TestSet({ | 193 TestSet({ |
177 getOwnPropertyDescriptor: function(k) { return null }, | 194 getOwnPropertyDescriptor: function(k) { return null }, |
178 getPropertyDescriptor: function(k) { return {writable: true} }, | 195 getPropertyDescriptor: function(k) { return {writable: true} }, |
179 defineProperty: function(k, desc) { key = k; val = desc.value } | 196 defineProperty: function(k, desc) { key = k; val = desc.value } |
180 }) | 197 }) |
| 198 |
181 TestSet({ | 199 TestSet({ |
182 getOwnPropertyDescriptor: function(k) { return null }, | 200 getOwnPropertyDescriptor: function(k) { return null }, |
183 getPropertyDescriptor: function(k) { | 201 getPropertyDescriptor: function(k) { |
184 return {get writable() { return true }} | 202 return {get writable() { return true }} |
185 }, | 203 }, |
186 defineProperty: function(k, desc) { key = k; val = desc.value } | 204 defineProperty: function(k, desc) { key = k; val = desc.value } |
187 }) | 205 }) |
| 206 |
188 TestSet({ | 207 TestSet({ |
189 getOwnPropertyDescriptor: function(k) { return null }, | 208 getOwnPropertyDescriptor: function(k) { return null }, |
190 getPropertyDescriptor: function(k) { | 209 getPropertyDescriptor: function(k) { |
191 return {set: function(v) { key = k; val = v }} | 210 return {set: function(v) { key = k; val = v }} |
192 } | 211 } |
193 }) | 212 }) |
| 213 |
194 TestSet({ | 214 TestSet({ |
195 getOwnPropertyDescriptor: function(k) { return null }, | 215 getOwnPropertyDescriptor: function(k) { return null }, |
196 getPropertyDescriptor: function(k) { return null }, | 216 getPropertyDescriptor: function(k) { return null }, |
197 defineProperty: function(k, desc) { key = k, val = desc.value } | 217 defineProperty: function(k, desc) { key = k, val = desc.value } |
198 }) | 218 }) |
199 | 219 |
200 TestSet(Proxy.create({ | 220 TestSet(Proxy.create({ |
201 get: function(pr, pk) { | 221 get: function(pr, pk) { |
202 return function(r, k, v) { key = k; val = v; return true } | 222 return function(r, k, v) { key = k; val = v; return true } |
203 } | 223 } |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 assertEquals("last", key) | 292 assertEquals("last", key) |
273 assertEquals(2, Object.getOwnPropertyNames(desc).length) | 293 assertEquals(2, Object.getOwnPropertyNames(desc).length) |
274 assertEquals(21, desc.value) | 294 assertEquals(21, desc.value) |
275 assertEquals(true, desc.configurable) | 295 assertEquals(true, desc.configurable) |
276 assertEquals(undefined, desc.mine) // Arguably a bug in the spec... | 296 assertEquals(undefined, desc.mine) // Arguably a bug in the spec... |
277 } | 297 } |
278 | 298 |
279 TestDefine({ | 299 TestDefine({ |
280 defineProperty: function(k, d) { key = k; desc = d; return true } | 300 defineProperty: function(k, d) { key = k; desc = d; return true } |
281 }) | 301 }) |
| 302 |
282 TestDefine({ | 303 TestDefine({ |
283 defineProperty: function(k, d) { return this.defineProperty2(k, d) }, | 304 defineProperty: function(k, d) { return this.defineProperty2(k, d) }, |
284 defineProperty2: function(k, d) { key = k; desc = d; return true } | 305 defineProperty2: function(k, d) { key = k; desc = d; return true } |
285 }) | 306 }) |
| 307 |
286 TestDefine(Proxy.create({ | 308 TestDefine(Proxy.create({ |
287 get: function(pr, pk) { | 309 get: function(pr, pk) { |
288 return function(k, d) { key = k; desc = d; return true } | 310 return function(k, d) { key = k; desc = d; return true } |
289 } | 311 } |
290 })) | 312 })) |
291 | 313 |
292 | 314 |
293 | 315 |
294 // Property deletion (delete). | 316 // Property deletion (delete). |
295 | 317 |
(...skipping 20 matching lines...) Expand all Loading... |
316 assertThrows(function() { delete o.z3 }, TypeError) | 338 assertThrows(function() { delete o.z3 }, TypeError) |
317 assertEquals("z3", key) | 339 assertEquals("z3", key) |
318 assertThrows(function() { delete o["z4"] }, TypeError) | 340 assertThrows(function() { delete o["z4"] }, TypeError) |
319 assertEquals("z4", key) | 341 assertEquals("z4", key) |
320 })() | 342 })() |
321 } | 343 } |
322 | 344 |
323 TestDelete({ | 345 TestDelete({ |
324 'delete': function(k) { key = k; return k < "z" } | 346 'delete': function(k) { key = k; return k < "z" } |
325 }) | 347 }) |
| 348 |
326 TestDelete({ | 349 TestDelete({ |
327 'delete': function(k) { return this.delete2(k) }, | 350 'delete': function(k) { return this.delete2(k) }, |
328 delete2: function(k) { key = k; return k < "z" } | 351 delete2: function(k) { key = k; return k < "z" } |
329 }) | 352 }) |
| 353 |
330 TestDelete(Proxy.create({ | 354 TestDelete(Proxy.create({ |
331 get: function(pr, pk) { | 355 get: function(pr, pk) { |
332 return function(k) { key = k; return k < "z" } | 356 return function(k) { key = k; return k < "z" } |
333 } | 357 } |
334 })) | 358 })) |
335 | 359 |
336 | 360 |
337 | 361 |
338 // Property descriptors (Object.getOwnPropertyDescriptor). | 362 // Property descriptors (Object.getOwnPropertyDescriptor). |
339 | 363 |
(...skipping 16 matching lines...) Expand all Loading... |
356 } | 380 } |
357 assertEquals(undefined, Object.getOwnPropertyDescriptor(o, "absent")) | 381 assertEquals(undefined, Object.getOwnPropertyDescriptor(o, "absent")) |
358 } | 382 } |
359 } | 383 } |
360 | 384 |
361 | 385 |
362 TestDescriptor({ | 386 TestDescriptor({ |
363 defineProperty: function(k, d) { this["__" + k] = d; return true }, | 387 defineProperty: function(k, d) { this["__" + k] = d; return true }, |
364 getOwnPropertyDescriptor: function(k) { return this["__" + k] } | 388 getOwnPropertyDescriptor: function(k) { return this["__" + k] } |
365 }) | 389 }) |
| 390 |
366 TestDescriptor({ | 391 TestDescriptor({ |
367 defineProperty: function(k, d) { this["__" + k] = d; return true }, | 392 defineProperty: function(k, d) { this["__" + k] = d; return true }, |
368 getOwnPropertyDescriptor: function(k) { | 393 getOwnPropertyDescriptor: function(k) { |
369 return this.getOwnPropertyDescriptor2(k) | 394 return this.getOwnPropertyDescriptor2(k) |
370 }, | 395 }, |
371 getOwnPropertyDescriptor2: function(k) { return this["__" + k] } | 396 getOwnPropertyDescriptor2: function(k) { return this["__" + k] } |
372 }) | 397 }) |
373 | 398 |
374 | 399 |
375 | 400 |
(...skipping 21 matching lines...) Expand all Loading... |
397 // Type. | 422 // Type. |
398 | 423 |
399 assertEquals("object", typeof Proxy.create({})) | 424 assertEquals("object", typeof Proxy.create({})) |
400 assertTrue(typeof Proxy.create({}) == "object") | 425 assertTrue(typeof Proxy.create({}) == "object") |
401 assertTrue("object" == typeof Proxy.create({})) | 426 assertTrue("object" == typeof Proxy.create({})) |
402 | 427 |
403 // No function proxies yet. | 428 // No function proxies yet. |
404 | 429 |
405 | 430 |
406 | 431 |
407 // Element (in). | 432 // Membership test (in). |
408 | 433 |
409 var key | 434 var key |
410 function TestIn(handler) { | 435 function TestIn(handler) { |
411 var o = Proxy.create(handler) | 436 var o = Proxy.create(handler) |
412 assertTrue("a" in o) | 437 assertTrue("a" in o) |
413 assertEquals("a", key) | 438 assertEquals("a", key) |
414 assertTrue(99 in o) | 439 assertTrue(99 in o) |
415 assertEquals("99", key) | 440 assertEquals("99", key) |
416 assertFalse("z" in o) | 441 assertFalse("z" in o) |
417 assertEquals("z", key) | 442 assertEquals("z", key) |
(...skipping 17 matching lines...) Expand all Loading... |
435 if (!("zzz" in o)) { | 460 if (!("zzz" in o)) { |
436 } else { | 461 } else { |
437 assertTrue(false) | 462 assertTrue(false) |
438 } | 463 } |
439 assertEquals("zzz", key) | 464 assertEquals("zzz", key) |
440 } | 465 } |
441 | 466 |
442 TestIn({ | 467 TestIn({ |
443 has: function(k) { key = k; return k < "z" } | 468 has: function(k) { key = k; return k < "z" } |
444 }) | 469 }) |
| 470 |
445 TestIn({ | 471 TestIn({ |
446 has: function(k) { return this.has2(k) }, | 472 has: function(k) { return this.has2(k) }, |
447 has2: function(k) { key = k; return k < "z" } | 473 has2: function(k) { key = k; return k < "z" } |
448 }) | 474 }) |
| 475 |
449 TestIn({ | 476 TestIn({ |
450 getPropertyDescriptor: function(k) { | 477 getPropertyDescriptor: function(k) { |
451 key = k; return k < "z" ? {value: 42} : void 0 | 478 key = k; return k < "z" ? {value: 42} : void 0 |
452 } | 479 } |
453 }) | 480 }) |
| 481 |
454 TestIn({ | 482 TestIn({ |
455 getPropertyDescriptor: function(k) { return this.getPropertyDescriptor2(k) }, | 483 getPropertyDescriptor: function(k) { return this.getPropertyDescriptor2(k) }, |
456 getPropertyDescriptor2: function(k) { | 484 getPropertyDescriptor2: function(k) { |
457 key = k; return k < "z" ? {value: 42} : void 0 | 485 key = k; return k < "z" ? {value: 42} : void 0 |
458 } | 486 } |
459 }) | 487 }) |
| 488 |
460 TestIn({ | 489 TestIn({ |
461 getPropertyDescriptor: function(k) { | 490 getPropertyDescriptor: function(k) { |
462 key = k; return k < "z" ? {get value() { return 42 }} : void 0 | 491 key = k; return k < "z" ? {get value() { return 42 }} : void 0 |
463 } | 492 } |
464 }) | 493 }) |
| 494 |
465 TestIn({ | 495 TestIn({ |
466 get: undefined, | 496 get: undefined, |
467 getPropertyDescriptor: function(k) { | 497 getPropertyDescriptor: function(k) { |
468 key = k; return k < "z" ? {value: 42} : void 0 | 498 key = k; return k < "z" ? {value: 42} : void 0 |
469 } | 499 } |
470 }) | 500 }) |
471 | 501 |
472 TestIn(Proxy.create({ | 502 TestIn(Proxy.create({ |
473 get: function(pr, pk) { | 503 get: function(pr, pk) { |
474 return function(k) { key = k; return k < "z" } | 504 return function(k) { key = k; return k < "z" } |
475 } | 505 } |
476 })) | 506 })) |
477 | 507 |
478 | 508 |
479 | 509 |
480 // Instanceof (instanceof). | 510 // Own Properties (Object.prototype.hasOwnProperty). |
| 511 |
| 512 var key |
| 513 function TestHasOwn(handler) { |
| 514 var o = Proxy.create(handler) |
| 515 assertTrue(Object.prototype.hasOwnProperty.call(o, "a")) |
| 516 assertEquals("a", key) |
| 517 assertTrue(Object.prototype.hasOwnProperty.call(o, 99)) |
| 518 assertEquals("99", key) |
| 519 assertFalse(Object.prototype.hasOwnProperty.call(o, "z")) |
| 520 assertEquals("z", key) |
| 521 } |
| 522 |
| 523 TestHasOwn({ |
| 524 hasOwn: function(k) { key = k; return k < "z" } |
| 525 }) |
| 526 |
| 527 TestHasOwn({ |
| 528 hasOwn: function(k) { return this.hasOwn2(k) }, |
| 529 hasOwn2: function(k) { key = k; return k < "z" } |
| 530 }) |
| 531 |
| 532 TestHasOwn({ |
| 533 getOwnPropertyDescriptor: function(k) { |
| 534 key = k; return k < "z" ? {value: 42} : void 0 |
| 535 } |
| 536 }) |
| 537 |
| 538 TestHasOwn({ |
| 539 getOwnPropertyDescriptor: function(k) { |
| 540 return this.getOwnPropertyDescriptor2(k) |
| 541 }, |
| 542 getOwnPropertyDescriptor2: function(k) { |
| 543 key = k; return k < "z" ? {value: 42} : void 0 |
| 544 } |
| 545 }) |
| 546 |
| 547 TestHasOwn({ |
| 548 getOwnPropertyDescriptor: function(k) { |
| 549 key = k; return k < "z" ? {get value() { return 42 }} : void 0 |
| 550 } |
| 551 }) |
| 552 |
| 553 TestHasOwn({ |
| 554 hasOwn: undefined, |
| 555 getOwnPropertyDescriptor: function(k) { |
| 556 key = k; return k < "z" ? {value: 42} : void 0 |
| 557 } |
| 558 }) |
| 559 |
| 560 TestHasOwn(Proxy.create({ |
| 561 get: function(pr, pk) { |
| 562 return function(k) { key = k; return k < "z" } |
| 563 } |
| 564 })) |
| 565 |
| 566 |
| 567 |
| 568 // Instanceof (instanceof) |
481 | 569 |
482 function TestInstanceof() { | 570 function TestInstanceof() { |
483 var o = {} | 571 var o = {} |
484 var p1 = Proxy.create({}) | 572 var p1 = Proxy.create({}) |
485 var p2 = Proxy.create({}, o) | 573 var p2 = Proxy.create({}, o) |
486 var p3 = Proxy.create({}, p2) | 574 var p3 = Proxy.create({}, p2) |
487 | 575 |
488 var f = function() {} | 576 var f = function() {} |
489 f.prototype = o | 577 f.prototype = o |
490 var f1 = function() {} | 578 var f1 = function() {} |
(...skipping 16 matching lines...) Expand all Loading... |
507 assertTrue(p3 instanceof Object) | 595 assertTrue(p3 instanceof Object) |
508 assertTrue(p3 instanceof f) | 596 assertTrue(p3 instanceof f) |
509 assertFalse(p3 instanceof f1) | 597 assertFalse(p3 instanceof f1) |
510 assertTrue(p3 instanceof f2) | 598 assertTrue(p3 instanceof f2) |
511 } | 599 } |
512 | 600 |
513 TestInstanceof() | 601 TestInstanceof() |
514 | 602 |
515 | 603 |
516 | 604 |
517 // Prototype (Object.getPrototypeOf). | 605 // Prototype (Object.getPrototypeOf, Object.prototype.isPrototypeOf). |
518 | 606 |
519 function TestPrototype() { | 607 function TestPrototype() { |
520 var o = {} | 608 var o = {} |
521 var p1 = Proxy.create({}) | 609 var p1 = Proxy.create({}) |
522 var p2 = Proxy.create({}, o) | 610 var p2 = Proxy.create({}, o) |
523 var p3 = Proxy.create({}, p2) | 611 var p3 = Proxy.create({}, p2) |
524 var p4 = Proxy.create({}, 666) | 612 var p4 = Proxy.create({}, 666) |
525 | 613 |
526 assertSame(Object.getPrototypeOf(o), Object.prototype) | 614 assertSame(Object.getPrototypeOf(o), Object.prototype) |
527 assertSame(Object.getPrototypeOf(p1), null) | 615 assertSame(Object.getPrototypeOf(p1), null) |
528 assertSame(Object.getPrototypeOf(p2), o) | 616 assertSame(Object.getPrototypeOf(p2), o) |
529 assertSame(Object.getPrototypeOf(p3), p2) | 617 assertSame(Object.getPrototypeOf(p3), p2) |
530 assertSame(Object.getPrototypeOf(p4), null) | 618 assertSame(Object.getPrototypeOf(p4), null) |
| 619 |
| 620 assertTrue(Object.prototype.isPrototypeOf(o)) |
| 621 assertFalse(Object.prototype.isPrototypeOf(p1)) |
| 622 assertTrue(Object.prototype.isPrototypeOf(p2)) |
| 623 assertTrue(Object.prototype.isPrototypeOf(p3)) |
| 624 assertFalse(Object.prototype.isPrototypeOf(p4)) |
| 625 assertTrue(Object.prototype.isPrototypeOf.call(Object.prototype, o)) |
| 626 assertFalse(Object.prototype.isPrototypeOf.call(Object.prototype, p1)) |
| 627 assertTrue(Object.prototype.isPrototypeOf.call(Object.prototype, p2)) |
| 628 assertTrue(Object.prototype.isPrototypeOf.call(Object.prototype, p3)) |
| 629 assertFalse(Object.prototype.isPrototypeOf.call(Object.prototype, p4)) |
| 630 assertFalse(Object.prototype.isPrototypeOf.call(o, o)) |
| 631 assertFalse(Object.prototype.isPrototypeOf.call(o, p1)) |
| 632 assertTrue(Object.prototype.isPrototypeOf.call(o, p2)) |
| 633 assertTrue(Object.prototype.isPrototypeOf.call(o, p3)) |
| 634 assertFalse(Object.prototype.isPrototypeOf.call(o, p4)) |
| 635 assertFalse(Object.prototype.isPrototypeOf.call(p1, p1)) |
| 636 assertFalse(Object.prototype.isPrototypeOf.call(p1, o)) |
| 637 assertFalse(Object.prototype.isPrototypeOf.call(p1, p2)) |
| 638 assertFalse(Object.prototype.isPrototypeOf.call(p1, p3)) |
| 639 assertFalse(Object.prototype.isPrototypeOf.call(p1, p4)) |
| 640 assertFalse(Object.prototype.isPrototypeOf.call(p2, p1)) |
| 641 assertFalse(Object.prototype.isPrototypeOf.call(p2, p2)) |
| 642 assertTrue(Object.prototype.isPrototypeOf.call(p2, p3)) |
| 643 assertFalse(Object.prototype.isPrototypeOf.call(p2, p4)) |
| 644 assertFalse(Object.prototype.isPrototypeOf.call(p3, p2)) |
531 } | 645 } |
532 | 646 |
533 TestPrototype() | 647 TestPrototype() |
534 | 648 |
535 | 649 |
536 | 650 |
537 // Property names (Object.getOwnPropertyNames, Object.keys). | 651 // Property names (Object.getOwnPropertyNames, Object.keys). |
538 | 652 |
539 function TestPropertyNames(names, handler) { | 653 function TestPropertyNames(names, handler) { |
540 var p = Proxy.create(handler) | 654 var p = Proxy.create(handler) |
541 assertArrayEquals(names, Object.getOwnPropertyNames(p)) | 655 assertArrayEquals(names, Object.getOwnPropertyNames(p)) |
542 } | 656 } |
543 | 657 |
544 TestPropertyNames([], { | 658 TestPropertyNames([], { |
545 getOwnPropertyNames: function() { return [] } | 659 getOwnPropertyNames: function() { return [] } |
546 }) | 660 }) |
| 661 |
547 TestPropertyNames(["a", "zz", " ", "0"], { | 662 TestPropertyNames(["a", "zz", " ", "0"], { |
548 getOwnPropertyNames: function() { return ["a", "zz", " ", 0] } | 663 getOwnPropertyNames: function() { return ["a", "zz", " ", 0] } |
549 }) | 664 }) |
| 665 |
550 TestPropertyNames(["throw", "function "], { | 666 TestPropertyNames(["throw", "function "], { |
551 getOwnPropertyNames: function() { return this.getOwnPropertyNames2() }, | 667 getOwnPropertyNames: function() { return this.getOwnPropertyNames2() }, |
552 getOwnPropertyNames2: function() { return ["throw", "function "] } | 668 getOwnPropertyNames2: function() { return ["throw", "function "] } |
553 }) | 669 }) |
| 670 |
554 TestPropertyNames(["[object Object]"], { | 671 TestPropertyNames(["[object Object]"], { |
555 get getOwnPropertyNames() { | 672 get getOwnPropertyNames() { |
556 return function() { return [{}] } | 673 return function() { return [{}] } |
557 } | 674 } |
558 }) | 675 }) |
559 | 676 |
560 | 677 |
561 function TestKeys(names, handler) { | 678 function TestKeys(names, handler) { |
562 var p = Proxy.create(handler) | 679 var p = Proxy.create(handler) |
563 assertArrayEquals(names, Object.keys(p)) | 680 assertArrayEquals(names, Object.keys(p)) |
564 } | 681 } |
565 | 682 |
566 TestKeys([], { | 683 TestKeys([], { |
567 keys: function() { return [] } | 684 keys: function() { return [] } |
568 }) | 685 }) |
| 686 |
569 TestKeys(["a", "zz", " ", "0"], { | 687 TestKeys(["a", "zz", " ", "0"], { |
570 keys: function() { return ["a", "zz", " ", 0] } | 688 keys: function() { return ["a", "zz", " ", 0] } |
571 }) | 689 }) |
| 690 |
572 TestKeys(["throw", "function "], { | 691 TestKeys(["throw", "function "], { |
573 keys: function() { return this.keys2() }, | 692 keys: function() { return this.keys2() }, |
574 keys2: function() { return ["throw", "function "] } | 693 keys2: function() { return ["throw", "function "] } |
575 }) | 694 }) |
| 695 |
576 TestKeys(["[object Object]"], { | 696 TestKeys(["[object Object]"], { |
577 get keys() { | 697 get keys() { |
578 return function() { return [{}] } | 698 return function() { return [{}] } |
579 } | 699 } |
580 }) | 700 }) |
| 701 |
581 TestKeys(["a", "0"], { | 702 TestKeys(["a", "0"], { |
582 getOwnPropertyNames: function() { return ["a", 23, "zz", "", 0] }, | 703 getOwnPropertyNames: function() { return ["a", 23, "zz", "", 0] }, |
583 getOwnPropertyDescriptor: function(k) { return {enumerable: k.length == 1} } | 704 getOwnPropertyDescriptor: function(k) { return {enumerable: k.length == 1} } |
584 }) | 705 }) |
| 706 |
585 TestKeys(["23", "zz", ""], { | 707 TestKeys(["23", "zz", ""], { |
586 getOwnPropertyNames: function() { return this.getOwnPropertyNames2() }, | 708 getOwnPropertyNames: function() { return this.getOwnPropertyNames2() }, |
587 getOwnPropertyNames2: function() { return ["a", 23, "zz", "", 0] }, | 709 getOwnPropertyNames2: function() { return ["a", 23, "zz", "", 0] }, |
588 getOwnPropertyDescriptor: function(k) { | 710 getOwnPropertyDescriptor: function(k) { |
589 return this.getOwnPropertyDescriptor2(k) | 711 return this.getOwnPropertyDescriptor2(k) |
590 }, | 712 }, |
591 getOwnPropertyDescriptor2: function(k) { return {enumerable: k.length != 1} } | 713 getOwnPropertyDescriptor2: function(k) { return {enumerable: k.length != 1} } |
592 }) | 714 }) |
| 715 |
593 TestKeys(["a", "b", "c", "5"], { | 716 TestKeys(["a", "b", "c", "5"], { |
594 get getOwnPropertyNames() { | 717 get getOwnPropertyNames() { |
595 return function() { return ["0", 4, "a", "b", "c", 5] } | 718 return function() { return ["0", 4, "a", "b", "c", 5] } |
596 }, | 719 }, |
597 get getOwnPropertyDescriptor() { | 720 get getOwnPropertyDescriptor() { |
598 return function(k) { return {enumerable: k >= "44"} } | 721 return function(k) { return {enumerable: k >= "44"} } |
599 } | 722 } |
600 }) | 723 }) |
| 724 |
601 TestKeys([], { | 725 TestKeys([], { |
602 get getOwnPropertyNames() { | 726 get getOwnPropertyNames() { |
603 return function() { return ["a", "b", "c"] } | 727 return function() { return ["a", "b", "c"] } |
604 }, | 728 }, |
605 getOwnPropertyDescriptor: function(k) { return {} } | 729 getOwnPropertyDescriptor: function(k) { return {} } |
606 }) | 730 }) |
607 | 731 |
608 | 732 |
609 | 733 |
610 // Fixing (Object.freeze, Object.seal, Object.preventExtensions, | 734 // Fixing (Object.freeze, Object.seal, Object.preventExtensions, |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
654 assertArrayEquals(names.sort(), Object.getOwnPropertyNames(o3).sort()) | 778 assertArrayEquals(names.sort(), Object.getOwnPropertyNames(o3).sort()) |
655 assertArrayEquals(names.filter(function(x) {return x < "z"}).sort(), | 779 assertArrayEquals(names.filter(function(x) {return x < "z"}).sort(), |
656 Object.keys(o3).sort()) | 780 Object.keys(o3).sort()) |
657 assertEquals(proto, Object.getPrototypeOf(o3)) | 781 assertEquals(proto, Object.getPrototypeOf(o3)) |
658 assertEquals(77, o3.p) | 782 assertEquals(77, o3.p) |
659 } | 783 } |
660 | 784 |
661 TestFix([], { | 785 TestFix([], { |
662 fix: function() { return {} } | 786 fix: function() { return {} } |
663 }) | 787 }) |
| 788 |
664 TestFix(["a", "b", "c", "d", "zz"], { | 789 TestFix(["a", "b", "c", "d", "zz"], { |
665 fix: function() { | 790 fix: function() { |
666 return { | 791 return { |
667 a: {value: "a", writable: true, configurable: false, enumerable: true}, | 792 a: {value: "a", writable: true, configurable: false, enumerable: true}, |
668 b: {value: 33, writable: false, configurable: false, enumerable: true}, | 793 b: {value: 33, writable: false, configurable: false, enumerable: true}, |
669 c: {value: 0, writable: true, configurable: true, enumerable: true}, | 794 c: {value: 0, writable: true, configurable: true, enumerable: true}, |
670 d: {value: true, writable: false, configurable: true, enumerable: true}, | 795 d: {value: true, writable: false, configurable: true, enumerable: true}, |
671 zz: {value: 0, enumerable: false} | 796 zz: {value: 0, enumerable: false} |
672 } | 797 } |
673 } | 798 } |
674 }) | 799 }) |
| 800 |
675 TestFix(["a"], { | 801 TestFix(["a"], { |
676 fix: function() { return this.fix2() }, | 802 fix: function() { return this.fix2() }, |
677 fix2: function() { | 803 fix2: function() { |
678 return {a: {value: 4, writable: true, configurable: true, enumerable: true}} | 804 return {a: {value: 4, writable: true, configurable: true, enumerable: true}} |
679 } | 805 } |
680 }) | 806 }) |
| 807 |
681 TestFix(["b"], { | 808 TestFix(["b"], { |
682 get fix() { | 809 get fix() { |
683 return function() { | 810 return function() { |
684 return {b: {configurable: true, writable: true, enumerable: true}} | 811 return {b: {configurable: true, writable: true, enumerable: true}} |
685 } | 812 } |
686 } | 813 } |
687 }) | 814 }) |
| 815 |
| 816 |
| 817 |
| 818 // String conversion (Object.prototype.toString, Object.prototype.toLocaleString
) |
| 819 |
| 820 var key |
| 821 function TestToString(handler) { |
| 822 var o = Proxy.create(handler) |
| 823 key = "" |
| 824 assertEquals("[object Object]", Object.prototype.toString.call(o)) |
| 825 assertEquals("", key) |
| 826 assertEquals("my_proxy", Object.prototype.toLocaleString.call(o)) |
| 827 assertEquals("toString", key) |
| 828 } |
| 829 |
| 830 TestToString({ |
| 831 get: function(r, k) { key = k; return function() { return "my_proxy" } } |
| 832 }) |
| 833 |
| 834 TestToString({ |
| 835 get: function(r, k) { return this.get2(r, k) }, |
| 836 get2: function(r, k) { key = k; return function() { return "my_proxy" } } |
| 837 }) |
| 838 |
| 839 TestToString(Proxy.create({ |
| 840 get: function(pr, pk) { |
| 841 return function(r, k) { key = k; return function() { return "my_proxy" } } |
| 842 } |
| 843 })) |
| 844 |
| 845 |
| 846 |
| 847 // Value conversion (Object.prototype.toValue) |
| 848 |
| 849 function TestValueOf(handler) { |
| 850 var o = Proxy.create(handler) |
| 851 assertSame(o, Object.prototype.valueOf.call(o)) |
| 852 } |
| 853 |
| 854 TestValueOf({}) |
| 855 |
| 856 |
| 857 |
| 858 // Enumerability (Object.prototype.propertyIsEnumerable) |
| 859 |
| 860 var key |
| 861 function TestIsEnumerable(handler) { |
| 862 var o = Proxy.create(handler) |
| 863 assertTrue(Object.prototype.propertyIsEnumerable.call(o, "a")) |
| 864 assertEquals("a", key) |
| 865 assertTrue(Object.prototype.propertyIsEnumerable.call(o, 2)) |
| 866 assertEquals("2", key) |
| 867 assertFalse(Object.prototype.propertyIsEnumerable.call(o, "z")) |
| 868 assertEquals("z", key) |
| 869 } |
| 870 |
| 871 TestIsEnumerable({ |
| 872 getOwnPropertyDescriptor: function(k) { |
| 873 key = k; return {enumerable: k < "z", configurable: true} |
| 874 }, |
| 875 }) |
| 876 |
| 877 TestIsEnumerable({ |
| 878 getOwnPropertyDescriptor: function(k) { |
| 879 return this.getOwnPropertyDescriptor2(k) |
| 880 }, |
| 881 getOwnPropertyDescriptor2: function(k) { |
| 882 key = k; return {enumerable: k < "z", configurable: true} |
| 883 }, |
| 884 }) |
| 885 |
| 886 TestIsEnumerable({ |
| 887 getOwnPropertyDescriptor: function(k) { |
| 888 key = k; return {get enumerable() { return k < "z" }, configurable: true} |
| 889 }, |
| 890 }) |
| 891 |
| 892 TestIsEnumerable(Proxy.create({ |
| 893 get: function(pr, pk) { |
| 894 return function(k) { |
| 895 key = k; return {enumerable: k < "z", configurable: true} |
| 896 } |
| 897 } |
| 898 })) |
OLD | NEW |