• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16class P {
17    constructor() {
18        this.a = 1;
19    }
20}
21// test normal object
22let proto = new P();
23let protoT = new P();
24
25// use to store results
26let conditionArray = [];
27conditionArray.push(!(proto === protoT))
28// share hclass
29conditionArray.push(ArkTools.compareHClass(proto, protoT));
30conditionArray.push(!ArkTools.isPrototype(proto));
31conditionArray.push(!ArkTools.isPrototype(protoT));
32
33print("start test!!!")
34let obj1 = {"b": "2b"};
35let obj2 = {"c": "3c"};
36let obj3 = {"d": "4d"};
37
38let obj11 = {"bb": "2bb"};
39let obj22 = {"cc": "3cc"};
40let obj33 = {"dd": "4dd"};
41
42Object.setPrototypeOf(obj1, proto);
43Object.setPrototypeOf(obj2, proto);
44Object.setPrototypeOf(obj3, proto);
45
46conditionArray.push(ArkTools.isPrototype(proto));
47conditionArray.push(!ArkTools.isPrototype(protoT));
48conditionArray.push(!ArkTools.compareHClass(proto, protoT));
49
50// set proto of obj11, obj22, obj33
51// obj11 --> proto --> obj1 --> proto --> proto object
52// obj22 --> proto --> obj1 --> proto --> proto object
53// obj33 --> proto --> obj2 --> proto --> proto object
54obj11.__proto__ = obj1;
55obj22.__proto__ = obj1;
56obj33.__proto__ = obj2;
57
58function test() {
59    for (let i = 0; i <= 50; i++) {
60        obj11["a"];
61    }
62}
63test();
64protoT["x"] = "2000x";
65obj1["www"] = "100www";
66
67// test proxy
68let protoP1 = new P();
69let protoP2 = new P();
70protoP1.a = "xxx";
71
72let proxyOfProto = new Proxy(protoP1, {});
73conditionArray.push(!(protoP1 === protoP2))
74conditionArray.push(ArkTools.compareHClass(protoP1, protoP2));
75conditionArray.push(!ArkTools.isPrototype(protoP1));
76conditionArray.push(!ArkTools.isPrototype(protoP2));
77
78// change proto of obj1
79// obj11 --> proto --> obj1 --> proto --> protoP1
80// obj22 --> proto --> obj1 --> proto --> protoP1
81Object.setPrototypeOf(obj1, proxyOfProto);
82Object.setPrototypeOf(obj1, proxyOfProto);
83conditionArray.push(ArkTools.compareHClass(protoP1, protoP2));
84// change proto of obj2
85// obj33 --> proto --> obj2 --> proto --> protoP2
86Object.setPrototypeOf(obj2, protoP2);
87Object.setPrototypeOf(obj3, proxyOfProto);
88
89conditionArray.push(!ArkTools.compareHClass(protoP1, protoP2));
90conditionArray.push(!ArkTools.isPrototype(protoP1));
91conditionArray.push(ArkTools.isPrototype(protoP2));
92conditionArray.push(!ArkTools.isPrototype(proxyOfProto));
93
94test();
95obj1["zzz"] = "100zzz";
96
97// check proto
98conditionArray.push(obj11.a === "xxx")
99conditionArray.push(obj22.a === "xxx")
100conditionArray.push(obj11.b === "2b")
101conditionArray.push(obj33.a === 1)
102conditionArray.push(obj33.c === "3c")
103conditionArray.push(obj11.zzz === "100zzz")
104conditionArray.push(obj33.zzz === undefined)
105
106let str = "";
107// check results
108for (let i of conditionArray) {
109    if (!i) {
110        str = "Test mutli proto ic fail!!!";
111        break;
112    }
113}
114if (str == "") {
115    str = "Test mutli proto ic success!!!";
116}
117print(str);
118