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 */ 22var a = new Array(1).join(" "); 23print(a.length); 24var str1 = JSON.stringify(Array(3).join("0")); 25print(str1); 26var str2 = JSON.stringify(new Array(3).join("0")); 27print(str2); 28const arr = [] 29arr.length = 3 30var str3 = JSON.stringify(arr.join("0")); 31print(str3) 32 33// test circular reference 34var arr1 = [1]; 35arr1.push(arr1); 36arr1.push(arr1); 37print(arr1.toString()); 38print(arr1.toString()); 39 40var arr2 = [1]; 41var arr3 = [2]; 42arr2[10] = arr3; 43arr3[10] = arr2; 44print(arr2.toString()); 45print(arr2.toString()); 46 47var arr4 = [1]; 48var arr5 = [2]; 49var arr6 = [3]; 50arr4.push(arr5); 51arr5.push(arr6); 52arr6.push(arr4); 53print(arr4.toString()); 54print(arr4.toString()); 55 56var arr7 = [ 57 { 58 toLocaleString() { 59 return [1, arr7]; 60 } 61 } 62]; 63print(arr7.toLocaleString()); 64 65var aa = this; 66var bb = {}; 67aa.length = 4294967296; // 2 ^ 32 (max array length + 1) 68try { 69 Array.prototype.join.call(aa,bb) 70} catch (e) { 71 print(e instanceof TypeError); 72} 73 74try { 75 Object.getOwnPropertyDescriptors(Array(1e9).join('c')) 76} catch (e) { 77 print(e instanceof RangeError); 78} 79 80([11])["join"]('쏄'); 81 82let proxy1 = new Proxy([123], {}); 83proxy1.pop(); 84proxy1.toString(); 85proxy1.push(456); 86print(`proxy1: ${proxy1}`); 87 88let proxy2 = new Proxy([123, 456], {}); 89proxy2.pop(); 90proxy2.toString(); 91proxy2.push(456); 92print(`proxy2: ${proxy2}`); 93 94const v5 = new Float32Array(1); 95v5[0] = NaN; 96print(v5.join(String.fromCodePoint(0))); 97 98const v6 = new Float32Array(1); 99v6[0] = NaN; 100v6[1] = NaN; 101print(v6.join(String.fromCodePoint(0))); 102 103const v7 = new Float32Array(2); 104v7[0] = NaN; 105print(v7.join(String.fromCodePoint(0))); 106 107const element = { 108 toString() { 109 Array.prototype[1] = 'b'; 110 return 'a'; 111 } 112}; 113const arr_join = [element, ,'c']; 114print("abc" == arr_join.join('')); 115 116{ 117 const a = [1,2,3]; 118 let callCount = 0; 119 const sep = { 120 toString() { 121 callCount++; 122 return a.join('-'); 123 } 124 }; 125 print(a.join(sep)); 126 print(callCount); 127 // Verify cycle detection works properly after nested call 128 print(a.join()); 129} 130 131{ 132 const a = [1,2,3]; 133 let callCount = 0; 134 print(a.join({ 135 toString() { 136 callCount++; 137 Object.defineProperty(a, '0', { 138 get(){ return 666; } 139 }); 140 return ','; 141 } 142 })); 143 print(callCount); 144 print(a.join()); 145} 146 147{ 148 const a = [1,2]; 149 a.length = 3; 150 let callCount = 0; 151 print(a.join({ 152 toString() { 153 callCount++; 154 a.__proto__ = { '2': 4 }; 155 return ','; 156 } 157 })); 158 print(callCount); 159 a.__proto__ = Array.prototype; 160 print(a.join()); 161} 162 163{ 164 const a = [1,2,3]; 165 let callCount = 0; 166 print(a.join({ 167 toString() { 168 callCount++; 169 a.pop(); 170 Object.defineProperty(Array.prototype, '2', { 171 get(){ return 777; } 172 }); 173 return ','; 174 } 175 })); 176 print(callCount); 177 print(a.join()); 178} 179 180const v9 = new Int16Array(128); 181const v10 = new Int8Array(128); 182v9[11] = [v9, v10]; 183print(v9[11]);