14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_cideclare interface ArkTools {
164514f5e3Sopenharmony_ci  isAOTCompiled(args: any): boolean;
174514f5e3Sopenharmony_ci}
184514f5e3Sopenharmony_cideclare function print(arg:any):string;
194514f5e3Sopenharmony_ci
204514f5e3Sopenharmony_cifunction printCharCodeAt(str, idx) {
214514f5e3Sopenharmony_ci  try {
224514f5e3Sopenharmony_ci    print(str.charCodeAt(idx))
234514f5e3Sopenharmony_ci  } finally {
244514f5e3Sopenharmony_ci  }
254514f5e3Sopenharmony_ci}
264514f5e3Sopenharmony_ci
274514f5e3Sopenharmony_cifunction printCharCodeAtWithoutTry(str, idx) {
284514f5e3Sopenharmony_ci  print(str.charCodeAt(idx))
294514f5e3Sopenharmony_ci}
304514f5e3Sopenharmony_ci
314514f5e3Sopenharmony_ci
324514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
334514f5e3Sopenharmony_ci//: 97
344514f5e3Sopenharmony_ciprint("aaaa".charCodeAt())
354514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
364514f5e3Sopenharmony_ci//: 97
374514f5e3Sopenharmony_ciprint("aaaa".charCodeAt(1,2,3))
384514f5e3Sopenharmony_ci
394514f5e3Sopenharmony_ci// Sequence String
404514f5e3Sopenharmony_cilet testLineStrUTF8 = "abcdefghijklmnopq"
414514f5e3Sopenharmony_cilet testLineStrUTF16 = "这是一个测试字符串"
424514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
434514f5e3Sopenharmony_ci//: 98
444514f5e3Sopenharmony_ciprint(testLineStrUTF8.charCodeAt(1))
454514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
464514f5e3Sopenharmony_ci//: NaN
474514f5e3Sopenharmony_ciprint(testLineStrUTF8.charCodeAt(-1))
484514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
494514f5e3Sopenharmony_ci//: NaN
504514f5e3Sopenharmony_ciprint(testLineStrUTF8.charCodeAt(testLineStrUTF8.length))
514514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
524514f5e3Sopenharmony_ci//: 26159
534514f5e3Sopenharmony_ciprint(testLineStrUTF16.charCodeAt(1))
544514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
554514f5e3Sopenharmony_ci//: NaN
564514f5e3Sopenharmony_ciprint(testLineStrUTF16.charCodeAt(-1))
574514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
584514f5e3Sopenharmony_ci//: NaN
594514f5e3Sopenharmony_ciprint(testLineStrUTF16.charCodeAt(testLineStrUTF16.length))
604514f5e3Sopenharmony_ci
614514f5e3Sopenharmony_ci// Slice String
624514f5e3Sopenharmony_cilet slicedStringUTF8 = "This is a segment of sliced string, please test charcodeat".slice(15, 30);
634514f5e3Sopenharmony_cilet slicedStringUTF16 = "这是一段Sliced String, 请用这段文字来测试charcodeatxxxxxxxxxxxxxx".slice(10, 30);
644514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
654514f5e3Sopenharmony_ci//: 102
664514f5e3Sopenharmony_ciprint(slicedStringUTF8.charCodeAt(4))
674514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
684514f5e3Sopenharmony_ci//: NaN
694514f5e3Sopenharmony_ciprint(slicedStringUTF8.charCodeAt(-1))
704514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
714514f5e3Sopenharmony_ci//: NaN
724514f5e3Sopenharmony_ciprint(slicedStringUTF8.charCodeAt(slicedStringUTF8.length))
734514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
744514f5e3Sopenharmony_ci//: 105
754514f5e3Sopenharmony_ciprint(slicedStringUTF16.charCodeAt(4))
764514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
774514f5e3Sopenharmony_ci//: NaN
784514f5e3Sopenharmony_ciprint(slicedStringUTF16.charCodeAt(-1))
794514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
804514f5e3Sopenharmony_ci//: NaN
814514f5e3Sopenharmony_ciprint(slicedStringUTF16.charCodeAt(slicedStringUTF16.length))
824514f5e3Sopenharmony_ci
834514f5e3Sopenharmony_ci// Tree String
844514f5e3Sopenharmony_cilet treeStringUTF8 = "This is tree String part A" + "This is tree String part B";
854514f5e3Sopenharmony_cilet treeStringUTF16 = "这是树形字符串的第一个部分" + "这是树形字符串的第二个部分";
864514f5e3Sopenharmony_ci// Check path for tree string is not flat
874514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
884514f5e3Sopenharmony_ci//: 32
894514f5e3Sopenharmony_ciprint(treeStringUTF8.charCodeAt(4))
904514f5e3Sopenharmony_ci// Check path for tree string is flat
914514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
924514f5e3Sopenharmony_ci//: 115
934514f5e3Sopenharmony_ciprint(treeStringUTF8.charCodeAt(6))
944514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
954514f5e3Sopenharmony_ci//: NaN
964514f5e3Sopenharmony_ciprint(treeStringUTF8.charCodeAt(-1))
974514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
984514f5e3Sopenharmony_ci//: NaN
994514f5e3Sopenharmony_ciprint(treeStringUTF8.charCodeAt(treeStringUTF8.length))
1004514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
1014514f5e3Sopenharmony_ci//: 23383
1024514f5e3Sopenharmony_ciprint(treeStringUTF16.charCodeAt(4))
1034514f5e3Sopenharmony_ci// Check path for tree string is flat
1044514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
1054514f5e3Sopenharmony_ci//: 20018
1064514f5e3Sopenharmony_ciprint(treeStringUTF16.charCodeAt(6))
1074514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
1084514f5e3Sopenharmony_ci//: NaN
1094514f5e3Sopenharmony_ciprint(treeStringUTF16.charCodeAt(-1))
1104514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
1114514f5e3Sopenharmony_ci//: NaN
1124514f5e3Sopenharmony_ciprint(treeStringUTF16.charCodeAt(treeStringUTF16.length))
1134514f5e3Sopenharmony_ci
1144514f5e3Sopenharmony_ci
1154514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:#*#printCharCodeAt@builtinStringCharCodeAt
1164514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotInt1
1174514f5e3Sopenharmony_ci//: 97
1184514f5e3Sopenharmony_ciprintCharCodeAt("aaaa", "1")
1194514f5e3Sopenharmony_ci
1204514f5e3Sopenharmony_cilet badString = {}
1214514f5e3Sopenharmony_cibadString.charCodeAt = String.prototype.charCodeAt
1224514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotString1
1234514f5e3Sopenharmony_ci//: 111
1244514f5e3Sopenharmony_ciprintCharCodeAt(badString, 1)
1254514f5e3Sopenharmony_ci
1264514f5e3Sopenharmony_cifunction replace(i)
1274514f5e3Sopenharmony_ci{
1284514f5e3Sopenharmony_ci  return i;
1294514f5e3Sopenharmony_ci}
1304514f5e3Sopenharmony_cilet testStr = "abcdefg";
1314514f5e3Sopenharmony_ci// Replace standard builtin
1324514f5e3Sopenharmony_cilet true_charCodeAt = String.prototype.charCodeAt
1334514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printCharCodeAt)) {
1344514f5e3Sopenharmony_ci  String.prototype.charCodeAt = replace
1354514f5e3Sopenharmony_ci}
1364514f5e3Sopenharmony_ci
1374514f5e3Sopenharmony_ci//aot: [trace] Check Type: NotCallTarget1
1384514f5e3Sopenharmony_ci//aot: 1
1394514f5e3Sopenharmony_ci//pgo: 98
1404514f5e3Sopenharmony_ciprintCharCodeAt(testStr, 1)
1414514f5e3Sopenharmony_ci
1424514f5e3Sopenharmony_ciString.prototype.charCodeAt = true_charCodeAt
1434514f5e3Sopenharmony_ci
1444514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:func_main_0@builtinStringCharCodeAt
1454514f5e3Sopenharmony_ci//: 97
1464514f5e3Sopenharmony_ciprint("aaa".charCodeAt(1))
1474514f5e3Sopenharmony_ci
1484514f5e3Sopenharmony_ci//aot: [trace] aot inline function name: #*#printCharCodeAtWithoutTry@builtinStringCharCodeAt caller function name: func_main_0@builtinStringCharCodeAt
1494514f5e3Sopenharmony_ci//aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:#*#printCharCodeAtWithoutTry@builtinStringCharCodeAt
1504514f5e3Sopenharmony_ci//: 97
1514514f5e3Sopenharmony_ciprintCharCodeAtWithoutTry("aaa", 1)
1524514f5e3Sopenharmony_ci
1534514f5e3Sopenharmony_ci// Test for AOT only
1544514f5e3Sopenharmony_ciif (ArkTools.isAOTCompiled(printCharCodeAt)) {
1554514f5e3Sopenharmony_ci  String.prototype.charCodeAt = function() {
1564514f5e3Sopenharmony_ci    throw new Error("custom Error")
1574514f5e3Sopenharmony_ci  }
1584514f5e3Sopenharmony_ci  try {
1594514f5e3Sopenharmony_ci    printCharCodeAtWithoutTry(testStr, 1);
1604514f5e3Sopenharmony_ci  } catch {
1614514f5e3Sopenharmony_ci    //aot: [trace] Check Type: NotCallTarget1
1624514f5e3Sopenharmony_ci    //aot: Exception In AOT1
1634514f5e3Sopenharmony_ci    print("Exception In AOT1")
1644514f5e3Sopenharmony_ci  }
1654514f5e3Sopenharmony_ci
1664514f5e3Sopenharmony_ci  String.prototype.charCodeAt = true_charCodeAt
1674514f5e3Sopenharmony_ci  let symbol = Symbol("aaa")
1684514f5e3Sopenharmony_ci  try {
1694514f5e3Sopenharmony_ci    printCharCodeAtWithoutTry(symbol, 1);
1704514f5e3Sopenharmony_ci  } catch {
1714514f5e3Sopenharmony_ci    //aot: [trace] Check Type: BuiltinInstanceHClassMismatch2
1724514f5e3Sopenharmony_ci    //aot: Exception In AOT2
1734514f5e3Sopenharmony_ci    print("Exception In AOT2")
1744514f5e3Sopenharmony_ci  }
1754514f5e3Sopenharmony_ci
1764514f5e3Sopenharmony_ci  let strNull = null
1774514f5e3Sopenharmony_ci  try {
1784514f5e3Sopenharmony_ci    printCharCodeAtWithoutTry(strNull, 0)
1794514f5e3Sopenharmony_ci  } catch {
1804514f5e3Sopenharmony_ci    //aot: [trace] Check Type: NotHeapObject1
1814514f5e3Sopenharmony_ci    //aot: Exception In AOT3
1824514f5e3Sopenharmony_ci    print("Exception In AOT3")
1834514f5e3Sopenharmony_ci  }
1844514f5e3Sopenharmony_ci
1854514f5e3Sopenharmony_ci  let strUndefine;
1864514f5e3Sopenharmony_ci  try {
1874514f5e3Sopenharmony_ci    printCharCodeAtWithoutTry(strUndefine, 0)
1884514f5e3Sopenharmony_ci  } catch {
1894514f5e3Sopenharmony_ci    //aot: [trace] Check Type: NotHeapObject1
1904514f5e3Sopenharmony_ci    //aot: Exception In AOT4
1914514f5e3Sopenharmony_ci    print("Exception In AOT4")
1924514f5e3Sopenharmony_ci  }
1934514f5e3Sopenharmony_ci
1944514f5e3Sopenharmony_ci  let strTest = "hello";
1954514f5e3Sopenharmony_ci  try {
1964514f5e3Sopenharmony_ci    printCharCodeAtWithoutTry(strTest, { toString: function() { throw new Error("Error"); } })
1974514f5e3Sopenharmony_ci  } catch {
1984514f5e3Sopenharmony_ci    //aot: [trace] aot inline builtin: String.prototype.charCodeAt, caller function name:#*#printCharCodeAtWithoutTry@builtinStringCharCodeAt
1994514f5e3Sopenharmony_ci    //aot: [trace] Check Type: NotInt1
2004514f5e3Sopenharmony_ci    //aot: Exception In AOT5
2014514f5e3Sopenharmony_ci    print("Exception In AOT5")
2024514f5e3Sopenharmony_ci  }
2034514f5e3Sopenharmony_ci}
204