1/* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16/* 17 * @tc.name:arrayjoin 18 * @tc.desc:test Array.join 19 * @tc.type: FUNC 20 * @tc.require: issueI5NO8G 21 */ 22 23// do not modify prototype before this 24{ 25 let arr = new Array(10).fill(1); 26 arr[0] = { 27 toString() { 28 arr.length = 10000; 29 return 2; 30 } 31 } 32 let res = arr.join(); 33 print(res); 34} 35 36{ 37 let arr = new Array(10).fill(1); 38 let res = arr.join({ 39 toString() { 40 arr.length = 10000; 41 return "-"; 42 } 43 }); 44 print(res); 45} 46 47var a = new Array(1).join(" "); 48print(a.length); 49var str1 = JSON.stringify(Array(3).join("0")); 50print(str1); 51var str2 = JSON.stringify(new Array(3).join("0")); 52print(str2); 53const arr = [] 54arr.length = 3 55var str3 = JSON.stringify(arr.join("0")); 56print(str3) 57 58// test circular reference 59var arr1 = [1]; 60arr1.push(arr1); 61arr1.push(arr1); 62print(arr1.toString()); 63print(arr1.toString()); 64 65var arr2 = [1]; 66var arr3 = [2]; 67arr2[10] = arr3; 68arr3[10] = arr2; 69print(arr2.toString()); 70print(arr2.toString()); 71 72var arr4 = [1]; 73var arr5 = [2]; 74var arr6 = [3]; 75arr4.push(arr5); 76arr5.push(arr6); 77arr6.push(arr4); 78print(arr4.toString()); 79print(arr4.toString()); 80 81var arr7 = [ 82 { 83 toLocaleString() { 84 return [1, arr7]; 85 } 86 } 87]; 88print(arr7.toLocaleString()); 89 90var aa = this; 91var bb = {}; 92aa.length = 4294967296; // 2 ^ 32 (max array length + 1) 93try { 94 Array.prototype.join.call(aa,bb) 95} catch (e) { 96 print(e instanceof TypeError); 97} 98 99try { 100 Object.getOwnPropertyDescriptors(Array(1e9).join('c')) 101} catch (e) { 102 print(e instanceof RangeError); 103} 104 105([11])["join"]('쏄'); 106 107let proxy1 = new Proxy([123], {}); 108proxy1.pop(); 109proxy1.toString(); 110proxy1.push(456); 111print(`proxy1: ${proxy1}`); 112 113let proxy2 = new Proxy([123, 456], {}); 114proxy2.pop(); 115proxy2.toString(); 116proxy2.push(456); 117print(`proxy2: ${proxy2}`); 118 119const v5 = new Float32Array(1); 120v5[0] = NaN; 121print(v5.join(String.fromCodePoint(0))); 122 123const v6 = new Float32Array(1); 124v6[0] = NaN; 125v6[1] = NaN; 126print(v6.join(String.fromCodePoint(0))); 127 128const v7 = new Float32Array(2); 129v7[0] = NaN; 130print(v7.join(String.fromCodePoint(0))); 131 132const element = { 133 toString() { 134 Array.prototype[1] = 'b'; 135 return 'a'; 136 } 137}; 138const arr_join = [element, ,'c']; 139print("abc" == arr_join.join('')); 140 141const v9 = new Int16Array(128); 142const v10 = new Int8Array(128); 143v9[11] = [v9, v10]; 144print(v9[11]);