From 2ef281a1c2ca8115e83f9540327adb34bd58efca Mon Sep 17 00:00:00 2001 From: Martin Sajti Date: Tue, 20 May 2025 10:33:34 +0200 Subject: [PATCH 1/3] Fix invlid code after primitive type refactor Change-Id: I0615ceabceb8503b09b036dbbb595fe63eda1bc7 Signed-off-by: Martin Sajti --- .../emitter/ani/ets/@ohos.events.json.ets | 430 +++++++++--------- 1 file changed, 216 insertions(+), 214 deletions(-) diff --git a/frameworks/emitter/ani/ets/@ohos.events.json.ets b/frameworks/emitter/ani/ets/@ohos.events.json.ets index 4e9a205..2d63b76 100644 --- a/frameworks/emitter/ani/ets/@ohos.events.json.ets +++ b/frameworks/emitter/ani/ets/@ohos.events.json.ets @@ -1,214 +1,216 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -type valueType = NullishType; - -class RecordWriter { - private buffer = new StringBuilder(); - private store = new Set(); - - public write(obj: Object): String { - this.writeObject(obj); - return this.buffer.toString(); - } - - private writeObject(obj: NullishType): void { - if (obj === null) { - this.buffer.append('null'); - } else if (obj === undefined) { - this.buffer.append('undefined'); - } else if (obj instanceof String) { - this.buffer.append(JSON.stringify(obj as String)); - } else if (this.writeValueType(obj as int)) { - // nothing to do - } else if (obj instanceof Array) { - this.writeArray(obj as Object as Array); - } else if (obj instanceof Record) { - this.writeRecord(obj as Object as Record); - } else { - const objType = Type.of(obj); - if (objType instanceof ArrayType) { - this.writeBuildArray(obj as int, Value.of(obj) as ArrayValue); - } else { - this.buffer.append('null'); - } - } - } - - private writeValueType(obj: Object): boolean { - if (obj instanceof Boolean) { - this.buffer.append(JSON.stringify(obj.unboxed())); - return true; - } else if (obj instanceof Byte) { - this.buffer.append(JSON.stringify(obj.unboxed())); - return true; - } else if (obj instanceof Char) { - this.buffer.append(JSON.stringify(obj.unboxed())); - return true; - } else if (obj instanceof Short) { - this.buffer.append(JSON.stringify(obj.unboxed())); - return true; - } else if (obj instanceof Int) { - this.buffer.append(JSON.stringify(obj.unboxed())); - return true; - } else if (obj instanceof Long) { - this.buffer.append(JSON.stringify(obj.unboxed())); - return true; - } else if (obj instanceof Float) { - this.buffer.append(JSON.stringify(obj.unboxed())); - return true; - } else if (obj instanceof Double) { - this.buffer.append(JSON.stringify(obj.unboxed())); - return true; - } else if (obj instanceof BigInt) { - this.buffer.append(JSON.stringify(obj)); - return true; - } else { - return false; - } - } - - private writeArray(arr: Array): void { - this.buffer.append('['); - const length = arr.length.toInt(); - this.checkReferencesCycle(arr); - this.store.add(arr); - for (let idx = 0; idx < length; idx++) { - if (arr[idx] == null) { - this.buffer.append('null'); - } else { - this.writeObject(arr[idx]); - } - if (idx < length - 1) { - this.buffer.append(','); - } - } - this.store.delete(arr); - this.buffer.append(']'); - } - - private writeBuildArray(arr: Object, arrayValue: ArrayValue): void { - this.buffer.append('['); - const length = arrayValue.getLength().toInt(); - this.checkReferencesCycle(arr); - this.store.add(arr); - for (let idx = 0; idx < length; idx++) { - let member = arrayValue.getElement(idx).getData(); - if (member == null) { - this.buffer.append('null'); - } else { - this.writeObject(member); - } - if (idx < length - 1) { - this.buffer.append(','); - } - } - this.store.delete(arr); - this.buffer.append(']'); - } - - private writeRecord(rec: Record): void { - this.buffer.append('{'); - this.checkReferencesCycle(rec); - this.store.add(rec); - let isFirst = true; - for (let key of rec.keys()) { - if (rec[key] !== undefined) { - if (!isFirst) { - this.buffer.append(','); - } else { - isFirst = false; - } - this.buffer.append(JSON.stringify(key as String)); - this.buffer.append(':'); - this.writeObject(rec[key]); - } - } - this.store.delete(rec); - this.buffer.append('}'); - } - - private checkReferencesCycle(obj: Object): void { - if (this.store.has(obj)) { - throw new TypeError('cyclic object value'); - } - } -} - -export class RecordSerializeTool { - public static stringifyNoThrow(obj: Record): String { - try { - return RecordSerializeTool.stringify(obj as Object as Record); - } catch (err) { - return err.toString(); - } - } - - public static parseNoThrow(text: string): Record { - try { - return RecordSerializeTool.parse(text) as Object as Record; - } catch (err) { - console.log('err: ' + err.toString()); - return new Record(); - } - } - - public static stringify(obj: Record): String { - return new RecordWriter().write(obj); - } - - public static parse(text: string): Record { - let jsonValue = JSONParser.parse(text); - let res = RecordSerializeTool.jsonValue2Object(jsonValue); - if (!(res instanceof Record)) { - throw new TypeError('RecordSerializeTool parse only used for Record'); - } - return res as Record; - } - - private static jsonValue2Object(value: JSONValue): string | number | boolean | null | - Array | Record { - if (value instanceof JSONString) { - return value.value; - } else if (value instanceof JSONNumber) { - return new Double(value.value); - } else if (value instanceof JSONTrue) { - return new Boolean(true); - } else if (value instanceof JSONFalse) { - return new Boolean(false); - } else if (value instanceof JSONNull) { - return null; - } else if (value instanceof JSONArray) { - let obj = value as JSONArray; - let values = obj.values; - let result: Array = new Array(); - for (let i: int = 0; i < values.length; i++) { - result.push(RecordSerializeTool.jsonValue2Object(values[i])); - } - return result; - } else if (value instanceof JSONObject) { - let obj = value as JSONObject; - let keys: Array = obj.keys_; - let values: Array = obj.values; - let result: Record = new Record(); - for (let i: int = 0; i < keys.length; i++) { - result[keys[i].value] = RecordSerializeTool.jsonValue2Object(values[i]); - } - return result; - } else { - throw new TypeError('unknown JSONValue'); - } - } -} +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +type valueType = NullishType; + +class RecordWriter { + private buffer = new StringBuilder(); + private store = new Set(); + + public write(obj: Object): String { + this.writeObject(obj); + return this.buffer.toString(); + } + + private writeObject(obj: NullishType): void { + if (obj === null) { + this.buffer.append('null'); + } else if (obj === undefined) { + this.buffer.append('undefined'); + } else if (obj instanceof String) { + this.buffer.append(JSON.stringify(obj as String)); + } else if (this.writeValueType(obj as int)) { + // nothing to do + } else if (obj instanceof Array) { + this.writeArray(obj as Object as Array); + } else if (obj instanceof Record) { + this.writeRecord(obj as Object as Record); + } else { + const objType = Type.of(obj); + if (objType instanceof ArrayType) { + this.writeBuildArray(obj as int, Value.of(obj) as ArrayValue); + } else { + this.buffer.append('null'); + } + } + } + + private writeValueType(obj: Object): boolean { + if (obj instanceof Boolean) { + this.buffer.append(JSON.stringify(obj.unboxed())); + return true; + } else if (obj instanceof Byte) { + this.buffer.append(JSON.stringify(obj.unboxed())); + return true; + } else if (obj instanceof Char) { + this.buffer.append(JSON.stringify(obj.unboxed())); + return true; + } else if (obj instanceof Short) { + this.buffer.append(JSON.stringify(obj.unboxed())); + return true; + } else if (obj instanceof Int) { + this.buffer.append(JSON.stringify(obj.unboxed())); + return true; + } else if (obj instanceof Long) { + this.buffer.append(JSON.stringify(obj.unboxed())); + return true; + } else if (obj instanceof Float) { + this.buffer.append(JSON.stringify(obj.unboxed())); + return true; + } else if (obj instanceof Double) { + this.buffer.append(JSON.stringify(obj.unboxed())); + return true; + } else if (obj instanceof BigInt) { + this.buffer.append(JSON.stringify(obj)); + return true; + } else { + return false; + } + } + + private writeArray(arr: Array): void { + this.buffer.append('['); + const length = arr.length.toInt(); + const length = arr.length.toInt(); + this.checkReferencesCycle(arr); + this.store.add(arr); + for (let idx = 0; idx < length; idx++) { + if (arr[idx] == null) { + this.buffer.append('null'); + } else { + this.writeObject(arr[idx]); + } + if (idx < length - 1) { + this.buffer.append(','); + } + } + this.store.delete(arr); + this.buffer.append(']'); + } + + private writeBuildArray(arr: Object, arrayValue: ArrayValue): void { + this.buffer.append('['); + const length = arrayValue.getLength().toInt(); + const length = arrayValue.getLength().toInt(); + this.checkReferencesCycle(arr); + this.store.add(arr); + for (let idx = 0; idx < length; idx++) { + let member = arrayValue.getElement(idx).getData(); + if (member == null) { + this.buffer.append('null'); + } else { + this.writeObject(member); + } + if (idx < length - 1) { + this.buffer.append(','); + } + } + this.store.delete(arr); + this.buffer.append(']'); + } + + private writeRecord(rec: Record): void { + this.buffer.append('{'); + this.checkReferencesCycle(rec); + this.store.add(rec); + let isFirst = true; + for (let key of rec.keys()) { + if (rec[key] !== undefined) { + if (!isFirst) { + this.buffer.append(','); + } else { + isFirst = false; + } + this.buffer.append(JSON.stringify(key as String)); + this.buffer.append(':'); + this.writeObject(rec[key]); + } + } + this.store.delete(rec); + this.buffer.append('}'); + } + + private checkReferencesCycle(obj: Object): void { + if (this.store.has(obj)) { + throw new TypeError('cyclic object value'); + } + } +} + +export class RecordSerializeTool { + public static stringifyNoThrow(obj: Record): String { + try { + return RecordSerializeTool.stringify(obj as Object as Record); + } catch (err) { + return err.toString(); + } + } + + public static parseNoThrow(text: string): Record { + try { + return RecordSerializeTool.parse(text) as Object as Record; + } catch (err) { + console.log('err: ' + err.toString()); + return new Record(); + } + } + + public static stringify(obj: Record): String { + return new RecordWriter().write(obj); + } + + public static parse(text: string): Record { + let jsonValue = JSONParser.parse(text); + let res = RecordSerializeTool.jsonValue2Object(jsonValue); + if (!(res instanceof Record)) { + throw new TypeError('RecordSerializeTool parse only used for Record'); + } + return res as Record; + } + + private static jsonValue2Object(value: JSONValue): string | number | boolean | null | + Array | Record { + if (value instanceof JSONString) { + return value.value; + } else if (value instanceof JSONNumber) { + return new Double(value.value); + } else if (value instanceof JSONTrue) { + return new Boolean(true); + } else if (value instanceof JSONFalse) { + return new Boolean(false); + } else if (value instanceof JSONNull) { + return null; + } else if (value instanceof JSONArray) { + let obj = value as JSONArray; + let values = obj.values; + let result: Array = new Array(); + for (let i: int = 0; i < values.length; i++) { + result.push(RecordSerializeTool.jsonValue2Object(values[i])); + } + return result; + } else if (value instanceof JSONObject) { + let obj = value as JSONObject; + let keys: Array = obj.keys_; + let values: Array = obj.values; + let result: Record = new Record(); + for (let i: int = 0; i < keys.length; i++) { + result[keys[i].value] = RecordSerializeTool.jsonValue2Object(values[i]); + } + return result; + } else { + throw new TypeError('unknown JSONValue'); + } + } +} -- Gitee From b2c862ea088dbd6ac8fa01c8267b6bbd2d0b3b6d Mon Sep 17 00:00:00 2001 From: lijunru Date: Sun, 13 Jul 2025 00:37:58 +0800 Subject: [PATCH 2/3] cherry-pick 0328 to 0702 Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/ICLUQW Signed-off-by: lijunru Change-Id: Ia2409e8668df5622a7b209e6d572a6ccaf8bb724 --- .../emitter/ani/ets/@ohos.events.emitter.ets | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/frameworks/emitter/ani/ets/@ohos.events.emitter.ets b/frameworks/emitter/ani/ets/@ohos.events.emitter.ets index 8b51a6d..4c08476 100644 --- a/frameworks/emitter/ani/ets/@ohos.events.emitter.ets +++ b/frameworks/emitter/ani/ets/@ohos.events.emitter.ets @@ -90,13 +90,6 @@ namespace emitter { } } - export function on(eventId: string, callback: Callback): void { - if (eventId === '') { - return; - } - emitter.OnOrOnceStringSync(eventId, false, callback, "eventData"); - } - export function once(eventId: string, callback: Callback): void { if (eventId === '') { return; @@ -111,13 +104,6 @@ namespace emitter { OffStringIdSync(eventId); } - export function off(eventId: string, callback: Callback): void { - if (eventId === '') { - return; - } - emitter.OffStringSync(eventId, callback); - } - export function off(eventId: string, callback: Callback | Callback>): void { if (eventId === '') { return; -- Gitee From ed44b3f65423683e55ef8fa79396b6e7db8f6e43 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Fri, 11 Jul 2025 20:13:39 +0800 Subject: [PATCH 3/3] modify Signed-off-by: Michael Wang Change-Id: I5e0f837c01d05f1b25f38e221c7bcefbfdaac557 --- frameworks/BUILD.gn | 2 +- frameworks/emitter/ani/ets/@ohos.events.json.ets | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn index dedd943..7d7f6e0 100644 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -31,4 +31,4 @@ group("emitter_packages") { } group("eventhandler_native_target") { deps = [ "native:eventhandler_native" ] -} \ No newline at end of file +} diff --git a/frameworks/emitter/ani/ets/@ohos.events.json.ets b/frameworks/emitter/ani/ets/@ohos.events.json.ets index 2d63b76..2c5e8b4 100644 --- a/frameworks/emitter/ani/ets/@ohos.events.json.ets +++ b/frameworks/emitter/ani/ets/@ohos.events.json.ets @@ -40,7 +40,7 @@ class RecordWriter { } else { const objType = Type.of(obj); if (objType instanceof ArrayType) { - this.writeBuildArray(obj as int, Value.of(obj) as ArrayValue); + this.writeBuildArray(obj as int, reflect.Value.of(obj) as ArrayValue); } else { this.buffer.append('null'); } @@ -83,7 +83,6 @@ class RecordWriter { private writeArray(arr: Array): void { this.buffer.append('['); const length = arr.length.toInt(); - const length = arr.length.toInt(); this.checkReferencesCycle(arr); this.store.add(arr); for (let idx = 0; idx < length; idx++) { @@ -103,7 +102,6 @@ class RecordWriter { private writeBuildArray(arr: Object, arrayValue: ArrayValue): void { this.buffer.append('['); const length = arrayValue.getLength().toInt(); - const length = arrayValue.getLength().toInt(); this.checkReferencesCycle(arr); this.store.add(arr); for (let idx = 0; idx < length; idx++) { -- Gitee