• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 function print(arg:any):string;
17var x = [1,2.5,NaN,undefined,null,false,true,"ark"]
18//aot: [trace] aot inline builtin: Array.prototype.find, caller function name:func_main_0@builtinArrayFindFindIndex
19var inlineFind = x.find(x=>{
20  return x === "ark"
21})
22//aot: [trace] aot inline builtin: Array.prototype.find, caller function name:func_main_0@builtinArrayFindFindIndex
23var inlineNotFind = x.find(x=>{
24  return x === "a_rk"
25})
26//aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:func_main_0@builtinArrayFindFindIndex
27var inlineFindIndex = x.findIndex(x=>{
28  return x === "ark"
29})
30//aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:func_main_0@builtinArrayFindFindIndex
31var inlineNotFindIndex = x.findIndex(x=>{
32  return x === "a_rk"
33})
34
35print(inlineFind) //: ark
36print(inlineNotFind) //: undefined
37print(inlineFindIndex) //: 7
38print(inlineNotFindIndex) //: -1
39
40//aot: [trace] aot inline builtin: Array.prototype.find, caller function name:func_main_0@builtinArrayFindFindIndex
41var inlineFindNumber = x.find(x=>{
42  return x == "1"
43})
44//aot: [trace] aot inline builtin: Array.prototype.find, caller function name:func_main_0@builtinArrayFindFindIndex
45var inlineNotFindNumber = x.find(x=>{
46  return x === "1"
47})
48//aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:func_main_0@builtinArrayFindFindIndex
49var inlineFindNumberIndex = x.findIndex(x=>{
50  return x > 1
51})
52//aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:func_main_0@builtinArrayFindFindIndex
53var inlineNotFindNumberIndex = x.findIndex(x=>{
54  return x < false
55})
56
57print(inlineFindNumber) //: 1
58print(inlineNotFindNumber) //: undefined
59print(inlineFindNumberIndex) //: 1
60print(inlineNotFindNumberIndex) //: -1
61
62// Check inside try-block
63try {
64  //aot: [trace] aot inline builtin: Array.prototype.find, caller function name:func_main_0@builtinArrayFindFindIndex
65  print(x.find(x => true)) //: 1
66  //aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:func_main_0@builtinArrayFindFindIndex
67  print(x.findIndex(x => true)) //: 0
68} catch(e) {
69}
70
71// Check without args
72try {
73  //aot: [trace] aot call builtin: Array.prototype.find, caller function name:func_main_0@builtinArrayFindFindIndex
74  print(x.find())
75} catch(e) {
76  print(e) //: TypeError: the predicate is not callable.
77}
78try {
79  //aot: [trace] aot call builtin: Array.prototype.findIndex, caller function name:func_main_0@builtinArrayFindFindIndex
80  print(x.findIndex())
81} catch(e) {
82  print(e) //: TypeError: the predicate is not callable.
83}
84
85//aot: [trace] aot inline builtin: Array.prototype.find, caller function name:func_main_0@builtinArrayFindFindIndex
86print(x.find(() => {})) //: undefined
87//aot: [trace] aot inline builtin: Array.prototype.find, caller function name:func_main_0@builtinArrayFindFindIndex
88print(x.find(x => true)) //: 1
89//aot: [trace] aot inline builtin: Array.prototype.find, caller function name:func_main_0@builtinArrayFindFindIndex
90print(x.find(x => false)) //: undefined
91//aot: [trace] aot inline builtin: Array.prototype.find, caller function name:func_main_0@builtinArrayFindFindIndex
92print(x.find((x,y) => {})) //: undefined
93//aot: [trace] aot inline builtin: Array.prototype.find, caller function name:func_main_0@builtinArrayFindFindIndex
94print(x.find((x,y) => x == true)) //: 1
95//aot: [trace] aot inline builtin: Array.prototype.find, caller function name:func_main_0@builtinArrayFindFindIndex
96print(x.find(x => x == true, 0)) //: 1
97
98//aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:func_main_0@builtinArrayFindFindIndex
99print(x.findIndex(() => {})) //: -1
100//aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:func_main_0@builtinArrayFindFindIndex
101print(x.findIndex(x => true)) //: 0
102//aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:func_main_0@builtinArrayFindFindIndex
103print(x.findIndex(x => false)) //: -1
104//aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:func_main_0@builtinArrayFindFindIndex
105print(x.findIndex((x,y) => {})) //: -1
106//aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:func_main_0@builtinArrayFindFindIndex
107print(x.findIndex((x,y) => x == true)) //: 0
108//aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:func_main_0@builtinArrayFindFindIndex
109print(x.findIndex(x => x == true, 0)) //: 0
110
111
112// Replace standard builtin
113function replace(a : any) {
114  return a;
115}
116
117let newArr = [1, 2]
118let true_find = newArr.find
119let true_findIndex = newArr.findIndex
120newArr.find = replace
121newArr.findIndex = replace
122
123print(newArr.find(x => {})); //: Cannot get source code of funtion
124newArr.find = true_find
125print(newArr.findIndex(x => { return x == 11 })); //: Cannot get source code of funtion
126newArr.findIndex = true_findIndex
127
128//aot: [trace] Check Type: BuiltinInstanceHClassMismatch
129print(newArr.find(x => true)); //: 1
130print(newArr.findIndex(x => true)); //: 0
131
132function findCase1() {
133  print('case 1 find') //: case 1 find
134  let arr1 = [1, 2]
135  let arr2 = [1, 2]
136  arr2.garbage = function(x: any): any {
137    return undefined;
138  }
139  //aot: [trace] aot inline builtin: Array.prototype.find, caller function name:#*#findCase1@builtinArrayFindFindIndex
140  print(arr1.find(x => x == 1)); //: 1
141  print(arr2.find(x => x == 1)); //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
142                                 //: 1
143}
144function findIndexCase1() {
145  print('case 1 findIndex') //: case 1 findIndex
146  let arr1 = [1, 2]
147  let arr2 = [1, 2]
148  arr2.garbage = function(x: any): any {
149    return undefined;
150  }
151  //aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:#*#findIndexCase1@builtinArrayFindFindIndex
152  print(arr1.findIndex(x => x == 1)); //: 0
153  print(arr2.findIndex(x => x == 1)); //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
154                                      //: 0
155}
156findCase1()
157findIndexCase1()
158
159
160function findCase2() {
161  print('case 2 find') //: case 2 find
162  let arr1 = [1, 2]
163  let arr2 = [1, 2]
164  arr2.find = function(x: any) {
165    return x
166  }
167
168  //aot: [trace] aot inline builtin: Object.getPrototypeOf, caller function name:#*#findCase2@builtinArrayFindFindIndex
169  print(Object.getPrototypeOf(arr2) === Array.prototype) //: true
170
171  //aot: [trace] aot inline builtin: Array.prototype.find, caller function name:#*#findCase2@builtinArrayFindFindIndex
172  print(arr1.find(x => x == 1)); //: 1
173  print(arr2.find(x => x == 1)); //: Cannot get source code of funtion
174}
175function findIndexCase2() {
176  print('case 2 findIndex') //: case 2 findIndex
177  let arr1 = [1, 2]
178  let arr2 = [1, 2]
179
180  //aot: [trace] aot inline builtin: Object.getPrototypeOf, caller function name:#*#findIndexCase2@builtinArrayFindFindIndex
181  print(Object.getPrototypeOf(arr2) === Array.prototype) //: true
182  arr2.findIndex = function(x: any) {
183    return x
184  }
185
186  //aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:#*#findIndexCase2@builtinArrayFindFindIndex
187  print(arr1.findIndex(x => x == 1)); //: 0
188  print(arr2.findIndex(x => x == 1)); //: Cannot get source code of funtion
189}
190findCase2()
191findIndexCase2()
192
193
194function findCase3() {
195  print('case 3 find') //: case 3 find
196  let marr = [1, 2]
197  let true_find = marr.find
198  let mimicArray = {
199    find: true_find,
200  }
201
202  //aot: [trace] aot inline builtin: Array.prototype.find, caller function name:#*#findCase3@builtinArrayFindFindIndex
203  print(marr.find(x => x == 1)); //: 1
204  //aot: [trace] aot call builtin: Object.SetPrototypeOf, caller function name:#*#findCase3@builtinArrayFindFindIndex
205  Object.setPrototypeOf(marr, mimicArray)
206
207  print(marr.find(x => x == 1)); //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
208                                 //: 1
209}
210function findIndexCase3() {
211  print('case 3 findIndex') //: case 3 findIndex
212
213  let marr = [1, 2]
214  let true_findIndex = marr.findIndex
215
216  let mimicArray = {
217    findIndex: true_findIndex
218  }
219
220  print(marr.findIndex(x => x == 1)); //aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:#*#findIndexCase3@builtinArrayFindFindIndex
221                                      //: 0
222  //aot: [trace] aot call builtin: Object.SetPrototypeOf, caller function name:#*#findIndexCase3@builtinArrayFindFindIndex
223  Object.setPrototypeOf(marr, mimicArray)
224
225  //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
226  print(marr.findIndex(x => x == 1)); //: 0
227}
228findCase3()
229findIndexCase3()
230
231
232function findCase4() {
233  print('case 4 find') //: case 4 find
234  let arr1 = [1, 2]
235  let arr2 = [1, 2]
236  let notArray = {
237    find(x: any) {
238        return x(0)
239    }
240  }
241  //aot: [trace] aot call builtin: Object.SetPrototypeOf, caller function name:#*#findCase4@builtinArrayFindFindIndex
242  Object.setPrototypeOf(arr2, notArray)
243  //aot: [trace] aot inline builtin: Array.prototype.find, caller function name:#*#findCase4@builtinArrayFindFindIndex
244  print(arr1.find(x => x == 1)); //: 1
245  //aot: [trace] aot inline function name: #*@6*#find@builtinArrayFindFindIndex caller function name: #*#findCase4@builtinArrayFindFindIndex
246  //aot: [trace] aot inline function name: #*@6*#^1@builtinArrayFindFindIndex caller function name: #*@6*#find@builtinArrayFindFindIndex
247  print(arr2.find(x => x == 1)); //: false
248}
249function findIndexCase4() {
250  print('case 4 findIndex') //: case 4 findIndex
251  let arr1 = [1, 2]
252  let arr2 = [1, 2]
253  let notArray = {
254    findIndex(x: any) {
255      return x(0)
256    }
257  }
258  //aot: [trace] aot call builtin: Object.SetPrototypeOf, caller function name:#*#findIndexCase4@builtinArrayFindFindIndex
259  Object.setPrototypeOf(arr2, notArray)
260
261  //aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:#*#findIndexCase4@builtinArrayFindFindIndex
262  print(arr1.findIndex(x => x == 1)); //: 0
263  //aot: [trace] aot inline function name: #*@7*#findIndex@builtinArrayFindFindIndex caller function name: #*#findIndexCase4@builtinArrayFindFindIndex
264  //aot: [trace] aot inline function name: #*@7*#^1@builtinArrayFindFindIndex caller function name: #*@7*#findIndex@builtinArrayFindFindIndex
265  print(arr2.findIndex(x => x == 1)); //: false
266}
267findCase4()
268findIndexCase4()
269
270
271function findCase5() {
272  print('case 5 find') //: case 5 find
273  let arr1 = [1, 2]
274  Array.prototype.find = function(x: any) {
275    return x(1)
276  }
277
278  //aot: [trace] aot inline function name: #*@8*#@builtinArrayFindFindIndex caller function name: #*#findCase5@builtinArrayFindFindIndex
279  //aot: [trace] aot inline function name: #*@8*#^1@builtinArrayFindFindIndex caller function name: #*@8*#@builtinArrayFindFindIndex
280  print(arr1.find(x => x == 1)); //: true
281}
282function findIndexCase5() {
283  print('case 5 findIndex') //: case 5 findIndex
284  let arr1 = [1, 2]
285  Array.prototype.findIndex = function(x: any) {
286    return x(1)
287  }
288
289  //aot: [trace] aot inline function name: #*@9*#@builtinArrayFindFindIndex caller function name: #*#findIndexCase5@builtinArrayFindFindIndex
290  //aot: [trace] aot inline function name: #*@9*#^1@builtinArrayFindFindIndex caller function name: #*@9*#@builtinArrayFindFindIndex
291  print(arr1.findIndex(x => x == 1));  //: true
292}
293findCase5()
294findIndexCase5()