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 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
397 // Type. | 397 // Type. |
398 | 398 |
399 assertEquals("object", typeof Proxy.create({})) | 399 assertEquals("object", typeof Proxy.create({})) |
400 assertTrue(typeof Proxy.create({}) == "object") | 400 assertTrue(typeof Proxy.create({}) == "object") |
401 assertTrue("object" == typeof Proxy.create({})) | 401 assertTrue("object" == typeof Proxy.create({})) |
402 | 402 |
403 // No function proxies yet. | 403 // No function proxies yet. |
404 | 404 |
405 | 405 |
406 | 406 |
407 // Element (in). | 407 // Membership test (in). |
408 | 408 |
409 var key | 409 var key |
410 function TestIn(handler) { | 410 function TestIn(handler) { |
411 var o = Proxy.create(handler) | 411 var o = Proxy.create(handler) |
412 assertTrue("a" in o) | 412 assertTrue("a" in o) |
413 assertEquals("a", key) | 413 assertEquals("a", key) |
414 assertTrue(99 in o) | 414 assertTrue(99 in o) |
415 assertEquals("99", key) | 415 assertEquals("99", key) |
416 assertFalse("z" in o) | 416 assertFalse("z" in o) |
417 assertEquals("z", key) | 417 assertEquals("z", key) |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
470 }) | 470 }) |
471 | 471 |
472 TestIn(Proxy.create({ | 472 TestIn(Proxy.create({ |
473 get: function(pr, pk) { | 473 get: function(pr, pk) { |
474 return function(k) { key = k; return k < "z" } | 474 return function(k) { key = k; return k < "z" } |
475 } | 475 } |
476 })) | 476 })) |
477 | 477 |
478 | 478 |
479 | 479 |
480 // Instanceof (instanceof). | 480 // Own Properties (Object.prototype.hasOwnProperty). |
481 | |
482 var key | |
483 function TestHasOwn(handler) { | |
484 var o = Proxy.create(handler) | |
485 assertTrue(Object.prototype.hasOwnProperty.call(o, "a")) | |
486 assertEquals("a", key) | |
487 assertTrue(Object.prototype.hasOwnProperty.call(o, 99)) | |
488 assertEquals("99", key) | |
489 assertFalse(Object.prototype.hasOwnProperty.call(o, "z")) | |
490 assertEquals("z", key) | |
491 } | |
492 | |
493 TestHasOwn({ | |
494 hasOwn: function(k) { key = k; return k < "z" } | |
495 }) | |
496 TestHasOwn({ | |
Mads Ager (chromium)
2011/07/19 15:41:05
Add whitespace between the calls?
rossberg
2011/07/21 11:08:42
Done, here and elsewhere.
| |
497 hasOwn: function(k) { return this.hasOwn2(k) }, | |
498 hasOwn2: function(k) { key = k; return k < "z" } | |
499 }) | |
500 TestHasOwn({ | |
501 getOwnPropertyDescriptor: function(k) { | |
502 key = k; return k < "z" ? {value: 42} : void 0 | |
503 } | |
504 }) | |
505 TestHasOwn({ | |
506 getOwnPropertyDescriptor: function(k) { | |
507 return this.getOwnPropertyDescriptor2(k) | |
508 }, | |
509 getOwnPropertyDescriptor2: function(k) { | |
510 key = k; return k < "z" ? {value: 42} : void 0 | |
511 } | |
512 }) | |
513 TestHasOwn({ | |
514 getOwnPropertyDescriptor: function(k) { | |
515 key = k; return k < "z" ? {get value() { return 42 }} : void 0 | |
516 } | |
517 }) | |
518 TestHasOwn({ | |
519 hasOwn: undefined, | |
520 getOwnPropertyDescriptor: function(k) { | |
521 key = k; return k < "z" ? {value: 42} : void 0 | |
522 } | |
523 }) | |
524 | |
525 TestHasOwn(Proxy.create({ | |
526 get: function(pr, pk) { | |
527 return function(k) { key = k; return k < "z" } | |
528 } | |
529 })) | |
530 | |
531 | |
532 | |
533 // Instanceof (instanceof) | |
481 | 534 |
482 function TestInstanceof() { | 535 function TestInstanceof() { |
483 var o = {} | 536 var o = {} |
484 var p1 = Proxy.create({}) | 537 var p1 = Proxy.create({}) |
485 var p2 = Proxy.create({}, o) | 538 var p2 = Proxy.create({}, o) |
486 var p3 = Proxy.create({}, p2) | 539 var p3 = Proxy.create({}, p2) |
487 | 540 |
488 var f = function() {} | 541 var f = function() {} |
489 f.prototype = o | 542 f.prototype = o |
490 var f1 = function() {} | 543 var f1 = function() {} |
(...skipping 16 matching lines...) Expand all Loading... | |
507 assertTrue(p3 instanceof Object) | 560 assertTrue(p3 instanceof Object) |
508 assertTrue(p3 instanceof f) | 561 assertTrue(p3 instanceof f) |
509 assertFalse(p3 instanceof f1) | 562 assertFalse(p3 instanceof f1) |
510 assertTrue(p3 instanceof f2) | 563 assertTrue(p3 instanceof f2) |
511 } | 564 } |
512 | 565 |
513 TestInstanceof() | 566 TestInstanceof() |
514 | 567 |
515 | 568 |
516 | 569 |
517 // Prototype (Object.getPrototypeOf). | 570 // Prototype (Object.getPrototypeOf, Object.prototype.isPrototypeOf). |
518 | 571 |
519 function TestPrototype() { | 572 function TestPrototype() { |
520 var o = {} | 573 var o = {} |
521 var p1 = Proxy.create({}) | 574 var p1 = Proxy.create({}) |
522 var p2 = Proxy.create({}, o) | 575 var p2 = Proxy.create({}, o) |
523 var p3 = Proxy.create({}, p2) | 576 var p3 = Proxy.create({}, p2) |
524 var p4 = Proxy.create({}, 666) | 577 var p4 = Proxy.create({}, 666) |
525 | 578 |
526 assertSame(Object.getPrototypeOf(o), Object.prototype) | 579 assertSame(Object.getPrototypeOf(o), Object.prototype) |
527 assertSame(Object.getPrototypeOf(p1), null) | 580 assertSame(Object.getPrototypeOf(p1), null) |
528 assertSame(Object.getPrototypeOf(p2), o) | 581 assertSame(Object.getPrototypeOf(p2), o) |
529 assertSame(Object.getPrototypeOf(p3), p2) | 582 assertSame(Object.getPrototypeOf(p3), p2) |
530 assertSame(Object.getPrototypeOf(p4), null) | 583 assertSame(Object.getPrototypeOf(p4), null) |
584 | |
585 assertTrue(Object.prototype.isPrototypeOf(o)) | |
586 assertFalse(Object.prototype.isPrototypeOf(p1)) | |
587 assertTrue(Object.prototype.isPrototypeOf(p2)) | |
588 assertTrue(Object.prototype.isPrototypeOf(p3)) | |
589 assertFalse(Object.prototype.isPrototypeOf(p4)) | |
590 assertTrue(Object.prototype.isPrototypeOf.call(Object.prototype, o)) | |
591 assertFalse(Object.prototype.isPrototypeOf.call(Object.prototype, p1)) | |
592 assertTrue(Object.prototype.isPrototypeOf.call(Object.prototype, p2)) | |
593 assertTrue(Object.prototype.isPrototypeOf.call(Object.prototype, p3)) | |
594 assertFalse(Object.prototype.isPrototypeOf.call(Object.prototype, p4)) | |
595 assertFalse(Object.prototype.isPrototypeOf.call(o, o)) | |
596 assertFalse(Object.prototype.isPrototypeOf.call(o, p1)) | |
597 assertTrue(Object.prototype.isPrototypeOf.call(o, p2)) | |
598 assertTrue(Object.prototype.isPrototypeOf.call(o, p3)) | |
599 assertFalse(Object.prototype.isPrototypeOf.call(o, p4)) | |
600 assertFalse(Object.prototype.isPrototypeOf.call(p1, p1)) | |
601 assertFalse(Object.prototype.isPrototypeOf.call(p1, o)) | |
602 assertFalse(Object.prototype.isPrototypeOf.call(p1, p2)) | |
603 assertFalse(Object.prototype.isPrototypeOf.call(p1, p3)) | |
604 assertFalse(Object.prototype.isPrototypeOf.call(p1, p4)) | |
605 assertFalse(Object.prototype.isPrototypeOf.call(p2, p1)) | |
606 assertFalse(Object.prototype.isPrototypeOf.call(p2, p2)) | |
607 assertTrue(Object.prototype.isPrototypeOf.call(p2, p3)) | |
608 assertFalse(Object.prototype.isPrototypeOf.call(p2, p4)) | |
609 assertFalse(Object.prototype.isPrototypeOf.call(p3, p2)) | |
531 } | 610 } |
532 | 611 |
533 TestPrototype() | 612 TestPrototype() |
534 | 613 |
535 | 614 |
536 | 615 |
537 // Property names (Object.getOwnPropertyNames, Object.keys). | 616 // Property names (Object.getOwnPropertyNames, Object.keys). |
538 | 617 |
539 function TestPropertyNames(names, handler) { | 618 function TestPropertyNames(names, handler) { |
540 var p = Proxy.create(handler) | 619 var p = Proxy.create(handler) |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
678 return {a: {value: 4, writable: true, configurable: true, enumerable: true}} | 757 return {a: {value: 4, writable: true, configurable: true, enumerable: true}} |
679 } | 758 } |
680 }) | 759 }) |
681 TestFix(["b"], { | 760 TestFix(["b"], { |
682 get fix() { | 761 get fix() { |
683 return function() { | 762 return function() { |
684 return {b: {configurable: true, writable: true, enumerable: true}} | 763 return {b: {configurable: true, writable: true, enumerable: true}} |
685 } | 764 } |
686 } | 765 } |
687 }) | 766 }) |
767 | |
768 | |
769 | |
770 // String conversion (Object.prototype.toString, Object.prototype.toLocaleString ) | |
771 | |
772 var key | |
773 function TestToString(handler) { | |
774 var o = Proxy.create(handler) | |
775 key = "" | |
776 assertEquals("[object Object]", Object.prototype.toString.call(o)) | |
777 assertEquals("", key) | |
778 assertEquals("my_proxy", Object.prototype.toLocaleString.call(o)) | |
779 assertEquals("toString", key) | |
780 } | |
781 | |
782 TestToString({ | |
783 get: function(r, k) { key = k; return function() { return "my_proxy" } } | |
784 }) | |
785 TestToString({ | |
Mads Ager (chromium)
2011/07/19 15:41:05
Ditto.
| |
786 get: function(r, k) { return this.get2(r, k) }, | |
787 get2: function(r, k) { key = k; return function() { return "my_proxy" } } | |
788 }) | |
789 TestToString(Proxy.create({ | |
790 get: function(pr, pk) { | |
791 return function(r, k) { key = k; return function() { return "my_proxy" } } | |
792 } | |
793 })) | |
794 | |
795 | |
796 | |
797 // Value conversion (Object.prototype.toValue) | |
798 | |
799 function TestValueOf(handler) { | |
800 var o = Proxy.create(handler) | |
801 assertSame(o, Object.prototype.valueOf.call(o)) | |
802 } | |
803 | |
804 TestValueOf({}) | |
805 | |
806 | |
807 | |
808 // Enumerability (Object.prototype.propertyIsEnumerable) | |
809 | |
810 var key | |
811 function TestIsEnumerable(handler) { | |
812 var o = Proxy.create(handler) | |
813 assertTrue(Object.prototype.propertyIsEnumerable.call(o, "a")) | |
814 assertEquals("a", key) | |
815 assertTrue(Object.prototype.propertyIsEnumerable.call(o, 2)) | |
816 assertEquals("2", key) | |
817 assertFalse(Object.prototype.propertyIsEnumerable.call(o, "z")) | |
818 assertEquals("z", key) | |
819 } | |
820 | |
821 TestIsEnumerable({ | |
822 getOwnPropertyDescriptor: function(k) { | |
823 key = k; return {enumerable: k < "z", configurable: true} | |
824 }, | |
825 }) | |
826 TestIsEnumerable({ | |
Mads Ager (chromium)
2011/07/19 15:41:05
Ditto.
| |
827 getOwnPropertyDescriptor: function(k) { | |
828 return this.getOwnPropertyDescriptor2(k) | |
829 }, | |
830 getOwnPropertyDescriptor2: function(k) { | |
831 key = k; return {enumerable: k < "z", configurable: true} | |
832 }, | |
833 }) | |
834 TestIsEnumerable({ | |
835 getOwnPropertyDescriptor: function(k) { | |
836 key = k; return {get enumerable() { return k < "z" }, configurable: true} | |
837 }, | |
838 }) | |
839 | |
840 TestIsEnumerable(Proxy.create({ | |
841 get: function(pr, pk) { | |
842 return function(k) { | |
843 key = k; return {enumerable: k < "z", configurable: true} | |
844 } | |
845 } | |
846 })) | |
OLD | NEW |