OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // Flags: --harmony-observation --harmony-proxies --harmony-collections | 28 // Flags: --harmony-observation --harmony-proxies --harmony-collections |
| 29 // Flags: --allow-natives-syntax |
29 | 30 |
30 var allObservers = []; | 31 var allObservers = []; |
31 function reset() { | 32 function reset() { |
32 allObservers.forEach(function(observer) { observer.reset(); }); | 33 allObservers.forEach(function(observer) { observer.reset(); }); |
33 } | 34 } |
34 | 35 |
35 function stringifyNoThrow(arg) { | 36 function stringifyNoThrow(arg) { |
36 try { | 37 try { |
37 return JSON.stringify(arg); | 38 return JSON.stringify(arg); |
38 } catch (e) { | 39 } catch (e) { |
(...skipping 825 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
864 ]); | 865 ]); |
865 | 866 |
866 // Function.prototype should not be observable except on the object itself | 867 // Function.prototype should not be observable except on the object itself |
867 reset(); | 868 reset(); |
868 var fun = function(){}; | 869 var fun = function(){}; |
869 var obj = { __proto__: fun }; | 870 var obj = { __proto__: fun }; |
870 Object.observe(obj, observer.callback); | 871 Object.observe(obj, observer.callback); |
871 obj.prototype = 7; | 872 obj.prototype = 7; |
872 Object.deliverChangeRecords(observer.callback); | 873 Object.deliverChangeRecords(observer.callback); |
873 observer.assertNotCalled(); | 874 observer.assertNotCalled(); |
| 875 |
| 876 |
| 877 // Check that changes in observation status are detected in all IC states and |
| 878 // in optimized code, especially in cases usually using fast elements. |
| 879 function TestFastElements(prepopulate, polymorphic, optimize) { |
| 880 var setElement = eval( |
| 881 "(function setElement(a, i, v) { a[i] = v " + |
| 882 "/* " + prepopulate + " " + polymorphic + " " + optimize + " */" + |
| 883 "})" |
| 884 ); |
| 885 print("TestFastElements:", setElement); |
| 886 |
| 887 var arr = prepopulate ? [1, 2, 3, 4, 5] : [0]; |
| 888 setElement(arr, 1, 210); |
| 889 setElement(arr, 1, 211); |
| 890 if (polymorphic) setElement(["M", "i", "l", "n", "e", "r"], 0, "m"); |
| 891 if (optimize) %OptimizeFunctionOnNextCall(setElement); |
| 892 setElement(arr, 1, 212); |
| 893 |
| 894 reset(); |
| 895 Object.observe(arr, observer.callback); |
| 896 setElement(arr, 1, 989898); |
| 897 Object.deliverChangeRecords(observer.callback); |
| 898 observer.assertCallbackRecords([ |
| 899 { object: arr, name: '1', type: 'updated', oldValue: 212 } |
| 900 ]); |
| 901 } |
| 902 |
| 903 for (var b1 = 0; b1 < 2; ++b1) |
| 904 for (var b2 = 0; b2 < 2; ++b2) |
| 905 for (var b3 = 0; b3 < 2; ++b3) |
| 906 TestFastElements(b1 != 0, b2 != 0, b3 != 0); |
| 907 |
| 908 |
| 909 function TestFastElementsLength(polymorphic, optimize, oldSize, newSize) { |
| 910 var setLength = eval( |
| 911 "(function setLength(a, n) { a.length = n " + |
| 912 "/* " + polymorphic + " " + optimize + " " + oldSize + " " + newSize + " */" |
| 913 + "})" |
| 914 ); |
| 915 print("TestFastElementsLength:", setLength); |
| 916 |
| 917 function array(n) { |
| 918 var arr = new Array(n); |
| 919 for (var i = 0; i < n; ++i) arr[i] = i; |
| 920 return arr; |
| 921 } |
| 922 |
| 923 setLength(array(oldSize), newSize); |
| 924 setLength(array(oldSize), newSize); |
| 925 if (polymorphic) setLength(array(oldSize).map(isNaN), newSize); |
| 926 if (optimize) %OptimizeFunctionOnNextCall(setLength); |
| 927 setLength(array(oldSize), newSize); |
| 928 |
| 929 reset(); |
| 930 var arr = array(oldSize); |
| 931 Object.observe(arr, observer.callback); |
| 932 setLength(arr, newSize); |
| 933 Object.deliverChangeRecords(observer.callback); |
| 934 if (oldSize === newSize) { |
| 935 observer.assertNotCalled(); |
| 936 } else { |
| 937 var count = oldSize > newSize ? oldSize - newSize : 0; |
| 938 observer.assertRecordCount(count + 1); |
| 939 var lengthRecord = observer.records[count]; |
| 940 assertSame(arr, lengthRecord.object); |
| 941 assertEquals('length', lengthRecord.name); |
| 942 assertEquals('updated', lengthRecord.type); |
| 943 assertSame(oldSize, lengthRecord.oldValue); |
| 944 } |
| 945 } |
| 946 |
| 947 for (var b1 = 0; b1 < 2; ++b1) |
| 948 for (var b2 = 0; b2 < 2; ++b2) |
| 949 for (var n1 = 0; n1 < 3; ++n1) |
| 950 for (var n2 = 0; n2 < 3; ++n2) |
| 951 TestFastElementsLength(b1 != 0, b2 != 0, 20*n1, 20*n2); |
OLD | NEW |