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 10 matching lines...) Expand all Loading... | |
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
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 // TODO(rossberg): test exception cases. | |
32 | |
31 | 33 |
32 // Getters. | 34 // Getters. |
33 | 35 |
34 function TestGet(handler) { | 36 function TestGet(handler) { |
35 var o = Proxy.create(handler) | 37 var o = Proxy.create(handler) |
36 assertEquals(42, o.a) | 38 assertEquals(42, o.a) |
37 assertEquals(42, o["b"]) | 39 assertEquals(42, o["b"]) |
38 } | 40 } |
39 | 41 |
40 TestGet({ | 42 TestGet({ |
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
395 // Type. | 397 // Type. |
396 | 398 |
397 assertEquals("object", typeof Proxy.create({})) | 399 assertEquals("object", typeof Proxy.create({})) |
398 assertTrue(typeof Proxy.create({}) == "object") | 400 assertTrue(typeof Proxy.create({}) == "object") |
399 assertTrue("object" == typeof Proxy.create({})) | 401 assertTrue("object" == typeof Proxy.create({})) |
400 | 402 |
401 // No function proxies yet. | 403 // No function proxies yet. |
402 | 404 |
403 | 405 |
404 | 406 |
407 // Element (in). | |
Mads Ager (chromium)
2011/07/19 08:18:05
We should have tests for the case where the Has tr
rossberg
2011/07/19 09:29:52
I left that for the TODO at the top, since I still
| |
408 | |
409 var key | |
410 function TestIn(handler) { | |
411 var o = Proxy.create(handler) | |
412 assertTrue("a" in o) | |
413 assertEquals("a", key) | |
414 assertTrue(99 in o) | |
415 assertEquals("99", key) | |
416 assertFalse("z" in o) | |
417 assertEquals("z", key) | |
418 | |
419 if ("b" in o) { | |
420 } else { | |
421 assertTrue(false) | |
422 } | |
423 assertEquals("b", key) | |
424 | |
425 if ("zz" in o) { | |
426 assertTrue(false) | |
427 } | |
428 assertEquals("zz", key) | |
429 | |
430 if (!("c" in o)) { | |
431 assertTrue(false) | |
432 } | |
433 assertEquals("c", key) | |
434 | |
435 if (!("zzz" in o)) { | |
436 } else { | |
437 assertTrue(false) | |
438 } | |
439 assertEquals("zzz", key) | |
440 } | |
441 | |
442 TestIn({ | |
443 has: function(k) { key = k; return k < "z" } | |
444 }) | |
445 TestIn({ | |
446 has: function(k) { return this.has2(k) }, | |
447 has2: function(k) { key = k; return k < "z" } | |
448 }) | |
449 TestIn({ | |
450 getPropertyDescriptor: function(k) { | |
451 key = k; return k < "z" ? {value: 42} : void 0 | |
452 } | |
453 }) | |
454 TestIn({ | |
455 getPropertyDescriptor: function(k) { return this.getPropertyDescriptor2(k) }, | |
456 getPropertyDescriptor2: function(k) { | |
457 key = k; return k < "z" ? {value: 42} : void 0 | |
458 } | |
459 }) | |
460 TestIn({ | |
461 getPropertyDescriptor: function(k) { | |
462 key = k; return k < "z" ? {get value() { return 42 }} : void 0 | |
463 } | |
464 }) | |
465 TestIn({ | |
466 get: undefined, | |
467 getPropertyDescriptor: function(k) { | |
468 key = k; return k < "z" ? {value: 42} : void 0 | |
469 } | |
470 }) | |
471 | |
472 TestIn(Proxy.create({ | |
473 get: function(pr, pk) { | |
474 return function(k) { key = k; return k < "z" } | |
475 } | |
476 })) | |
477 | |
478 | |
479 | |
405 // Instanceof (instanceof). | 480 // Instanceof (instanceof). |
406 | 481 |
407 function TestInstanceof() { | 482 function TestInstanceof() { |
408 var o = {} | 483 var o = {} |
409 var p1 = Proxy.create({}) | 484 var p1 = Proxy.create({}) |
410 var p2 = Proxy.create({}, o) | 485 var p2 = Proxy.create({}, o) |
411 var p3 = Proxy.create({}, p2) | 486 var p3 = Proxy.create({}, p2) |
412 | 487 |
413 var f = function() {} | 488 var f = function() {} |
414 f.prototype = o | 489 f.prototype = o |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
522 get getOwnPropertyDescriptor() { | 597 get getOwnPropertyDescriptor() { |
523 return function(k) { return {enumerable: k >= "44"} } | 598 return function(k) { return {enumerable: k >= "44"} } |
524 } | 599 } |
525 }) | 600 }) |
526 TestKeys([], { | 601 TestKeys([], { |
527 get getOwnPropertyNames() { | 602 get getOwnPropertyNames() { |
528 return function() { return ["a", "b", "c"] } | 603 return function() { return ["a", "b", "c"] } |
529 }, | 604 }, |
530 getOwnPropertyDescriptor: function(k) { return {} } | 605 getOwnPropertyDescriptor: function(k) { return {} } |
531 }) | 606 }) |
607 | |
608 | |
609 | |
610 // Fixing (Object.freeze, Object.seal, Object.preventExtensions, | |
611 // Object.isFrozen, Object.isSealed, Object.isExtensible) | |
612 | |
613 function TestFix(names, handler) { | |
614 var proto = {p: 77} | |
615 var assertFixing = function(o, s, f, e) { | |
616 assertEquals(s, Object.isSealed(o)) | |
617 assertEquals(f, Object.isFrozen(o)) | |
618 assertEquals(e, Object.isExtensible(o)) | |
619 } | |
620 | |
621 var o1 = Proxy.create(handler, proto) | |
622 assertFixing(o1, false, false, true) | |
623 Object.seal(o1) | |
624 assertFixing(o1, true, names.length === 0, false) | |
625 assertArrayEquals(names.sort(), Object.getOwnPropertyNames(o1).sort()) | |
626 assertArrayEquals(names.filter(function(x) {return x < "z"}).sort(), | |
627 Object.keys(o1).sort()) | |
628 assertEquals(proto, Object.getPrototypeOf(o1)) | |
629 assertEquals(77, o1.p) | |
630 for (var n in o1) { | |
631 var desc = Object.getOwnPropertyDescriptor(o1, n) | |
632 if (desc !== undefined) assertFalse(desc.configurable) | |
633 } | |
634 | |
635 var o2 = Proxy.create(handler, proto) | |
636 assertFixing(o2, false, false, true) | |
637 Object.freeze(o2) | |
638 assertFixing(o2, true, true, false) | |
639 assertArrayEquals(names.sort(), Object.getOwnPropertyNames(o2).sort()) | |
640 assertArrayEquals(names.filter(function(x) {return x < "z"}).sort(), | |
641 Object.keys(o2).sort()) | |
642 assertEquals(proto, Object.getPrototypeOf(o2)) | |
643 assertEquals(77, o2.p) | |
644 for (var n in o2) { | |
645 var desc = Object.getOwnPropertyDescriptor(o2, n) | |
646 if (desc !== undefined) assertFalse(desc.writable) | |
647 if (desc !== undefined) assertFalse(desc.configurable) | |
648 } | |
649 | |
650 var o3 = Proxy.create(handler, proto) | |
651 assertFixing(o3, false, false, true) | |
652 Object.preventExtensions(o3) | |
653 assertFixing(o3, names.length === 0, names.length === 0, false) | |
654 assertArrayEquals(names.sort(), Object.getOwnPropertyNames(o3).sort()) | |
655 assertArrayEquals(names.filter(function(x) {return x < "z"}).sort(), | |
656 Object.keys(o3).sort()) | |
657 assertEquals(proto, Object.getPrototypeOf(o3)) | |
658 assertEquals(77, o3.p) | |
659 } | |
660 | |
661 TestFix([], { | |
662 fix: function() { return {} } | |
663 }) | |
664 TestFix(["a", "b", "c", "d", "zz"], { | |
665 fix: function() { | |
666 return { | |
667 a: {value: "a", writable: true, configurable: false, enumerable: true}, | |
668 b: {value: 33, writable: false, configurable: false, enumerable: true}, | |
669 c: {value: 0, writable: true, configurable: true, enumerable: true}, | |
670 d: {value: true, writable: false, configurable: true, enumerable: true}, | |
671 zz: {value: 0, enumerable: false} | |
672 } | |
673 } | |
674 }) | |
675 TestFix(["a"], { | |
676 fix: function() { return this.fix2() }, | |
677 fix2: function() { | |
678 return {a: {value: 4, writable: true, configurable: true, enumerable: true}} | |
679 } | |
680 }) | |
681 TestFix(["b"], { | |
682 get fix() { | |
683 return function() { | |
684 return {b: {configurable: true, writable: true, enumerable: true}} | |
685 } | |
686 } | |
687 }) | |
OLD | NEW |