• 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
16/*
17 * @tc.name:object
18 * @tc.desc:test object
19 * @tc.type: FUNC
20 * @tc.require: issueI9DUIF
21 */
22
23// test case for getPrototypeOf
24var _handler, _target;
25var target = {};
26var handler = {
27  getPrototypeOf: function(t) {
28    _handler = this;
29    _target = t;
30    return {};
31  }
32};
33
34var prototypeObj = {};
35var obj01 = Object.create(prototypeObj);
36var obj02 = new Proxy(target, handler);
37
38Object.getPrototypeOf(obj01);
39print(_handler === handler);
40print(_target === target);
41
42Object.getPrototypeOf(obj02);
43print(_handler === handler);
44print(_target === target);
45
46let obj1 = new Int8Array([-5, 10, 20, 30, 40, 50, 60.6]);
47let obj2 = {
48    a: 1,
49    b: 2,
50    c: 3,
51};
52Object.defineProperty(obj2, '23', {
53    value: 31,
54    enumerable: false,
55})
56Object.defineProperty(obj2, 'abc', {
57    value: 31,
58    enumerable: false,
59})
60let obj3 = {
61    a: 1,
62    b: 2,
63    c: 3,
64};
65let obj4 = {
66    1: 1,
67    2: 2,
68    3: 3,
69};
70let obj5 = {
71    1: 1,
72    2: 2,
73    3: 3,
74    a: 1,
75    b: 2,
76    c: 3,
77};
78let obj6 = {
79    a: 1,
80    b: 2,
81    c: 3,
82};
83const a = Symbol('a');
84const b = Symbol.for('b');
85obj6[a] = 'aSymbol';
86obj6[b] = 'bSymbol';
87let obj7 = Object.create(
88    {},
89    {
90        getBar: {
91            value() {
92                return 55;
93            },
94            writable: false,
95            enumerable: false,
96            configurable: false,
97        },
98    },
99);
100let obj11 = new Int8Array([-5, 10, 20, 30, 40, 50, 60.6]);
101let obj12 = {
102    a: 1,
103    b: 2,
104    c: 3,
105};
106Object.defineProperty(obj12, '23', {
107    value: 31,
108    enumerable: false,
109})
110Object.defineProperty(obj12, 'abc', {
111    value: 31,
112    enumerable: false,
113})
114let obj13 = {
115    a: 1,
116    b: 2,
117    c: 3,
118};
119let obj14 = new Object();
120Object.preventExtensions(obj14);
121let obj15 = {
122    1: 1,
123    2: 2,
124    3: 3,
125};
126Object.preventExtensions(obj15);
127let obj16 = {
128    1: 1,
129    2: 2,
130    3: 3,
131    a: 1,
132    b: 2,
133    c: 3,
134};
135Object.preventExtensions(obj16);
136let obj17 = Object.create(
137    {},
138    {
139        getBar: {
140            value() {
141                return 55;
142            },
143            configurable: true,
144            writable: true,
145        },
146    },
147);
148Object.preventExtensions(obj17);
149let obj18 = Object.create(
150    {},
151    {
152        getBar: {
153            value() {
154                return 55;
155            },
156            configurable: false,
157            writable: true,
158        },
159    },
160);
161Object.preventExtensions(obj18);
162let obj19 = Object.create(
163    {},
164    {
165        getBar: {
166            value() {
167                return 55;
168            },
169            configurable: false,
170            writable: false,
171        },
172    },
173);
174Object.preventExtensions(obj19);
175
176// test for getOwnPropertyNames()
177print(Object.getOwnPropertyNames(obj1));
178print(Object.getOwnPropertyNames(obj2));
179print(Object.getOwnPropertyNames(obj3));
180print(Object.getOwnPropertyNames(obj4));
181print(Object.getOwnPropertyNames(obj5));
182print(Object.getOwnPropertyNames(obj6));
183// test for getOwnPropertySymbols()
184print(Object.getOwnPropertySymbols(obj1).length);
185print(Object.getOwnPropertySymbols(obj2).length);
186print(Object.getOwnPropertySymbols(obj3).length);
187print(Object.getOwnPropertySymbols(obj4).length);
188print(Object.getOwnPropertySymbols(obj5).length);
189print(Object.getOwnPropertySymbols(obj6).length);
190// test for isFrozen()
191print(Object.isFrozen(obj11));
192print(Object.isFrozen(obj12));
193print(Object.isFrozen(obj13));
194print(Object.isFrozen(obj14));
195print(Object.isFrozen(obj15));
196print(Object.isFrozen(obj16));
197print(Object.isFrozen(obj17));
198print(Object.isFrozen(obj18));
199print(Object.isFrozen(obj19));
200// test for isSealed()
201print(Object.isSealed(obj11));
202print(Object.isSealed(obj12));
203print(Object.isSealed(obj13));
204print(Object.isSealed(obj14));
205print(Object.isSealed(obj15));
206print(Object.isSealed(obj16));
207print(Object.isSealed(obj17));
208print(Object.isSealed(obj18));
209print(Object.isSealed(obj19));
210// test for getOwnPropertyDescriptors()
211let desc1 = Object.getOwnPropertyDescriptors(obj1);
212let desc2 = Object.getOwnPropertyDescriptors(obj2);
213let desc3 = Object.getOwnPropertyDescriptors(obj3);
214let desc4 = Object.getOwnPropertyDescriptors(obj4);
215let desc5 = Object.getOwnPropertyDescriptors(obj5);
216let desc6 = Object.getOwnPropertyDescriptors(obj6);
217let desc7 = Object.getOwnPropertyDescriptors(obj7);
218print(JSON.stringify(desc1[0]));
219print(JSON.stringify(desc2.abc));
220print(JSON.stringify(desc3.a));
221print(JSON.stringify(desc4[1]));
222print(JSON.stringify(desc5[3]));
223print(JSON.stringify(desc6.c));
224print(JSON.stringify(desc7.getBar));
225
226class A {
227    p1 = 1;
228    p2 = 2;
229    p3 = 3;
230    constructor() {
231        this.p4 = 4;
232    }
233}
234var a1 = new A();
235a1.jjj = 4;
236print(ArkTools.getInlinedPropertiesCount(a1));
237var a2 = new A();
238a2.aaa1 = 5;
239a2.aaa2 = 5;
240a2.aaa3 = 5;
241a2.aaa4 = 5;
242a2.aaa5 = 5;
243a2.aaa6 = 5;
244print(ArkTools.getInlinedPropertiesCount(a2));
245var a3 = new A();
246var a4 = new A();
247var a5 = new A();
248var a6 = new A();
249var a7 = new A();
250print(ArkTools.getInlinedPropertiesCount(a7));
251var a8 = new A();
252print(a3.p2);
253print(a7.p3);
254
255for (let i = 0; i < 2000; i++) {
256    var tmp = "jj" + i;
257    a2[tmp] = i;
258}
259print(a2.aaa6);
260print(a2.jj1000);
261
262class B extends A {
263    p5 = 5;
264    constructor() {
265        super();
266        this.p6 = 6;
267    }
268}
269var b1 = new B();
270b1.jjj = 4;
271print(ArkTools.getInlinedPropertiesCount(b1));
272var b2 = new B();
273var b3 = new B();
274var b4 = new B();
275var b5 = new B();
276var b6 = new B();
277var b7 = new B();
278print(ArkTools.getInlinedPropertiesCount(b7));
279
280class C extends Date {
281    p5 = 5;
282    constructor() {
283        super();
284        this.p6 = 6;
285    }
286}
287var c1 = new C();
288c1.jjj = 4;
289print(ArkTools.getInlinedPropertiesCount(c1));
290var c2 = new C();
291var c3 = new C();
292var c4 = new C();
293var c5 = new C();
294var c6 = new C();
295var c7 = new C();
296print(ArkTools.getInlinedPropertiesCount(c7));
297
298function func1() {
299    this.a1 = 1;
300    this.a2 = 2;
301    this.a3 = 2;
302}
303var f1 = new func1();
304f1.jjj = 4;
305print(ArkTools.getInlinedPropertiesCount(f1));
306var f2 = new func1();
307print(ArkTools.getInlinedPropertiesCount(f2));
308var f3 = new func1();
309f3.aaa1 = 5;
310f3.aaa2 = 5;
311f3.aaa3 = 5;
312f3.aaa4 = 5;
313f3.aaa5 = 5;
314f3.aaa6 = 5;
315var f4 = new func1();
316var f5 = new func1();
317var f6 = new func1();
318var f7 = new func1();
319print(ArkTools.getInlinedPropertiesCount(f7));
320
321function func2() {
322    this.a1 = 1;
323    this.a2 = 2;
324    this.a3 = 2;
325}
326var f8 = new func2();
327print(ArkTools.getInlinedPropertiesCount(f8));
328func2.prototype = A.prototype;
329var f9 = new func2();
330print(ArkTools.getInlinedPropertiesCount(f9));
331
332let res = Object.getOwnPropertyDescriptors(Object);
333print(JSON.stringify(res.prototype));
334
335let obj = {};
336Object.defineProperty(obj, "key1", {
337    get() { print("get"); return 6; },
338    set(x) {}
339})
340let res2 = Object.getOwnPropertyDescriptors(obj);
341print(res2["key1"].get);
342print(JSON.stringify(res2));