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