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 */ 15declare interface ArkTools { 16 isAOTCompiled(args: any): boolean; 17} 18declare function print(arg:any):string; 19 20function printCharCodeAt(str, idx) { 21 try { 22 print(str.charCodeAt(idx)) 23 } finally { 24 } 25} 26 27function printCharCodeAtWithoutTry(str, idx) { 28 print(str.charCodeAt(idx)) 29} 30 31 32//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 33//: 97 34print("aaaa".charCodeAt()) 35//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 36//: 97 37print("aaaa".charCodeAt(1,2,3)) 38 39// Sequence String 40let testLineStrUTF8 = "abcdefghijklmnopq" 41let testLineStrUTF16 = "这是一个测试字符串" 42//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 43//: 98 44print(testLineStrUTF8.charCodeAt(1)) 45//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 46//: NaN 47print(testLineStrUTF8.charCodeAt(-1)) 48//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 49//: NaN 50print(testLineStrUTF8.charCodeAt(testLineStrUTF8.length)) 51//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 52//: 26159 53print(testLineStrUTF16.charCodeAt(1)) 54//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 55//: NaN 56print(testLineStrUTF16.charCodeAt(-1)) 57//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 58//: NaN 59print(testLineStrUTF16.charCodeAt(testLineStrUTF16.length)) 60 61// Slice String 62let slicedStringUTF8 = "This is a segment of sliced string, please test charcodeat".slice(15, 30); 63let slicedStringUTF16 = "这是一段Sliced String, 请用这段文字来测试charcodeatxxxxxxxxxxxxxx".slice(10, 30); 64//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 65//: 102 66print(slicedStringUTF8.charCodeAt(4)) 67//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 68//: NaN 69print(slicedStringUTF8.charCodeAt(-1)) 70//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 71//: NaN 72print(slicedStringUTF8.charCodeAt(slicedStringUTF8.length)) 73//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 74//: 105 75print(slicedStringUTF16.charCodeAt(4)) 76//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 77//: NaN 78print(slicedStringUTF16.charCodeAt(-1)) 79//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 80//: NaN 81print(slicedStringUTF16.charCodeAt(slicedStringUTF16.length)) 82 83// Tree String 84let treeStringUTF8 = "This is tree String part A" + "This is tree String part B"; 85let treeStringUTF16 = "这是树形字符串的第一个部分" + "这是树形字符串的第二个部分"; 86// Check path for tree string is not flat 87//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 88//: 32 89print(treeStringUTF8.charCodeAt(4)) 90// Check path for tree string is flat 91//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 92//: 115 93print(treeStringUTF8.charCodeAt(6)) 94//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 95//: NaN 96print(treeStringUTF8.charCodeAt(-1)) 97//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 98//: NaN 99print(treeStringUTF8.charCodeAt(treeStringUTF8.length)) 100//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 101//: 23383 102print(treeStringUTF16.charCodeAt(4)) 103// Check path for tree string is flat 104//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 105//: 20018 106print(treeStringUTF16.charCodeAt(6)) 107//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 108//: NaN 109print(treeStringUTF16.charCodeAt(-1)) 110//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 111//: NaN 112print(treeStringUTF16.charCodeAt(treeStringUTF16.length)) 113 114 115//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:#*#printCharCodeAt@builtinStringCharCodeAt 116//aot: [trace] Check Type: NotInt1 117//: 97 118printCharCodeAt("aaaa", "1") 119 120let badString = {} 121badString.charCodeAt = String.prototype.charCodeAt 122//aot: [trace] Check Type: NotString1 123//: 111 124printCharCodeAt(badString, 1) 125 126function replace(i) 127{ 128 return i; 129} 130let testStr = "abcdefg"; 131// Replace standard builtin 132let true_charCodeAt = String.prototype.charCodeAt 133if (ArkTools.isAOTCompiled(printCharCodeAt)) { 134 String.prototype.charCodeAt = replace 135} 136 137//aot: [trace] Check Type: NotCallTarget1 138//aot: 1 139//pgo: 98 140printCharCodeAt(testStr, 1) 141 142String.prototype.charCodeAt = true_charCodeAt 143 144//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt 145//: 97 146print("aaa".charCodeAt(1)) 147 148//aot: [trace] aot inline function name: #*#printCharCodeAtWithoutTry@builtinStringCharCodeAt caller function name: func_main_0@builtinStringCharCodeAt 149//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:#*#printCharCodeAtWithoutTry@builtinStringCharCodeAt 150//: 97 151printCharCodeAtWithoutTry("aaa", 1) 152 153// Test for AOT only 154if (ArkTools.isAOTCompiled(printCharCodeAt)) { 155 String.prototype.charCodeAt = function() { 156 throw new Error("custom Error") 157 } 158 try { 159 printCharCodeAtWithoutTry(testStr, 1); 160 } catch { 161 //aot: [trace] Check Type: NotCallTarget1 162 //aot: Exception In AOT1 163 print("Exception In AOT1") 164 } 165 166 String.prototype.charCodeAt = true_charCodeAt 167 let symbol = Symbol("aaa") 168 try { 169 printCharCodeAtWithoutTry(symbol, 1); 170 } catch { 171 //aot: [trace] Check Type: BuiltinInstanceHClassMismatch2 172 //aot: Exception In AOT2 173 print("Exception In AOT2") 174 } 175 176 let strNull = null 177 try { 178 printCharCodeAtWithoutTry(strNull, 0) 179 } catch { 180 //aot: [trace] Check Type: NotHeapObject1 181 //aot: Exception In AOT3 182 print("Exception In AOT3") 183 } 184 185 let strUndefine; 186 try { 187 printCharCodeAtWithoutTry(strUndefine, 0) 188 } catch { 189 //aot: [trace] Check Type: NotHeapObject1 190 //aot: Exception In AOT4 191 print("Exception In AOT4") 192 } 193 194 let strTest = "hello"; 195 try { 196 printCharCodeAtWithoutTry(strTest, { toString: function() { throw new Error("Error"); } }) 197 } catch { 198 //aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:#*#printCharCodeAtWithoutTry@builtinStringCharCodeAt 199 //aot: [trace] Check Type: NotInt1 200 //aot: Exception In AOT5 201 print("Exception In AOT5") 202 } 203} 204