This is an automated email from the ASF dual-hosted git repository.
chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fory.git
The following commit(s) were added to refs/heads/main by this push:
new 184253ceb feat(javascript): align buffer read/write API with Java
naming (#3346)
184253ceb is described below
commit 184253cebd0956ffa200b88217db587c0cd0f7ad
Author: Talha Amjad <[email protected]>
AuthorDate: Wed Feb 18 14:24:36 2026 +0500
feat(javascript): align buffer read/write API with Java naming (#3346)
## Summary
Aligns the JavaScript buffer API with Java and other xlang
implementations by using explicit `read*` / `write*` method names only.
No compatibility aliases; one-shot breaking change.
## Changes
- **BinaryWriter** (`lib/writer/index.ts`): Removed short-name aliases
(`int8`, `uint8`, `int16`, etc.). Public write API is now only the
existing `write*` methods (e.g. `writeInt8`, `writeUint8`, `writeInt32`,
`writeFloat64`, `writeGetCursor`, etc.).
- **BinaryReader** (`lib/reader/index.ts`): Removed short-name aliases.
Public read API is now only the existing `read*` methods (e.g.
`readInt8`, `readUint8`, `readInt32`, `readGetCursor`, `readSetCursor`,
`readSkip`, etc.).
- **Codegen** (`lib/gen/builder.ts`): `BinaryWriterBuilder` now emits
`write*` calls (e.g. `bw.writeInt8(...)`); `BinaryReaderBuilder` now
emits `read*` calls (e.g. `br.readInt8()`, `br.readGetCursor()`).
- **Tests** (`test/io.test.ts`): Updated dynamic writer calls from
`writer['uint${x}']` / `writer['int${x}']` to `writer['writeUint${x}']`
/ `writer['writeInt${x}']`.
## Requirements met
- [x] Writer methods use `write*` prefix (e.g. `writeInt8`,
`writeUint32`).
- [x] Reader methods use `read*` prefix (e.g. `readInt8`, `readUint32`).
- [x] One-shot breaking rename; no old names, aliases, or deprecation
shims.
- [x] All internal call sites, codegen, and tests updated.
- [x] Runtime behavior and performance unchanged.
## Testing
- `cd javascript && node ./node_modules/.bin/jest --ci` — all 18 suites
pass (112 tests, 1 skipped).
Fixes #3345
---
javascript/packages/fory/lib/fory.ts | 4 +-
javascript/packages/fory/lib/gen/any.ts | 2 +-
javascript/packages/fory/lib/gen/bool.ts | 4 +-
javascript/packages/fory/lib/gen/builder.ts | 160 ++++++++++-----------
javascript/packages/fory/lib/gen/collection.ts | 40 +++---
javascript/packages/fory/lib/gen/datetime.ts | 22 +--
javascript/packages/fory/lib/gen/enum.ts | 12 +-
javascript/packages/fory/lib/gen/ext.ts | 4 +-
javascript/packages/fory/lib/gen/map.ts | 50 +++----
javascript/packages/fory/lib/gen/number.ts | 64 ++++-----
javascript/packages/fory/lib/gen/serializer.ts | 26 ++--
javascript/packages/fory/lib/gen/struct.ts | 16 +--
javascript/packages/fory/lib/gen/typedArray.ts | 28 ++--
javascript/packages/fory/lib/meta/TypeMeta.ts | 40 +++---
javascript/packages/fory/lib/metaStringResolver.ts | 14 +-
javascript/packages/fory/lib/reader/index.ts | 80 +++++------
javascript/packages/fory/lib/referenceResolver.ts | 2 +-
javascript/packages/fory/lib/typeMetaResolver.ts | 6 +-
javascript/packages/fory/lib/writer/index.ts | 56 ++++----
javascript/test/crossLanguage.test.ts | 128 ++++++++---------
javascript/test/io.test.ts | 112 +++++++--------
javascript/test/reader.test.ts | 8 +-
javascript/test/writer.test.ts | 12 +-
23 files changed, 445 insertions(+), 445 deletions(-)
diff --git a/javascript/packages/fory/lib/fory.ts
b/javascript/packages/fory/lib/fory.ts
index a554eb6df..d26b6b6fa 100644
--- a/javascript/packages/fory/lib/fory.ts
+++ b/javascript/packages/fory/lib/fory.ts
@@ -145,7 +145,7 @@ export default class {
this.binaryReader.reset(bytes);
this.typeMetaResolver.reset();
this.metaStringResolver.reset();
- const bitmap = this.binaryReader.uint8();
+ const bitmap = this.binaryReader.readUint8();
if ((bitmap & ConfigFlags.isNullFlag) === ConfigFlags.isNullFlag) {
return null;
}
@@ -177,7 +177,7 @@ export default class {
bitmap |= ConfigFlags.isNullFlag;
}
bitmap |= ConfigFlags.isCrossLanguageFlag;
- this.binaryWriter.uint8(bitmap);
+ this.binaryWriter.writeUint8(bitmap);
// reserve fixed size
this.binaryWriter.reserve(serializer.fixedSize);
// start write
diff --git a/javascript/packages/fory/lib/gen/any.ts
b/javascript/packages/fory/lib/gen/any.ts
index 9874cc6bc..f2f963776 100644
--- a/javascript/packages/fory/lib/gen/any.ts
+++ b/javascript/packages/fory/lib/gen/any.ts
@@ -28,7 +28,7 @@ import { TypeMeta } from "../meta/TypeMeta";
export class AnyHelper {
static detectSerializer(fory: Fory) {
- const typeId = fory.binaryReader.uint8();
+ const typeId = fory.binaryReader.readUint8();
let userTypeId = -1;
if (TypeId.needsUserTypeId(typeId) && typeId !== TypeId.COMPATIBLE_STRUCT)
{
userTypeId = fory.binaryReader.readVarUint32Small7();
diff --git a/javascript/packages/fory/lib/gen/bool.ts
b/javascript/packages/fory/lib/gen/bool.ts
index 6978b0ee0..dfc8ef5be 100644
--- a/javascript/packages/fory/lib/gen/bool.ts
+++ b/javascript/packages/fory/lib/gen/bool.ts
@@ -33,11 +33,11 @@ class BoolSerializerGenerator extends
BaseSerializerGenerator {
}
write(accessor: string): string {
- return this.builder.writer.uint8(`${accessor} ? 1 : 0`);
+ return this.builder.writer.writeUint8(`${accessor} ? 1 : 0`);
}
read(accessor: (expr: string) => string): string {
- return accessor(`${this.builder.reader.uint8()} === 1`);
+ return accessor(`${this.builder.reader.readUint8()} === 1`);
}
getFixedSize(): number {
diff --git a/javascript/packages/fory/lib/gen/builder.ts
b/javascript/packages/fory/lib/gen/builder.ts
index e8610c0fe..23a0afebb 100644
--- a/javascript/packages/fory/lib/gen/builder.ts
+++ b/javascript/packages/fory/lib/gen/builder.ts
@@ -39,16 +39,16 @@ export class BinaryReaderBuilder {
return this.holder;
}
- getCursor() {
- return `${this.holder}.getCursor()`;
+ readGetCursor() {
+ return `${this.holder}.readGetCursor()`;
}
- setCursor(v: number | string) {
- return `${this.holder}.setCursor(${v})`;
+ readSetCursor(v: number | string) {
+ return `${this.holder}.readSetCursor(${v})`;
}
- varInt32() {
- return `${this.holder}.varInt32()`;
+ readVarInt32() {
+ return `${this.holder}.readVarInt32()`;
}
readTaggedInt64() {
@@ -59,20 +59,20 @@ export class BinaryReaderBuilder {
return `${this.holder}.readTaggedUInt64()`;
}
- varInt64() {
- return `${this.holder}.varInt64()`;
+ readVarInt64() {
+ return `${this.holder}.readVarInt64()`;
}
- varUInt32() {
- return `${this.holder}.varUInt32()`;
+ readVarUInt32() {
+ return `${this.holder}.readVarUInt32()`;
}
- varUInt64() {
- return `${this.holder}.varUInt64()`;
+ readVarUInt64() {
+ return `${this.holder}.readVarUInt64()`;
}
- int8() {
- return `${this.holder}.int8()`;
+ readInt8() {
+ return `${this.holder}.readInt8()`;
}
buffer(len: string | number) {
@@ -83,8 +83,8 @@ export class BinaryReaderBuilder {
return `${this.holder}.bufferRef(${len})`;
}
- uint8() {
- return `${this.holder}.uint8()`;
+ readUint8() {
+ return `${this.holder}.readUint8()`;
}
stringUtf8At() {
@@ -103,56 +103,56 @@ export class BinaryReaderBuilder {
return `${this.holder}.stringWithHeader()`;
}
- float64() {
- return `${this.holder}.float64()`;
+ readFloat64() {
+ return `${this.holder}.readFloat64()`;
}
- float32() {
- return `${this.holder}.float32()`;
+ readFloat32() {
+ return `${this.holder}.readFloat32()`;
}
- float16() {
- return `${this.holder}.float16()`;
+ readFloat16() {
+ return `${this.holder}.readFloat16()`;
}
- bfloat16() {
- return `${this.holder}.bfloat16()`;
+ readBfloat16() {
+ return `${this.holder}.readBfloat16()`;
}
- uint16() {
- return `${this.holder}.uint16()`;
+ readUint16() {
+ return `${this.holder}.readUint16()`;
}
- int16() {
- return `${this.holder}.int16()`;
+ readInt16() {
+ return `${this.holder}.readInt16()`;
}
readVarUint32Small7() {
return `${this.holder}.readVarUint32Small7()`;
}
- uint64() {
- return `${this.holder}.uint64()`;
+ readUint64() {
+ return `${this.holder}.readUint64()`;
}
- skip(v: number) {
- return `${this.holder}.skip(${v})`;
+ readSkip(v: number) {
+ return `${this.holder}.readSkip(${v})`;
}
- int64() {
- return `${this.holder}.int64()`;
+ readInt64() {
+ return `${this.holder}.readInt64()`;
}
- sliInt64() {
- return `${this.holder}.sliInt64()`;
+ readSliInt64() {
+ return `${this.holder}.readSliInt64()`;
}
- uint32() {
- return `${this.holder}.uint32()`;
+ readUint32() {
+ return `${this.holder}.readUint32()`;
}
- int32() {
- return `${this.holder}.int32()`;
+ readInt32() {
+ return `${this.holder}.readInt32()`;
}
}
@@ -165,8 +165,8 @@ class BinaryWriterBuilder {
return this.holder;
}
- skip(v: number | string) {
- return `${this.holder}.skip(${v})`;
+ writeSkip(v: number | string) {
+ return `${this.holder}.writeSkip(${v})`;
}
getByteLen() {
@@ -185,40 +185,40 @@ class BinaryWriterBuilder {
return `${this.holder}.arrayBuffer(${buffer}, ${byteOffset},
${byteLength})`;
}
- uint16(v: number | string) {
- return `${this.holder}.uint16(${v})`;
+ writeUint16(v: number | string) {
+ return `${this.holder}.writeUint16(${v})`;
}
- int8(v: number | string) {
- return `${this.holder}.int8(${v})`;
+ writeInt8(v: number | string) {
+ return `${this.holder}.writeInt8(${v})`;
}
- int24(v: number | string) {
- return `${this.holder}.int24(${v})`;
+ writeInt24(v: number | string) {
+ return `${this.holder}.writeInt24(${v})`;
}
- uint8(v: number | string) {
- return `${this.holder}.uint8(${v})`;
+ writeUint8(v: number | string) {
+ return `${this.holder}.writeUint8(${v})`;
}
- int16(v: number | string) {
- return `${this.holder}.int16(${v})`;
+ writeInt16(v: number | string) {
+ return `${this.holder}.writeInt16(${v})`;
}
- varInt32(v: number | string) {
- return `${this.holder}.varInt32(${v})`;
+ writeVarInt32(v: number | string) {
+ return `${this.holder}.writeVarInt32(${v})`;
}
writeVarUint32Small7(v: number | string) {
return `${this.holder}.writeVarUint32Small7(${v})`;
}
- varUInt32(v: number | string) {
- return `${this.holder}.varUInt32(${v})`;
+ writeVarUInt32(v: number | string) {
+ return `${this.holder}.writeVarUInt32(${v})`;
}
- varUInt64(v: number | string) {
- return `${this.holder}.varUInt64(${v})`;
+ writeVarUInt64(v: number | string) {
+ return `${this.holder}.writeVarUInt64(${v})`;
}
writeTaggedInt64(v: number | string) {
@@ -229,8 +229,8 @@ class BinaryWriterBuilder {
return `${this.holder}.writeTaggedUInt64(${v})`;
}
- varInt64(v: number | string) {
- return `${this.holder}.varInt64(${v})`;
+ writeVarInt64(v: number | string) {
+ return `${this.holder}.writeVarInt64(${v})`;
}
stringWithHeader(str: string) {
@@ -241,48 +241,48 @@ class BinaryWriterBuilder {
return `${this.holder}.bufferWithoutMemCheck(${v})`;
}
- uint64(v: number | string) {
- return `${this.holder}.uint64(${v})`;
+ writeUint64(v: number | string) {
+ return `${this.holder}.writeUint64(${v})`;
}
buffer(v: string) { // Accepting Uint8Array as a parameter
return `${this.holder}.buffer(${v})`;
}
- float64(v: number | string) {
- return `${this.holder}.float64(${v})`;
+ writeFloat64(v: number | string) {
+ return `${this.holder}.writeFloat64(${v})`;
}
- float32(v: number | string) {
- return `${this.holder}.float32(${v})`;
+ writeFloat32(v: number | string) {
+ return `${this.holder}.writeFloat32(${v})`;
}
- float16(v: number | string) {
- return `${this.holder}.float16(${v})`;
+ writeFloat16(v: number | string) {
+ return `${this.holder}.writeFloat16(${v})`;
}
- bfloat16(v: number | string) {
- return `${this.holder}.bfloat16(${v})`;
+ writeBfloat16(v: number | string) {
+ return `${this.holder}.writeBfloat16(${v})`;
}
- int64(v: number | string) {
- return `${this.holder}.int64(${v})`;
+ writeInt64(v: number | string) {
+ return `${this.holder}.writeInt64(${v})`;
}
- sliInt64(v: number | string) {
- return `${this.holder}.sliInt64(${v})`;
+ writeSliInt64(v: number | string) {
+ return `${this.holder}.writeSliInt64(${v})`;
}
- uint32(v: number | string) {
- return `${this.holder}.uint32(${v})`;
+ writeUint32(v: number | string) {
+ return `${this.holder}.writeUint32(${v})`;
}
- int32(v: number | string) {
- return `${this.holder}.int32(${v})`;
+ writeInt32(v: number | string) {
+ return `${this.holder}.writeInt32(${v})`;
}
- getCursor() {
- return `${this.holder}.getCursor()`;
+ writeGetCursor() {
+ return `${this.holder}.writeGetCursor()`;
}
setUint32Position(offset: number | string, v: number | string) {
diff --git a/javascript/packages/fory/lib/gen/collection.ts
b/javascript/packages/fory/lib/gen/collection.ts
index c8b7566e4..9278b6dcc 100644
--- a/javascript/packages/fory/lib/gen/collection.ts
+++ b/javascript/packages/fory/lib/gen/collection.ts
@@ -82,7 +82,7 @@ class CollectionAnySerializer {
if (trackingRef) {
flag |= CollectionFlags.TRACKING_REF;
}
- this.fory.binaryWriter.uint8(flag);
+ this.fory.binaryWriter.writeUint8(flag);
return {
serializer,
isSame,
@@ -106,9 +106,9 @@ class CollectionAnySerializer {
} else if (includeNone) {
for (const item of value) {
if (item === null || item === undefined) {
- this.fory.binaryWriter.int8(RefFlags.NullFlag);
+ this.fory.binaryWriter.writeInt8(RefFlags.NullFlag);
} else {
- this.fory.binaryWriter.int8(RefFlags.NotNullValueFlag);
+ this.fory.binaryWriter.writeInt8(RefFlags.NotNullValueFlag);
serializer!.write(item);
}
}
@@ -126,10 +126,10 @@ class CollectionAnySerializer {
} else if (includeNone) {
for (const item of value) {
if (item === null || item === undefined) {
- this.fory.binaryWriter.int8(RefFlags.NullFlag);
+ this.fory.binaryWriter.writeInt8(RefFlags.NullFlag);
} else {
const serializer =
this.fory.typeResolver.getSerializerByData(item);
- this.fory.binaryWriter.int8(RefFlags.NotNullValueFlag);
+ this.fory.binaryWriter.writeInt8(RefFlags.NotNullValueFlag);
serializer!.writeNoRef(item);
}
}
@@ -145,7 +145,7 @@ class CollectionAnySerializer {
read(accessor: (result: any, index: number, v: any) => void,
createCollection: (len: number) => any, fromRef: boolean): any {
void fromRef;
const len = this.fory.binaryReader.readVarUint32Small7();
- const flags = this.fory.binaryReader.uint8();
+ const flags = this.fory.binaryReader.readUint8();
const isSame = flags & CollectionFlags.SAME_TYPE;
const includeNone = flags & CollectionFlags.HAS_NULL;
const refTracking = flags & CollectionFlags.TRACKING_REF;
@@ -158,7 +158,7 @@ class CollectionAnySerializer {
serializer.readRef();
const refFlag = this.fory.referenceResolver.readRefFlag();
if (refFlag === RefFlags.RefFlag) {
- const refId = this.fory.binaryReader.varUInt32();
+ const refId = this.fory.binaryReader.readVarUInt32();
accessor(result, i,
this.fory.referenceResolver.getReadObject(refId));
} else if (refFlag === RefFlags.RefValueFlag) {
accessor(result, i, serializer!.read(true));
@@ -168,7 +168,7 @@ class CollectionAnySerializer {
}
} else if (includeNone) {
for (let i = 0; i < len; i++) {
- const flag = this.fory.binaryReader.int8();
+ const flag = this.fory.binaryReader.readInt8();
if (flag === RefFlags.NullFlag) {
accessor(result, i, null);
} else {
@@ -188,7 +188,7 @@ class CollectionAnySerializer {
}
} else if (includeNone) {
for (let i = 0; i < len; i++) {
- const flag = this.fory.binaryReader.int8();
+ const flag = this.fory.binaryReader.readInt8();
if (flag === RefFlags.NullFlag) {
accessor(result, i, null);
} else {
@@ -242,7 +242,7 @@ export abstract class CollectionSerializerGenerator extends
BaseSerializerGenera
}
}
`);
- stmts.push(`${this.builder.writer.uint8(flagAccessor)}`);
+ stmts.push(`${this.builder.writer.writeUint8(flagAccessor)}`);
return stmts.join("\n");
}
@@ -261,24 +261,24 @@ export abstract class CollectionSerializerGenerator
extends BaseSerializerGenera
if (${item} !== null && ${item} !== undefined) {
const ${existsId} =
${this.builder.referenceResolver.existsWriteObject(item)};
if (typeof ${existsId} === "number") {
- ${this.builder.writer.int8(RefFlags.RefFlag)}
- ${this.builder.writer.varUInt32(existsId)}
+ ${this.builder.writer.writeInt8(RefFlags.RefFlag)}
+ ${this.builder.writer.writeVarUInt32(existsId)}
} else {
${this.builder.referenceResolver.writeRef(item)}
- ${this.builder.writer.int8(RefFlags.RefValueFlag)};
+
${this.builder.writer.writeInt8(RefFlags.RefValueFlag)};
${this.innerGenerator.writeEmbed().write(item)}
}
} else {
- ${this.builder.writer.int8(RefFlags.NullFlag)};
+ ${this.builder.writer.writeInt8(RefFlags.NullFlag)};
}
}
} else if (${flags} & ${CollectionFlags.HAS_NULL}) {
for (const ${item} of ${accessor}) {
if (${item} !== null && ${item} !== undefined) {
- ${this.builder.writer.int8(RefFlags.NotNullValueFlag)};
+
${this.builder.writer.writeInt8(RefFlags.NotNullValueFlag)};
${this.innerGenerator.writeEmbed().write(item)}
} else {
- ${this.builder.writer.int8(RefFlags.NullFlag)};
+ ${this.builder.writer.writeInt8(RefFlags.NullFlag)};
}
}
} else {
@@ -297,19 +297,19 @@ export abstract class CollectionSerializerGenerator
extends BaseSerializerGenera
const refFlag = this.scope.uniqueName("refFlag");
return `
const ${len} = ${this.builder.reader.readVarUint32Small7()};
- const ${flags} = ${this.builder.reader.uint8()};
+ const ${flags} = ${this.builder.reader.readUint8()};
const ${result} = ${this.newCollection(len)};
${this.maybeReference(result, refState)}
if (${flags} & ${CollectionFlags.TRACKING_REF}) {
for (let ${idx} = 0; ${idx} < ${len}; ${idx}++) {
- const ${refFlag} = ${this.builder.reader.int8()};
+ const ${refFlag} = ${this.builder.reader.readInt8()};
switch (${refFlag}) {
case ${RefFlags.NotNullValueFlag}:
case ${RefFlags.RefValueFlag}:
${this.innerGenerator.readEmbed().read((x: any) =>
`${this.putAccessor(result, x, idx)}`, `${refFlag} ===
${RefFlags.RefValueFlag}`)}
break;
case ${RefFlags.RefFlag}:
- ${this.putAccessor(result,
this.builder.referenceResolver.getReadObject(this.builder.reader.varUInt32()),
idx)}
+ ${this.putAccessor(result,
this.builder.referenceResolver.getReadObject(this.builder.reader.readVarUInt32()),
idx)}
break;
case ${RefFlags.NullFlag}:
${this.putAccessor(result, "null", idx)}
@@ -318,7 +318,7 @@ export abstract class CollectionSerializerGenerator extends
BaseSerializerGenera
}
} else if (${flags} & ${CollectionFlags.HAS_NULL}) {
for (let ${idx} = 0; ${idx} < ${len}; ${idx}++) {
- if (${this.builder.reader.int8()} == ${RefFlags.NullFlag})
{
+ if (${this.builder.reader.readInt8()} ==
${RefFlags.NullFlag}) {
${this.putAccessor(result, "null", idx)}
} else {
${this.innerGenerator.readEmbed().read((x: any) =>
`${this.putAccessor(result, x, idx)}`, "false")}
diff --git a/javascript/packages/fory/lib/gen/datetime.ts
b/javascript/packages/fory/lib/gen/datetime.ts
index 071e7c3d2..b785fec63 100644
--- a/javascript/packages/fory/lib/gen/datetime.ts
+++ b/javascript/packages/fory/lib/gen/datetime.ts
@@ -40,14 +40,14 @@ class TimestampSerializerGenerator extends
BaseSerializerGenerator {
const ${msVar} = (${accessor} instanceof Date) ? ${accessor}.getTime() :
${accessor};
const ${secondsVar} = Math.floor(${msVar} / 1000);
const ${nanosVar} = (${msVar} - ${secondsVar} * 1000) * 1000000;
- ${this.builder.writer.int64(`${secondsVar}`)}
- ${this.builder.writer.uint32(`${nanosVar}`)}
+ ${this.builder.writer.writeInt64(`${secondsVar}`)}
+ ${this.builder.writer.writeUint32(`${nanosVar}`)}
`;
}
read(accessor: (expr: string) => string): string {
- const seconds = this.builder.reader.int64();
- const nanos = this.builder.reader.uint32();
+ const seconds = this.builder.reader.readInt64();
+ const nanos = this.builder.reader.readUint32();
return accessor(`new Date(Number(${seconds}) * 1000 + Math.floor(${nanos}
/ 1000000))`);
}
@@ -72,14 +72,14 @@ class DurationSerializerGenerator extends
BaseSerializerGenerator {
const ${msVar} = ${accessor};
const ${secondsVar} = Math.floor(${msVar} / 1000);
const ${nanosVar} = (${msVar} - ${secondsVar} * 1000) * 1000000;
- ${this.builder.writer.int64(`${secondsVar}`)}
- ${this.builder.writer.uint32(`${nanosVar}`)}
+ ${this.builder.writer.writeInt64(`${secondsVar}`)}
+ ${this.builder.writer.writeUint32(`${nanosVar}`)}
`;
}
read(accessor: (expr: string) => string): string {
- const seconds = this.builder.reader.int64();
- const nanos = this.builder.reader.uint32();
+ const seconds = this.builder.reader.readInt64();
+ const nanos = this.builder.reader.readUint32();
return accessor(`Number(${seconds}) * 1000 + Math.floor(${nanos} /
1000000)`);
}
@@ -100,16 +100,16 @@ class DateSerializerGenerator extends
BaseSerializerGenerator {
const epoch = this.scope.declareByName("epoch", `new Date("1970/01/01
00:00").getTime()`);
return `
if (${accessor} instanceof Date) {
- ${this.builder.writer.int32(`Math.floor((${accessor}.getTime() -
${epoch}) / 1000 / (24 * 60 * 60))`)}
+ ${this.builder.writer.writeInt32(`Math.floor((${accessor}.getTime() -
${epoch}) / 1000 / (24 * 60 * 60))`)}
} else {
- ${this.builder.writer.int32(`Math.floor((${accessor} - ${epoch}) /
1000 / (24 * 60 * 60))`)}
+ ${this.builder.writer.writeInt32(`Math.floor((${accessor} - ${epoch})
/ 1000 / (24 * 60 * 60))`)}
}
`;
}
read(accessor: (expr: string) => string): string {
const epoch = this.scope.declareByName("epoch", `new Date("1970/01/01
00:00").getTime()`);
- return accessor(`new Date(${epoch} + (${this.builder.reader.int32()} * (24
* 60 * 60) * 1000))`);
+ return accessor(`new Date(${epoch} + (${this.builder.reader.readInt32()} *
(24 * 60 * 60) * 1000))`);
}
getFixedSize(): number {
diff --git a/javascript/packages/fory/lib/gen/enum.ts
b/javascript/packages/fory/lib/gen/enum.ts
index cc2a31498..7e9091801 100644
--- a/javascript/packages/fory/lib/gen/enum.ts
+++ b/javascript/packages/fory/lib/gen/enum.ts
@@ -34,7 +34,7 @@ class EnumSerializerGenerator extends BaseSerializerGenerator
{
write(accessor: string): string {
if (!this.typeInfo.options?.enumProps) {
- return this.builder.writer.varUInt32(accessor);
+ return this.builder.writer.writeVarUInt32(accessor);
}
if (Object.values(this.typeInfo.options.enumProps).length < 1) {
throw new Error("An enum must contain at least one field");
@@ -51,7 +51,7 @@ class EnumSerializerGenerator extends BaseSerializerGenerator
{
}
const safeValue = typeof value === "string" ? `"${value}"` : value;
return ` if (${accessor} === ${safeValue}) {
- ${this.builder.writer.varUInt32(index)}
+ ${this.builder.writer.writeVarUInt32(index)}
}`;
}).join(" else ")}
else {
@@ -87,7 +87,7 @@ class EnumSerializerGenerator extends BaseSerializerGenerator
{
return `
${
// skip the typeId
- this.builder.reader.uint8()
+ this.builder.reader.readUint8()
}
${readUserTypeIdStmt}
${namesStmt}
@@ -117,7 +117,7 @@ class EnumSerializerGenerator extends
BaseSerializerGenerator {
break;
}
return `
- ${this.builder.writer.uint8(this.getTypeId())};
+ ${this.builder.writer.writeUint8(this.getTypeId())};
${writeUserTypeIdStmt}
${typeMeta}
`;
@@ -125,11 +125,11 @@ class EnumSerializerGenerator extends
BaseSerializerGenerator {
read(accessor: (expr: string) => string): string {
if (!this.typeInfo.options?.enumProps) {
- return accessor(this.builder.reader.varUInt32());
+ return accessor(this.builder.reader.readVarUInt32());
}
const enumValue = this.scope.uniqueName("enum_v");
return `
- const ${enumValue} = ${this.builder.reader.varUInt32()};
+ const ${enumValue} = ${this.builder.reader.readVarUInt32()};
switch(${enumValue}) {
${Object.values(this.typeInfo.options.enumProps).map((value,
index) => {
if (typeof value !== "string" && typeof value !== "number") {
diff --git a/javascript/packages/fory/lib/gen/ext.ts
b/javascript/packages/fory/lib/gen/ext.ts
index 4569b202a..c9c18133e 100644
--- a/javascript/packages/fory/lib/gen/ext.ts
+++ b/javascript/packages/fory/lib/gen/ext.ts
@@ -96,7 +96,7 @@ class ExtSerializerGenerator extends BaseSerializerGenerator {
}
return `
${
- this.builder.reader.uint8()
+ this.builder.reader.readUint8()
};
${readUserTypeIdStmt}
${
@@ -166,7 +166,7 @@ class ExtSerializerGenerator extends
BaseSerializerGenerator {
break;
}
return `
- ${this.builder.writer.uint8(this.getTypeId())};
+ ${this.builder.writer.writeUint8(this.getTypeId())};
${writeUserTypeIdStmt}
${typeMeta}
`;
diff --git a/javascript/packages/fory/lib/gen/map.ts
b/javascript/packages/fory/lib/gen/map.ts
index f1af7b1e8..d94f05606 100644
--- a/javascript/packages/fory/lib/gen/map.ts
+++ b/javascript/packages/fory/lib/gen/map.ts
@@ -91,12 +91,12 @@ class MapChunkWriter {
// KV header
const header = this.getHead(keyInfo, valueInfo);
// chunkSize default 0 | KV header
- this.fory.binaryWriter.uint8(header);
+ this.fory.binaryWriter.writeUint8(header);
if (!withOutSize) {
// chunkSize, max 255
- this.chunkOffset = this.fory.binaryWriter.getCursor();
- this.fory.binaryWriter.uint8(0);
+ this.chunkOffset = this.fory.binaryWriter.writeGetCursor();
+ this.fory.binaryWriter.writeUint8(0);
} else {
this.chunkOffset = 0;
}
@@ -152,11 +152,11 @@ class MapAnySerializer {
if (header & MapFlags.TRACKING_REF) {
const keyRef = this.fory.referenceResolver.existsWriteObject(v);
if (keyRef !== undefined) {
- this.fory.binaryWriter.int8(RefFlags.RefFlag);
- this.fory.binaryWriter.varUInt32(keyRef);
+ this.fory.binaryWriter.writeInt8(RefFlags.RefFlag);
+ this.fory.binaryWriter.writeVarUInt32(keyRef);
return true;
} else {
- this.fory.binaryWriter.int8(RefFlags.RefValueFlag);
+ this.fory.binaryWriter.writeInt8(RefFlags.RefValueFlag);
return false;
}
}
@@ -218,13 +218,13 @@ class MapAnySerializer {
return serializer!.read(false);
}
- const flag = this.fory.binaryReader.int8();
+ const flag = this.fory.binaryReader.readInt8();
switch (flag) {
case RefFlags.RefValueFlag:
serializer = serializer == null ?
AnyHelper.detectSerializer(this.fory) : serializer;
return serializer!.read(true);
case RefFlags.RefFlag:
- return
this.fory.referenceResolver.getReadObject(this.fory.binaryReader.varUInt32());
+ return
this.fory.referenceResolver.getReadObject(this.fory.binaryReader.readVarUInt32());
case RefFlags.NullFlag:
return null;
case RefFlags.NotNullValueFlag:
@@ -240,14 +240,14 @@ class MapAnySerializer {
this.fory.referenceResolver.reference(result);
}
while (count > 0) {
- const header = this.fory.binaryReader.uint8();
+ const header = this.fory.binaryReader.readUint8();
const valueHeader = (header >> 3) & 0b111;
const keyHeader = header & 0b111;
let chunkSize = 0;
if ((valueHeader & MapFlags.HAS_NULL) || (keyHeader &
MapFlags.HAS_NULL)) {
chunkSize = 1;
} else {
- chunkSize = this.fory.binaryReader.uint8();
+ chunkSize = this.fory.binaryReader.readUint8();
}
let keySerializer = this.keySerializer;
let valueSerializer = this.valueSerializer;
@@ -322,13 +322,13 @@ export class MapSerializerGenerator extends
BaseSerializerGenerator {
${chunkSize} = 0;
}
if (keyIsNull || valueIsNull) {
- ${this.builder.writer.uint8(
+ ${this.builder.writer.writeUint8(
`((${valueHeader} | (valueIsNull ? ${MapFlags.HAS_NULL} : 0))
<< 3) | (${keyHeader} | (keyIsNull ? ${MapFlags.HAS_NULL} : 0))`
)
}
} else {
- ${chunkSizeOffset} = ${this.builder.writer.getCursor()} + 1;
- ${this.builder.writer.uint16(
+ ${chunkSizeOffset} = ${this.builder.writer.writeGetCursor()} + 1;
+ ${this.builder.writer.writeUint16(
`((${valueHeader} | (valueIsNull ? ${MapFlags.HAS_NULL} : 0))
<< 3) | (${keyHeader} | (keyIsNull ? ${MapFlags.HAS_NULL} : 0))`
)
}
@@ -341,10 +341,10 @@ export class MapSerializerGenerator extends
BaseSerializerGenerator {
? `
const ${keyRef} =
${this.builder.referenceResolver.existsWriteObject(v)};
if (${keyRef} !== undefined) {
- ${this.builder.writer.int8(RefFlags.RefFlag)};
- ${this.builder.writer.varUInt32(keyRef)};
+ ${this.builder.writer.writeInt8(RefFlags.RefFlag)};
+ ${this.builder.writer.writeVarUInt32(keyRef)};
} else {
- ${this.builder.writer.int8(RefFlags.RefValueFlag)};
+ ${this.builder.writer.writeInt8(RefFlags.RefValueFlag)};
${this.keyGenerator.writeEmbed().write(k)}
}
`
@@ -356,10 +356,10 @@ export class MapSerializerGenerator extends
BaseSerializerGenerator {
? `
const ${valueRef} =
${this.builder.referenceResolver.existsWriteObject(v)};
if (${valueRef} !== undefined) {
- ${this.builder.writer.int8(RefFlags.RefFlag)};
- ${this.builder.writer.varUInt32(valueRef)};
+ ${this.builder.writer.writeInt8(RefFlags.RefFlag)};
+ ${this.builder.writer.writeVarUInt32(valueRef)};
} else {
- ${this.builder.writer.int8(RefFlags.RefValueFlag)};
+ ${this.builder.writer.writeInt8(RefFlags.RefValueFlag)};
${this.valueGenerator.writeEmbed().write(v)};
}
`
@@ -407,7 +407,7 @@ export class MapSerializerGenerator extends
BaseSerializerGenerator {
${this.builder.referenceResolver.reference(result)}
}
while (${count} > 0) {
- const header = ${this.builder.reader.uint8()};
+ const header = ${this.builder.reader.readUint8()};
const keyHeader = header & 0b111;
const valueHeader = (header >> 3) & 0b111;
const keyIncludeNone = keyHeader & ${MapFlags.HAS_NULL};
@@ -416,7 +416,7 @@ export class MapSerializerGenerator extends
BaseSerializerGenerator {
const valueTrackingRef = valueHeader & ${MapFlags.TRACKING_REF};
let chunkSize = 1;
if (!keyIncludeNone && !valueIncludeNone) {
- chunkSize = ${this.builder.reader.uint8()};
+ chunkSize = ${this.builder.reader.readUint8()};
}
for (let index = 0; index < chunkSize; index++) {
let key;
@@ -424,13 +424,13 @@ export class MapSerializerGenerator extends
BaseSerializerGenerator {
if (keyIncludeNone) {
key = null;
} else if (keyTrackingRef) {
- const flag = ${this.builder.reader.int8()};
+ const flag = ${this.builder.reader.readInt8()};
switch (flag) {
case ${RefFlags.RefValueFlag}:
${this.keyGenerator.read(x => `key = ${x}`, "true")}
break;
case ${RefFlags.RefFlag}:
- key =
${this.builder.referenceResolver.getReadObject(this.builder.reader.varUInt32())}
+ key =
${this.builder.referenceResolver.getReadObject(this.builder.reader.readVarUInt32())}
break;
case ${RefFlags.NullFlag}:
key = null;
@@ -446,13 +446,13 @@ export class MapSerializerGenerator extends
BaseSerializerGenerator {
if (valueIncludeNone) {
value = null;
} else if (valueTrackingRef) {
- const flag = ${this.builder.reader.int8()};
+ const flag = ${this.builder.reader.readInt8()};
switch (flag) {
case ${RefFlags.RefValueFlag}:
${this.valueGenerator.read(x => `value = ${x}`, "true")}
break;
case ${RefFlags.RefFlag}:
- value =
${this.builder.referenceResolver.getReadObject(this.builder.reader.varUInt32())}
+ value =
${this.builder.referenceResolver.getReadObject(this.builder.reader.readVarUInt32())}
break;
case ${RefFlags.NullFlag}:
value = null;
diff --git a/javascript/packages/fory/lib/gen/number.ts
b/javascript/packages/fory/lib/gen/number.ts
index 0eef57bae..55e54554d 100644
--- a/javascript/packages/fory/lib/gen/number.ts
+++ b/javascript/packages/fory/lib/gen/number.ts
@@ -49,36 +49,36 @@ function buildNumberSerializer(writeFun: (builder:
CodecBuilder, accessor: strin
CodegenRegistry.register(TypeId.INT8,
buildNumberSerializer(
- (builder, accessor) => builder.writer.int8(accessor),
- builder => builder.reader.int8()
+ (builder, accessor) => builder.writer.writeInt8(accessor),
+ builder => builder.reader.readInt8()
)
);
CodegenRegistry.register(TypeId.INT16,
buildNumberSerializer(
- (builder, accessor) => builder.writer.int16(accessor),
- builder => builder.reader.int16()
+ (builder, accessor) => builder.writer.writeInt16(accessor),
+ builder => builder.reader.readInt16()
)
);
CodegenRegistry.register(TypeId.INT32,
buildNumberSerializer(
- (builder, accessor) => builder.writer.int32(accessor),
- builder => builder.reader.int32()
+ (builder, accessor) => builder.writer.writeInt32(accessor),
+ builder => builder.reader.readInt32()
)
);
CodegenRegistry.register(TypeId.VARINT32,
buildNumberSerializer(
- (builder, accessor) => builder.writer.varInt32(accessor),
- builder => builder.reader.varInt32()
+ (builder, accessor) => builder.writer.writeVarInt32(accessor),
+ builder => builder.reader.readVarInt32()
)
);
CodegenRegistry.register(TypeId.INT64,
buildNumberSerializer(
- (builder, accessor) => builder.writer.int64(accessor),
- builder => builder.reader.int64()
+ (builder, accessor) => builder.writer.writeInt64(accessor),
+ builder => builder.reader.readInt64()
)
);
@@ -98,74 +98,74 @@ CodegenRegistry.register(TypeId.TAGGED_UINT64,
CodegenRegistry.register(TypeId.FLOAT16,
buildNumberSerializer(
- (builder, accessor) => builder.writer.float16(accessor),
- builder => builder.reader.float16()
+ (builder, accessor) => builder.writer.writeFloat16(accessor),
+ builder => builder.reader.readFloat16()
)
);
CodegenRegistry.register(TypeId.BFLOAT16,
buildNumberSerializer(
- (builder, accessor) => builder.writer.bfloat16(accessor),
- builder => builder.reader.bfloat16()
+ (builder, accessor) => builder.writer.writeBfloat16(accessor),
+ builder => builder.reader.readBfloat16()
)
);
CodegenRegistry.register(TypeId.FLOAT32,
buildNumberSerializer(
- (builder, accessor) => builder.writer.float32(accessor),
- builder => builder.reader.float32()
+ (builder, accessor) => builder.writer.writeFloat32(accessor),
+ builder => builder.reader.readFloat32()
)
);
CodegenRegistry.register(TypeId.FLOAT64,
buildNumberSerializer(
- (builder, accessor) => builder.writer.float64(accessor),
- builder => builder.reader.float64()
+ (builder, accessor) => builder.writer.writeFloat64(accessor),
+ builder => builder.reader.readFloat64()
)
);
CodegenRegistry.register(TypeId.UINT8,
buildNumberSerializer(
- (builder, accessor) => builder.writer.uint8(accessor),
- builder => builder.reader.uint8()
+ (builder, accessor) => builder.writer.writeUint8(accessor),
+ builder => builder.reader.readUint8()
)
);
CodegenRegistry.register(TypeId.UINT16,
buildNumberSerializer(
- (builder, accessor) => builder.writer.uint16(accessor),
- builder => builder.reader.uint16()
+ (builder, accessor) => builder.writer.writeUint16(accessor),
+ builder => builder.reader.readUint16()
)
);
CodegenRegistry.register(TypeId.UINT32,
buildNumberSerializer(
- (builder, accessor) => builder.writer.uint32(accessor),
- builder => builder.reader.uint32()
+ (builder, accessor) => builder.writer.writeUint32(accessor),
+ builder => builder.reader.readUint32()
)
);
CodegenRegistry.register(TypeId.VAR_UINT32,
buildNumberSerializer(
- (builder, accessor) => builder.writer.varUInt32(accessor),
- builder => builder.reader.varUInt32()
+ (builder, accessor) => builder.writer.writeVarUInt32(accessor),
+ builder => builder.reader.readVarUInt32()
)
);
CodegenRegistry.register(TypeId.UINT64,
buildNumberSerializer(
- (builder, accessor) => builder.writer.uint64(accessor),
- builder => builder.reader.uint64()
+ (builder, accessor) => builder.writer.writeUint64(accessor),
+ builder => builder.reader.readUint64()
)
);
CodegenRegistry.register(TypeId.VAR_UINT64,
buildNumberSerializer(
- (builder, accessor) => builder.writer.varUInt64(accessor),
- builder => builder.reader.varUInt64()
+ (builder, accessor) => builder.writer.writeVarUInt64(accessor),
+ builder => builder.reader.readVarUInt64()
)
);
CodegenRegistry.register(TypeId.VARINT64,
buildNumberSerializer(
- (builder, accessor) => builder.writer.varInt64(accessor),
- builder => builder.reader.varInt64()
+ (builder, accessor) => builder.writer.writeVarInt64(accessor),
+ builder => builder.reader.readVarInt64()
)
);
diff --git a/javascript/packages/fory/lib/gen/serializer.ts
b/javascript/packages/fory/lib/gen/serializer.ts
index df8fd499c..912ce095b 100644
--- a/javascript/packages/fory/lib/gen/serializer.ts
+++ b/javascript/packages/fory/lib/gen/serializer.ts
@@ -26,8 +26,8 @@ import { BinaryWriter } from "../writer";
export const makeHead = (flag: RefFlags, typeId: number) => {
const writer = new BinaryWriter();
- writer.uint8(flag);
- writer.uint8(typeId);
+ writer.writeUint8(flag);
+ writer.writeUint8(typeId);
const buffer = writer.dump();
return buffer;
};
@@ -134,20 +134,20 @@ export abstract class BaseSerializerGenerator implements
SerializerGenerator {
refFlagStmt = `
const ${existsId} =
${this.builder.referenceResolver.existsWriteObject(accessor)};
if (typeof ${existsId} === "number") {
- ${this.builder.writer.int8(RefFlags.RefFlag)}
- ${this.builder.writer.varUInt32(existsId)}
+ ${this.builder.writer.writeInt8(RefFlags.RefFlag)}
+ ${this.builder.writer.writeVarUInt32(existsId)}
${assignStmt("true")};
} else {
- ${this.builder.writer.int8(RefFlags.RefValueFlag)}
+ ${this.builder.writer.writeInt8(RefFlags.RefValueFlag)}
${this.builder.referenceResolver.writeRef(accessor)}
}
`;
} else {
- refFlagStmt = this.builder.writer.int8(RefFlags.NotNullValueFlag);
+ refFlagStmt = this.builder.writer.writeInt8(RefFlags.NotNullValueFlag);
}
return `
if (${accessor} === null || ${accessor} === undefined) {
- ${this.builder.writer.int8(RefFlags.NullFlag)};
+ ${this.builder.writer.writeInt8(RefFlags.NullFlag)};
${assignStmt("true")};
} else {
${refFlagStmt}
@@ -163,7 +163,7 @@ export abstract class BaseSerializerGenerator implements
SerializerGenerator {
? this.builder.writer.writeVarUint32Small7(userTypeId)
: "";
return `
- ${this.builder.writer.uint8(typeId)};
+ ${this.builder.writer.writeUint8(typeId)};
${userTypeStmt}
`;
}
@@ -192,7 +192,7 @@ export abstract class BaseSerializerGenerator implements
SerializerGenerator {
? `${this.builder.reader.readVarUint32Small7()};`
: "";
return `
- ${this.builder.reader.uint8()};
+ ${this.builder.reader.readUint8()};
${readUserTypeStmt}
`;
}
@@ -207,14 +207,14 @@ export abstract class BaseSerializerGenerator implements
SerializerGenerator {
readRefWithoutTypeInfo(assignStmt: (v: string) => string): string {
const refFlag = this.scope.uniqueName("refFlag");
return `
- const ${refFlag} = ${this.builder.reader.int8()};
+ const ${refFlag} = ${this.builder.reader.readInt8()};
switch (${refFlag}) {
case ${RefFlags.NotNullValueFlag}:
case ${RefFlags.RefValueFlag}:
${this.read(assignStmt, `${refFlag} ===
${RefFlags.RefValueFlag}`)}
break;
case ${RefFlags.RefFlag}:
-
${assignStmt(this.builder.referenceResolver.getReadObject(this.builder.reader.varUInt32()))}
+
${assignStmt(this.builder.referenceResolver.getReadObject(this.builder.reader.readVarUInt32()))}
break;
case ${RefFlags.NullFlag}:
${assignStmt("null")}
@@ -226,14 +226,14 @@ export abstract class BaseSerializerGenerator implements
SerializerGenerator {
readRef(assignStmt: (v: string) => string): string {
const refFlag = this.scope.uniqueName("refFlag");
return `
- const ${refFlag} = ${this.builder.reader.int8()};
+ const ${refFlag} = ${this.builder.reader.readInt8()};
switch (${refFlag}) {
case ${RefFlags.NotNullValueFlag}:
case ${RefFlags.RefValueFlag}:
${this.readNoRef(assignStmt, `${refFlag} ===
${RefFlags.RefValueFlag}`)}
break;
case ${RefFlags.RefFlag}:
-
${assignStmt(this.builder.referenceResolver.getReadObject(this.builder.reader.varUInt32()))}
+
${assignStmt(this.builder.referenceResolver.getReadObject(this.builder.reader.readVarUInt32()))}
break;
case ${RefFlags.NullFlag}:
${assignStmt("null")}
diff --git a/javascript/packages/fory/lib/gen/struct.ts
b/javascript/packages/fory/lib/gen/struct.ts
index d753e16ae..33ed24319 100644
--- a/javascript/packages/fory/lib/gen/struct.ts
+++ b/javascript/packages/fory/lib/gen/struct.ts
@@ -113,9 +113,9 @@ class StructSerializerGenerator extends
BaseSerializerGenerator {
} else if (refMode == RefMode.NULL_ONLY) {
stmt = `
if (${fieldAccessor} === null || ${fieldAccessor} === undefined) {
- ${this.builder.writer.int8(RefFlags.NullFlag)}
+ ${this.builder.writer.writeInt8(RefFlags.NullFlag)}
} else {
- ${this.builder.writer.int8(RefFlags.NotNullValueFlag)}
+ ${this.builder.writer.writeInt8(RefFlags.NotNullValueFlag)}
${embedGenerator.write(fieldAccessor)}
}
`;
@@ -134,9 +134,9 @@ class StructSerializerGenerator extends
BaseSerializerGenerator {
} else if (refMode == RefMode.NULL_ONLY) {
stmt = `
if (${fieldAccessor} === null || ${fieldAccessor} === undefined) {
- ${this.builder.writer.int8(RefFlags.NullFlag)}
+ ${this.builder.writer.writeInt8(RefFlags.NullFlag)}
} else {
- ${this.builder.writer.int8(RefFlags.NotNullValueFlag)}
+ ${this.builder.writer.writeInt8(RefFlags.NotNullValueFlag)}
${embedGenerator.writeNoRef(fieldAccessor)}
}
`;
@@ -156,7 +156,7 @@ class StructSerializerGenerator extends
BaseSerializerGenerator {
write(accessor: string): string {
const hash = this.typeMeta.computeStructHash();
return `
- ${!this.builder.fory.isCompatible() ? this.builder.writer.int32(hash) :
""}
+ ${!this.builder.fory.isCompatible() ?
this.builder.writer.writeInt32(hash) : ""}
${this.sortedProps.map(({ key, typeInfo }) => {
const InnerGeneratorClass = CodegenRegistry.get(typeInfo.typeId);
if (!InnerGeneratorClass) {
@@ -176,7 +176,7 @@ class StructSerializerGenerator extends
BaseSerializerGenerator {
return `
${!this.builder.fory.isCompatible()
? `
- if(${this.builder.reader.int32()} !== ${hash}) {
+ if(${this.builder.reader.readInt32()} !== ${hash}) {
throw new Error("Read class version is not consistent with ${hash} ")
}
`
@@ -268,7 +268,7 @@ class StructSerializerGenerator extends
BaseSerializerGenerator {
}
return `
${
- this.builder.reader.uint8()
+ this.builder.reader.readUint8()
};
${readUserTypeIdStmt}
${
@@ -348,7 +348,7 @@ class StructSerializerGenerator extends
BaseSerializerGenerator {
break;
}
return `
- ${this.builder.writer.uint8(this.getTypeId())};
+ ${this.builder.writer.writeUint8(this.getTypeId())};
${writeUserTypeIdStmt}
${typeMeta}
`;
diff --git a/javascript/packages/fory/lib/gen/typedArray.ts
b/javascript/packages/fory/lib/gen/typedArray.ts
index 4330443fa..50ae7c432 100644
--- a/javascript/packages/fory/lib/gen/typedArray.ts
+++ b/javascript/packages/fory/lib/gen/typedArray.ts
@@ -35,7 +35,7 @@ function build(inner: TypeInfo, creator: string, size:
number) {
write(accessor: string): string {
return `
- ${this.builder.writer.varUInt32(`${accessor}.byteLength`)}
+ ${this.builder.writer.writeVarUInt32(`${accessor}.byteLength`)}
${this.builder.writer.arrayBuffer(`${accessor}.buffer`,
`${accessor}.byteOffset`, `${accessor}.byteLength`)}
`;
}
@@ -46,7 +46,7 @@ function build(inner: TypeInfo, creator: string, size:
number) {
const copied = this.scope.uniqueName("copied");
return `
- const ${len} = ${this.builder.reader.varUInt32()};
+ const ${len} = ${this.builder.reader.readVarUInt32()};
const ${copied} = ${this.builder.reader.buffer(len)}
const ${result} = new ${creator}(${copied}.buffer,
${copied}.byteOffset, ${copied}.byteLength / ${size});
${this.maybeReference(result, refState)}
@@ -71,10 +71,10 @@ class BoolArraySerializerGenerator extends
BaseSerializerGenerator {
write(accessor: string): string {
const item = this.scope.uniqueName("item");
return `
- ${this.builder.writer.varUInt32(`${accessor}.length`)}
+ ${this.builder.writer.writeVarUInt32(`${accessor}.length`)}
${this.builder.writer.reserve(`${accessor}.length`)};
for (const ${item} of ${accessor}) {
- ${this.builder.writer.uint8(`${item} ? 1 : 0`)}
+ ${this.builder.writer.writeUint8(`${item} ? 1 : 0`)}
}
`;
}
@@ -84,11 +84,11 @@ class BoolArraySerializerGenerator extends
BaseSerializerGenerator {
const len = this.scope.uniqueName("len");
const idx = this.scope.uniqueName("idx");
return `
- const ${len} = ${this.builder.reader.varUInt32()};
+ const ${len} = ${this.builder.reader.readVarUInt32()};
const ${result} = new Array(${len});
${this.maybeReference(result, refState)}
for (let ${idx} = 0; ${idx} < ${len}; ${idx}++) {
- ${result}[${idx}] = ${this.builder.reader.uint8()} === 1;
+ ${result}[${idx}] = ${this.builder.reader.readUint8()} === 1;
}
${accessor(result)}
`;
@@ -110,10 +110,10 @@ class Float16ArraySerializerGenerator extends
BaseSerializerGenerator {
write(accessor: string): string {
const item = this.scope.uniqueName("item");
return `
- ${this.builder.writer.varUInt32(`${accessor}.length * 2`)}
+ ${this.builder.writer.writeVarUInt32(`${accessor}.length * 2`)}
${this.builder.writer.reserve(`${accessor}.length * 2`)};
for (const ${item} of ${accessor}) {
- ${this.builder.writer.float16(item)}
+ ${this.builder.writer.writeFloat16(item)}
}
`;
}
@@ -123,11 +123,11 @@ class Float16ArraySerializerGenerator extends
BaseSerializerGenerator {
const len = this.scope.uniqueName("len");
const idx = this.scope.uniqueName("idx");
return `
- const ${len} = ${this.builder.reader.varUInt32()} / 2;
+ const ${len} = ${this.builder.reader.readVarUInt32()} / 2;
const ${result} = new Array(${len});
${this.maybeReference(result, refState)}
for (let ${idx} = 0; ${idx} < ${len}; ${idx}++) {
- ${result}[${idx}] = ${this.builder.reader.float16()};
+ ${result}[${idx}] = ${this.builder.reader.readFloat16()};
}
${accessor(result)}
`;
@@ -149,10 +149,10 @@ class BFloat16ArraySerializerGenerator extends
BaseSerializerGenerator {
write(accessor: string): string {
const item = this.scope.uniqueName("item");
return `
- ${this.builder.writer.varUInt32(`${accessor}.length * 2`)}
+ ${this.builder.writer.writeVarUInt32(`${accessor}.length * 2`)}
${this.builder.writer.reserve(`${accessor}.length * 2`)};
for (const ${item} of ${accessor}) {
- ${this.builder.writer.bfloat16(item)}
+ ${this.builder.writer.writeBfloat16(item)}
}
`;
}
@@ -162,11 +162,11 @@ class BFloat16ArraySerializerGenerator extends
BaseSerializerGenerator {
const len = this.scope.uniqueName("len");
const idx = this.scope.uniqueName("idx");
return `
- const ${len} = ${this.builder.reader.varUInt32()} / 2;
+ const ${len} = ${this.builder.reader.readVarUInt32()} / 2;
const ${result} = new Array(${len});
${this.maybeReference(result, refState)}
for (let ${idx} = 0; ${idx} < ${len}; ${idx}++) {
- ${result}[${idx}] = ${this.builder.reader.bfloat16()};
+ ${result}[${idx}] = ${this.builder.reader.readBfloat16()};
}
${accessor(result)}
`;
diff --git a/javascript/packages/fory/lib/meta/TypeMeta.ts
b/javascript/packages/fory/lib/meta/TypeMeta.ts
index d407c6622..8022b69b3 100644
--- a/javascript/packages/fory/lib/meta/TypeMeta.ts
+++ b/javascript/packages/fory/lib/meta/TypeMeta.ts
@@ -161,7 +161,7 @@ export class FieldInfo {
}
writer.writeVarUint32Small7(typeId);
} else {
- writer.uint8(typeId);
+ writer.writeUint8(typeId);
}
switch (typeInfo.typeId) {
case TypeId.LIST:
@@ -281,22 +281,22 @@ export class TypeMeta {
static fromBytes(reader: BinaryReader): TypeMeta {
// Read header with hash and flags
- const headerLong = reader.int64();
+ const headerLong = reader.readInt64();
// todo support compress.
// const isCompressed = (headerLong & COMPRESS_META_FLAG) !== 0n;
// const hasFieldsMeta = (headerLong & HAS_FIELDS_META_FLAG) !== 0n;
let metaSize = Number(headerLong & BigInt(META_SIZE_MASKS));
if (metaSize === META_SIZE_MASKS) {
- metaSize += reader.varUInt32();
+ metaSize += reader.readVarUInt32();
}
// Read class header
- const classHeader = reader.uint8();
+ const classHeader = reader.readUint8();
let numFields = classHeader & SMALL_NUM_FIELDS_THRESHOLD;
if (numFields === SMALL_NUM_FIELDS_THRESHOLD) {
- numFields += reader.varUInt32();
+ numFields += reader.readVarUInt32();
}
let typeId: number;
@@ -310,8 +310,8 @@ export class TypeMeta {
typeName = this.readTypeName(reader);
typeId = TypeId.NAMED_STRUCT; // Default for named types
} else {
- typeId = reader.uint8();
- userTypeId = reader.varUInt32();
+ typeId = reader.readUint8();
+ userTypeId = reader.readVarUInt32();
}
// Read fields
@@ -333,7 +333,7 @@ export class TypeMeta {
}
private static readFieldInfo(reader: BinaryReader): FieldInfo {
- const header = reader.int8();
+ const header = reader.readInt8();
const encodingFlags = (header >>> 6) & 0b11;
let size = (header >>> 2) & 0b1111;
const bigSize = size === FIELD_NAME_SIZE_THRESHOLD;
@@ -378,7 +378,7 @@ export class TypeMeta {
this.readNestedTypeInfo(reader, typeId, options);
return { typeId, userTypeId: -1, nullable, trackingRef, options };
}
- let typeId = reader.uint8();
+ let typeId = reader.readUint8();
if (typeId === TypeId.NAMED_ENUM) {
typeId = TypeId.ENUM;
} else if (typeId === TypeId.NAMED_UNION || typeId === TypeId.TYPED_UNION)
{
@@ -414,7 +414,7 @@ export class TypeMeta {
}
private static readName(reader: BinaryReader, encodings: Encoding[],
decoder: MetaStringDecoder): string {
- const header = reader.uint8();
+ const header = reader.readUint8();
const encodingIndex = header & 0b11;
let size = (header >> 2) & 0b111111;
@@ -448,20 +448,20 @@ export class TypeMeta {
toBytes() {
const writer = new BinaryWriter({});
- writer.uint8(-1); // placeholder for header, update later
+ writer.writeUint8(-1); // placeholder for header, update later
let currentClassHeader = this.fields.length;
if (this.fields.length >= SMALL_NUM_FIELDS_THRESHOLD) {
currentClassHeader = SMALL_NUM_FIELDS_THRESHOLD;
- writer.varUInt32(this.fields.length - SMALL_NUM_FIELDS_THRESHOLD);
+ writer.writeVarUInt32(this.fields.length - SMALL_NUM_FIELDS_THRESHOLD);
}
if (!TypeId.isNamedType(this.type.typeId)) {
- writer.uint8(this.type.typeId);
+ writer.writeUint8(this.type.typeId);
if (this.type.userTypeId === undefined || this.type.userTypeId === -1) {
throw new Error(`userTypeId required for typeId ${this.type.typeId}`);
}
- writer.varUInt32(this.type.userTypeId);
+ writer.writeVarUInt32(this.type.userTypeId);
} else {
currentClassHeader |= REGISTER_BY_NAME_FLAG;
const ns = this.type.namespace;
@@ -500,11 +500,11 @@ export class TypeMeta {
const bigSize = encoded.length >= BIG_NAME_THRESHOLD;
if (bigSize) {
const header = (BIG_NAME_THRESHOLD << 2) | encoding;
- writer.uint8(header);
+ writer.writeUint8(header);
writer.writeVarUint32Small7(encoded.length - BIG_NAME_THRESHOLD);
} else {
const header = (encoded.length << 2) | encoding;
- writer.uint8(header);
+ writer.writeUint8(header);
}
writer.buffer(encoded);
}
@@ -543,11 +543,11 @@ export class TypeMeta {
if (bigSize) {
header |= 0b00111100;
- writer.int8(header);
+ writer.writeInt8(header);
writer.writeVarUint32Small7(size - FIELD_NAME_SIZE_THRESHOLD);
} else {
header |= (size << 2);
- writer.int8(header);
+ writer.writeInt8(header);
}
FieldInfo.writeTypeId(writer, fieldInfo);
@@ -633,10 +633,10 @@ export class TypeMeta {
header |= BigInt(Math.min(metaSize, META_SIZE_MASKS));
const writer = new BinaryWriter({});
- writer.int64(header);
+ writer.writeInt64(header);
if (metaSize > META_SIZE_MASKS) {
- writer.varUInt32(metaSize - META_SIZE_MASKS);
+ writer.writeVarUInt32(metaSize - META_SIZE_MASKS);
}
writer.buffer(buffer);
diff --git a/javascript/packages/fory/lib/metaStringResolver.ts
b/javascript/packages/fory/lib/metaStringResolver.ts
index 85244f274..50f4afc09 100644
--- a/javascript/packages/fory/lib/metaStringResolver.ts
+++ b/javascript/packages/fory/lib/metaStringResolver.ts
@@ -46,15 +46,15 @@ export class MetaStringResolver {
writeBytes(writer: BinaryWriter, bytes: MetaStringBytes) {
if (bytes.dynamicWriteStringId !== -1) {
- writer.varUInt32(((this.dynamicNameId + 1) << 1) | 1);
+ writer.writeVarUInt32(((this.dynamicNameId + 1) << 1) | 1);
} else {
bytes.dynamicWriteStringId = this.dynamicNameId;
this.dynamicNameId += 1;
this.disposeMetaStringBytes.push(bytes);
const len = bytes.bytes.getBytes().byteLength;
- writer.varUInt32(len << 1);
+ writer.writeVarUInt32(len << 1);
if (len !== 0) {
- writer.uint8(bytes.bytes.getEncoding());
+ writer.writeUint8(bytes.bytes.getEncoding());
}
writer.buffer(bytes.bytes.getBytes());
}
@@ -69,7 +69,7 @@ export class MetaStringResolver {
}
public readTypeName(reader: BinaryReader) {
- const idOrLen = reader.varUInt32();
+ const idOrLen = reader.readVarUInt32();
if (idOrLen & 1) {
return this.names[idOrLen >> 1];
} else {
@@ -78,7 +78,7 @@ export class MetaStringResolver {
this.names.push("");
return "";
}
- const encoding = reader.uint8();
+ const encoding = reader.readUint8();
const name = this.typenameDecoder.decode(reader, len, encoding);
this.names.push(name);
return name;
@@ -86,7 +86,7 @@ export class MetaStringResolver {
}
public readNamespace(reader: BinaryReader) {
- const idOrLen = reader.varUInt32();
+ const idOrLen = reader.readVarUInt32();
if (idOrLen & 1) {
return this.names[idOrLen >> 1];
} else {
@@ -95,7 +95,7 @@ export class MetaStringResolver {
this.names.push("");
return "";
}
- const encoding = reader.uint8();
+ const encoding = reader.readUint8();
const name = this.namespaceDecoder.decode(reader, len, encoding);
this.names.push(name);
return name;
diff --git a/javascript/packages/fory/lib/reader/index.ts
b/javascript/packages/fory/lib/reader/index.ts
index 9786b0bf2..4300bc9c5 100644
--- a/javascript/packages/fory/lib/reader/index.ts
+++ b/javascript/packages/fory/lib/reader/index.ts
@@ -47,62 +47,62 @@ export class BinaryReader {
this.cursor = 0;
}
- uint8() {
+ readUint8() {
return this.dataView.getUint8(this.cursor++);
}
- int8() {
+ readInt8() {
return this.dataView.getInt8(this.cursor++);
}
- uint16() {
+ readUint16() {
const result = this.dataView.getUint16(this.cursor, true);
this.cursor += 2;
return result;
}
- int16() {
+ readInt16() {
const result = this.dataView.getInt16(this.cursor, true);
this.cursor += 2;
return result;
}
- skip(len: number) {
+ readSkip(len: number) {
this.cursor += len;
}
- int32() {
+ readInt32() {
const result = this.dataView.getInt32(this.cursor, true);
this.cursor += 4;
return result;
}
- uint32() {
+ readUint32() {
const result = this.dataView.getUint32(this.cursor, true);
this.cursor += 4;
return result;
}
- int64() {
+ readInt64() {
const result = this.dataView.getBigInt64(this.cursor, true);
this.cursor += 8;
return result;
}
- uint64() {
+ readUint64() {
const result = this.dataView.getBigUint64(this.cursor, true);
this.cursor += 8;
return result;
}
- sliInt64() {
+ readSliInt64() {
const i = this.dataView.getUint32(this.cursor, true);
if ((i & 0b1) != 0b1) {
this.cursor += 4;
return BigInt(i >> 1);
}
this.cursor += 1;
- return this.varInt64();
+ return this.readVarInt64();
}
/**
@@ -127,7 +127,7 @@ export class BinaryReader {
throw new Error("Insufficient bytes for big tagged int64");
}
this.cursor = readIdx + 1; // Skip the flag byte
- return this.int64();
+ return this.readInt64();
}
}
@@ -153,17 +153,17 @@ export class BinaryReader {
throw new Error("Insufficient bytes for big tagged uint64");
}
this.cursor = readIdx + 1; // Skip the flag byte
- return this.uint64();
+ return this.readUint64();
}
}
- float32() {
+ readFloat32() {
const result = this.dataView.getFloat32(this.cursor, true);
this.cursor += 4;
return result;
}
- float64() {
+ readFloat64() {
const result = this.dataView.getFloat64(this.cursor, true);
this.cursor += 8;
return result;
@@ -233,7 +233,7 @@ export class BinaryReader {
return result;
}
- varUInt32() {
+ readVarUInt32() {
// Reduce memory reads as much as possible. Reading a uint32 at once is
far faster than reading four uint8s separately.
if (this.byteLength - this.cursor >= 5) {
const fourByteValue = this.dataView.getUint32(this.cursor++, true);
@@ -252,26 +252,26 @@ export class BinaryReader {
// 0xfe00000: 0b1111111 << 21
result |= (fourByteValue >>> 3) & 0xfe00000;
if ((fourByteValue & 0x80000000) != 0) {
- result |= (this.uint8()) << 28;
+ result |= (this.readUint8()) << 28;
}
}
}
}
return result;
}
- let byte = this.uint8();
+ let byte = this.readUint8();
let result = byte & 0x7f;
if ((byte & 0x80) != 0) {
- byte = this.uint8();
+ byte = this.readUint8();
result |= (byte & 0x7f) << 7;
if ((byte & 0x80) != 0) {
- byte = this.uint8();
+ byte = this.readUint8();
result |= (byte & 0x7f) << 14;
if ((byte & 0x80) != 0) {
- byte = this.uint8();
+ byte = this.readUint8();
result |= (byte & 0x7f) << 21;
if ((byte & 0x80) != 0) {
- byte = this.uint8();
+ byte = this.readUint8();
result |= (byte) << 28;
}
}
@@ -360,19 +360,19 @@ export class BinaryReader {
}
private readVarUint36Slow(): number {
- let b = this.uint8();
+ let b = this.readUint8();
let result = b & 0x7F;
if ((b & 0x80) !== 0) {
- b = this.uint8();
+ b = this.readUint8();
result |= (b & 0x7F) << 7;
if ((b & 0x80) !== 0) {
- b = this.uint8();
+ b = this.readUint8();
result |= (b & 0x7F) << 14;
if ((b & 0x80) !== 0) {
- b = this.uint8();
+ b = this.readUint8();
result |= (b & 0x7F) << 21;
if ((b & 0x80) !== 0) {
- b = this.uint8();
+ b = this.readUint8();
result |= (b & 0xFF) << 28;
}
}
@@ -381,16 +381,16 @@ export class BinaryReader {
return result;
}
- varInt32() {
- const v = this.varUInt32();
+ readVarInt32() {
+ const v = this.readVarUInt32();
return (v >>> 1) ^ -(v & 1); // zigZag decode
}
bigUInt8() {
- return BigInt(this.uint8() >>> 0);
+ return BigInt(this.readUint8() >>> 0);
}
- varUInt64() {
+ readVarUInt64() {
// Creating BigInts is too performance-intensive; we'll use uint32 instead.
if (this.byteLength - this.cursor < 8) {
let byte = this.bigUInt8();
@@ -462,7 +462,7 @@ export class BinaryReader {
this.cursor++;
rh28 |= (byte & 0x7f) << 21;
if ((byte & 0x80) != 0) {
- return (BigInt(this.uint8()) << 56n) | BigInt(rh28) << 28n
| BigInt(rl28);
+ return (BigInt(this.readUint8()) << 56n) | BigInt(rh28) <<
28n | BigInt(rl28);
}
}
}
@@ -475,13 +475,13 @@ export class BinaryReader {
return BigInt(rh28) << 28n | BigInt(rl28);
}
- varInt64() {
- const v = this.varUInt64();
+ readVarInt64() {
+ const v = this.readVarUInt64();
return (v >> 1n) ^ -(v & 1n); // zigZag decode
}
- float16() {
- const asUint16 = this.uint16();
+ readFloat16() {
+ const asUint16 = this.readUint16();
const sign = asUint16 >> 15;
const exponent = (asUint16 >> 10) & 0x1F;
const mantissa = asUint16 & 0x3FF;
@@ -509,15 +509,15 @@ export class BinaryReader {
}
}
- bfloat16(): BFloat16 {
- return BFloat16.fromBits(this.uint16());
+ readBfloat16(): BFloat16 {
+ return BFloat16.fromBits(this.readUint16());
}
- getCursor() {
+ readGetCursor() {
return this.cursor;
}
- setCursor(v: number) {
+ readSetCursor(v: number) {
this.cursor = v;
}
}
diff --git a/javascript/packages/fory/lib/referenceResolver.ts
b/javascript/packages/fory/lib/referenceResolver.ts
index ae77f818f..ec919c966 100644
--- a/javascript/packages/fory/lib/referenceResolver.ts
+++ b/javascript/packages/fory/lib/referenceResolver.ts
@@ -40,7 +40,7 @@ export class ReferenceResolver {
}
readRefFlag() {
- return this.binaryReader.int8() as RefFlags;
+ return this.binaryReader.readInt8() as RefFlags;
}
reference(object: any) {
diff --git a/javascript/packages/fory/lib/typeMetaResolver.ts
b/javascript/packages/fory/lib/typeMetaResolver.ts
index 6db2fe148..ad13ea22a 100644
--- a/javascript/packages/fory/lib/typeMetaResolver.ts
+++ b/javascript/packages/fory/lib/typeMetaResolver.ts
@@ -87,7 +87,7 @@ export class TypeMetaResolver {
}
readTypeMeta(reader: BinaryReader): TypeMeta {
- const idOrLen = reader.varUInt32();
+ const idOrLen = reader.readVarUInt32();
if (idOrLen & 1) {
return this.typeMeta[idOrLen >> 1];
} else {
@@ -101,14 +101,14 @@ export class TypeMetaResolver {
writeTypeMeta(typeInfo: TypeInfo, writer: BinaryWriter, bytes: Uint8Array) {
if (typeInfo.dynamicTypeId !== -1) {
// Reference to previously written type: (index << 1) | 1, LSB=1
- writer.varUInt32((typeInfo.dynamicTypeId << 1) | 1);
+ writer.writeVarUInt32((typeInfo.dynamicTypeId << 1) | 1);
} else {
// New type: index << 1, LSB=0, followed by TypeMeta bytes inline
const index = this.dynamicTypeId;
typeInfo.dynamicTypeId = index;
this.dynamicTypeId += 1;
this.disposeTypeInfo.push(typeInfo);
- writer.varUInt32(index << 1);
+ writer.writeVarUInt32(index << 1);
writer.buffer(bytes);
}
}
diff --git a/javascript/packages/fory/lib/writer/index.ts
b/javascript/packages/fory/lib/writer/index.ts
index 2d7992c47..c7e021a9a 100644
--- a/javascript/packages/fory/lib/writer/index.ts
+++ b/javascript/packages/fory/lib/writer/index.ts
@@ -86,46 +86,46 @@ export class BinaryWriter {
this.cursor++;
}
- uint8(v: number) {
+ writeUint8(v: number) {
this.dataView.setUint8(this.cursor, v);
this.cursor++;
}
- int8(v: number) {
+ writeInt8(v: number) {
this.dataView.setInt8(this.cursor, v);
this.cursor++;
}
- int24(v: number) {
+ writeInt24(v: number) {
this.dataView.setUint32(this.cursor, v, true);
this.cursor += 3;
}
- uint16(v: number) {
+ writeUint16(v: number) {
this.dataView.setUint16(this.cursor, v, true);
this.cursor += 2;
}
- int16(v: number) {
+ writeInt16(v: number) {
this.dataView.setInt16(this.cursor, v, true);
this.cursor += 2;
}
- skip(len: number) {
+ writeSkip(len: number) {
this.cursor += len;
}
- int32(v: number) {
+ writeInt32(v: number) {
this.dataView.setInt32(this.cursor, v, true);
this.cursor += 4;
}
- uint32(v: number) {
+ writeUint32(v: number) {
this.dataView.setUint32(this.cursor, v, true);
this.cursor += 4;
}
- int64(v: bigint) {
+ writeInt64(v: bigint) {
if (typeof v !== "bigint") {
this.dataView.setBigInt64(this.cursor, BigInt(v), true);
} else {
@@ -134,7 +134,7 @@ export class BinaryWriter {
this.cursor += 8;
}
- sliInt64(v: bigint | number) {
+ writeSliInt64(v: bigint | number) {
if (v <= HalfMaxInt32 && v >= HalfMinInt32) {
// write:
// 00xxx -> 0xxx
@@ -148,7 +148,7 @@ export class BinaryWriter {
const BIG_LONG_FLAG = 0b1; // bit 0 set, means big long.
this.dataView.setUint8(this.cursor, BIG_LONG_FLAG);
this.cursor += 1;
- this.varInt64(BigInt(v));
+ this.writeVarInt64(BigInt(v));
}
}
@@ -209,12 +209,12 @@ export class BinaryWriter {
}
}
- float32(v: number) {
+ writeFloat32(v: number) {
this.dataView.setFloat32(this.cursor, v, true);
this.cursor += 4;
}
- float64(v: number) {
+ writeFloat64(v: number) {
this.dataView.setFloat64(this.cursor, v, true);
this.cursor += 8;
}
@@ -231,7 +231,7 @@ export class BinaryWriter {
this.cursor += v.length;
}
- uint64(v: bigint) {
+ writeUint64(v: bigint) {
this.dataView.setBigUint64(this.cursor, v, true);
this.cursor += 8;
}
@@ -288,7 +288,7 @@ export class BinaryWriter {
const isLatin1 = this.internalStringDetector!(v);
if (isLatin1) {
const len = v.length;
- this.varUInt32((len << 2) | LATIN1);
+ this.writeVarUInt32((len << 2) | LATIN1);
this.reserve(len);
if (len < 40) {
for (let index = 0; index < v.length; index++) {
@@ -300,7 +300,7 @@ export class BinaryWriter {
this.cursor += len;
} else {
const len = v.length * 2;
- this.varUInt32((len << 2) | UTF16);
+ this.writeVarUInt32((len << 2) | UTF16);
this.reserve(len);
this.platformBuffer.write(v, this.cursor, "utf16le");
this.cursor += len;
@@ -310,7 +310,7 @@ export class BinaryWriter {
stringWithHeaderCompatibly(v: string) {
const len = strByteLength(v);
const isLatin1 = len === v.length;
- this.varUInt32((len << 2) | (isLatin1 ? LATIN1 : UTF8));
+ this.writeVarUInt32((len << 2) | (isLatin1 ? LATIN1 : UTF8));
this.reserve(len);
if (isLatin1) {
if (len < 40) {
@@ -330,11 +330,11 @@ export class BinaryWriter {
this.cursor += len;
}
- varInt32(v: number) {
- return this.varUInt32((v << 1) ^ (v >> 31));
+ writeVarInt32(v: number) {
+ return this.writeVarUInt32((v << 1) ^ (v >> 31));
}
- varUInt32(value: number) {
+ writeVarUInt32(value: number) {
value = (value >>> 0) & 0xFFFFFFFF; // keep only the lower 32 bits
if (value >> 7 == 0) {
@@ -400,14 +400,14 @@ export class BinaryWriter {
return 5;
}
- varInt64(v: bigint) {
+ writeVarInt64(v: bigint) {
if (typeof v !== "bigint") {
v = BigInt(v);
}
- return this.varUInt64((v << 1n) ^ (v >> 63n));
+ return this.writeVarUInt64((v << 1n) ^ (v >> 63n));
}
- varUInt64(val: bigint | number) {
+ writeVarUInt64(val: bigint | number) {
if (typeof val !== "bigint") {
val = BigInt(val);
}
@@ -452,17 +452,17 @@ export class BinaryWriter {
};
}
- float16(value: number) {
- this.uint16(toFloat16(value));
+ writeFloat16(value: number) {
+ this.writeUint16(toFloat16(value));
}
- bfloat16(value: BFloat16 | number) {
+ writeBfloat16(value: BFloat16 | number) {
const bits
= value instanceof BFloat16 ? value.toBits() : toBFloat16(value);
- this.uint16(bits);
+ this.writeUint16(bits);
}
- getCursor() {
+ writeGetCursor() {
return this.cursor;
}
diff --git a/javascript/test/crossLanguage.test.ts
b/javascript/test/crossLanguage.test.ts
index 27c099760..00e0412e4 100644
--- a/javascript/test/crossLanguage.test.ts
+++ b/javascript/test/crossLanguage.test.ts
@@ -62,15 +62,15 @@ describe("bool", () => {
const buffer = new BinaryWriter();
buffer.reserve(32);
buffer.bool(true);
- buffer.uint8(Byte.MAX_VALUE);
- buffer.int16(Short.MAX_VALUE);
- buffer.int32(Integer.MAX_VALUE);
- buffer.int64(Long.MAX_VALUE);
- buffer.float32(-1.1);
- buffer.float64(-1.1);
- buffer.varUInt32(100);
+ buffer.writeUint8(Byte.MAX_VALUE);
+ buffer.writeInt16(Short.MAX_VALUE);
+ buffer.writeInt32(Integer.MAX_VALUE);
+ buffer.writeInt64(Long.MAX_VALUE);
+ buffer.writeFloat32(-1.1);
+ buffer.writeFloat64(-1.1);
+ buffer.writeVarUInt32(100);
const bytes = ['a'.charCodeAt(0), 'b'.charCodeAt(0)];
- buffer.int32(bytes.length);
+ buffer.writeInt32(bytes.length);
buffer.buffer(new Uint8Array(bytes));
writeToFile(buffer.dump() as Buffer);
});
@@ -99,7 +99,7 @@ describe("bool", () => {
Integer.MAX_VALUE,
];
for (const expected of varInt32Values) {
- expect(reader.varInt32()).toBe(expected);
+ expect(reader.readVarInt32()).toBe(expected);
}
const varUInt32Values = [
@@ -117,7 +117,7 @@ describe("bool", () => {
Integer.MAX_VALUE,
];
for (const expected of varUInt32Values) {
- expect(reader.varUInt32()).toBe(expected);
+ expect(reader.readVarUInt32()).toBe(expected);
}
const varUInt64Values = [
@@ -142,7 +142,7 @@ describe("bool", () => {
Long.MAX_VALUE,
];
for (const expected of varUInt64Values) {
- expect(reader.varUInt64()).toBe(expected);
+ expect(reader.readVarUInt64()).toBe(expected);
}
const varInt64Values = [
@@ -163,22 +163,22 @@ describe("bool", () => {
Long.MAX_VALUE,
];
for (const expected of varInt64Values) {
- expect(reader.varInt64()).toBe(expected);
+ expect(reader.readVarInt64()).toBe(expected);
}
const writer = new BinaryWriter();
writer.reserve(256);
for (const value of varInt32Values) {
- writer.varInt32(value);
+ writer.writeVarInt32(value);
}
for (const value of varUInt32Values) {
- writer.varUInt32(value);
+ writer.writeVarUInt32(value);
}
for (const value of varUInt64Values) {
- writer.varUInt64(value);
+ writer.writeVarUInt64(value);
}
for (const value of varInt64Values) {
- writer.varInt64(value);
+ writer.writeVarInt64(value);
}
writeToFile(writer.dump() as Buffer);
});
@@ -187,8 +187,8 @@ describe("bool", () => {
const reader = new BinaryReader({});
reader.reset(content);
let dataview = x64hash128(new Uint8Array([1, 2, 8]), 47);
- expect(reader.int64()).toEqual(dataview.getBigInt64(0));
- expect(reader.int64()).toEqual(dataview.getBigInt64(8));
+ expect(reader.readInt64()).toEqual(dataview.getBigInt64(0));
+ expect(reader.readInt64()).toEqual(dataview.getBigInt64(8));
});
test("test_string_serializer", () => {
const fory = new Fory({
@@ -199,7 +199,7 @@ describe("bool", () => {
let cursor = 0;
for (let i = 0; i < 7; i++) { // 7 test strings
const deserializedString = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
deserializedStrings.push(deserializedString);
}
const bfs = []
@@ -229,7 +229,7 @@ describe("bool", () => {
let cursor = 0;
for (let i = 0; i < 27; i++) { // 28 serialized items from Java
const deserializedItem = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
deserializedData.push(deserializedItem);
}
@@ -400,7 +400,7 @@ describe("bool", () => {
let cursor = 0;
for (let i = 0; i < 4; i++) { // 4 lists
const deserializedList = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
deserializedLists.push(deserializedList);
}
@@ -433,7 +433,7 @@ describe("bool", () => {
let cursor = 0;
for (let i = 0; i < 2; i++) { // 2 maps
const deserializedMap = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
deserializedMaps.push(deserializedMap);
}
@@ -478,7 +478,7 @@ describe("bool", () => {
let cursor = 0;
for (let i = 0; i < 7; i++) { // 1 item + 6 integers
const deserializedItem = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
deserializedData.push(deserializedItem);
}
@@ -513,7 +513,7 @@ describe("bool", () => {
let cursor = 0;
for (let i = 0; i < 3; i++) { // 3 items
const deserializedItem = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
deserializedItems.push(deserializedItem);
}
@@ -549,7 +549,7 @@ describe("bool", () => {
let cursor = 0;
for (let i = 0; i < 4; i++) { // 4 colors
const deserializedColor = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
deserializedColors.push(deserializedColor);
}
@@ -585,7 +585,7 @@ describe("bool", () => {
let cursor = 0;
for (let i = 0; i < 2; i++) { // 2 structs
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
deserializedStructs.push(deserializedStruct);
}
@@ -622,7 +622,7 @@ describe("bool", () => {
let cursor = 0;
for (let i = 0; i < 2; i++) { // 2 structs
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
deserializedStructs.push(deserializedStruct);
}
@@ -700,10 +700,10 @@ describe("bool", () => {
}
fory1.registerSerializer(MyExt, {
write: (value: MyExt, writer: BinaryWriter, fory: Fory) => {
- writer.varInt32(value.id);
+ writer.writeVarInt32(value.id);
},
read: (result: MyExt, reader: BinaryReader, fory: Fory) => {
- result.id = reader.varInt32();
+ result.id = reader.readVarInt32();
}
});
@@ -735,10 +735,10 @@ describe("bool", () => {
fory2.registerSerializer(MyExt, {
write: (value: MyExt, writer: BinaryWriter, fory: Fory) => {
- writer.varInt32(value.id);
+ writer.writeVarInt32(value.id);
},
read: (result: MyExt, reader: BinaryReader, fory: Fory) => {
- result.id = reader.varInt32();
+ result.id = reader.readVarInt32();
}
});
@@ -758,7 +758,7 @@ describe("bool", () => {
// Deserialize empty from Java
let cursor = 0;
const deserializedEmpty = fory1.deserialize(content.subarray(cursor));
- cursor += fory1.binaryReader.getCursor();
+ cursor += fory1.binaryReader.readGetCursor();
expect(deserializedEmpty instanceof Empty).toEqual(true);
// Create wrapper object
@@ -785,10 +785,10 @@ describe("bool", () => {
}
fory1.registerSerializer(MyExt, {
write: (value: MyExt, writer: BinaryWriter, fory: Fory) => {
- writer.varInt32(value.id);
+ writer.writeVarInt32(value.id);
},
read: (result: MyExt, reader: BinaryReader, fory: Fory) => {
- result.id = reader.varInt32();
+ result.id = reader.readVarInt32();
}
});
@@ -820,10 +820,10 @@ describe("bool", () => {
fory2.registerSerializer(MyExt, {
write: (value: MyExt, writer: BinaryWriter, fory: Fory) => {
- writer.varInt32(value.id);
+ writer.writeVarInt32(value.id);
},
read: (result: MyExt, reader: BinaryReader, fory: Fory) => {
- result.id = reader.varInt32();
+ result.id = reader.readVarInt32();
}
});
@@ -843,7 +843,7 @@ describe("bool", () => {
// Deserialize empty from Java
let cursor = 0;
const deserializedEmpty = fory1.deserialize(content.subarray(cursor));
- cursor += fory1.binaryReader.getCursor();
+ cursor += fory1.binaryReader.readGetCursor();
expect(deserializedEmpty instanceof Empty).toEqual(true);
// Create wrapper object
@@ -887,10 +887,10 @@ describe("bool", () => {
}
fory.registerSerializer(MyExt, {
write: (value: MyExt, writer: BinaryWriter, fory: Fory) => {
- writer.varInt32(value.id);
+ writer.writeVarInt32(value.id);
},
read: (result: MyExt, reader: BinaryReader, fory: Fory) => {
- result.id = reader.varInt32();
+ result.id = reader.readVarInt32();
}
});
@@ -899,7 +899,7 @@ describe("bool", () => {
let cursor = 0;
for (let i = 0; i < 9; i++) { // 3 colors + 3 structs + 3 exts
const deserializedItem = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
deserializedData.push(deserializedItem);
}
@@ -943,7 +943,7 @@ describe("bool", () => {
// Deserialize struct from Java
let cursor = 0;
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized struct back
const serializedData = fory.serialize(deserializedStruct);
@@ -992,7 +992,7 @@ describe("bool", () => {
let cursor = 0;
for (let i = 0; i < 2; i++) { // animals array + holder
const deserializedItem = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
deserializedData.push(deserializedItem);
}
@@ -1051,7 +1051,7 @@ describe("bool", () => {
let cursor = 0;
for (let i = 0; i < 2; i++) { // animal map + holder
const deserializedItem = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
deserializedData.push(deserializedItem);
}
@@ -1083,7 +1083,7 @@ describe("bool", () => {
// Deserialize struct from Java
let cursor = 0;
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized struct back
const serializedData = fory.serialize(deserializedStruct);
@@ -1108,7 +1108,7 @@ describe("bool", () => {
// Deserialize struct from Java
let cursor = 0;
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized struct back
const serializedData = fory.serialize(deserializedStruct);
@@ -1133,7 +1133,7 @@ describe("bool", () => {
// Deserialize struct from Java
let cursor = 0;
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized struct back
const serializedData = fory.serialize(deserializedStruct);
@@ -1157,7 +1157,7 @@ describe("bool", () => {
// Deserialize empty struct from Java
let cursor = 0;
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized struct back
const serializedData = fory.serialize(deserializedStruct);
@@ -1176,7 +1176,7 @@ describe("bool", () => {
// Deserialize empty struct from Java
let cursor = 0;
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized struct back
const serializedData = fory.serialize(deserializedStruct);
@@ -1209,7 +1209,7 @@ describe("bool", () => {
// Deserialize struct from Java
let cursor = 0;
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized struct back
const serializedData = fory.serialize(deserializedStruct);
@@ -1240,7 +1240,7 @@ describe("bool", () => {
// Deserialize struct from Java
let cursor = 0;
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized struct back
const serializedData = fory.serialize(deserializedStruct);
@@ -1273,7 +1273,7 @@ describe("bool", () => {
// Deserialize struct from Java
let cursor = 0;
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized struct back
const serializedData = fory.serialize(deserializedStruct);
@@ -1306,7 +1306,7 @@ describe("bool", () => {
// Deserialize struct from Java
let cursor = 0;
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized struct back
const serializedData = fory.serialize(deserializedStruct);
@@ -1334,7 +1334,7 @@ describe("bool", () => {
// Deserialize empty struct from Java
let cursor = 0;
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized struct back
const serializedData = fory.serialize(deserializedStruct);
@@ -1486,7 +1486,7 @@ describe("bool", () => {
// Deserialize struct from Java
let cursor = 0;
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized struct back
const serializedData = fory.serialize(deserializedStruct);
@@ -1506,7 +1506,7 @@ describe("bool", () => {
// Deserialize struct from Java
let cursor = 0;
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized struct back
const serializedData = fory.serialize(deserializedStruct);
@@ -1524,7 +1524,7 @@ describe("bool", () => {
// Deserialize struct from Java
let cursor = 0;
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized struct back
const serializedData = fory.serialize(deserializedStruct);
@@ -1544,7 +1544,7 @@ describe("bool", () => {
// Deserialize struct from Java
let cursor = 0;
const deserializedStruct: InstanceType<ReturnType<typeof buildClass>> |
null = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
if (deserializedStruct === null) {
throw new Error("deserializedStruct is null");
@@ -1584,7 +1584,7 @@ describe("bool", () => {
// Deserialize outer struct from Java
let cursor = 0;
const deserializedOuter = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized outer struct back
const serializedData = fory.serialize(deserializedOuter);
@@ -1621,7 +1621,7 @@ describe("bool", () => {
// Deserialize outer struct from Java
let cursor = 0;
const deserializedOuter = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized outer struct back
const serializedData = fory.serialize(deserializedOuter);
@@ -1650,7 +1650,7 @@ describe("bool", () => {
// Deserialize circular struct from Java
let cursor = 0;
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized struct back
const serializedData = fory.serialize(deserializedStruct);
@@ -1676,7 +1676,7 @@ describe("bool", () => {
// Deserialize circular struct from Java
let cursor = 0;
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized struct back
const serializedData = fory.serialize(deserializedStruct);
@@ -1702,7 +1702,7 @@ describe("bool", () => {
// Deserialize struct from Java
let cursor = 0;
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized struct back
const serializedData = fory.serialize(deserializedStruct);
@@ -1758,7 +1758,7 @@ describe("bool", () => {
// Deserialize struct from Java
let cursor = 0;
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized struct back
const serializedBackData = fory.serialize(deserializedStruct);
@@ -1814,7 +1814,7 @@ describe("bool", () => {
// Deserialize struct from Java
let cursor = 0;
const deserializedStruct = fory.deserialize(content.subarray(cursor));
- cursor += fory.binaryReader.getCursor();
+ cursor += fory.binaryReader.readGetCursor();
// Serialize the deserialized struct back
const serializedData = fory.serialize(deserializedStruct);
diff --git a/javascript/test/io.test.ts b/javascript/test/io.test.ts
index 3c978defc..296a1eeba 100644
--- a/javascript/test/io.test.ts
+++ b/javascript/test/io.test.ts
@@ -43,42 +43,42 @@ function num2Bin(num: number) {
32,
].forEach((x, y) => {
{
- writer[`uint${x}`](10);
+ writer[`writeUint${x}`](10);
len += x / 8;
var ab = writer.dump();
expect(ab.byteLength).toBe(len);
if (x === 64) {
- expect(new DataView(ab.buffer,
ab.byteOffset)[`getBigUint${x}`](writer.getCursor() - x / 8,
true)).toBe(BigInt(10));
+ expect(new DataView(ab.buffer,
ab.byteOffset)[`getBigUint${x}`](writer.writeGetCursor() - x / 8,
true)).toBe(BigInt(10));
} else {
- expect(new DataView(ab.buffer,
ab.byteOffset)[`getUint${x}`](writer.getCursor() - x / 8, true)).toBe(10);
+ expect(new DataView(ab.buffer,
ab.byteOffset)[`getUint${x}`](writer.writeGetCursor() - x / 8, true)).toBe(10);
}
- expect(writer.getCursor()).toBe(len);
+ expect(writer.writeGetCursor()).toBe(len);
}
{
- writer[`uint${x}`](-1);
+ writer[`writeUint${x}`](-1);
len += x / 8;
var ab = writer.dump();
expect(ab.byteLength).toBe(len);
if (x === 64) {
- expect(new DataView(ab.buffer,
ab.byteOffset)[`getBigUint${x}`](writer.getCursor() - x / 8,
true)).toBe(BigInt(2 ** x) - BigInt(1));
+ expect(new DataView(ab.buffer,
ab.byteOffset)[`getBigUint${x}`](writer.writeGetCursor() - x / 8,
true)).toBe(BigInt(2 ** x) - BigInt(1));
} else {
- expect(new DataView(ab.buffer,
ab.byteOffset)[`getUint${x}`](writer.getCursor() - x / 8, true)).toBe(2 ** x -
1);
+ expect(new DataView(ab.buffer,
ab.byteOffset)[`getUint${x}`](writer.writeGetCursor() - x / 8, true)).toBe(2 **
x - 1);
}
- expect(writer.getCursor()).toBe(len);
+ expect(writer.writeGetCursor()).toBe(len);
}
{
- writer[`uint${x}`](2 ** x);
+ writer[`writeUint${x}`](2 ** x);
len += x / 8;
var ab = writer.dump();
expect(ab.byteLength).toBe(len);
if (x === 64) {
- expect(new DataView(ab.buffer,
ab.byteOffset)[`getBigUint${x}`](writer.getCursor() - x / 8,
true)).toBe(BigInt(0));
+ expect(new DataView(ab.buffer,
ab.byteOffset)[`getBigUint${x}`](writer.writeGetCursor() - x / 8,
true)).toBe(BigInt(0));
} else {
- expect(new DataView(ab.buffer,
ab.byteOffset)[`getUint${x}`](writer.getCursor() - x / 8, true)).toBe(0);
+ expect(new DataView(ab.buffer,
ab.byteOffset)[`getUint${x}`](writer.writeGetCursor() - x / 8, true)).toBe(0);
}
- expect(writer.getCursor()).toBe(len);
+ expect(writer.writeGetCursor()).toBe(len);
}
})
@@ -94,42 +94,42 @@ function num2Bin(num: number) {
32,
].forEach((x, y) => {
{
- writer[`int${x}`](10);
+ writer[`writeInt${x}`](10);
len += x / 8;
var ab = writer.dump();
expect(ab.byteLength).toBe(len);
if (x === 64) {
- expect(new DataView(ab.buffer,
ab.byteOffset)[`getBigInt${x}`](writer.getCursor() - x / 8,
true)).toBe(BigInt(10));
+ expect(new DataView(ab.buffer,
ab.byteOffset)[`getBigInt${x}`](writer.writeGetCursor() - x / 8,
true)).toBe(BigInt(10));
} else {
- expect(new DataView(ab.buffer,
ab.byteOffset)[`getInt${x}`](writer.getCursor() - x / 8, true)).toBe(10);
+ expect(new DataView(ab.buffer,
ab.byteOffset)[`getInt${x}`](writer.writeGetCursor() - x / 8, true)).toBe(10);
}
- expect(writer.getCursor()).toBe(len);
+ expect(writer.writeGetCursor()).toBe(len);
}
{
- writer[`int${x}`](2 ** x);
+ writer[`writeInt${x}`](2 ** x);
len += x / 8;
var ab = writer.dump();
expect(ab.byteLength).toBe(len);
if (x === 64) {
- expect(new DataView(ab.buffer,
ab.byteOffset)[`getBigInt${x}`](writer.getCursor() - x / 8,
true)).toBe(BigInt(0));
+ expect(new DataView(ab.buffer,
ab.byteOffset)[`getBigInt${x}`](writer.writeGetCursor() - x / 8,
true)).toBe(BigInt(0));
} else {
- expect(new DataView(ab.buffer,
ab.byteOffset)[`getInt${x}`](writer.getCursor() - x / 8, true)).toBe(0);
+ expect(new DataView(ab.buffer,
ab.byteOffset)[`getInt${x}`](writer.writeGetCursor() - x / 8, true)).toBe(0);
}
- expect(writer.getCursor()).toBe(len);
+ expect(writer.writeGetCursor()).toBe(len);
}
{
- writer[`int${x}`](-1);
+ writer[`writeInt${x}`](-1);
len += x / 8;
var ab = writer.dump();
expect(ab.byteLength).toBe(len);
if (x === 64) {
- expect(new DataView(ab.buffer,
ab.byteOffset)[`getBigInt${x}`](writer.getCursor() - x / 8,
true)).toBe(BigInt(-1));
+ expect(new DataView(ab.buffer,
ab.byteOffset)[`getBigInt${x}`](writer.writeGetCursor() - x / 8,
true)).toBe(BigInt(-1));
} else {
- expect(new DataView(ab.buffer,
ab.byteOffset)[`getInt${x}`](writer.getCursor() - x / 8, true)).toBe(-1);
+ expect(new DataView(ab.buffer,
ab.byteOffset)[`getInt${x}`](writer.writeGetCursor() - x / 8, true)).toBe(-1);
}
- expect(writer.getCursor()).toBe(len);
+ expect(writer.writeGetCursor()).toBe(len);
}
})
});
@@ -144,7 +144,7 @@ function num2Bin(num: number) {
{
const writer = new BinaryWriter(config);
const value = (2 ** (x * 7)) - 1;
- writer.varUInt32(value);
+ writer.writeVarUInt32(value);
const ab = writer.dump();
expect(ab.byteLength).toBe(x);
for (let index = 0; index < ab.byteLength - 1; index++) {
@@ -153,13 +153,13 @@ function num2Bin(num: number) {
expect(num2Bin(ab[ab.byteLength - 1])).toBe('1111111');
const reader = new BinaryReader(config);
reader.reset(ab);
- const vari32 = reader.varUInt32();
+ const vari32 = reader.readVarUInt32();
expect(vari32).toBe(value);
}
{
const writer = new BinaryWriter(config);
const value = (2 ** (x * 7));
- writer.varUInt32(value);
+ writer.writeVarUInt32(value);
const ab = writer.dump();
expect(ab.byteLength).toBe(x + 1);
for (let index = 0; index < ab.byteLength - 1; index++) {
@@ -168,7 +168,7 @@ function num2Bin(num: number) {
expect(num2Bin(ab[ab.byteLength - 1])).toBe('1');
const reader = new BinaryReader(config);
reader.reset(ab);
- const vari32 = reader.varUInt32();
+ const vari32 = reader.readVarUInt32();
expect(vari32).toBe(value);
}
});
@@ -177,13 +177,13 @@ function num2Bin(num: number) {
test('should varInt32 work', () => {
const writer = new BinaryWriter(config);
const value = -1;
- writer.varInt32(value);
+ writer.writeVarInt32(value);
const ab = writer.dump();
expect(ab.byteLength).toBe(1);
expect(num2Bin(ab[0])).toBe('1');
const reader = new BinaryReader(config);
reader.reset(ab);
- const vari32 = reader.varInt32();
+ const vari32 = reader.readVarInt32();
expect(vari32).toBe(value);
});
@@ -286,7 +286,7 @@ function num2Bin(num: number) {
test('should setUint32Position work', () => {
const writer = new BinaryWriter(config);
- writer.skip(10);
+ writer.writeSkip(10);
writer.setUint32Position(0, 100);
writer.setUint32Position(5, 100);
const ab = writer.dump();
@@ -297,118 +297,118 @@ function num2Bin(num: number) {
test('should float work', () => {
const writer = new BinaryWriter(config);
- writer.float32(10.01);
+ writer.writeFloat32(10.01);
const ab = writer.dump();
expect(ab.byteLength).toBe(4);
const reader = new BinaryReader(config);
reader.reset(ab);
- expect(reader.float32().toFixed(2)).toBe((10.01).toFixed(2));
+ expect(reader.readFloat32().toFixed(2)).toBe((10.01).toFixed(2));
});
test('should float64 work', () => {
const writer = new BinaryWriter(config);
- writer.float64(10.01);
+ writer.writeFloat64(10.01);
const ab = writer.dump();
expect(ab.byteLength).toBe(8);
const reader = new BinaryReader(config);
reader.reset(ab);
- expect(reader.float64().toFixed(2)).toBe((10.01).toFixed(2));
+ expect(reader.readFloat64().toFixed(2)).toBe((10.01).toFixed(2));
});
test('should reserve work', () => {
const writer = new BinaryWriter(config);
const byteLength = writer.getByteLen();
- const cursor = writer.getCursor();
+ const cursor = writer.writeGetCursor();
const reserved = writer.getReserved();
writer.reserve(10);
expect(writer.getReserved()).toBe(reserved + 10);
expect(writer.getByteLen()).toBe(byteLength);
- expect(writer.getCursor()).toBe(cursor);
+ expect(writer.writeGetCursor()).toBe(cursor);
});
test('should reserve work', () => {
const writer = new BinaryWriter(config);
const byteLength = writer.getByteLen();
- const cursor = writer.getCursor();
+ const cursor = writer.writeGetCursor();
const reserved = writer.getReserved();
writer.reserve(1024 * 101);
expect(writer.getReserved()).toBe(reserved + 1024 * 101);
expect(writer.getByteLen()).toBe(byteLength * 2 + 1024 * 101);
- expect(writer.getCursor()).toBe(cursor);
+ expect(writer.writeGetCursor()).toBe(cursor);
});
test('should reset work', () => {
const writer = new BinaryWriter(config);
- writer.int16(100);
+ writer.writeInt16(100);
writer.reset();
- expect(writer.getCursor()).toBe(0);
+ expect(writer.writeGetCursor()).toBe(0);
expect(writer.getReserved()).toBe(0);
});
test('should int24 work', () => {
const writer = new BinaryWriter(config);
- writer.int24( (20 << 8) | 10);
+ writer.writeInt24( (20 << 8) | 10);
const ab = writer.dump();
const reader = new BinaryReader({});
reader.reset(ab)
- expect(reader.int8()).toBe(10);
- expect(reader.int16()).toBe(20);
+ expect(reader.readInt8()).toBe(10);
+ expect(reader.readInt16()).toBe(20);
});
test('should varUInt64 work', () => {
const writer = new BinaryWriter(config);
- writer.varUInt64(2n ** 2n);
+ writer.writeVarUInt64(2n ** 2n);
const ab = writer.dump();
const reader = new BinaryReader({});
reader.reset(ab)
- expect(reader.varUInt64()).toBe(2n ** 2n);
+ expect(reader.readVarUInt64()).toBe(2n ** 2n);
});
test('should varUInt64 work', () => {
const writer = new BinaryWriter(config);
- writer.varUInt64(2n ** 63n);
+ writer.writeVarUInt64(2n ** 63n);
const ab = writer.dump();
const reader = new BinaryReader({});
reader.reset(ab)
- expect(reader.varUInt64()).toBe(2n ** 63n);
+ expect(reader.readVarUInt64()).toBe(2n ** 63n);
});
test('should varInt64 work', () => {
const writer = new BinaryWriter(config);
- writer.varInt64(2n ** 2n);
+ writer.writeVarInt64(2n ** 2n);
const ab = writer.dump();
const reader = new BinaryReader({});
reader.reset(ab)
- expect(reader.varInt64()).toBe(2n ** 2n);
+ expect(reader.readVarInt64()).toBe(2n ** 2n);
});
test('should varInt64 work', () => {
const writer = new BinaryWriter(config);
- writer.varInt64(2n ** 62n);
+ writer.writeVarInt64(2n ** 62n);
const ab = writer.dump();
const reader = new BinaryReader({});
reader.reset(ab)
- expect(reader.varInt64()).toBe(2n ** 62n);
+ expect(reader.readVarInt64()).toBe(2n ** 62n);
});
test('should silong work', () => {
const writer = new BinaryWriter(config);
- writer.sliInt64(2n ** 2n);
+ writer.writeSliInt64(2n ** 2n);
const ab = writer.dump();
const reader = new BinaryReader({});
reader.reset(ab)
- expect(reader.sliInt64()).toBe(2n ** 2n);
+ expect(reader.readSliInt64()).toBe(2n ** 2n);
});
test('should silong work', () => {
const writer = new BinaryWriter(config);
- writer.sliInt64(2n ** 62n);
+ writer.writeSliInt64(2n ** 62n);
const ab = writer.dump();
const reader = new BinaryReader({});
reader.reset(ab)
- expect(reader.sliInt64()).toBe(2n ** 62n);
+ expect(reader.readSliInt64()).toBe(2n ** 62n);
});
});
})
diff --git a/javascript/test/reader.test.ts b/javascript/test/reader.test.ts
index 8519d1f2a..2bd32c6f4 100644
--- a/javascript/test/reader.test.ts
+++ b/javascript/test/reader.test.ts
@@ -27,20 +27,20 @@ describe('writer', () => {
test('should uint8 work', () => {
const writer = new BinaryWriter({});
{
- writer.uint8(10);
+ writer.writeUint8(10);
var ab = writer.dump();
expect(ab.byteLength).toBe(1);
expect(ab[0]).toBe(10);
- expect(writer.getCursor()).toBe(1);
+ expect(writer.writeGetCursor()).toBe(1);
}
{
- writer.uint8(256);
+ writer.writeUint8(256);
var ab = writer.dump();
expect(ab.byteLength).toBe(2);
expect(ab[1]).toBe(0);
- expect(writer.getCursor()).toBe(2);
+ expect(writer.writeGetCursor()).toBe(2);
}
});
});
diff --git a/javascript/test/writer.test.ts b/javascript/test/writer.test.ts
index df5dcfcfc..9e516ed92 100644
--- a/javascript/test/writer.test.ts
+++ b/javascript/test/writer.test.ts
@@ -25,22 +25,22 @@ describe('writer', () => {
test('should dumpOwn dispose work', () => {
const writer = new BinaryWriter({});
{
- writer.uint8(256);
+ writer.writeUint8(256);
const { get, dispose } = writer.dumpAndOwn();
const ab = get();
expect(ab.byteLength).toBe(1);
expect(ab[0]).toBe(0);
- expect(writer.getCursor()).toBe(1);
+ expect(writer.writeGetCursor()).toBe(1);
dispose();
}
writer.reset();
{
- writer.uint8(256);
+ writer.writeUint8(256);
const { get, dispose } = writer.dumpAndOwn();
const ab = get();
expect(ab.byteLength).toBe(1);
expect(ab[0]).toBe(0);
- expect(writer.getCursor()).toBe(1);
+ expect(writer.writeGetCursor()).toBe(1);
dispose();
}
});
@@ -48,12 +48,12 @@ describe('writer', () => {
test('should dumpOwn work', () => {
const writer = new BinaryWriter({});
{
- writer.uint8(256);
+ writer.writeUint8(256);
const { get } = writer.dumpAndOwn();
const ab = get();
expect(ab.byteLength).toBe(1);
expect(ab[0]).toBe(0);
- expect(writer.getCursor()).toBe(1);
+ expect(writer.writeGetCursor()).toBe(1);
}
try {
writer.reset();
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]