1# ArkCompiler Subsystem Changelog
2
3## cl.arkcompiler.1 Callback Function Declaration of map Provided by Sendable TypedArray Is Changed
4
5**Access Level**
6
7Public API
8
9**Reason for Change**
10
11The sendable TypedArray container provides the **map** API. This API performs an operation or conversion (through the return value of **callbackFn**) on each element in a typed array and returns a new typed array that contains the elements after the processing.
12
13The following uses Uint8Array as an example. Before the change, the **callbackFn** function declaration of the **map** API has no return value. As a result, the data after conversion is lost.
14- Declare the callback function of the **map** API as follows: **map(callbackFn: TypedArrayForEachCallback\<number, Uint8Array>): Uint8Array**.
15- **TypedArrayForEachCallback** is defined without a return value, as follows: **type TypedArrayForEachCallback\<ElementType, ArrayType> = (value: ElementType, index: number, array: ArrayType) => void**.
16
17**Change Impact**
18
19This change is a non-compatible change.
20
21**Before Change**
22
23- Case 1: The **callbackFn** function in the **map** API has no return value. The code can be compiled, but the implementation is different from that expected.
24- Case 2: The **callbackFn** function in the **map** API has a return value, the type of which is not number. The code can be compiled, and the implementation is the same as that expected.
25- Case 3: The **callbackFn** function in the **map** API has a return value, the type of which is number. The code can be compiled, and the implementation is the same as that expected.
26
27  ```
28  let arr = [1, 2, 3, 4, 5];
29
30  // Create a Uint8Array.
31  let uint8: collections.Uint8Array = new collections.Uint8Array(arr);
32
33  // Case 1: The feature of map is not implemented. callbackFn has no return value, and the map API returns a new collections.Uint8Array.
34  let zeroMappedArray: collections.Uint8Array = uint8.map((value: number) => {}); // The compilation is successful.
35  console.info('' + zeroMappedArray); // Output: collections.Uint8Array [0, 0, 0, 0, 0]
36
37  // Case 2: The feature of map is implemented. callbackFn returns an element value (in the form of a string) after map is called, and the map API returns a new collections.Uint8Array.
38  let wrongTypeMapped: collections.Uint8Array = uint8.map((value: number) => value + "1"); // The compilation is successful.
39  console.info('' + wrongTypeMapped); //Output: collections.Uint8Array [11, 21, 31, 41, 51]
40
41  // Case 3: The feature of map is implemented. callbackFn returns an element value after map is called, and the map API returns a new collections.Uint8Array.
42  let normalMapped: collections.Uint8Array = uint8.map((value: number) => value * 2); // The compilation is successful.
43  console.info('' + normalMapped); // Output: collections.Uint8Array [2, 4, 6, 8, 10]
44  ```
45
46**After Change**
47
48- Case 1: The **callbackFn** function in the **map** API has no return value. The code cannot be compiled. (This change is incompatible.)
49- Case 2: The **callbackFn** function in the **map** API has a return value, the type of which is not number. The code cannot be compiled. (This change is incompatible.)
50- Case 3: The **callbackFn** function in the **map** API has a return value, the type of which is number. The code can be compiled, and the implementation is the same as that expected. (This change is compatible.)
51
52
53    ```
54    let arr = [1, 2, 3, 4, 5];
55
56    // Create a Uint8Array.
57    let uint8: collections.Uint8Array = new collections.Uint8Array(arr);
58
59    // Case 1: The feature of map is not implemented. callbackFn has no return value, and the map API returns a new collections.Uint8Array.
60    let zeroMappedArray: collections.Uint8Array = uint8.map((value: number) => {}); // An incompatible change. The compilation fails.
61
62    // Case 2: The feature of map is implemented. callbackFn returns a string after map is called, and the map API returns a new collections.Uint8Array.
63    let wrongTypeMapped: collections.Uint8Array = uint8.map((value: number) => value + "1"); // An incompatible change. The compilation fails.
64
65    // Case 3: The feature of map is implemented. callbackFn returns an element value after map is called, and the map API returns a new collections.Uint8Array.
66    let normalMapped: collections.Uint8Array = uint8.map((value: number) => value * 2); // The compilation is successful.
67    console.info('' + normalMapped); // Output: collections.Uint8Array [2, 4, 6, 8, 10]
68    ```
69
70**Start API Level**
71
72API version 12
73
74**Change Since**
75
76OpenHarmony SDK 5.0.0.31
77
78**Key API/Component Changes**
79
80**map** API of TypedArray (including Int8Array/Uint8Array/Int16Array/Uint16Array/Int32Array/Uint32Array) in the **/interface/sdk-js/arkts/@arkts.collections.d.ets** file.
81
82**Adaptation Guide**
83
84- In the preceding case 2, you can make adaptation as follows:
85
86  ```
87  let wrongTypeMapped: collections.Uint8Array = uint8.map((value: number) => parseInt(value + "1")); // Use parseInt to convert a string to a number.
88  ```
89
90- For details, see the following sample code:
91
92  [ArkTS Collections - Typed Array](../../../application-dev/reference/apis-arkts/js-apis-arkts-collections.md#collectionstypedarray)
93
94## cl.arkcompiler.2 Compilation Check Enhanced for ArkTS Sendable Syntax Rules
95
96**Access Level**
97
98Others
99
100**Reason for Change**
101
102A sendable object must comply with the [usage rules](../../..//application-dev/arkts-utils/arkts-sendable.md#sendable-usage-rules). In sendable generic class scenarios where some constraints should be made, the compiler does not check for these constraints. As a result, a sendable object using these syntaxes runs abnormally in concurrent scenarios, but no compilation error is reported. In this version update, a compile-time check is added for these constraints. You can find the code that fails to meet the sendable usage constraints earlier through compile-time errors, reducing fault locating costs at the runtime.
103
104**Change Impact**
105
106This change is a non-compatible change.
107
108
109Before the change: When a sendable generic class is declared as a type, the formal parameter of the type can be of a non-sendable type. In this case, no error message is displayed on the DevEco Studio editing page, and no compilation error is reported.
110
111After the change: When a sendable generic class is declared as a type, the formal parameter of the type cannot be of a non-sendable type. Otherwise, an error message is displayed on the DevEco Studio editing page, and a compilation error is reported.
112
113For variables, parameters, and return values that are declared using a sendable generic class but assigned to a non-sendable object, if they are used in concurrent instance sharing scenarios, runtime exceptions occur before the change, whereas compilation errors are reported after the change. If they are used as common objects, no runtime error is reported before the change, whereas compilation errors are reported after the change.
114
115
116Example scenarios:
117
118Sendable Generic Class Constraints
119
120Scenario 1: When a sendable object is shared by multiple threads, a runtime exception is reported before the change, whereas a compilation error is reported after the change.
121
122Before change:
123
124```ts
125// declaration.ets
126export class NonSendableClass {};
127
128// main.ets
129import { NonSendableClass } from './declaration';
130import collections from '@arkts.collections';
131
132@Sendable
133class SendableClass {
134    private arr: collections.Array<NonSendableClass> = new collections.Array();
135    constructor() {
136        this.arr.push(new NonSendableClass()); // Runtime ERROR
137    }
138}
139let sendableclassObject: SendableClass = new SendableClass();
140```
141
142After change:
143
144```ts
145// declaration.ets
146export class NonSendableClass {};
147
148// main.ets
149import { NonSendableClass } from './declaration';
150import collections from '@arkts.collections';
151
152@Sendable
153class SendableClass {
154    private arr: collections.Array<NonSendableClass> = new collections.Array(); // ArkTS compile-time error
155    constructor() {
156        this.arr.push(new NonSendableClass());
157    }
158}
159let sendableclassObject: SendableClass = new SendableClass();
160```
161
162Scenario 2: When a sendable object is used as a common object, no error is reported before the change, whereas a compilation error is reported after the change. This change is incompatible.
163
164Before change:
165
166```ts
167@Sendable
168class SendableClassA<T> {
169    one: string = '1';
170}
171class NoneSendableClassA<T> {
172    one: string = '1';
173}
174let sendableObjectA: SendableClassA<NoneSendableClassA<number>> = new SendableClassA();
175```
176
177After change:
178
179```ts
180@Sendable
181class SendableClassA<T> {
182    one: string = '1';
183}
184class NoneSendableClassA<T> {
185    one: string = '1';
186}
187let sendableObjectA: SendableClassA<NoneSendableClassA<number>> = new SendableClassA(); // ArkTS compile-time error
188```
189
190**Start API Level**
191
192API version 12
193
194**Change Since**
195
196OpenHarmony SDK 5.0.0.31
197
198**Key API/Component Changes**
199
200N/A
201
202**Adaptation Guide**
203
204The type of a sendable generic class must be sendable.
205
206## cl.arkcompiler.3 Compilation Check Enhanced for ArkTS Sendable Syntax Rules for Assigning Values
207
208**Access Level**
209
210Others
211
212**Reason for Change**
213
214Sendable value assignment must comply with the [usage rules](../../..//application-dev/arkts-utils/arkts-sendable.md#sendable-usage-rules). However, a non-sendable object can be assigned to the sendable type, and the compiler does not provide a check for this scenario. As a result, a runtime exception occurs when a non-sendable object is used as a sendable object, but no compilation error is reported. In this version update, a compile-time check is added for the constraint. You can find the code that fails to meet the sendable usage constraints earlier through compile-time errors, reducing fault locating costs at the runtime.
215
216Error object: a variable, parameter, or return value that is declared using the sendable type or interface but assigned to a non-sendable object.
217
218**Change Impact**
219
220This change is a non-compatible change.
221
222Before the change: In some scenarios where a non-sendable object is assigned to the sendable type, no error message is displayed on the DevEco Studio editing page, and no compilation error is reported.
223
224After the change: In some scenarios where a non-sendable object is assigned to the sendable type, an error message is displayed on the DevEco Studio editing page, and a compilation error is reported.
225
226When the error object is used as a sendable object, a runtime exception is reported before the change, whereas a compilation error is reported after the change. When the error object is used as a common object, no error is reported before the change, whereas a compilation error is reported after the change. Before the change, a non-sendable object can be assigned to the sendable type in some scenarios. After the change, a non-sendable object cannot be assigned to the sendable type.
227
228An error is reported in the following scenarios:
229
230Case 1: The error object is used as a sendable object.
231
232Before change:
233
234```ts
235// declaration.ets
236export class NonSendableClass {};
237@Sendable
238export class SendableClass {};
239
240export class NonSendableClassT<T> {};
241@Sendable
242export class SendableClassT<T> {};
243
244// main.ets
245import { NonSendableClass, SendableClass, NonSendableClassT, SendableClassT } from './declaration';
246import collections from '@arkts.collections';
247
248@Sendable
249class SendableData {
250    propA: SendableClass = new NonSendableClass(); // Runtime ERROR
251    propB: SendableClassT<number>;
252    propC: SendableClass;
253    propD: SendableClass;
254    propE: SendableClass;
255
256    constructor(sendableT: SendableClassT<number>) {
257      const sendableList: SendableClass[] = [new NonSendableClass()];
258      this.propB = new NonSendableClassT<number>(); // Runtime ERROR
259      this.propC = this.getSendable(); // Runtime ERROR
260      this.propD = sendableList[0]; // Runtime ERROR
261      this.propE = sendableT; // Runtime ERROR
262    }
263
264    getSendable(): SendableClass {
265        return new NonSendableClass();
266    }
267}
268
269new SendableData(new NonSendableClassT<number>());
270
271const sendable: SendableClassT<number> = new NonSendableClassT<number>();
272const sendableArray: collections.Array<SendableClass> = new collections.Array<SendableClass>();
273sendableArray.push(sendable); // Runtime ERROR
274
275```
276
277After change:
278
279```ts
280// declaration.ets
281export class NonSendableClass {};
282@Sendable
283export class SendableClass {};
284
285export class NonSendableClassT<T> {};
286@Sendable
287export class SendableClassT<T> {};
288
289// main.ets
290import { NonSendableClass, SendableClass, NonSendableClassT, SendableClassT } from './declaration';
291import collections from '@arkts.collections';
292
293@Sendable
294class SendableData {
295    propA: SendableClass = new NonSendableClass(); // ArkTS compile-time error
296    propB: SendableClassT<number>;
297    propC: SendableClass;
298    propD: SendableClass;
299    propE: SendableClass;
300
301    constructor(sendableT: SendableClassT<number>) {
302      const sendableList: SendableClass[] = [new NonSendableClass()]; // ArkTS compile-time error
303      this.propB = new NonSendableClassT<number>(); // ArkTS compile-time error
304      this.propC = this.getSendable();
305      this.propD = sendableList[0];
306      this.propE = sendableT;
307    }
308
309    getSendable(): SendableClass {
310        return new NonSendableClass(); // ArkTS compile-time error
311    }
312}
313
314new SendableData(new NonSendableClassT<number>()); // ArkTS compile-time error
315
316const sendable: SendableClassT<number> = new NonSendableClassT<number>(); // ArkTS compile-time error
317const sendableArray: collections.Array<SendableClass> = new collections.Array<SendableClass>();
318sendableArray.push(sendable);
319
320```
321
322Case 2: The error object is used as a common object.
323
324Before change:
325
326```ts
327class NonSendableClass {};
328@Sendable
329class SendableClass {};
330
331class NonSendableClassT<T> {};
332@Sendable
333class SendableClassT<T> {};
334
335function getSendable(): SendableClass {
336    return new NonSendableClass();
337}
338
339const objectA: SendableClass = getSendable();
340const objectB: SendableClassT<number> = new NonSendableClassT<number>();
341
342```
343
344After change:
345
346```ts
347class NonSendableClass {};
348@Sendable
349class SendableClass {};
350
351class NonSendableClassT<T> {};
352@Sendable
353class SendableClassT<T> {};
354
355function getSendable(): SendableClass {
356    return new NonSendableClass(); // ArkTS compile-time error
357}
358
359const objectA: SendableClass = getSendable();
360const objectB: SendableClassT<number> = new NonSendableClassT<number>(); // ArkTS compile-time error
361
362```
363
364**Start API Level**
365
366API version 12
367
368**Change Since**
369
370OpenHarmony SDK 5.0.0.31
371
372**Key API/Component Changes**
373
374N/A
375
376**Adaptation Guide**
377
378Do not assign a non-sendable object to a sendable variable, parameter, or return value.
379