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}
19
20let arraybuffer = new ArrayBuffer(10)
21let dataView = new DataView(arraybuffer)
22print(ArrayBuffer.isView(dataView)) //: true
23print(ArrayBuffer.isView(dataView, 0)) //: true
24let str = "123"
25print(ArrayBuffer.isView(str)) //: false
26print(ArrayBuffer.isView()) //: false
27print(ArrayBuffer.isView(123)) //: false
28print(ArrayBuffer.isView(new Float32Array())) //: true
29print(ArrayBuffer.isView({})) //: false
30print(ArrayBuffer.isView([])) //: false
31print(ArrayBuffer.isView(undefined)) //: false
32
33// Check inside try-block
34try {
35  print('try') //: try
36  print(ArrayBuffer.isView({})) //: false
37} catch(e) { 
38}
39
40let obj = {};
41obj.valueOf = (() => { return 5; })
42print(ArrayBuffer.isView(obj)); //: false
43
44function Throwing() {
45    this.value = 2;
46    Throwing.prototype.valueOf = function() {
47        if (this.value > 0) {
48            throw new Error("positive");
49        }
50        return this.value;
51    }
52}
53let throwingObj = new Throwing();
54try {
55    print(ArrayBuffer.isView(throwingObj)); //: false
56} catch(e) {
57    print(e);
58} finally {
59    print(ArrayBuffer.isView(obj)); //: false
60}
61
62// Replace standard builtin
63function replace(a : any) {
64  return a;
65}
66
67let true_isView = ArrayBuffer.isView
68ArrayBuffer.isView = replace
69
70print(ArrayBuffer.isView(undefined)); //: undefined
71ArrayBuffer.isView = true_isView
72print(ArrayBuffer.isView(undefined)); //: false
73
74
75function doIsView(x: any): any {
76  return ArrayBuffer.isView(x);
77}
78
79function printIsView(x: any) {
80  try {
81      print(doIsView(x));
82  } finally {
83  }
84}
85
86if (ArkTools.isAOTCompiled(printIsView)) {
87  // Replace standard builtin after call to standard builtin was profiled
88  ArrayBuffer.isView = replace
89}
90printIsView(-1); //pgo: false
91//aot: [trace] Check Type: NotCallTarget1
92//aot: -1
93
94printIsView("abc"); //pgo: false
95//aot: [trace] Check Type: NotCallTarget1
96//aot: abc
97
98ArrayBuffer.isView = true_isView