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; 20function replace(a : number) 21{ 22 return a; 23} 24 25function doValues(x : any) { 26 return myMap.values(x); 27} 28 29function printValues(x : any) { 30 try { 31 print(doValues(x)); 32 } finally { 33 } 34} 35 36let myMap = new Map([[0, 0], [0.0, 5], [-1, 1], [2.5, -2.5], [NaN, Infinity], [2000, 0.0], [56, "oops"], ["xyz", "12345"]]); 37 38// Check without params 39//aot: [trace] aot call builtin: Map.values, caller function name:func_main_0@builtinMapValues 40print(myMap.values()); //: [object Map Iterator] 41 42// Check with single param 43//aot: [trace] aot call builtin: Map.values, caller function name:func_main_0@builtinMapValues 44//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 45print(myMap.values(0).next().value); //: 5 46 47// Check with 2 params 48//aot: [trace] aot call builtin: Map.values, caller function name:func_main_0@builtinMapValues 49//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 50print(myMap.values(0, 0).next().value); //: 5 51 52// Check with 3 params 53//aot: [trace] aot call builtin: Map.values, caller function name:func_main_0@builtinMapValues 54//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 55print(myMap.values(-1, 10.2, 15).next().value); //: 5 56 57// Check own methods 58//aot: [trace] aot call builtin: Map.values, caller function name:func_main_0@builtinMapValues 59print(myMap.values().throw); //: function throw() { [native code] } 60//aot: [trace] aot call builtin: Map.values, caller function name:func_main_0@builtinMapValues 61print(myMap.values().return); //: function return() { [native code] } 62 63// Check using in loop 64//aot: [trace] aot call builtin: Map.values, caller function name:func_main_0@builtinMapValues 65for (let key of myMap.values()) { 66 print(key); 67} 68//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 69//: 5 70//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 71//: 1 72//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 73//: -2.5 74//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 75//: Infinity 76//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 77//: 0 78//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 79//: oops 80//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 81//: 12345 82//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 83 84// Replace standard builtin 85let true_values = myMap.values 86myMap.values = replace 87 88// no deopt 89print(myMap.values(2.5)); //: 2.5 90myMap.values = true_values 91 92if (ArkTools.isAOTCompiled(printValues)) { 93 // Replace standard builtin after call to standard builtin was profiled 94 myMap.values = replace 95} 96printValues(2.5); //pgo: [object Map Iterator] 97//aot: [trace] Check Type: NotCallTarget1 98//aot: 2.5 99 100printValues("abc"); //pgo: [object Map Iterator] 101//aot: [trace] Check Type: NotCallTarget1 102//aot: abc 103 104myMap.values = true_values 105 106// Check IR correctness inside try-block 107try { 108 //aot: [trace] aot call builtin: Map.values, caller function name:#*#doValues@builtinMapValues 109 printValues(2.5); //: [object Map Iterator] 110 //aot: [trace] aot call builtin: Map.values, caller function name:#*#doValues@builtinMapValues 111 printValues("abc"); //: [object Map Iterator] 112} catch (e) { 113} 114 115// Check using in a loop 116//aot: [trace] aot call builtin: Map.values, caller function name:func_main_0@builtinMapValues 117let iter1 = myMap.values(); 118for (let key of iter1) { 119 print(key); 120} 121//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 122//: 5 123//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 124//: 1 125//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 126//: -2.5 127//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 128//: Infinity 129//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 130//: 0 131//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 132//: oops 133//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 134//: 12345 135//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 136 137// Check reusing possibility 138for (let key of iter1) { 139 //aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 140 print(key); 141} // <nothing> 142 143// Check using out of boundaries 144//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 145print(iter1.next().value); //: undefined 146 147// Check using after inserting / deleting 148//aot: [trace] aot call builtin: Map.values, caller function name:func_main_0@builtinMapValues 149let iter2 = myMap.values(); 150//aot: [trace] aot inline builtin: Map.delete, caller function name:func_main_0@builtinMapValues 151myMap.delete(NaN); 152//aot: [trace] aot call builtin: Map.Set, caller function name:func_main_0@builtinMapValues 153myMap.set(2000, 1e-98); 154//aot: [trace] aot call builtin: Map.Set, caller function name:func_main_0@builtinMapValues 155myMap.set("xyz", -100); 156for (let key of iter2) { 157 print(key); 158} 159//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 160//: 5 161//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 162//: 1 163//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 164//: -2.5 165//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 166//: 1e-98 167//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 168//: oops 169//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues 170//: -100 171//aot: [trace] aot call builtin: MapIterator.next, caller function name:func_main_0@builtinMapValues