From 0fa9f38d6a49c023091722b9dfce24b26aedc951 Mon Sep 17 00:00:00 2001 From: liyou Date: Thu, 17 Jul 2025 17:41:29 +0800 Subject: [PATCH] fix code style and illegal param Signed-off-by: liyou --- interop/src/arkts/DeserializerBase.ts | 52 ++++++++++-------- interop/src/arkts/SerializerBase.ts | 78 +++++++++++++++------------ 2 files changed, 74 insertions(+), 56 deletions(-) diff --git a/interop/src/arkts/DeserializerBase.ts b/interop/src/arkts/DeserializerBase.ts index fe7c02666..81784ad90 100644 --- a/interop/src/arkts/DeserializerBase.ts +++ b/interop/src/arkts/DeserializerBase.ts @@ -21,7 +21,7 @@ import { Tags, CallbackResource } from "./SerializerBase"; import { ResourceHolder, Disposable } from "./ResourceManager" export class DeserializerBase implements Disposable { - private position : int64 = 0 + private _position : int64 = 0 private _buffer: KSerializerBuffer private readonly _isOwnBuffer: boolean; private readonly _length: int32 @@ -54,7 +54,7 @@ export class DeserializerBase implements Disposable { const newBuffer = this._buffer; this._length = length - this.position = newBuffer + this._position = newBuffer this._end = newBuffer + length; } @@ -62,7 +62,7 @@ export class DeserializerBase implements Disposable { if (this._isOwnBuffer) { InteropNativeModule._Free(this._buffer) this._buffer = 0 - this.position = 0 + this._position = 0 } } @@ -70,60 +70,64 @@ export class DeserializerBase implements Disposable { return this._buffer } + final currentPosition(): int64 { + return this._position + } + final resetCurrentPosition(): void { - this.position = this._buffer + this._position = this._buffer } final readInt8(): int32 { - const pos = this.position + const pos = this._position const newPos = pos + 1 if (newPos > this._end) { throw new Error(`value size(1) is less than remaining buffer length`) } - this.position = newPos + this._position = newPos return unsafeMemory.readInt8(pos) } final readInt32(): int32 { - const pos = this.position + const pos = this._position const newPos = pos + 4 if (newPos > this._end) { throw new Error(`value size(4) is less than remaining buffer length`) } - this.position = newPos + this._position = newPos return unsafeMemory.readInt32(pos) } final readPointer(): pointer { - const pos = this.position + const pos = this._position const newPos = pos + 8 if (newPos > this._end) { throw new Error(`value size(8) is less than remaining buffer length`) } - this.position = newPos + this._position = newPos return unsafeMemory.readInt64(pos) } final readInt64(): int64 { - const pos = this.position + const pos = this._position const newPos = pos + 8 if (newPos > this._end) { throw new Error(`value size(8) is less than remaining buffer length`) } - this.position = newPos + this._position = newPos return unsafeMemory.readInt64(pos) } final readFloat32(): float32 { - const pos = this.position + const pos = this._position const newPos = pos + 4 if (newPos > this._end) { @@ -131,12 +135,12 @@ export class DeserializerBase implements Disposable { } - this.position = newPos + this._position = newPos return unsafeMemory.readFloat32(pos) } final readFloat64(): double { - const pos = this.position + const pos = this._position const newPos = pos + 8 if (newPos > this._end) { @@ -144,12 +148,12 @@ export class DeserializerBase implements Disposable { } - this.position = newPos + this._position = newPos return unsafeMemory.readFloat64(pos) } final readBoolean(): boolean { - const pos = this.position + const pos = this._position const newPos = pos + 1 if (newPos > this._end) { @@ -157,9 +161,9 @@ export class DeserializerBase implements Disposable { } - this.position = newPos + this._position = newPos const value = unsafeMemory.readInt8(pos); - if (value == 5) + if (value == Tags.UNDEFINED) return false; return value == 1 @@ -180,14 +184,18 @@ export class DeserializerBase implements Disposable { final readString(): string { const encodedLength = this.readInt32(); - const pos = this.position + if (encodedLength <= 0) { + throw new Error(`value size(${encodedLength}) is equal or less than zero`) + } + + const pos = this._position const newPos = pos + encodedLength if (newPos > this._end) { throw new Error(`value size(${encodedLength}) is less than remaining buffer length`) } - this.position = newPos + this._position = newPos // NOTE: skip null-terminated byte return unsafeMemory.readString(pos, encodedLength - 1) } @@ -206,7 +214,7 @@ export class DeserializerBase implements Disposable { } final readNumber(): number | undefined { - const pos = this.position + const pos = this._position const tag = this.readInt8() as int switch (tag) { case Tags.UNDEFINED as int: diff --git a/interop/src/arkts/SerializerBase.ts b/interop/src/arkts/SerializerBase.ts index f58474679..47d3e14e0 100644 --- a/interop/src/arkts/SerializerBase.ts +++ b/interop/src/arkts/SerializerBase.ts @@ -50,6 +50,10 @@ export enum Tags { OBJECT = 107, } +const VALUE_TRUE: number = 1 +const VALUE_FALSE: number = 0 +const MAX_SAFE_INT32: number = 2147483647 + export function runtimeType(value: T): int32 { if (value === undefined) return RuntimeType.UNDEFINED; @@ -121,7 +125,7 @@ class DateSerializer extends CustomSerializer { SerializerBase.registerCustomSerializer(new DateSerializer()) export class SerializerBase implements Disposable { - private position: int64 = 0 + private _position: int64 = 0 private _buffer: KSerializerBuffer private _length: int32 private _last: int64 @@ -162,13 +166,13 @@ export class SerializerBase implements Disposable { let length = 96 this._buffer = InteropNativeModule._Malloc(length.toLong()) this._length = length - this.position = this._buffer + this._position = this._buffer this._last = this._buffer + length - 1; } public release() { this.releaseResources() - this.position = this._buffer + this._position = this._buffer if (this !== SerializerBase.pool[SerializerBase.poolTop - 1]) { throw new Error("Serializers should be release in LIFO order") } @@ -183,7 +187,7 @@ export class SerializerBase implements Disposable { return this._buffer } final length(): int32 { - return (this.position - this._buffer).toInt() + return (this._position - this._buffer).toInt() } final toArray(): byte[] { @@ -199,14 +203,18 @@ export class SerializerBase implements Disposable { let buffSize = this._length const minSize = buffSize + value const resizedSize = Math.max(minSize, Math.round(3 * buffSize / 2)).toInt() + if (value <= 0 || resizedSize <= 0) { + throw new Error(`bug: value(${value}) resizedSize(${resizedSize}) is illegal`) + } let resizedBuffer = InteropNativeModule._Malloc(resizedSize) let oldBuffer = this._buffer - for (let i = oldBuffer; i < this.position; i++) { - let val = unsafeMemory.readInt8(i); - unsafeMemory.writeInt8(resizedBuffer - oldBuffer + i, val) + let offset = this._position - oldBuffer; + for (let i = 0; i < offset; i++) { + let val = unsafeMemory.readInt8(oldBuffer + i); + unsafeMemory.writeInt8(resizedBuffer + i, val) } this._buffer = resizedBuffer - this.position = this.position - oldBuffer + resizedBuffer + this._position = this._position - oldBuffer + resizedBuffer this._length = resizedSize this._last = resizedBuffer + resizedSize - 1; InteropNativeModule._Free(oldBuffer) @@ -304,7 +312,10 @@ export class SerializerBase implements Disposable { this.writeTag(Tags.UNDEFINED) return } - if (value == Math.round(value)) { + if (value > MAX_SAFE_INT32) { + throw new Error(`bug: ${value} is illegal`) + } + if (Number.isInteger(value)) { this.writeTag(Tags.INT32) this.writeInt32(value.toInt()) } else { @@ -314,93 +325,93 @@ export class SerializerBase implements Disposable { } final writeInt8(value: int32) { - let pos = this.position + let pos = this._position let newPos = pos + 1 if (newPos > this._last) { this.updateCapacity(1) - pos = this.position + pos = this._position newPos = pos + 1 } unsafeMemory.writeInt8(pos, value.toByte()) - this.position = newPos + this._position = newPos } final writeInt32(value: int32) { - let pos = this.position + let pos = this._position let newPos = pos + 4 if (newPos > this._last) { this.updateCapacity(4) - pos = this.position + pos = this._position newPos = pos + 4 } unsafeMemory.writeInt32(pos, value) - this.position = newPos + this._position = newPos } final writeInt64(value: int64) { - let pos = this.position + let pos = this._position let newPos = pos + 8 if (newPos > this._last) { this.updateCapacity(8) - pos = this.position + pos = this._position newPos = pos + 8 } unsafeMemory.writeInt64(pos, value) - this.position = newPos + this._position = newPos } final writeFloat32(value: float32) { - let pos = this.position + let pos = this._position let newPos = pos + 4 if (newPos > this._last) { this.updateCapacity(4) - pos = this.position + pos = this._position newPos = pos + 4 } unsafeMemory.writeFloat32(pos, value) - this.position = newPos + this._position = newPos } final writeFloat64(value: double) { - let pos = this.position + let pos = this._position let newPos = pos + 8 if (newPos > this._last) { this.updateCapacity(8) - pos = this.position + pos = this._position newPos = pos + 8 } unsafeMemory.writeFloat64(pos, value) - this.position = newPos + this._position = newPos } final writePointer(value: pointer) { this.writeInt64(value) } final writeBoolean(value: boolean|undefined) { - let pos = this.position + let pos = this._position let newPos = pos + 1 if (newPos > this._last) { this.updateCapacity(1) - pos = this.position + pos = this._position newPos = pos + 1 } - this.position = newPos + this._position = newPos if (value == undefined) - unsafeMemory.writeInt8(pos, 5 as byte); + unsafeMemory.writeInt8(pos, Tags.UNDEFINED as byte); else if (value == true) - unsafeMemory.writeInt8(pos, 1 as byte); + unsafeMemory.writeInt8(pos, VALUE_TRUE as byte); else if (value == false) - unsafeMemory.writeInt8(pos, 0 as byte); + unsafeMemory.writeInt8(pos, VALUE_FALSE as byte); } final writeString(value: string) { const encodedLength = unsafeMemory.getStringSizeInBytes(value) - let pos = this.position + let pos = this._position if (pos + encodedLength + 5 > this._last) { this.updateCapacity(encodedLength + 5) - pos = this.position + pos = this._position } if (encodedLength > 0) @@ -409,7 +420,7 @@ export class SerializerBase implements Disposable { // need check native part fot utf16 cases and probably change this solution. unsafeMemory.writeInt8(pos + encodedLength + 4, 0 as byte) unsafeMemory.writeInt32(pos, encodedLength + 1) - this.position = pos + encodedLength + 4 + 1 + this._position = pos + encodedLength + 4 + 1 } //TODO: Needs to be implemented final writeBuffer(value: NativeBuffer) { @@ -418,4 +429,3 @@ export class SerializerBase implements Disposable { this.writeInt64(value.length) } } - -- Gitee