• 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]
18function foo(ele){
19    print(ele)
20    if (ele != 1 || ele != true){
21        return true
22    }
23    return false
24}
25//aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:func_main_0@builtinArrayForEach
26x.forEach(ele=>{
27    print(ele)
28    if (ele != 1 || ele != true){
29        return true
30    }
31})
32//: 1
33//: 2.5
34//: NaN
35//: undefined
36//: null
37//: false
38//: true
39//aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:func_main_0@builtinArrayForEach
40x.forEach(foo)
41//: 1
42//: 2.5
43//: NaN
44//: undefined
45//: null
46//: false
47//: true
48
49// Check without args
50try {
51    //aot: [trace] aot call builtin: Array.prototype.foreach, caller function name:func_main_0@builtinArrayForEach
52    print(x.forEach())
53} catch(e) {
54    print(e) //: TypeError: the callbackfun is not callable.
55}
56
57//aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:func_main_0@builtinArrayForEach
58print(x.forEach(() => {})) //: undefined
59//aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:func_main_0@builtinArrayForEach
60print(x.forEach(x => true)) //: undefined
61//aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:func_main_0@builtinArrayForEach
62print(x.forEach(x => false)) //: undefined
63//aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:func_main_0@builtinArrayForEach
64print(x.forEach((x,y) => {})) //: undefined
65//aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:func_main_0@builtinArrayForEach
66print(x.forEach((x,y) => x == true)) //: undefined
67//aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:func_main_0@builtinArrayForEach
68print(x.forEach(x => x == true, 0)) //: undefined
69
70// Check inside try-block
71try {
72    //aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:func_main_0@builtinArrayForEach
73    print(x.forEach(x => true)) //: undefined
74} catch(e) {
75}
76
77// Replace standard builtin
78function replace(a : any) {
79    return a;
80}
81
82let newArr = [1, 2]
83let true_forEach = newArr.forEach
84newArr.forEach = replace
85
86print(newArr.forEach(x => {})); //: Cannot get source code of funtion
87newArr.forEach = true_forEach
88
89//aot: [trace] Check Type: BuiltinInstanceHClassMismatch
90print(newArr.forEach(x => true)); //: undefined
91
92function testForeachEndHoleArray() {
93    let y = [1,2,3,,,]
94    y.forEach(x=>{
95        print(x)
96    })
97}
98//aot: [trace] Check Type: InconsistentElementsKind
99//: 1
100//: 2
101//: 3
102testForeachEndHoleArray()
103
104function forEachCase1() {
105    print('case 1 forEach') //: case 1 forEach
106    let arr1 = [1, 2]
107    let arr2 = [1, 2]
108    arr2.garbage = function(x: any): any {
109        return undefined;
110    }
111    //aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:#*#forEachCase1@builtinArrayForEach
112    print(arr1.forEach(x => x == 1)); //: undefined
113    print(arr2.forEach(x => x == 1)); //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
114                                      //: undefined
115}
116forEachCase1()
117
118
119function forEachCase2() {
120    print('case 2 forEach') //: case 2 forEach
121    let arr1 = [1, 2]
122    let arr2 = [1, 2]
123    arr2.forEach = function(x: any) {
124        return x
125    }
126
127    //aot: [trace] aot inline builtin: Object.getPrototypeOf, caller function name:#*#forEachCase2@builtinArrayForEach
128    print(Object.getPrototypeOf(arr2) === Array.prototype) //: true
129
130    //aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:#*#forEachCase2@builtinArrayForEach
131    print(arr1.forEach(x => x == 1)); //: undefined
132    print(arr2.forEach(x => x == 1)); //: Cannot get source code of funtion
133}
134forEachCase2()
135
136
137function forEachCase3() {
138    print('case 3 forEach') //: case 3 forEach
139    let marr = [1, 2]
140    let true_forEach = marr.forEach
141    let mimicArray = {
142        forEach: true_forEach,
143    }
144
145    //aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:#*#forEachCase3@builtinArrayForEach
146    print(marr.forEach(x => x == 1)); //: undefined
147    //aot: [trace] aot call builtin: Object.SetPrototypeOf, caller function name:#*#forEachCase3@builtinArrayForEach
148    Object.setPrototypeOf(marr, mimicArray)
149
150    print(marr.forEach(x => x == 1)); //aot: [trace] Check Type: BuiltinInstanceHClassMismatch
151                                      //: undefined
152}
153forEachCase3()
154
155
156function forEachCase4() {
157    print('case 4 forEach') //: case 4 forEach
158    let arr1 = [1, 2]
159    let arr2 = [1, 2]
160    let notArray = {
161        forEach(x: any) {
162            return x(0)
163        }
164    }
165    //aot: [trace] aot call builtin: Object.SetPrototypeOf, caller function name:#*#forEachCase4@builtinArrayForEach
166    Object.setPrototypeOf(arr2, notArray)
167
168    //aot: [trace] aot inline builtin: Array.prototype.foreach, caller function name:#*#forEachCase4@builtinArrayForEach
169    print(arr1.forEach(x => x == 1)); //: undefined
170    //aot: [trace] aot inline function name: #*@4*#forEach@builtinArrayForEach caller function name: #*#forEachCase4@builtinArrayForEach
171    //aot: [trace] aot inline function name: #*@4*#^1@builtinArrayForEach caller function name: #*@4*#forEach@builtinArrayForEach
172    print(arr2.forEach(x => x == 1)); //: false
173}
174forEachCase4()
175
176
177function forEachCase5() {
178    print('case 5 forEach') //: case 5 forEach
179    let arr1 = [1, 2]
180    Array.prototype.foreach = function(x: any) {
181        return x(1)
182    }
183
184    //aot: [trace] Check Type: BuiltinPrototypeHClassMismatch1
185    print(arr1.forEach(x => x == 1)); //: undefined
186}
187forEachCase5()
188