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  print(x.find())
74} catch(e) {
75  print(e) //: TypeError: the predicate is not callable.
76}
77try {
78  print(x.findIndex())
79} catch(e) {
80  print(e) //: TypeError: the predicate is not callable.
81}
82
83//aot: [trace] aot inline builtin: Array.prototype.find, caller function name:func_main_0@builtinArrayFindFindIndex
84print(x.find(() => {})) //: undefined
85//aot: [trace] aot inline builtin: Array.prototype.find, caller function name:func_main_0@builtinArrayFindFindIndex
86print(x.find(x => true)) //: 1
87//aot: [trace] aot inline builtin: Array.prototype.find, caller function name:func_main_0@builtinArrayFindFindIndex
88print(x.find(x => false)) //: undefined
89//aot: [trace] aot inline builtin: Array.prototype.find, caller function name:func_main_0@builtinArrayFindFindIndex
90print(x.find((x,y) => {})) //: undefined
91//aot: [trace] aot inline builtin: Array.prototype.find, caller function name:func_main_0@builtinArrayFindFindIndex
92print(x.find((x,y) => x == true)) //: 1
93//aot: [trace] aot inline builtin: Array.prototype.find, caller function name:func_main_0@builtinArrayFindFindIndex
94print(x.find(x => x == true, 0)) //: 1
95
96//aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:func_main_0@builtinArrayFindFindIndex
97print(x.findIndex(() => {})) //: -1
98//aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:func_main_0@builtinArrayFindFindIndex
99print(x.findIndex(x => true)) //: 0
100//aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:func_main_0@builtinArrayFindFindIndex
101print(x.findIndex(x => false)) //: -1
102//aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:func_main_0@builtinArrayFindFindIndex
103print(x.findIndex((x,y) => {})) //: -1
104//aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:func_main_0@builtinArrayFindFindIndex
105print(x.findIndex((x,y) => x == true)) //: 0
106//aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:func_main_0@builtinArrayFindFindIndex
107print(x.findIndex(x => x == true, 0)) //: 0
108
109
110// Replace standard builtin
111function replace(a : any) {
112  return a;
113}
114
115let newArr = [1, 2]
116let true_find = newArr.find
117let true_findIndex = newArr.findIndex
118newArr.find = replace
119newArr.findIndex = replace
120
121print(newArr.find(x => {})); //: Cannot get source code of funtion
122newArr.find = true_find
123print(newArr.findIndex(x => { return x == 11 })); //: Cannot get source code of funtion
124newArr.findIndex = true_findIndex
125
126//aot: [trace] Check Type: BuiltinInstanceHClassMismatch
127print(newArr.find(x => true)); //: 1
128print(newArr.findIndex(x => true)); //: 0
129
130function findCase1() {
131  print('case 1 find') //: case 1 find
132  let arr1 = [1, 2]
133  let arr2 = [1, 2]
134  arr2.garbage = function(x: any): any {
135    return undefined;
136  }
137  //aot: [trace] aot inline builtin: Array.prototype.find, caller function name:#*#findCase1@builtinArrayFindFindIndex
138  print(arr1.find(x => x == 1)); //: 1
139  print(arr2.find(x => x == 1)); //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
140                                 //: 1
141}
142function findIndexCase1() {
143  print('case 1 findIndex') //: case 1 findIndex
144  let arr1 = [1, 2]
145  let arr2 = [1, 2]
146  arr2.garbage = function(x: any): any {
147    return undefined;
148  }
149  //aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:#*#findIndexCase1@builtinArrayFindFindIndex
150  print(arr1.findIndex(x => x == 1)); //: 0
151  print(arr2.findIndex(x => x == 1)); //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
152                                      //: 0
153}
154findCase1()
155findIndexCase1()
156
157
158function findCase2() {
159  print('case 2 find') //: case 2 find
160  let arr1 = [1, 2]
161  let arr2 = [1, 2]
162  arr2.find = function(x: any) {
163    return x
164  }
165
166  //aot: [trace] aot inline builtin: Object.getPrototypeOf, caller function name:#*#findCase2@builtinArrayFindFindIndex
167  print(Object.getPrototypeOf(arr2) === Array.prototype) //: true
168
169  //aot: [trace] aot inline builtin: Array.prototype.find, caller function name:#*#findCase2@builtinArrayFindFindIndex
170  print(arr1.find(x => x == 1)); //: 1
171  print(arr2.find(x => x == 1)); //: Cannot get source code of funtion
172}
173function findIndexCase2() {
174  print('case 2 findIndex') //: case 2 findIndex
175  let arr1 = [1, 2]
176  let arr2 = [1, 2]
177
178  //aot: [trace] aot inline builtin: Object.getPrototypeOf, caller function name:#*#findIndexCase2@builtinArrayFindFindIndex
179  print(Object.getPrototypeOf(arr2) === Array.prototype) //: true
180  arr2.findIndex = function(x: any) {
181    return x
182  }
183
184  //aot: [trace] aot inline builtin: Array.prototype.findIndex, caller function name:#*#findIndexCase2@builtinArrayFindFindIndex
185  print(arr1.findIndex(x => x == 1)); //: 0
186  print(arr2.findIndex(x => x == 1)); //: Cannot get source code of funtion
187}
188findCase2()
189findIndexCase2()
190
191
192function findCase3() {
193  print('case 3 find') //: case 3 find
194  let marr = [1, 2]
195  let true_find = marr.find
196  let mimicArray = {
197    find: true_find,
198  }
199
200  //aot: [trace] aot inline builtin: Array.prototype.find, caller function name:#*#findCase3@builtinArrayFindFindIndex
201  print(marr.find(x => x == 1)); //: 1
202  Object.setPrototypeOf(marr, mimicArray)
203
204  print(marr.find(x => x == 1)); //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
205                                 //: 1
206}
207function findIndexCase3() {
208  print('case 3 findIndex') //: case 3 findIndex
209
210  let marr = [1, 2]
211  let true_findIndex = marr.findIndex
212
213  let mimicArray = {
214    findIndex: true_findIndex
215  }
216
217  print(marr.findIndex(x => x == 1)); //aot: [trace] Check Type: NotStableArray1
218                                      //: 0
219
220  Object.setPrototypeOf(marr, mimicArray)
221
222  print(marr.findIndex(x => x == 1)); //: 0
223}
224findCase3()
225findIndexCase3()
226
227
228function findCase4() {
229  print('case 4 find') //: case 4 find
230  let arr1 = [1, 2]
231  let arr2 = [1, 2]
232  let notArray = {
233    find(x: any) {
234        return x(0)
235    }
236  }
237  Object.setPrototypeOf(arr2, notArray)
238
239  //aot: [trace] Check Type: NotStableArray1
240  print(arr1.find(x => x == 1)); //: 1
241  //aot: [trace] aot inline function name: #*@6*#^1@builtinArrayFindFindIndex caller function name: #*@6*#find@builtinArrayFindFindIndex
242  print(arr2.find(x => x == 1)); //: false
243}
244function findIndexCase4() {
245  print('case 4 findIndex') //: case 4 findIndex
246  let arr1 = [1, 2]
247  let arr2 = [1, 2]
248  let notArray = {
249    findIndex(x: any) {
250      return x(0)
251    }
252  }
253  Object.setPrototypeOf(arr2, notArray)
254
255  //aot: [trace] Check Type: NotStableArray1
256  print(arr1.findIndex(x => x == 1)); //: 0
257  //aot: [trace] aot inline function name: #*@7*#^1@builtinArrayFindFindIndex caller function name: #*@7*#findIndex@builtinArrayFindFindIndex
258  print(arr2.findIndex(x => x == 1)); //: false
259}
260findCase4()
261findIndexCase4()
262
263
264function findCase5() {
265  print('case 5 find') //: case 5 find
266  let arr1 = [1, 2]
267  Array.prototype.find = function(x: any) {
268    return x(1)
269  }
270
271  //aot: [trace] Check Type: NotStableArray1
272  //aot: [trace] aot inline function name: #*@8*#^1@builtinArrayFindFindIndex caller function name: #*@8*#@builtinArrayFindFindIndex
273  print(arr1.find(x => x == 1)); //: true
274}
275function findIndexCase5() {
276  print('case 5 findIndex') //: case 5 findIndex
277  let arr1 = [1, 2]
278  Array.prototype.findIndex = function(x: any) {
279    return x(1)
280  }
281
282  //aot: [trace] Check Type: NotStableArray1
283  //aot: [trace] aot inline function name: #*@9*#^1@builtinArrayFindFindIndex caller function name: #*@9*#@builtinArrayFindFindIndex
284  print(arr1.findIndex(x => x == 1));  //: true
285}
286findCase5()
287findIndexCase5()