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 */
15declare function print(arg: any): string;
16declare interface ArkTools {
17    isAOTCompiled(args: any): boolean;
18}
19let buffer = new ArrayBuffer(8);
20let sample = new DataView(buffer, 0);
21
22//aot: [trace] aot inline builtin: DataView.prototype.SetFloat32, caller function name:func_main_0@builtinDataViewGet
23sample.setFloat32(0);
24//aot: [trace] aot inline builtin: DataView.prototype.GetFloat32, caller function name:func_main_0@builtinDataViewGet
25print(sample.getFloat32(0)) //: NaN
26//aot: [trace] aot inline builtin: DataView.prototype.GetFloat32, caller function name:func_main_0@builtinDataViewGet
27print(sample.getFloat32(0, true)) //: NaN
28//aot: [trace] aot inline builtin: DataView.prototype.SetFloat64, caller function name:func_main_0@builtinDataViewGet
29sample.setFloat64(0)
30print(sample.getFloat64()) //: NaN
31//aot: [trace] aot inline builtin: DataView.prototype.GetFloat64, caller function name:func_main_0@builtinDataViewGet
32print(sample.getFloat64(0)) //: NaN
33
34//aot: [trace] aot inline builtin: DataView.prototype.SetUint8, caller function name:func_main_0@builtinDataViewGet
35sample.setUint8(0, 127);
36//aot: [trace] aot inline builtin: DataView.prototype.SetUint8, caller function name:func_main_0@builtinDataViewGet
37sample.setUint8(1, 128);
38//aot: [trace] aot inline builtin: DataView.prototype.SetUint8, caller function name:func_main_0@builtinDataViewGet
39sample.setUint8(2, 0);
40//aot: [trace] aot inline builtin: DataView.prototype.SetUint8, caller function name:func_main_0@builtinDataViewGet
41sample.setUint8(3, 0);
42//aot: [trace] aot inline builtin: DataView.prototype.SetUint8, caller function name:func_main_0@builtinDataViewGet
43sample.setUint8(4, 255);
44//aot: [trace] aot inline builtin: DataView.prototype.SetUint8, caller function name:func_main_0@builtinDataViewGet
45sample.setUint8(5, 128);
46//aot: [trace] aot inline builtin: DataView.prototype.SetUint8, caller function name:func_main_0@builtinDataViewGet
47sample.setUint8(6, 0);
48//aot: [trace] aot inline builtin: DataView.prototype.SetUint8, caller function name:func_main_0@builtinDataViewGet
49sample.setUint8(7, 0);
50
51//aot: [trace] aot inline builtin: DataView.prototype.GetFloat32, caller function name:func_main_0@builtinDataViewGet
52print(sample.getFloat32(0)) //: Infinity
53//aot: [trace] aot inline builtin: DataView.prototype.GetFloat32, caller function name:func_main_0@builtinDataViewGet
54print(sample.getFloat32(4)) //: -Infinity
55
56//aot: [trace] aot inline builtin: DataView.prototype.SetUint8, caller function name:func_main_0@builtinDataViewGet
57sample.setUint8(0, 75);
58//aot: [trace] aot inline builtin: DataView.prototype.SetUint8, caller function name:func_main_0@builtinDataViewGet
59sample.setUint8(1, 75);
60//aot: [trace] aot inline builtin: DataView.prototype.SetUint8, caller function name:func_main_0@builtinDataViewGet
61sample.setUint8(2, 76);
62//aot: [trace] aot inline builtin: DataView.prototype.SetUint8, caller function name:func_main_0@builtinDataViewGet
63sample.setUint8(3, 76);
64//aot: [trace] aot inline builtin: DataView.prototype.SetUint8, caller function name:func_main_0@builtinDataViewGet
65sample.setUint8(4, 75);
66//aot: [trace] aot inline builtin: DataView.prototype.SetUint8, caller function name:func_main_0@builtinDataViewGet
67sample.setUint8(5, 75);
68//aot: [trace] aot inline builtin: DataView.prototype.SetUint8, caller function name:func_main_0@builtinDataViewGet
69sample.setUint8(6, 76);
70//aot: [trace] aot inline builtin: DataView.prototype.SetUint8, caller function name:func_main_0@builtinDataViewGet
71sample.setUint8(7, 76);
72
73//32th bit is 1
74//aot: [trace] aot inline builtin: DataView.prototype.SetInt32, caller function name:func_main_0@builtinDataViewGet
75sample.setInt32(0, -10000)
76//aot: [trace] aot inline builtin: DataView.prototype.GetUint32, caller function name:func_main_0@builtinDataViewGet
77print(sample.getUint32(0)) //: 4294957296
78
79//16th bit is 1
80//aot: [trace] aot inline builtin: DataView.prototype.SetInt16, caller function name:func_main_0@builtinDataViewGet
81sample.setInt16(0, -100)
82//aot: [trace] aot inline builtin: DataView.prototype.GetUint16, caller function name:func_main_0@builtinDataViewGet
83print(sample.getUint16(0)) //: 65436
84//set Int NaN
85//aot: [trace] aot inline builtin: DataView.prototype.SetUint32, caller function name:func_main_0@builtinDataViewGet
86sample.setUint32(0, NaN)
87//aot: [trace] aot inline builtin: DataView.prototype.GetUint32, caller function name:func_main_0@builtinDataViewGet
88print(sample.getUint32(0)) //: 0
89//aot: [trace] aot inline builtin: DataView.prototype.GetUint32, caller function name:func_main_0@builtinDataViewGet
90print(sample.getUint32(4)) //: 1263225932
91
92//aot: [trace] aot inline builtin: DataView.prototype.SetUint8, caller function name:func_main_0@builtinDataViewGet
93print(sample.setUint8(0, -1)) //: undefined
94//aot: [trace] aot inline builtin: DataView.prototype.GetUint8, caller function name:func_main_0@builtinDataViewGet
95print(sample.getUint8(0)) //: 255
96//aot: [trace] aot inline builtin: DataView.prototype.GetInt8, caller function name:func_main_0@builtinDataViewGet
97print(sample.getInt8(0)) //: -1
98
99print(sample.setUint8(0, -1, 90, 78)) //: undefined
100//aot: [trace] aot inline builtin: DataView.prototype.GetUint8, caller function name:func_main_0@builtinDataViewGet
101print(sample.getUint8(0)) //: 255
102//aot: [trace] aot inline builtin: DataView.prototype.GetInt8, caller function name:func_main_0@builtinDataViewGet
103print(sample.getInt8(0)) //: -1
104
105//break tagged mark
106//aot: [trace] aot inline builtin: DataView.prototype.SetFloat64, caller function name:func_main_0@builtinDataViewGet
107sample.setFloat64(0, 9007199254740991)
108//aot: [trace] aot inline builtin: DataView.prototype.GetFloat64, caller function name:func_main_0@builtinDataViewGet
109print(sample.getFloat64(0, true)) //: NaN
110//aot: [trace] aot inline builtin: DataView.prototype.GetFloat64, caller function name:func_main_0@builtinDataViewGet
111print(sample.getFloat64(0, false)) //: 9007199254740991
112
113//greater safe number
114//aot: [trace] aot inline builtin: DataView.prototype.SetFloat64, caller function name:func_main_0@builtinDataViewGet
115sample.setFloat64(0, 9007199254740992)
116//aot: [trace] aot inline builtin: DataView.prototype.GetFloat64, caller function name:func_main_0@builtinDataViewGet
117print(sample.getFloat64(0, true)) //: 8.128e-320
118//aot: [trace] aot inline builtin: DataView.prototype.GetFloat64, caller function name:func_main_0@builtinDataViewGet
119print(sample.getFloat64(0, false)) //: 9007199254740992
120
121//aot: [trace] aot inline builtin: DataView.prototype.SetFloat32, caller function name:func_main_0@builtinDataViewGet
122sample.setFloat32(0, 0.50000001)
123//aot: [trace] aot inline builtin: DataView.prototype.GetFloat32, caller function name:func_main_0@builtinDataViewGet
124print(sample.getFloat32(0, true)) //: 8.828180325246348e-44
125//aot: [trace] aot inline builtin: DataView.prototype.GetFloat32, caller function name:func_main_0@builtinDataViewGet
126print(sample.getFloat32(0, false)) //: 0.5
127
128//special value
129//aot: [trace] aot inline builtin: DataView.prototype.SetFloat32, caller function name:func_main_0@builtinDataViewGet
130sample.setFloat32(0, null)
131//aot: [trace] aot inline builtin: DataView.prototype.GetFloat32, caller function name:func_main_0@builtinDataViewGet
132print(sample.getFloat32(0)) //: 0
133//aot: [trace] aot inline builtin: DataView.prototype.SetInt32, caller function name:func_main_0@builtinDataViewGet
134sample.setInt32(0, null)
135//aot: [trace] aot inline builtin: DataView.prototype.GetInt32, caller function name:func_main_0@builtinDataViewGet
136print(sample.getInt32(0)) //: 0
137//aot: [trace] aot inline builtin: DataView.prototype.SetFloat64, caller function name:func_main_0@builtinDataViewGet
138sample.setFloat64(0, true)
139//aot: [trace] aot inline builtin: DataView.prototype.GetFloat64, caller function name:func_main_0@builtinDataViewGet
140print(sample.getFloat64(0)) //: 1
141//aot: [trace] Check Type: NotBool1
142print(sample.getFloat64(0, 'abc')) //: 3.03865e-319
143print(sample.getFloat64('abc')) //: 1
144
145// Check inside try-block
146try {
147    print(sample.setUint8(0, -1, 90, 78)) //: undefined
148    print(sample.getUint8(0)) //: 255
149} catch(e) {
150}
151
152let obj = {};
153obj.valueOf = (() => { return 5; })
154print(sample.setUint8(obj)); //: undefined
155
156function Throwing() {
157    this.value = 2;
158    Throwing.prototype.valueOf = function() {
159        if (this.value > 0) {
160            throw new Error("positive");
161        }
162        return this.value;
163    }
164}
165let throwingObj = new Throwing();
166try {
167    print(sample.setUint8(throwingObj));
168} catch(e) {
169    print(e); //: Error: positive
170} finally {
171    print(sample.setUint8(obj)); //: undefined
172}
173
174sample.getUint8(0)
175sample.setInt16(0, -100)
176
177// Replace standard builtin
178function replace(a : any) {
179  return a;
180}
181
182let true_get = sample.getInt8
183sample.getInt8 = replace
184
185print(sample.getInt8(0)); //: 0
186sample.getInt8 = true_get
187print(sample.getInt8(0)); //: -1
188
189
190function doGet(x: any): any {
191  return sample.getInt8(x);
192}
193
194function printGet(x: any) {
195  try {
196      print(doGet(x));
197  } finally {
198  }
199}
200
201if (ArkTools.isAOTCompiled(printGet)) {
202  // Replace standard builtin after call to standard builtin was profiled
203  sample.getInt8 = replace
204}
205printGet(1); //pgo: -100
206//aot: [trace] Check Type: NotCallTarget1
207//aot: 1
208
209printGet("abc"); //pgo: -1
210//aot: [trace] Check Type: NotCallTarget1
211//aot: abc
212
213sample.getInt8 = true_get
214
215function getCase1() {
216    print('case 1 get') //: case 1 get
217    let buffer = new ArrayBuffer(8);
218    let sample1 = new DataView(buffer, 0);
219    let sample2 = new DataView(buffer, 0);
220    sample2.garbage = function(x: any): any {
221        return undefined;
222    }
223
224    //aot: [trace] aot inline builtin: DataView.prototype.SetInt32, caller function name:#*#getCase1@builtinDataViewGet
225    sample1.setInt32(0, null)
226    //aot: [trace] aot inline builtin: DataView.prototype.SetInt32, caller function name:#*#getCase1@builtinDataViewGet
227    sample2.setInt32(0, null)
228
229    //aot: [trace] aot inline builtin: DataView.prototype.GetInt32, caller function name:#*#getCase1@builtinDataViewGet
230    print(sample1.getInt32(0)); //: 0
231    //aot: [trace] aot inline builtin: DataView.prototype.GetInt32, caller function name:#*#getCase1@builtinDataViewGet
232    print(sample2.getInt32(0)); //: 0
233}
234getCase1()
235
236
237function getCase2() {
238    print('case 2 get') //: case 2 get
239    let buffer = new ArrayBuffer(8);
240    let sample1 = new DataView(buffer, 0);
241    let sample2 = new DataView(buffer, 0);
242    sample2.getInt32 = function(x: any) {
243        return x + 1
244    }
245
246    //aot: [trace] aot inline builtin: Object.getPrototypeOf, caller function name:#*#getCase2@builtinDataViewGet
247    print(Object.getPrototypeOf(sample2) === DataView.prototype) //: true
248
249    //aot: [trace] aot inline builtin: DataView.prototype.SetInt32, caller function name:#*#getCase2@builtinDataViewGet
250    sample1.setInt32(0, null)
251    //aot: [trace] aot inline builtin: DataView.prototype.SetInt32, caller function name:#*#getCase2@builtinDataViewGet
252    sample2.setInt32(0, null)
253
254    //aot: [trace] aot inline builtin: DataView.prototype.GetInt32, caller function name:#*#getCase2@builtinDataViewGet
255    print(sample1.getInt32(0)); //: 0
256    //aot: [trace] aot inline function name: #*@2*#@builtinDataViewGet caller function name: #*#getCase2@builtinDataViewGet
257    print(sample2.getInt32(0)); //: 1
258}
259getCase2()
260
261
262function getCase3() {
263    print('case 3 get') //: case 3 get
264    let buffer = new ArrayBuffer(8);
265    let msample = new DataView(buffer, 0);
266    let true_getInt32 = msample.getInt32
267    let mimicDataView = {
268        getInt32: true_getInt32,
269    }
270
271    //aot: [trace] aot inline builtin: DataView.prototype.SetInt32, caller function name:#*#getCase3@builtinDataViewGet
272    msample.setInt32(0, null)
273    //aot: [trace] aot inline builtin: DataView.prototype.GetInt32, caller function name:#*#getCase3@builtinDataViewGet
274    print(msample.getInt32(0)); //: 0
275    Object.setPrototypeOf(msample, mimicDataView)
276
277    //aot: [trace] aot inline builtin: DataView.prototype.GetInt32, caller function name:#*#getCase3@builtinDataViewGet
278    print(msample.getInt32(0)); //: 0
279}
280getCase3()
281
282
283function getCase4() {
284    print('case 4 get') //: case 4 get
285    let buffer = new ArrayBuffer(8);
286    let sample1 = new DataView(buffer, 0);
287    let sample2 = new DataView(buffer, 0);
288    let true_setInt32 = sample2.setInt32
289    let notDataView = {
290        getInt32(x: any) {
291            return -100
292        },
293        setInt32: true_setInt32
294    }
295    Object.setPrototypeOf(sample2, notDataView)
296    //aot: [trace] aot inline builtin: DataView.prototype.SetInt32, caller function name:#*#getCase4@builtinDataViewGet
297    sample1.setInt32(0, null)
298    //aot: [trace] aot inline builtin: DataView.prototype.SetInt32, caller function name:#*#getCase4@builtinDataViewGet
299    sample2.setInt32(0, null)
300
301    //aot: [trace] aot inline builtin: DataView.prototype.GetInt32, caller function name:#*#getCase4@builtinDataViewGet
302    print(sample1.getInt32(0)); //: 0
303    //aot: [trace] aot inline function name: #*@3*#getInt32@builtinDataViewGet caller function name: #*#getCase4@builtinDataViewGet
304    print(sample2.getInt32(0)); //: -100
305}
306getCase4()
307
308
309function getCase5() {
310    print('case 5 get') //: case 5 get
311    let buffer = new ArrayBuffer(8);
312    let sample1 = new DataView(buffer, 0);
313    DataView.prototype.getInt32 = function(x: any) {
314        return x + 45
315    }
316
317    //aot: [trace] aot inline function name: #*@4*#@builtinDataViewGet caller function name: #*#getCase5@builtinDataViewGet
318    print(sample1.getInt32(1)); //: 46
319}
320getCase5()
321