• 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
16/*
17 * @tc.name:sharedcheck
18 * @tc.desc:test sharedcheck
19 * @tc.type: FUNC
20 * @tc.require: issueI8QUU0
21 */
22
23// @ts-nocheck
24/*
25 * Copyright (c) 2023 Huawei Device Co., Ltd.
26 * Licensed under the Apache License, Version 2.0 (the "License");
27 * you may not use this file except in compliance with the License.
28 * You may obtain a copy of the License at
29 *
30 *     http://www.apache.org/licenses/LICENSE-2.0
31 *
32 * Unless required by applicable law or agreed to in writing, software
33 * distributed under the License is distributed on an "AS IS" BASIS,
34 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35 * See the License for the specific language governing permissions and
36 * limitations under the License.
37 */
38
39/*
40 * @tc.name:sharedcheck
41 * @tc.desc:test sharedcheck
42 * @tc.type: FUNC
43 * @tc.require: issueI8QUU0
44 */
45
46// @ts-nocheck
47declare function print(str: any): string;
48
49class SimpleStringSendable {
50  propString: string = "I'm simple sendable's propString";
51  constructor() {
52    "use sendable"
53    print(this.propString)
54  }
55}
56
57class SimpleNumberSendable {
58  propNumber: number = 2023;
59  constructor() {
60    "use sendable"
61    print("I'm constructor for SimpleNumberSendable")
62  }
63}
64
65class SuperClass {
66  propString: string = "I'm propString"
67  propNumber: number = 5
68  propBool: boolean = false
69  static staticPropString: string = "I'm staticPropString";
70  publicPropString: string = "I'm privatePropString";
71
72  get accessorPrivatePropString() {
73    return this.publicPropString;
74  }
75
76  set accessorPrivatePropString(prop: string) {
77    this.publicPropString = prop;
78  }
79  constructor() {
80    "use sendable"
81    print(this.propString)
82  }
83}
84
85class SubClass extends SuperClass {
86  static staticSubClassPropString: string = "I'm staticSubClassPropString";
87  static staticFunc() {
88    print("Hi static func");
89  }
90  static staticFunc1() {
91    print("Hi static func1");
92  }
93
94  func() {
95    print("Hi func");
96    this.publicFunc();
97  }
98
99  publicFunc() {
100    print("Hi public func");
101  }
102
103  subClassPropString: string = "I'm subClassPropString"
104  subClassPropSendable: SimpleStringSendable
105  constructor() {
106    "use sendable"
107    super()
108  }
109}
110
111function testDelete(testObj: SubClass) {
112  print("Start testDelete");
113  try {
114    delete testObj.propNumber;
115    print("Success delete propNumber")
116  } catch (error) {
117    print("Fail to delete propNumber. err: " + error)
118  }
119}
120
121function testExtend(testObj: SubClass) {
122  print("Start testExtend");
123  try {
124    Object.defineProperty(testObj, "tmpProp", {value: 321, writable: true });
125    print("Success to extend prop with defineProperty")
126  } catch (error) {
127    print("Fail to extend prop with defineProperty. err: " + error);
128  }
129
130  try {
131    Object.defineProperty(testObj, "prop1", { writable: true });
132    print("Success extend prop1 with defineProperty")
133  } catch (error) {
134    print("Fail to extend prop1 with defineProperty. err: " + error)
135  }
136
137  try {
138    Object.defineProperties(testObj, { prop2: { writable: true } });
139    print("Success extend prop2 with defineProperties")
140  } catch (error) {
141    print("Fail to extend prop2 with defineProperties. err: " + error)
142  }
143
144  try {
145    testObj.prop3 = {};
146    print("Success extend prop3 with stobjbyname")
147  } catch (error) {
148    print("Fail to extend prop3 with stobjbyname. err: " + error)
149  }
150}
151
152function testUpdateInstanceFunction(testObj: SubClass) {
153  print("Start testUpdateInstanceFunction");
154  try {
155    testObj.func = function () { }
156    print("Success replace instance's func");
157  } catch (error) {
158    print("Fail to replace instance's func. err: " + error);
159  }
160}
161
162function testUpdatePrototype(testObj: SubClass) {
163  print("Start testUpdatePrototype");
164  try {
165    SuperClass.prototype.staticSubClassPropString = 1;
166    print("Success update prototype")
167  } catch (error) {
168    print("Fail to update prototype. err: " + error)
169  }
170
171  try {
172    SubClass.prototype.tmpProp = 123;
173    print("Success to extend prop to constructor's prototype.");
174  } catch (error) {
175    print("Fail to extend prop to constructor's prototype. err: " + error);
176  }
177
178  let superClass = new SuperClass()
179  try {
180    testObj.__proto__.constructor = superClass.__proto__.constructor;
181    print("Success to change constructor of instance's prototype.");
182  } catch (error) {
183    print("Fail to change constructor of instance's prototype. err: " + error);
184  }
185
186  try {
187    testObj.__proto__ = superClass.__proto__;
188    print("Success to replace instance's prototype.");
189  } catch (error) {
190    print("Fail to replace instance's prototype. err: " + error);
191  }
192
193  try {
194    Object.defineProperty(testObj.__proto__, "abc", { value: 123 });
195    print("Success to extend instance's prototype.");
196  } catch (error) {
197    print("Fail to extend instance's prototype. err: " + error);
198  }
199}
200
201function testUpdateInstancePropsToNull(testObj: SubClass) {
202  print("Start testUpdateInstancePropsToNull");
203  try {
204    testObj.propString = null
205    print("Success update propString to null with stobjbyname")
206  } catch (error) {
207    print("Fail to update propString to null with stobjbyname. err: " + error)
208  }
209
210  try {
211    testObj.subClassPropSendable = null
212    print("Success update subClassPropSendable to null with stobjbyname")
213  } catch (error) {
214    print("Fail to update subClassPropSendable to null with stobjbyname. err: " + error)
215  }
216
217  try {
218    testObj.propNumber = null
219    print("Success update propNumber to null with stobjbyname")
220  } catch (error) {
221    print("Fail to update propNumber to null with stobjbyname. err: " + error)
222  }
223
224  try {
225    testObj.propBool = null
226    print("Success update propNumber to null with stobjbyname")
227  } catch (error) {
228    print("Fail to update propNumber to null with stobjbyname. err: " + error)
229  }
230}
231
232function testUpdateInstancePropsToUndefined(testObj: SubClass) {
233  print("Start testUpdateInstancePropsToUndefined");
234  try {
235    testObj.propString = undefined
236    print("Success update propString to undefined with stobjbyname")
237  } catch (error) {
238    print("Fail to update propString to undefined with stobjbyname. err: " + error)
239  }
240
241  try {
242    testObj.subClassPropSendable = undefined
243    print("Success update subClassPropSendable to undefined with stobjbyname")
244  } catch (error) {
245    print("Fail to update subClassPropSendable to undefined with stobjbyname. err: " + error)
246  }
247
248  try {
249    testObj.propNumber = undefined
250    print("Success update propNumber to undefined with stobjbyname")
251  } catch (error) {
252    print("Fail to update propNumber to undefined with stobjbyname. err: " + error)
253  }
254
255  try {
256    testObj.propBool = undefined
257    print("Success update propNumber to undefined with stobjbyname")
258  } catch (error) {
259    print("Fail to update propNumber to undefined with stobjbyname. err: " + error)
260  }
261}
262
263function testUpdateInstanceProps(testObj: SubClass) {
264  testUpdateInstancePropsToNull(testObj);
265  testUpdateInstancePropsToUndefined(testObj);
266  try {
267    Object.defineProperties(testObj, { subClassPropString: { value: "hello", writable: true } });
268    print("Success update subClassPropString with defineProperties")
269  } catch (error) {
270    print("Fail to update subClassPropString with defineProperties. err: " + error)
271  }
272
273  try {
274    Object.defineProperty(testObj, "propNumber", { value: 100, configurable: false });
275    print("Success update propNumber with defineProperty")
276  } catch (error) {
277    print("Fail to update propNumber with defineProperty. err: " + error)
278  }
279
280  try {
281    testObj.subClassPropString = 33;
282    print("Success update subClassPropString with stobjbyname")
283  } catch (error) {
284    print("Fail to update subClassPropString with stobjbyname. err: " + error)
285  }
286}
287
288function testUpdateInstanceAccessor(testObj: SubClass) {
289  print("Start testUpdateInstanceAccessor");
290  try {
291    Object.defineProperty(testObj, "accessorPrivatePropString",
292      {
293        get accessorPrivatePropString() { print("Replaced get accessor") },
294        set accessorPrivatePropString(prop: string) { print("Replaced set accessor") }
295      })
296    print("Success replace accessor");
297  } catch (error) {
298    print("Fail to replace accessor. err: " + error);
299  }
300
301  try {
302    testObj.accessorPrivatePropString = "123"
303    print("Success set prop through accessor with matched type");
304  } catch (error) {
305    print("Fail to set prop through accessor with matched type. err: " + error);
306  }
307
308  try {
309    testObj.accessorPrivatePropString = 123
310    print("Success set prop through accessor with mismatched type");
311  } catch (error) {
312    print("Fail to set prop through accessor with mismatched type. err: " + error);
313  }
314}
315
316function testUpdateConstructor() {
317  print("Start testUpdateConstructor");
318  try {
319    SubClass.staticFunc = function () { };
320    print("Success to modify constructor's method.");
321  } catch (error) {
322    print("Fail to modify constructor's method. err: " + error);
323  }
324
325  try {
326    SubClass.staticSubClassPropString = "modify static";
327    print("Success to modify property to constructor's property.");
328  } catch (error) {
329    print("Fail to modify property to constructor's property. err: " + error);
330  }
331}
332
333function testObjectProtoFunc(testObj: SubClass) {
334  print("Start testObjectProtoFunc");
335  testObjectAssign(testObj);
336  testObjectCreate(testObj);
337  testObjectSetPrototypeOf(testObj);
338  testObjectAttributesAndExtensible(testObj);
339}
340
341function testObjectCreate(testObj: SubClass)
342{
343  print("Start testObjectCreate");
344  try {
345    let sendableSimple: SimpleStringSendable = Object.create(SimpleStringSendable);
346    print("Success to call Object.create");
347  } catch (error) {
348    print("Fail to call Object.create. err: " + error);
349  }
350}
351
352function testObjectSetPrototypeOf(testObj: SubClass)
353{
354  print("Start testObjectSetPrototypeOf");
355  try {
356    Object.setPrototypeOf(testObj, new Object)
357    print("Success to call Object.setPrototypeOf")
358  } catch (error) {
359    print("Fail to call Object.setPrototypeOf. err: " + error)
360  }
361}
362
363function testObjectAttributesAndExtensible(testObj: SubClass)
364{
365  print("Start testObjectAttributesAndExtensible");
366  try {
367    Object.defineProperty(testObj, "propNumber", { configurable: true });
368    print("Success to update propNumber to configurable with defineProperty")
369  } catch (error) {
370    print("Fail to update propNumber to configurable with defineProperty. err: " + error)
371  }
372  print("isFrozen: " + Object.isFrozen(testObj))
373  try {
374    Object.freeze(testObj);
375    print("Success to call Object.freeze")
376  } catch (error) {
377    print("Fail to call Object.freeze. err: " + error)
378  }
379  print("isSealed: " + Object.isSealed(testObj))
380  try {
381    Object.seal(testObj);
382    print("Success to call Object.seal in sealed state")
383  } catch (error) {
384    print("Fail to call Object.seal in sealed state. err: " + error)
385  }
386  print("isExtensible: " + Object.isExtensible(testObj))
387  try {
388    Object.preventExtensions(testObj);
389    print("Success to call Object.preventExtensions in preventExtensions state.")
390  } catch (error) {
391    print("Fail to to call Object.preventExtensions in preventExtensions state. err: " + error)
392  }
393}
394
395function testObjectAssign(testObj: SubClass)
396{
397  print("Start testObjectAssign");
398  try {
399    Object.assign(testObj, new Object({ a: 1, b: "abc" }));
400    print("Success to call Object.assign to extend target");
401  } catch (error) {
402    print("Fail to call Object.assign to extend target. err: " + error);
403  }
404
405  try {
406    Object.assign(testObj, new Object({ propString: undefined }));
407    print("Success to call Object.assign to update propString with mismatched type");
408  } catch (error) {
409    print("Fail to call Object.assign to update propString with mismatched type. err: " + error);
410  }
411
412  try {
413    Object.assign(testObj, new Object({ propString: "abc" }));
414    print("Success to call Object.assign to update propString");
415  } catch (error) {
416    print("Fail to call Object.assign to update propString. err: " + error);
417  }
418}
419
420function testKeywords(testObj: SubClass)
421{
422  print("Start testKeywords");
423  print("typeof sendable object: " + (typeof testObj))
424  print("typeof sendable function: " + (typeof testObj.func))
425  print("sendable instanceof Object: " + (testObj instanceof Object))
426  print("sendable instanceof SubClass: " + (testObj instanceof SubClass))
427  print("sendable instanceof SuperClass: " + (testObj instanceof SuperClass))
428}
429
430function testUpdate(testObj: SubClass) {
431  testUpdateInstanceProps(testObj);
432  testUpdateInstanceAccessor(testObj);
433  testUpdateInstanceFunction(testObj);
434  testUpdatePrototype(testObj);
435  testUpdateConstructor();
436}
437
438function testUpdateWithType(testObj: SubClass) {
439  print("Start testUpdateWithType");
440  try {
441    testObj.propString = 1;
442    print("Success update string to int with stobjbynamme.")
443  } catch (error) {
444    print("Fail to update string to int with stobjbynamme. err: " + error);
445  }
446
447  try {
448    Object.defineProperty(testObj, "subClassPropSendable", { value: 123, writable: true });
449    print("Success update subClassPropSendable to number with defineProperty.")
450  } catch (error) {
451    print("Fail to update subClassPropSendable to number with defineProperty. err: " + error);
452  }
453
454  try {
455    Object.defineProperty(testObj, "subClassPropSendable", { value: new SimpleNumberSendable(), writable: true });
456    print("Success update subClassPropSendable to numberSendable with defineProperty.")
457  } catch (error) {
458    print("Fail to update subClassPropSendable to numberSendable with defineProperty. err: " + error);
459  }
460}
461
462function testNormInherit() {
463  print("Start testNormInherit");
464  try {
465    class NormalClass extends SimpleStringSendable {
466      constructor() {
467        super()
468      }
469    }
470    print("Success to define normal class inherit from sendable class")
471  } catch (error) {
472    print("Fail to define normal class inherit from sendable class, err: " + error)
473  }
474}
475
476function testICCheckingForUpdateInstanceProps(testObj: SubClass) {
477  let loopIndex: number = 0;
478  try {
479    for (loopIndex = 0; loopIndex < 2000; loopIndex++) {
480      testObj.subClassPropString = loopIndex < 1000 ? "hello" : 1;
481    }
482    print("[IC] Success update subClassPropString with mismatch type")
483  } catch (error) {
484    print("[IC] Fail to update subClassPropString with mismatch type. err: " + error + ", loopIndex: " + loopIndex)
485  }
486
487  try {
488    for (loopIndex = 0; loopIndex < 2000; loopIndex++) {
489      testObj.propNumber = loopIndex < 1000 ? 100 : "Hi";
490    }
491    print("[IC] Success update propNumber with mismatch type")
492  } catch (error) {
493    print("[IC] Fail to update propNumber with mismatch type. err: " + error + ", loopIndex: " + loopIndex);
494  }
495
496  try {
497    for (loopIndex = 0; loopIndex < 2000; loopIndex++) {
498      testObj.subClassPropString = loopIndex < 1000 ? 33 : "Hi";
499    }
500    print("[IC] Success update subClassPropString with mismatch type")
501  } catch (error) {
502    print("[IC] Fail to update subClassPropString with mismatch type. err: " + error + ", loopIndex: " + loopIndex);
503  }
504
505  try {
506    let simpleSendable = new SimpleStringSendable();
507    for (loopIndex = 0; loopIndex < 2000; loopIndex++) {
508      testObj.subClassPropSendable = loopIndex < 1000 ? simpleSendable : 1;
509    }
510    print("[IC] Success to update subClassPropSendable with mismatch type.");
511  } catch (error) {
512    print("[IC] Fail to update subClassPropSendable with mismatch type. err: " + error + ", loopIndex: " + loopIndex);
513  }
514
515  try {
516    for (loopIndex = 0; loopIndex < 2000; loopIndex++) {
517      SubClass.staticSubClassPropString = loopIndex < 1000 ? "modify static" : 1;
518    }
519    print("[IC] Success to modify constructor's property with mismatch type.");
520  } catch (error) {
521    print("[IC] Fail to modify constructor's property with mismatch type. err: " + error + ", loopIndex: " + loopIndex);
522  }
523}
524
525function testHotFunction(testObj: SubClass, loopIndex: number) {
526  testObj.accessorPrivatePropString = loopIndex < 1000 ? "123" : 1;
527}
528
529function testICCheckingUpdateInstanceAccessor(testObj: SubClass) {
530  let loopIndex: number = 0;
531  try {
532    for (loopIndex = 0; loopIndex < 2000; loopIndex++) {
533        testHotFunction(testObj, loopIndex);
534    }
535    print("[IC] Success set prop through accessor with matched type");
536  } catch (error) {
537    print("[IC] Fail to set prop through accessor with matched type. err: " + error);
538  }
539}
540
541function testICChecking(testObj: SubClass)
542{
543  print("Start testICChecking");
544  testICCheckingForUpdateInstanceProps(testObj);
545  testICCheckingUpdateInstanceAccessor(testObj);
546}
547
548let b = new SubClass()
549b.subClassPropSendable = new SimpleStringSendable()
550testUpdate(b)
551testDelete(b)
552testExtend(b)
553testObjectProtoFunc(b)
554testUpdateWithType(b)
555testKeywords(b)
556testNormInherit()
557testICChecking(b);