1/* 2 * Copyright (c) 2024 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 16declare interface ArkTools { 17 isAOTCompiled(args: any): boolean; 18} 19declare function print(arg:any):string; 20let arr = ["a",2,3,4]; 21//aot: [trace] aot call builtin: Array.prototype.push, caller function name:func_main_0@builtinArrayPush 22print(arr.push()) //: 4 23 24function testArrayPushagrs1() { 25 print("testArrayPushagrs1") //: testArrayPushagrs1 26 //aot: [trace] aot inline builtin: Array.prototype.push, caller function name:#*#testArrayPushagrs1@builtinArrayPush 27 let arr = ["a",2,3,4]; 28 let x = arr.push("s"); 29 print(arr.length) //: 5 30 print(arr[4]) //: s 31 print(x) //: 5 32 33 let arr2 = new Array() 34 //aot: [trace] aot inline builtin: Array.prototype.push, caller function name:#*#testArrayPushagrs1@builtinArrayPush 35 let y = arr2.push(1); 36 print(y) //: 1 37} 38testArrayPushagrs1(); 39 40//aot: [trace] aot inline function name: #*#testArrayPushagrs2@builtinArrayPush caller function name: func_main_0@builtinArrayPush 41function testArrayPushagrs2() { 42 print("testArrayPushagrs2") //: testArrayPushagrs2 43 let arr = ["a",2,3,4]; 44 //aot: [trace] aot call builtin: Array.prototype.push, caller function name:#*#testArrayPushagrs2@builtinArrayPush 45 print(arr.push(1,2)) //: 6 46 print(arr[4]) //: 1 47 print(arr[5]) //: 2 48} 49testArrayPushagrs2() 50//aot: [trace] aot inline function name: #*#testArrayPushNotStableArray@builtinArrayPush caller function name: func_main_0@builtinArrayPush 51function testArrayPushNotStableArray() { 52 print("testArrayPushNotStableArray") //: testArrayPushNotStableArray 53 let arr = new Array(1025); 54 //aot: [trace] aot inline builtin: Array.prototype.push, caller function name:#*#testArrayPushNotStableArray@builtinArrayPush 55 //aot: [trace] Check Type: NotStableArray2 56 print(arr.push(0)); //: 1026 57} 58testArrayPushNotStableArray() 59 60 61function testPushToNonWritableLength() { 62 print("testPushToNonWritableLength") //: testPushToNonWritableLength 63 var arr_push = []; 64 Object.defineProperty(arr_push, "length", { writable : false}); 65 try { 66 //aot: [trace] aot inline builtin: Array.prototype.push, caller function name:#*#testPushToNonWritableLength@builtinArrayPush 67 //aot: [trace] Check Type: BuiltinInstanceHClassMismatch 68 arr_push.push("5"); 69 } catch (e) { 70 print(e instanceof TypeError); //: true 71 } 72} 73testPushToNonWritableLength(); 74 75 76 77 78function testArrayPushNotStableArray4() { 79 print("testArrayPushNotStableArray4") //: testArrayPushNotStableArray4 80 var arr = new Array(1023) 81 for(let i = 0; i < 5; i++){ 82 //aot: [trace] aot inline builtin: Array.prototype.push, caller function name:#*#testArrayPushNotStableArray4@builtinArrayPush 83 //aot: [trace] aot inline builtin: Array.prototype.push, caller function name:#*#testArrayPushNotStableArray4@builtinArrayPush 84 //aot: [trace] aot inline builtin: Array.prototype.push, caller function name:#*#testArrayPushNotStableArray4@builtinArrayPush 85 //aot: [trace] aot inline builtin: Array.prototype.push, caller function name:#*#testArrayPushNotStableArray4@builtinArrayPush 86 //aot: [trace] aot inline builtin: Array.prototype.push, caller function name:#*#testArrayPushNotStableArray4@builtinArrayPush 87 arr.push(i); 88 } 89 //aot: [trace] aot call builtin: Array.prototype.push, caller function name:#*#testArrayPushNotStableArray4@builtinArrayPush 90 print(arr.push()) //: 1028 91} 92testArrayPushNotStableArray4() 93 94 95 96function testArrayPushException(){ 97 print("testArrayPushException") //: testArrayPushException 98 Object.freeze(arr); 99 let error = null; 100 try { 101 //aot: [trace] aot inline builtin: Array.prototype.push, caller function name:#*#testArrayPushException@builtinArrayPush 102 //aot: [trace] Check Type: NotStableArray2 103 arr.push(1); 104 } catch (e) { 105 error = e; 106 print("exception happend") //: exception happend 107 } 108 } 109testArrayPushException() 110 111function testArrayPushMaxLength(){ 112 print("testArrayPushMaxLength") //: testArrayPushMaxLength 113 let arr = []; 114 //aot: [trace] aot inline builtin: Math.pow, caller function name:#*#testArrayPushMaxLength@builtinArrayPush 115 arr.length = Math.pow(2, 32) - 1; // 设置数组长度为最大值 116 try { 117 //aot: [trace] aot inline builtin: Array.prototype.push, caller function name:#*#testArrayPushMaxLength@builtinArrayPush 118 //aot: [trace] Check Type: NotStableArray2 119 arr.push(1); 120 } catch (e) { 121 print("exception happend") //: exception happend 122 } 123 } 124 testArrayPushMaxLength() 125 126 function testArrayLikePush(){ 127 print("testArrayLikePush"); //: testArrayLikePush 128 let arrayLike = { 129 length: 0, 130 0: "initialValue" 131 }; 132 133 try { 134 //aot: [trace] aot call builtin: Function.prototype.call, caller function name:#*#testArrayLikePush@builtinArrayPush 135 Array.prototype.push.call(arrayLike, 1); 136 //aot: [trace] aot call builtin: Function.prototype.call, caller function name:#*#testArrayLikePush@builtinArrayPush 137 Array.prototype.push.call(arrayLike, 2); 138 if (arrayLike.length === 2 && arrayLike[0] === 1 && arrayLike[1] === 2) { 139 print("Array-like push succeeded"); //: Array-like push succeeded 140 } else { 141 print("Array-like push failed"); 142 } 143 } catch (e) { 144 print("exception happened"); 145 } 146} 147testArrayLikePush() 148 149function testArrayPushAsProto(){ 150 print("testArrayPushAsProto") //: testArrayPushAsProto 151 //aot: [trace] aot inline builtin: Array.prototype.push, caller function name:#*#testArrayPushAsProto@builtinArrayPush 152 //aot: [trace] aot inline builtin: Array.prototype.push, caller function name:#*#testArrayPushAsProto@builtinArrayPush 153 var arr = new Array() 154 arr.push(0) 155 let temp = new Array(); 156 temp.__proto__ = arr; 157 print(arr.push(0)) //: 2 158 //aot: [trace] aot inline builtin: Array.prototype.push, caller function name:#*#testArrayPushAsProto@builtinArrayPush 159 //aot: [trace] Check Type: BuiltinInstanceHClassMismatch 160 print(temp.push(0)) //: 1 161} 162testArrayPushAsProto() 163 164function testArrayPushNotStableArray2() { 165 print("testArrayPushNotStableArray2") //: testArrayPushNotStableArray2 166 var arr = [,]; 167 arr.__proto__.push(0); 168 //aot: [trace] aot inline builtin: Array.prototype.push, caller function name:#*#testArrayPushNotStableArray2@builtinArrayPush 169 //aot: [trace] Check Type: BuiltinInstanceHClassMismatch 170 print(arr.push(0)); //: 2 171} 172testArrayPushNotStableArray2() 173 174function testArrayPushNotStableArray3() { 175 print("testArrayPushNotStableArray3") //: testArrayPushNotStableArray3 176 var arr = [,]; 177 let k = function push() { 178 return arr.push(1); 179 } 180 //aot: [trace] aot inline builtin: Array.prototype.push, caller function name:#*#testArrayPushNotStableArray3@builtinArrayPush 181 //aot: [trace] Check Type: NotStableArray1 182 arr.__proto__.push(0); 183 //aot: [trace] Check Type: NotStableArray1 184 print(k()); //: 2 185} 186testArrayPushNotStableArray3()