OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --harmony-concat-spreadable | 5 // Flags: --harmony-concat-spreadable --harmony-proxies --harmony-reflect |
6 | 6 |
7 (function testArrayConcatArity() { | 7 (function testArrayConcatArity() { |
8 "use strict"; | 8 "use strict"; |
9 assertEquals(1, Array.prototype.concat.length); | 9 assertEquals(1, Array.prototype.concat.length); |
10 })(); | 10 })(); |
11 | 11 |
12 | 12 |
13 (function testArrayConcatNoPrototype() { | 13 (function testArrayConcatNoPrototype() { |
14 "use strict"; | 14 "use strict"; |
15 assertEquals(void 0, Array.prototype.concat.prototype); | 15 assertEquals(void 0, Array.prototype.concat.prototype); |
(...skipping 682 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
698 function mkGetter(i) { return function() { trace.push(i); }; } | 698 function mkGetter(i) { return function() { trace.push(i); }; } |
699 arr3.length = 10000; | 699 arr3.length = 10000; |
700 for (var i = 0; i < 100; i++) { | 700 for (var i = 0; i < 100; i++) { |
701 Object.defineProperty(arr3, i * i, {get: mkGetter(i)}); | 701 Object.defineProperty(arr3, i * i, {get: mkGetter(i)}); |
702 expectedTrace[i] = i; | 702 expectedTrace[i] = i; |
703 expectedTrace[100 + i] = i; | 703 expectedTrace[100 + i] = i; |
704 } | 704 } |
705 var r4 = [0].concat(arr3, arr3); | 705 var r4 = [0].concat(arr3, arr3); |
706 assertEquals(1 + arr3.length * 2, r4.length); | 706 assertEquals(1 + arr3.length * 2, r4.length); |
707 assertEquals(expectedTrace, trace); | 707 assertEquals(expectedTrace, trace); |
| 708 |
| 709 // Clean up. |
| 710 delete Array.prototype[123]; |
| 711 delete Array.prototype["123"]; |
| 712 delete Array.prototype["moe"]; |
708 })(); | 713 })(); |
| 714 |
| 715 |
| 716 |
| 717 |
| 718 //////////////////////////////////////////////////////////////////////////////// |
| 719 // Tests with proxies |
| 720 |
| 721 // Note: concat does not currently support species so there is no difference |
| 722 // between [].concat(foo) and Array.prototype.concat.apply(foo). |
| 723 |
| 724 |
| 725 var log = []; |
| 726 var logger = {}; |
| 727 var handler = new Proxy({}, logger); |
| 728 |
| 729 logger.get = function(t, trap, r) { |
| 730 return function(...args) { |
| 731 log.push([trap, ...args]); |
| 732 return Reflect[trap](...args); |
| 733 } |
| 734 }; |
| 735 |
| 736 |
| 737 (function testUnspreadableNonArrayLikeProxy() { |
| 738 var target = {0: "a", 1: "b"}; |
| 739 var obj = new Proxy(target, handler); |
| 740 |
| 741 log.length = 0; |
| 742 assertEquals([obj], [].concat(obj)); |
| 743 assertEquals(1, log.length); |
| 744 for (var i in log) assertSame(target, log[i][1]); |
| 745 assertEquals(["get", target, Symbol.isConcatSpreadable, obj], log[0]); |
| 746 |
| 747 log.length = 0; |
| 748 assertEquals([obj], Array.prototype.concat.apply(obj)); |
| 749 assertEquals(1, log.length); |
| 750 for (var i in log) assertSame(target, log[i][1]); |
| 751 assertEquals(["get", target, Symbol.isConcatSpreadable, obj], log[0]); |
| 752 })(); |
| 753 |
| 754 |
| 755 (function testSpreadableNonArrayLikeProxy() { |
| 756 var target = {0: "a", 1: "b", [Symbol.isConcatSpreadable]: "truish"}; |
| 757 var obj = new Proxy(target, handler); |
| 758 |
| 759 log.length = 0; |
| 760 assertEquals([], [].concat(obj)); |
| 761 assertEquals(2, log.length); |
| 762 for (var i in log) assertSame(target, log[i][1]); |
| 763 assertEquals(["get", target, Symbol.isConcatSpreadable, obj], log[0]); |
| 764 assertEquals(["get", target, "length", obj], log[1]); |
| 765 |
| 766 log.length = 0; |
| 767 assertEquals([], Array.prototype.concat.apply(obj)); |
| 768 assertEquals(2, log.length); |
| 769 for (var i in log) assertSame(target, log[i][1]); |
| 770 assertEquals(["get", target, Symbol.isConcatSpreadable, obj], log[0]); |
| 771 assertEquals(["get", target, "length", obj], log[1]); |
| 772 |
| 773 target.length = 3; |
| 774 |
| 775 log.length = 0; |
| 776 assertEquals(["a", "b", undefined], [].concat(obj)); |
| 777 assertEquals(7, log.length); |
| 778 for (var i in log) assertSame(target, log[i][1]); |
| 779 assertEquals(["get", target, Symbol.isConcatSpreadable, obj], log[0]); |
| 780 assertEquals(["get", target, "length", obj], log[1]); |
| 781 assertEquals(["has", target, "0"], log[2]); |
| 782 assertEquals(["get", target, "0", obj], log[3]); |
| 783 assertEquals(["has", target, "1"], log[4]); |
| 784 assertEquals(["get", target, "1", obj], log[5]); |
| 785 assertEquals(["has", target, "2"], log[6]); |
| 786 |
| 787 log.length = 0; |
| 788 assertEquals(["a", "b", undefined], Array.prototype.concat.apply(obj)); |
| 789 assertEquals(7, log.length); |
| 790 for (var i in log) assertSame(target, log[i][1]); |
| 791 assertEquals(["get", target, Symbol.isConcatSpreadable, obj], log[0]); |
| 792 assertEquals(["get", target, "length", obj], log[1]); |
| 793 assertEquals(["has", target, "0"], log[2]); |
| 794 assertEquals(["get", target, "0", obj], log[3]); |
| 795 assertEquals(["has", target, "1"], log[4]); |
| 796 assertEquals(["get", target, "1", obj], log[5]); |
| 797 assertEquals(["has", target, "2"], log[6]); |
| 798 })(); |
| 799 |
| 800 |
| 801 (function testUnspreadableArrayLikeProxy() { |
| 802 var target = ["a", "b"]; |
| 803 target[Symbol.isConcatSpreadable] = ""; |
| 804 var obj = new Proxy(target, handler); |
| 805 |
| 806 log.length = 0; |
| 807 assertEquals([obj], [].concat(obj)); |
| 808 assertEquals(1, log.length); |
| 809 for (var i in log) assertSame(target, log[i][1]); |
| 810 assertEquals(["get", target, Symbol.isConcatSpreadable, obj], log[0]); |
| 811 |
| 812 log.length = 0; |
| 813 assertEquals([obj], Array.prototype.concat.apply(obj)); |
| 814 assertEquals(1, log.length); |
| 815 for (var i in log) assertSame(target, log[i][1]); |
| 816 assertEquals(["get", target, Symbol.isConcatSpreadable, obj], log[0]); |
| 817 })(); |
| 818 |
| 819 |
| 820 (function testSpreadableArrayLikeProxy() { |
| 821 var target = ["a", "b"]; |
| 822 target[Symbol.isConcatSpreadable] = undefined; |
| 823 var obj = new Proxy(target, handler); |
| 824 |
| 825 log.length = 0; |
| 826 assertEquals(["a", "b"], [].concat(obj)); |
| 827 assertEquals(6, log.length); |
| 828 for (var i in log) assertSame(target, log[i][1]); |
| 829 assertEquals(["get", target, Symbol.isConcatSpreadable, obj], log[0]); |
| 830 assertEquals(["get", target, "length", obj], log[1]); |
| 831 assertEquals(["has", target, "0"], log[2]); |
| 832 assertEquals(["get", target, "0", obj], log[3]); |
| 833 assertEquals(["has", target, "1"], log[4]); |
| 834 assertEquals(["get", target, "1", obj], log[5]); |
| 835 |
| 836 log.length = 0; |
| 837 assertEquals(["a", "b"], Array.prototype.concat.apply(obj)); |
| 838 assertEquals(6, log.length); |
| 839 for (var i in log) assertSame(target, log[i][1]); |
| 840 assertEquals(["get", target, Symbol.isConcatSpreadable, obj], log[0]); |
| 841 assertEquals(["get", target, "length", obj], log[1]); |
| 842 assertEquals(["has", target, "0"], log[2]); |
| 843 assertEquals(["get", target, "0", obj], log[3]); |
| 844 assertEquals(["has", target, "1"], log[4]); |
| 845 assertEquals(["get", target, "1", obj], log[5]); |
| 846 })(); |
| 847 |
| 848 |
| 849 (function testSpreadableArrayLikeProxyWithNontrivialLength() { |
| 850 var getTrap = function(t, key) { |
| 851 if (key === "length") return {[Symbol.toPrimitive]() {return 3}}; |
| 852 if (key === "2") return "baz"; |
| 853 if (key === "3") return "bar"; |
| 854 }; |
| 855 var target = []; |
| 856 var obj = new Proxy(target, {get: getTrap, has: () => true}); |
| 857 |
| 858 assertEquals([undefined, undefined, "baz"], [].concat(obj)); |
| 859 assertEquals([undefined, undefined, "baz"], Array.prototype.concat.apply(obj)) |
| 860 })(); |
| 861 |
| 862 |
| 863 (function testSpreadableArrayLikeProxyWithBogusLength() { |
| 864 var getTrap = function(t, key) { |
| 865 if (key === "length") return Symbol(); |
| 866 if (key === "2") return "baz"; |
| 867 if (key === "3") return "bar"; |
| 868 }; |
| 869 var target = []; |
| 870 var obj = new Proxy(target, {get: getTrap, has: () => true}); |
| 871 |
| 872 assertThrows(() => [].concat(obj), TypeError); |
| 873 assertThrows(() => Array.prototype.concat.apply(obj), TypeError); |
| 874 })(); |
OLD | NEW |