• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ArkCompiler Subsystem Changelog
2
3## cl.arkcompiler.1 Behavior Change for Property Lookup and Setting on TypedArray
4
5**Access Level**
6
7Public API
8
9**Reason for Change**
10
11The ECMAScript specification defines the behavior of calling **Reflect.set** and** [[HasProperty]]** on JS objects, both of which involve traversing the prototype chain.
12If a TypedArray object is involved in the process of traversing the prototype chain, the current runtime implementation causes the return value to be inconsistent with the expected result defined in the specifications.
13
14**Impact of the Change**
15
16This change is a non-compatible change.
17
18**Before**
19
20- Case 1: When an object (o) has a prototype that is a TypedArray object (ta), using **in** to check whether a property exists on (o) returns an incorrect result if the key being checked is an index greater than the length of (ta).
21- Case 2: When an object (o) has a prototype that is a TypedArray object (ta), using **in** to check whether a property exists on (o) returns an incorrect result if the key being checked is a Number type and exists on the prototype of (ta).
22- Case 3: Using **Reflect.set** to set a value for a property key on an object (o), where the receiver in the local call is a TypedArray object (ta), returns an incorrect result if the property key is a NumericIndex type with a value greater than the length of (ta).
23
24```js
25// Case 1: When an object (o) has a prototype that is a TypedArray object (ta), using in to check whether a property exists on (o), and the key being checked is an index greater than the length of (ta).
26function BehaviorChange1() {
27    // Create a TypedArray object using Int8Array as an example.
28    const typedArray = new Int8Array(5);
29    typedArray[1] = 42;
30
31    // Create an Object object and set its prototype to the TypedArray object.
32    const obj = Object.create(typedArray);
33    // Traverse the prototype chain to check whether the object has the property "6".
34    print(6 in obj); // Output: true
35}
36BehaviorChange1();
37
38// Case 2: When an object (o) has a prototype that is a TypedArray object (ta), using in to check whether a property exists on (o), and the key being checked is a Number type that exists on the prototype of (ta).
39function BehaviorChange2() {
40    // Create a regular Object object holding the property "6".
41    const obj1 = {};
42    obj1[6] = "b";
43
44    // Create a TypedArray object using Int8Array as an example.
45    const typedArray = new Int8Array(5);
46    typedArray[1] = 42;
47    // Set the prototype of typedArray to the obj1 object created in the first step.
48    typedArray.__proto__ = obj1;
49
50    // Create an Object object and set its prototype to the TypedArray object.
51    const obj = Object.create(typedArray);
52    // Traverse the prototype chain to check whether the object has the property "6".
53    print("6" in obj); // Output: true
54}
55BehaviorChange2()
56
57// Case 3: Using Reflect.set to set a value for a property key on an object (o), where the receiver in the local call is a TypedArray object (ta), and the property key is a NumericIndex type with a value greater than the length of (ta).
58function BehaviorChange3() {
59    // The second parameter is propKey, the third parameter is value, and the fourth parameter is receiver.
60    print(Reflect.set({}, 100, 123, new Int32Array())); // Output: true
61}
62BehaviorChange3();
63```
64
65**After**
66
67- Case 1: When an object (o) has a prototype that is a TypedArray object (ta), using **in** to check whether a property exists on (o) returns the correct result if the key being checked is an index greater than the length of (ta). (incompatible change)
68- Case 2: When an object (o) has a prototype that is a TypedArray object (ta), using **in** to check whether a property exists on (o) returns the correct result if the key being checked is a Number type that exists on the prototype of (ta). (incompatible change)
69- Case 3: Using **Reflect.set** to set a value for a property key on an object (o), where the receiver in the local call is a TypedArray object (ta), returns the correct result if the property key is a NumericIndex type with a value greater than the length of (ta). (incompatible change)
70
71```js
72// Case 1: When an object (o) has a prototype that is a TypedArray object (ta), using in to check whether a property exists on (o), and the key being checked is an index greater than the length of (ta).
73function BehaviorChange1() {
74    // Create a TypedArray object using Int8Array as an example.
75    const typedArray = new Int8Array(5);
76    typedArray[1] = 42;
77
78    // Create an Object object and set its prototype to the TypedArray object.
79    const obj = Object.create(typedArray);
80    // Traverse the prototype chain to check whether the object has the property "6".
81    print(6 in obj); // Output: false (consistent with the specification. The property cannot be found even after traversing the prototype chain.)
82}
83BehaviorChange1();
84
85// Case 2: When an object (o) has a prototype that is a TypedArray object (ta), using in to check whether a property exists on (o), and the key being checked is a Number type that exists on the prototype of (ta).
86function BehaviorChange2() {
87    // Create a regular Object object holding the property "6".
88    const obj1 = {};
89    obj1[6] = "b";
90
91    // Create a TypedArray object using Int8Array as an example.
92    const typedArray = new Int8Array(5);
93    typedArray[1] = 42;
94    // Set the prototype of typedArray to the obj1 object created in the first step.
95    typedArray.__proto__ = obj1;
96
97    // Create an Object object and set its prototype to the TypedArray object.
98    const obj = Object.create(typedArray);
99    // Traverse the prototype chain to check whether the object has the property "6".
100    print("6" in obj); // Output: false (consistent with the specification. The key is a NumericIndex type, and the search stops at typedArray.)
101}
102BehaviorChange2()
103
104// Case 3: Using Reflect.set to set a value for a property key on an object (o), where the receiver in the local call is a TypedArray object (ta), and the property key is a NumericIndex type with a value greater than the length of (ta).
105function BehaviorChange3() {
106    // The second parameter is propKey, the third parameter is value, and the fourth parameter is receiver.
107    print(Reflect.set({}, 100, 123, new Int32Array())); // Output: false (100 is greater than the length of the TypedArray object. Therefore, adding the property fails.)
108}
109BehaviorChange3();
110```
111
112**Start API Level**
113
114API 9
115
116**Change Since**
117
118OpenHarmony 5.1.0.46
119
120**Key API/Component Changes**
121
122Using the ECMAScript built-in functions **in** and **Reflect.set** for property access-related operations on Object type objects with TypedArray objects (including Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, Uint8ClampedArray, Float32Array, Float64Array) as their prototypes.
123
124**Adaptation Guide**
125
126When accessing or setting properties on TypedArray objects in the scenarios described above, pay attention to the changes in return values.
127