1/*
2 * Copyright (c) 2023 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
16/*
17 * @tc.name:primitiveic
18 * @tc.desc:test primitiveic
19 * @tc.type: FUNC
20 */
21
22// for number_ic
23const numObj1 = 10928;
24const numObj2 = 123.456;
25const numObj3 = new Number(42);
26for (let i = 0; i < 100; i++) {
27   let res1 = numObj1.toString();
28   let res2 = numObj2.toPrecision(4);
29   let res3 = numObj3.valueOf();
30}
31
32// for number_ic & string_ic hasAccessor
33{
34  function foo() { return -4096;}
35  Object.defineProperty(String.prototype, "a", {get:foo});
36  Object.defineProperty(Number.prototype, "b", {get:foo});
37  for (let i = 0; i < 50; i++)
38  {
39      const num = 123456;
40      const str = "normallize";
41      num.a;
42      str.b;
43  }
44  Object.defineProperty(Number.prototype, "c", {value:"123"});
45  for (let i = 0; i < 50; i++)
46  {
47      const num = 123456;
48      const str = "normallize";
49      num.a;
50      str.b;
51  }
52}
53
54// for number_ic & string_ic notHasAccessor
55{
56  Object.defineProperty(String.prototype, "d", {value:"456"});
57  Object.defineProperty(Number.prototype, "e", {value:"789"});
58  for (let i = 0; i < 50; i++)
59  {
60      const num = 123456;
61      const str = "normallize";
62      num.d;
63      str.e;
64  }
65}
66
67print("primitiveic load success")
68
69for(let i=0;i<2;i++){
70  let obj={};
71  let x=obj.__proto__;
72  Object.freeze(x);
73}
74print("load ic by name test2 success!")
75
76function f(a, b) {
77  a.name;
78}
79
80for (let i = 0; i < 100; i++) {
81  f(Number, 1);
82  f(120, 1);
83  f(Number, 1);
84}
85print("load Number ic by name success!")
86
87function f(a, b) {
88  a.valueOf();
89}
90
91for (let i = 0; i < 100; i++) {
92  f(Number.prototype, 1);
93}
94for (let i = 0; i < 100; i++) {
95  f(120, 1);
96}
97print("load Number ic by name success1!")
98
99print('================Test proto SendableTypedArray IC================');
100function testProtoIc(ctor) {
101  for (let i = 0; i < 100; i++) { };
102  let obj = new ctor(100);
103  try {
104    let obj1 = {
105      __proto__: obj,
106    };
107    let obj2 = {
108      __proto__: obj1,
109    };
110    let obj3 = {
111      __proto__: obj2,
112    };
113    print(ctor.name, 'set proto with sendable object success.');
114  } catch (err) {
115    print(ctor.name, 'set proto with sendable object failed. err: ' + err);
116  }
117}
118
119[
120  SendableFloat64Array,
121  SendableFloat32Array,
122  SendableInt32Array,
123  SendableInt16Array,
124  SendableInt8Array,
125  SendableUint32Array,
126  SendableUint16Array,
127  SendableUint8Array,
128  SendableUint8ClampedArray,
129].forEach((ctor) => {
130  testProtoIc(ctor);
131});
132print('================Test proto SendableTypedArray IC success!================');
133function f(){return 1};
134Object.defineProperty(this,"g",{
135    get:f,
136    set:f,
137})
138for(let i=0;i<2;i++){
139    print(g)
140}
141print("load global ic with accessor success!");
142
143function func1(o, v) {
144  let  res;
145  for (let i = 0; i < 100; i++) {
146      res=o.x;
147      if (res != v) {
148          print("Error");
149      }
150  }
151  return res;
152}
153{
154  let pro = {
155      get x() {
156          return 1;
157      }
158  }
159  let o = {
160      __proto__: pro
161  };
162  o[102500] = 1;
163  o["test"] = "test";
164  print(func1(o, 1));
165  Object.defineProperty(o, "x", { value: 2 });
166  print("change")
167  print(func1(o, 2));
168}
169
170{
171  let pro = {
172      get x() {
173          return 1;
174      }
175  }
176  let pro2 = {
177      __proto__: pro
178  };
179  let o = {
180      __proto__: pro2
181  };
182  pro2[102500] = 1;
183  pro2["test"] = "test";
184  print(func1(o, 1));
185  Object.defineProperty(pro2, "x", { value: 2 });
186  print("change")
187  func1(o, 2);
188}
189
190{
191  function getNumber(o) {
192      let res;
193      for (let i = 0; i < 100; i++) {
194        res=o.Number;
195      }
196      return res;
197  }
198  let pro = globalThis
199  let pro2 = {
200      __proto__: pro
201  };
202  let o = {
203      __proto__: pro2
204  };
205  pro2[102500] = 1;
206  pro2["test"] = "test";
207  for (let i = 0; i < 2; i++) {
208      print(getNumber(o))
209  }
210  Object.defineProperty(o, "Number", { value: 2 });
211  print("change")
212  for (let i = 0; i < 2; i++) {
213      print(getNumber(o))
214  }
215}
216