diff --git a/javascript/packages/fury/lib/classResolver.ts b/javascript/packages/fury/lib/classResolver.ts index 62cc1b0bfd..2156640b5d 100644 --- a/javascript/packages/fury/lib/classResolver.ts +++ b/javascript/packages/fury/lib/classResolver.ts @@ -84,7 +84,7 @@ export default class SerializerResolver { private writeStringIndex: number[] = []; private registerSerializer(fury: Fury, description: TypeDescription) { - return fury.classResolver.registerSerializerById(description.type, generateSerializer(fury, description)); + return fury.classResolver.registerSerializerById(SerializerResolver.getTypeIdByInternalSerializerType(description.type), generateSerializer(fury, description)); } private initInternalSerializer(fury: Fury) { @@ -92,27 +92,27 @@ export default class SerializerResolver { this.registerSerializer(fury, Type.array(Type.any())); this.registerSerializer(fury, Type.map(Type.any(), Type.any())); this.registerSerializer(fury, Type.bool()); - this.registerSerializer(fury, Type.uint8()); this.registerSerializer(fury, Type.int8()); - this.registerSerializer(fury, Type.uint16()); this.registerSerializer(fury, Type.int16()); - this.registerSerializer(fury, Type.uint32()); this.registerSerializer(fury, Type.int32()); - this.registerSerializer(fury, Type.uint64()); + this.registerSerializer(fury, Type.varInt32()); this.registerSerializer(fury, Type.int64()); - this.registerSerializer(fury, Type.float()); - this.registerSerializer(fury, Type.double()); + this.registerSerializer(fury, Type.sliInt64()); + this.registerSerializer(fury, Type.float16()); + this.registerSerializer(fury, Type.float32()); + this.registerSerializer(fury, Type.float64()); this.registerSerializer(fury, Type.timestamp()); - this.registerSerializer(fury, Type.date()); + this.registerSerializer(fury, Type.duration()); this.registerSerializer(fury, Type.set(Type.any())); this.registerSerializer(fury, Type.binary()); - this.registerSerializer(fury, Type.stringTypedArray()); - this.registerSerializer(fury, Type.boolTypedArray()); - this.registerSerializer(fury, Type.shortTypedArray()); - this.registerSerializer(fury, Type.intTypedArray()); - this.registerSerializer(fury, Type.longTypedArray()); - this.registerSerializer(fury, Type.floatTypedArray()); - this.registerSerializer(fury, Type.doubleTypedArray()); + this.registerSerializer(fury, Type.boolArray()); + this.registerSerializer(fury, Type.int8Array()); + this.registerSerializer(fury, Type.int16Array()); + this.registerSerializer(fury, Type.int32Array()); + this.registerSerializer(fury, Type.int64Array()); + this.registerSerializer(fury, Type.float16Array()); + this.registerSerializer(fury, Type.float32Array()); + this.registerSerializer(fury, Type.float64Array()); } init(fury: Fury) { @@ -124,7 +124,11 @@ export default class SerializerResolver { this.writeStringIndex.fill(-1); } - getSerializerById(id: InternalSerializerType) { + getSerializerByType(type: InternalSerializerType) { + return this.internalSerializer[SerializerResolver.getTypeIdByInternalSerializerType(type)]; + } + + getSerializerById(id: number) { return this.internalSerializer[id]; } @@ -213,37 +217,105 @@ export default class SerializerResolver { } if (typeof v === "number") { - return this.getSerializerById(InternalSerializerType.DOUBLE); + return this.getSerializerByType(InternalSerializerType.FLOAT64); } if (typeof v === "bigint") { - return this.getSerializerById(InternalSerializerType.INT64); + return this.getSerializerByType(InternalSerializerType.INT64); } if (typeof v === "boolean") { - return this.getSerializerById(InternalSerializerType.BOOL); + return this.getSerializerByType(InternalSerializerType.BOOL); } if (v instanceof Date) { - return this.getSerializerById(InternalSerializerType.TIMESTAMP); + return this.getSerializerByType(InternalSerializerType.TIMESTAMP); } if (typeof v === "string") { - return this.getSerializerById(InternalSerializerType.STRING); + return this.getSerializerByType(InternalSerializerType.STRING); } if (v instanceof Map) { - return this.getSerializerById(InternalSerializerType.MAP); + return this.getSerializerByType(InternalSerializerType.MAP); } if (v instanceof Set) { - return this.getSerializerById(InternalSerializerType.FURY_SET); + return this.getSerializerByType(InternalSerializerType.SET); } if (Array.isArray(v)) { - return this.getSerializerById(InternalSerializerType.ARRAY); + return this.getSerializerByType(InternalSerializerType.ARRAY); } throw new Error(`Failed to detect the Fury type from JavaScript type: ${typeof v}`); } + + static getTypeIdByInternalSerializerType(type: InternalSerializerType) { + switch (type) { + case InternalSerializerType.BOOL: + return 1; + case InternalSerializerType.INT8: + return 2; + case InternalSerializerType.INT16: + return 3; + case InternalSerializerType.INT32: + return 4; + case InternalSerializerType.VAR_INT32: + return 5; + case InternalSerializerType.INT64: + return 6; + case InternalSerializerType.VAR_INT64: + return 7; + case InternalSerializerType.SLI_INT64: + return 8; + case InternalSerializerType.FLOAT16: + return 9; + case InternalSerializerType.FLOAT32: + return 10; + case InternalSerializerType.FLOAT64: + return 11; + case InternalSerializerType.STRING: + return 12; + case InternalSerializerType.ENUM: + return 13; + case InternalSerializerType.LIST: + return 14; + case InternalSerializerType.SET: + return 15; + case InternalSerializerType.MAP: + return 16; + case InternalSerializerType.DURATION: + return 17; + case InternalSerializerType.TIMESTAMP: + return 18; + case InternalSerializerType.DECIMAL: + return 19; + case InternalSerializerType.BINARY: + return 20; + case InternalSerializerType.TUPLE: + case InternalSerializerType.ARRAY: + return 21; + case InternalSerializerType.BOOL_ARRAY: + return 22; + case InternalSerializerType.INT8_ARRAY: + return 23; + case InternalSerializerType.INT16_ARRAY: + return 24; + case InternalSerializerType.INT32_ARRAY: + return 25; + case InternalSerializerType.INT64_ARRAY: + return 26; + case InternalSerializerType.FLOAT16_ARRAY: + return 27; + case InternalSerializerType.FLOAT32_ARRAY: + return 28; + case InternalSerializerType.FLOAT64_ARRAY: + return 29; + case InternalSerializerType.OBJECT: // todo + return 256; + default: + throw new Error(`typeId is not assigned to type ${InternalSerializerType[type]}`); + } + } } diff --git a/javascript/packages/fury/lib/description.ts b/javascript/packages/fury/lib/description.ts index d332e74028..de837bad9b 100644 --- a/javascript/packages/fury/lib/description.ts +++ b/javascript/packages/fury/lib/description.ts @@ -141,7 +141,7 @@ type SetProps = T extends { : unknown; export type InputType = T extends { - type: InternalSerializerType.FURY_TYPE_TAG; + type: InternalSerializerType.OBJECT; } ? Props : T extends { @@ -154,19 +154,19 @@ export type InputType = T extends { ? TupleProps : T extends { type: - | InternalSerializerType.UINT8 - | InternalSerializerType.UINT16 - | InternalSerializerType.UINT32 | InternalSerializerType.INT8 | InternalSerializerType.INT16 | InternalSerializerType.INT32 - | InternalSerializerType.FLOAT - | InternalSerializerType.DOUBLE; + | InternalSerializerType.VAR_INT32 + | InternalSerializerType.FLOAT16 + | InternalSerializerType.FLOAT32 + | InternalSerializerType.FLOAT64; } ? number : T extends { - type: InternalSerializerType.UINT64 + type: InternalSerializerType.VAR_INT64 + | InternalSerializerType.SLI_INT64 | InternalSerializerType.INT64; } ? bigint @@ -175,7 +175,7 @@ export type InputType = T extends { } ? MapProps : T extends { - type: InternalSerializerType.FURY_SET; + type: InternalSerializerType.SET; } ? SetProps : T extends { @@ -187,7 +187,7 @@ export type InputType = T extends { } ? boolean : T extends { - type: InternalSerializerType.DATE; + type: InternalSerializerType.DURATION; } ? Date : T extends { @@ -195,13 +195,12 @@ export type InputType = T extends { } ? number : T extends { - type: InternalSerializerType.BINARY; + type: InternalSerializerType.ANY; } - ? Uint8Array - : T extends { - type: InternalSerializerType.ANY; + ? any + : T extends { type: InternalSerializerType.BINARY; } - ? any + ? Uint8Array : T extends { type: InternalSerializerType.ENUM; } @@ -210,7 +209,7 @@ export type InputType = T extends { } ? OneofProps : unknown; export type ResultType = T extends { - type: InternalSerializerType.FURY_TYPE_TAG; + type: InternalSerializerType.OBJECT; } ? Props : T extends { @@ -223,19 +222,18 @@ export type ResultType = T extends { ? TupleProps : T extends { type: - | InternalSerializerType.UINT8 - | InternalSerializerType.UINT16 - | InternalSerializerType.UINT32 | InternalSerializerType.INT8 | InternalSerializerType.INT16 | InternalSerializerType.INT32 - | InternalSerializerType.FLOAT - | InternalSerializerType.DOUBLE; + | InternalSerializerType.VAR_INT32 + | InternalSerializerType.FLOAT16 + | InternalSerializerType.FLOAT32 + | InternalSerializerType.FLOAT64; } ? number : T extends { - type: InternalSerializerType.UINT64 + type: InternalSerializerType.SLI_INT64 | InternalSerializerType.INT64; } ? bigint @@ -244,7 +242,7 @@ export type ResultType = T extends { } ? MapProps : T extends { - type: InternalSerializerType.FURY_SET; + type: InternalSerializerType.SET; } ? SetProps : T extends { @@ -256,18 +254,16 @@ export type ResultType = T extends { } ? boolean : T extends { - type: InternalSerializerType.DATE; + type: InternalSerializerType.DURATION; } ? Date : T extends { type: InternalSerializerType.TIMESTAMP; } ? number - : T extends { - type: InternalSerializerType.BINARY; + : T extends { type: InternalSerializerType.BINARY; } - ? Uint8Array - : T extends { + ? Uint8Array : T extends { type: InternalSerializerType.ANY; } ? any @@ -335,7 +331,7 @@ export const Type = { }, set(key: T) { return { - type: InternalSerializerType.FURY_SET as const, + type: InternalSerializerType.SET as const, options: { key, }, @@ -348,61 +344,56 @@ export const Type = { }, object(tag: string, props?: T) { return { - type: InternalSerializerType.FURY_TYPE_TAG as const, + type: InternalSerializerType.OBJECT as const, options: { tag, props, }, }; }, - uint8() { + int8() { return { - type: InternalSerializerType.UINT8 as const, + type: InternalSerializerType.INT8 as const, }; }, - uint16() { + int16() { return { - type: InternalSerializerType.UINT16 as const, + type: InternalSerializerType.INT16 as const, }; }, - uint32() { + int32() { return { - type: InternalSerializerType.UINT32 as const, + type: InternalSerializerType.INT32 as const, }; }, - uint64() { + varInt32() { return { - type: InternalSerializerType.UINT64 as const, + type: InternalSerializerType.VAR_INT32 as const, }; }, - int8() { - return { - type: InternalSerializerType.INT8 as const, - }; - }, - int16() { + int64() { return { - type: InternalSerializerType.INT16 as const, + type: InternalSerializerType.INT64 as const, }; }, - int32() { + sliInt64() { return { - type: InternalSerializerType.INT32 as const, + type: InternalSerializerType.SLI_INT64 as const, }; }, - int64() { + float16() { return { - type: InternalSerializerType.INT64 as const, + type: InternalSerializerType.FLOAT16 as const, }; }, - float() { + float32() { return { - type: InternalSerializerType.FLOAT as const, + type: InternalSerializerType.FLOAT32 as const, }; }, - double() { + float64() { return { - type: InternalSerializerType.DOUBLE as const, + type: InternalSerializerType.FLOAT64 as const, }; }, binary() { @@ -410,9 +401,9 @@ export const Type = { type: InternalSerializerType.BINARY as const, }; }, - date() { + duration() { return { - type: InternalSerializerType.DATE as const, + type: InternalSerializerType.DURATION as const, }; }, timestamp() { @@ -420,39 +411,44 @@ export const Type = { type: InternalSerializerType.TIMESTAMP as const, }; }, - stringTypedArray() { + boolArray() { + return { + type: InternalSerializerType.BOOL_ARRAY as const, + }; + }, + int8Array() { return { - type: InternalSerializerType.FURY_STRING_ARRAY as const, + type: InternalSerializerType.INT8_ARRAY as const, }; }, - boolTypedArray() { + int16Array() { return { - type: InternalSerializerType.FURY_PRIMITIVE_BOOL_ARRAY as const, + type: InternalSerializerType.INT16_ARRAY as const, }; }, - shortTypedArray() { + int32Array() { return { - type: InternalSerializerType.FURY_PRIMITIVE_SHORT_ARRAY as const, + type: InternalSerializerType.INT32_ARRAY as const, }; }, - intTypedArray() { + int64Array() { return { - type: InternalSerializerType.FURY_PRIMITIVE_INT_ARRAY as const, + type: InternalSerializerType.INT64_ARRAY as const, }; }, - longTypedArray() { + float16Array() { return { - type: InternalSerializerType.FURY_PRIMITIVE_LONG_ARRAY as const, + type: InternalSerializerType.FLOAT16_ARRAY as const, }; }, - floatTypedArray() { + float32Array() { return { - type: InternalSerializerType.FURY_PRIMITIVE_FLOAT_ARRAY as const, + type: InternalSerializerType.FLOAT32_ARRAY as const, }; }, - doubleTypedArray() { + float64Array() { return { - type: InternalSerializerType.FURY_PRIMITIVE_DOUBLE_ARRAY as const, + type: InternalSerializerType.FLOAT64_ARRAY as const, }; }, }; diff --git a/javascript/packages/fury/lib/gen/any.ts b/javascript/packages/fury/lib/gen/any.ts index bb6c8d54a6..cc3900fb57 100644 --- a/javascript/packages/fury/lib/gen/any.ts +++ b/javascript/packages/fury/lib/gen/any.ts @@ -25,6 +25,7 @@ import { InternalSerializerType, RefFlags, Serializer } from "../type"; import { Scope } from "./scope"; import Fury from "../fury"; import { Meta, getMeta } from "../meta"; +import SerializerResolver from "../classResolver"; export class AnySerializer { meta: Meta; @@ -44,7 +45,7 @@ export class AnySerializer { detectSerializer() { const typeId = this.fury.binaryReader.int16(); let serializer: Serializer; - if (typeId === InternalSerializerType.FURY_TYPE_TAG) { + if (typeId === SerializerResolver.getTypeIdByInternalSerializerType(InternalSerializerType.OBJECT)) { const tag = this.fury.classResolver.readTag(this.fury.binaryReader)(); serializer = this.fury.classResolver.getSerializerByTag(tag); } else { diff --git a/javascript/packages/fury/lib/gen/builder.ts b/javascript/packages/fury/lib/gen/builder.ts index 1811852e5b..9859dd8873 100644 --- a/javascript/packages/fury/lib/gen/builder.ts +++ b/javascript/packages/fury/lib/gen/builder.ts @@ -87,12 +87,16 @@ class BinaryReaderBuilder { return `${this.holder}.stringOfVarUInt32()`; } - double() { - return `${this.holder}.double()`; + float64() { + return `${this.holder}.float64()`; } - float() { - return `${this.holder}.float()`; + float32() { + return `${this.holder}.float32()`; + } + + float16() { + return `${this.holder}.float16()`; } uint16() { @@ -115,8 +119,8 @@ class BinaryReaderBuilder { return `${this.holder}.int64()`; } - sliLong() { - return `${this.holder}.sliLong()`; + sliInt64() { + return `${this.holder}.sliInt64()`; } uint32() { @@ -205,20 +209,20 @@ class BinaryWriterBuilder { return `${this.holder}.buffer(${v})`; } - double(v: number | string) { - return `${this.holder}.double(${v})`; + float64(v: number | string) { + return `${this.holder}.float64(${v})`; } - float(v: number | string) { - return `${this.holder}.float(${v})`; + float32(v: number | string) { + return `${this.holder}.float32(${v})`; } int64(v: number | string) { return `${this.holder}.int64(${v})`; } - sliLong(v: number | string) { - return `${this.holder}.sliLong(${v})`; + sliInt64(v: number | string) { + return `${this.holder}.sliInt64(${v})`; } uint32(v: number | string) { diff --git a/javascript/packages/fury/lib/gen/collection.ts b/javascript/packages/fury/lib/gen/collection.ts index 7dfe22977a..0b53edb044 100644 --- a/javascript/packages/fury/lib/gen/collection.ts +++ b/javascript/packages/fury/lib/gen/collection.ts @@ -118,7 +118,7 @@ class CollectionAnySerializer { let serializer: Serializer; if (isSame) { - serializer = this.fury.classResolver.getSerializerById(this.fury.binaryReader.int16()); + serializer = this.fury.classResolver.getSerializerByType(this.fury.binaryReader.int16()); } const len = this.fury.binaryReader.varUInt32(); const result = createCollection(len); @@ -134,7 +134,7 @@ class CollectionAnySerializer { } } if (!isSame) { - serializer = this.fury.classResolver.getSerializerById(this.fury.binaryReader.int16()); + serializer = this.fury.classResolver.getSerializerByType(this.fury.binaryReader.int16()); } accessor(result, index, serializer!.read()); } diff --git a/javascript/packages/fury/lib/gen/datetime.ts b/javascript/packages/fury/lib/gen/datetime.ts index bce8239ca5..cea0dd2618 100644 --- a/javascript/packages/fury/lib/gen/datetime.ts +++ b/javascript/packages/fury/lib/gen/datetime.ts @@ -44,7 +44,7 @@ class TimestampSerializerGenerator extends BaseSerializerGenerator { } } -class DateSerializerGenerator extends BaseSerializerGenerator { +class DurationSerializerGenerator extends BaseSerializerGenerator { description: TypeDescription; constructor(description: TypeDescription, builder: CodecBuilder, scope: Scope) { @@ -72,5 +72,5 @@ class DateSerializerGenerator extends BaseSerializerGenerator { } } -CodegenRegistry.register(InternalSerializerType.DATE, DateSerializerGenerator); +CodegenRegistry.register(InternalSerializerType.DURATION, DurationSerializerGenerator); CodegenRegistry.register(InternalSerializerType.TIMESTAMP, TimestampSerializerGenerator); diff --git a/javascript/packages/fury/lib/gen/index.ts b/javascript/packages/fury/lib/gen/index.ts index c482c1790b..ee449b750b 100644 --- a/javascript/packages/fury/lib/gen/index.ts +++ b/javascript/packages/fury/lib/gen/index.ts @@ -35,7 +35,6 @@ import "./tuple"; import "./typedArray"; import Fury from "../fury"; import "./enum"; -import "./oneof"; export { AnySerializer } from "./any"; @@ -61,7 +60,7 @@ export const generate = (fury: Fury, description: TypeDescription) => { }; function regDependencies(fury: Fury, description: TypeDescription) { - if (description.type === InternalSerializerType.FURY_TYPE_TAG) { + if (description.type === InternalSerializerType.OBJECT) { const options = (description).options; if (options.props) { fury.classResolver.registerSerializerByTag(options.tag); @@ -75,7 +74,7 @@ function regDependencies(fury: Fury, description: TypeDescription) { if (description.type === InternalSerializerType.ARRAY) { regDependencies(fury, (description).options.inner); } - if (description.type === InternalSerializerType.FURY_SET) { + if (description.type === InternalSerializerType.SET) { regDependencies(fury, (description).options.key); } if (description.type === InternalSerializerType.MAP) { @@ -99,7 +98,7 @@ function regDependencies(fury: Fury, description: TypeDescription) { export const generateSerializer = (fury: Fury, description: TypeDescription) => { regDependencies(fury, description); - if (description.type === InternalSerializerType.FURY_TYPE_TAG) { + if (description.type === InternalSerializerType.OBJECT) { return fury.classResolver.getSerializerByTag((description).options.tag); } const func = generate(fury, description); diff --git a/javascript/packages/fury/lib/gen/number.ts b/javascript/packages/fury/lib/gen/number.ts index 1ca410a562..135e6311d2 100644 --- a/javascript/packages/fury/lib/gen/number.ts +++ b/javascript/packages/fury/lib/gen/number.ts @@ -43,63 +43,62 @@ function buildNumberSerializer(writeFun: (builder: CodecBuilder, accessor: strin }; } -CodegenRegistry.register(InternalSerializerType.UINT8, - buildNumberSerializer( - (builder, accessor) => builder.writer.uint8(accessor), - builder => builder.reader.uint8() - ) -); CodegenRegistry.register(InternalSerializerType.INT8, buildNumberSerializer( (builder, accessor) => builder.writer.int8(accessor), builder => builder.reader.int8() ) ); -CodegenRegistry.register(InternalSerializerType.UINT16, - buildNumberSerializer( - (builder, accessor) => builder.writer.uint16(accessor), - builder => builder.reader.uint16() - ) -); + CodegenRegistry.register(InternalSerializerType.INT16, buildNumberSerializer( (builder, accessor) => builder.writer.int16(accessor), builder => builder.reader.int16() ) ); -CodegenRegistry.register(InternalSerializerType.UINT32, - buildNumberSerializer( - (builder, accessor) => builder.writer.uint32(accessor), - builder => builder.reader.uint32() - ) -); + CodegenRegistry.register(InternalSerializerType.INT32, buildNumberSerializer( (builder, accessor) => builder.writer.int32(accessor), builder => builder.reader.int32() ) ); -CodegenRegistry.register(InternalSerializerType.UINT64, + +CodegenRegistry.register(InternalSerializerType.VAR_INT32, buildNumberSerializer( - (builder, accessor) => builder.writer.varUInt64(accessor), - builder => builder.reader.varUInt64() + (builder, accessor) => builder.writer.varInt32(accessor), + builder => builder.reader.varInt32() ) ); + CodegenRegistry.register(InternalSerializerType.INT64, buildNumberSerializer( - (builder, accessor) => builder.writer.sliLong(accessor), - builder => builder.reader.sliLong() + (builder, accessor) => builder.writer.sliInt64(accessor), + builder => builder.reader.sliInt64() + ) +); + +CodegenRegistry.register(InternalSerializerType.SLI_INT64, + buildNumberSerializer( + (builder, accessor) => builder.writer.sliInt64(accessor), + builder => builder.reader.sliInt64() + ) +); +CodegenRegistry.register(InternalSerializerType.FLOAT16, + buildNumberSerializer( + (builder, accessor) => builder.writer.float32(accessor), + builder => builder.reader.float16() ) ); -CodegenRegistry.register(InternalSerializerType.FLOAT, +CodegenRegistry.register(InternalSerializerType.FLOAT32, buildNumberSerializer( - (builder, accessor) => builder.writer.float(accessor), - builder => builder.reader.float() + (builder, accessor) => builder.writer.float32(accessor), + builder => builder.reader.float32() ) ); -CodegenRegistry.register(InternalSerializerType.DOUBLE, +CodegenRegistry.register(InternalSerializerType.FLOAT64, buildNumberSerializer( - (builder, accessor) => builder.writer.double(accessor), - builder => builder.reader.double() + (builder, accessor) => builder.writer.float64(accessor), + builder => builder.reader.float64() ) ); diff --git a/javascript/packages/fury/lib/gen/object.ts b/javascript/packages/fury/lib/gen/object.ts index 2258dee71a..3ea2eb9bb0 100644 --- a/javascript/packages/fury/lib/gen/object.ts +++ b/javascript/packages/fury/lib/gen/object.ts @@ -24,6 +24,7 @@ import { ObjectTypeDescription, TypeDescription } from "../description"; import { fromString } from "../platformBuffer"; import { CodegenRegistry } from "./router"; import { BaseSerializerGenerator, RefState } from "./serializer"; +import SerializerResolver from "../classResolver"; function computeFieldHash(hash: number, id: number): number { let newHash = (hash) * 31 + (id); @@ -48,10 +49,8 @@ const computeStringHash = (str: string) => { const computeStructHash = (description: TypeDescription) => { let hash = 17; for (const [, value] of Object.entries((description).options.props).sort()) { - let id = value.type; - if (value.type === InternalSerializerType.ARRAY || value.type === InternalSerializerType.TUPLE || value.type === InternalSerializerType.MAP) { - id = Math.floor(value.type); // TODO add map key&value type into schema hash - } else if (value.type === InternalSerializerType.FURY_TYPE_TAG) { + let id = SerializerResolver.getTypeIdByInternalSerializerType(value.type); + if (value.type === InternalSerializerType.OBJECT) { id = computeStringHash((value).options.tag); } hash = computeFieldHash(hash, id); @@ -139,4 +138,4 @@ class ObjectSerializerGenerator extends BaseSerializerGenerator { } } -CodegenRegistry.register(InternalSerializerType.FURY_TYPE_TAG, ObjectSerializerGenerator); +CodegenRegistry.register(InternalSerializerType.OBJECT, ObjectSerializerGenerator); diff --git a/javascript/packages/fury/lib/gen/oneof.ts b/javascript/packages/fury/lib/gen/oneof.ts deleted file mode 100644 index 4cb8b93b44..0000000000 --- a/javascript/packages/fury/lib/gen/oneof.ts +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import { OneofTypeDescription, Type, TypeDescription } from "../description"; -import { CodecBuilder } from "./builder"; -import { BaseSerializerGenerator } from "./serializer"; -import { CodegenRegistry } from "./router"; -import { InternalSerializerType, RefFlags } from "../type"; -import { Scope } from "./scope"; - -class OneofSerializerGenerator extends BaseSerializerGenerator { - description: OneofTypeDescription; - - constructor(description: TypeDescription, builder: CodecBuilder, scope: Scope) { - super(description, builder, scope); - this.description = description; - } - - writeStmt(): string { - throw new Error("Type oneof writeStmt can't inline"); - } - - readStmt(): string { - throw new Error("Type oneof readStmt can't inline"); - } - - toWriteEmbed(accessor: string, excludeHead = false) { - if (excludeHead) { - throw new Error("Oneof can't excludeHead"); - } - if (Object.values(this.description.options.inner).length < 1) { - throw new Error("Type oneof must contain at least one field"); - } - const stmts = [ - `if (${accessor} === null || ${accessor} === undefined) { - ${this.builder.writer.int8(RefFlags.NullFlag)}; - }`, - ]; - Object.entries(this.description.options.inner).forEach(([key, value]) => { - const InnerGeneratorClass = CodegenRegistry.get(value.type); - if (!InnerGeneratorClass) { - throw new Error(`${value.type} generator not exists`); - } - const innerGenerator = new InnerGeneratorClass(value, this.builder, this.scope); - stmts.push(` if (${CodecBuilder.safeString(key)} in ${accessor}) { - ${innerGenerator.toWriteEmbed(`${accessor}${CodecBuilder.safePropAccessor(key)}`)} - }`); - }); - stmts.push(` - { - ${this.builder.writer.int8(RefFlags.NullFlag)}; - } - `); - return stmts.join("else"); - } - - toReadEmbed(accessor: (expr: string) => string, excludeHead = false): string { - const AnyGeneratorClass = CodegenRegistry.get(InternalSerializerType.ANY)!; - const anyGenerator = new AnyGeneratorClass(Type.any(), this.builder, this.scope); - return anyGenerator.toReadEmbed(accessor, excludeHead); - } - - toSerializer() { - this.scope.assertNameNotDuplicate("read"); - this.scope.assertNameNotDuplicate("readInner"); - this.scope.assertNameNotDuplicate("write"); - this.scope.assertNameNotDuplicate("writeInner"); - - const declare = ` - const readInner = (fromRef) => { - throw new Error("Type oneof readInner can't call directly"); - }; - const read = () => { - ${this.toReadEmbed(expr => `return ${expr}`)} - }; - const writeInner = (v) => { - throw new Error("Type oneof writeInner can't call directly"); - }; - const write = (v) => { - ${this.toWriteEmbed("v")} - }; - `; - return ` - return function (fury, external) { - ${this.scope.generate()} - ${declare} - return { - read, - readInner, - write, - writeInner, - meta: ${JSON.stringify(this.builder.meta(this.description))} - }; - } - `; - } -} - -CodegenRegistry.register(InternalSerializerType.ONEOF, OneofSerializerGenerator); diff --git a/javascript/packages/fury/lib/gen/serializer.ts b/javascript/packages/fury/lib/gen/serializer.ts index 16d7020efb..6439aaad65 100644 --- a/javascript/packages/fury/lib/gen/serializer.ts +++ b/javascript/packages/fury/lib/gen/serializer.ts @@ -20,6 +20,7 @@ import { InternalSerializerType } from "../type"; import { CodecBuilder } from "./builder"; import { makeHead } from "../referenceResolver"; +import SerializerResolver from "../classResolver"; import { RefFlags } from "../type"; import { Scope } from "./scope"; import { TypeDescription, ObjectTypeDescription } from "../description"; @@ -126,7 +127,7 @@ export abstract class BaseSerializerGenerator implements SerializerGenerator { const meta = this.builder.meta(this.description); const maybeTag = () => { - if (this.description.type !== InternalSerializerType.FURY_TYPE_TAG) { + if (this.description.type !== InternalSerializerType.OBJECT) { return ""; } const safeTag = CodecBuilder.replaceBackslashAndQuote(( this.description).options.tag); @@ -174,7 +175,7 @@ export abstract class BaseSerializerGenerator implements SerializerGenerator { switch (${refFlag}) { case ${RefFlags.NotNullValueFlag}: case ${RefFlags.RefValueFlag}: - if (${this.builder.reader.int16()} === ${InternalSerializerType.FURY_TYPE_TAG}) { + if (${this.builder.reader.int16()} === ${SerializerResolver.getTypeIdByInternalSerializerType(InternalSerializerType.OBJECT)}) { ${this.builder.classResolver.readTag(this.builder.reader.ownName())}; } ${stmt(accessor, RefState.fromCondition(`${refFlag} === ${RefFlags.RefValueFlag}`))} diff --git a/javascript/packages/fury/lib/gen/set.ts b/javascript/packages/fury/lib/gen/set.ts index 6d806a5597..42228d6567 100644 --- a/javascript/packages/fury/lib/gen/set.ts +++ b/javascript/packages/fury/lib/gen/set.ts @@ -49,4 +49,4 @@ class SetSerializerGenerator extends CollectionSerializerGenerator { } } -CodegenRegistry.register(InternalSerializerType.FURY_SET, SetSerializerGenerator); +CodegenRegistry.register(InternalSerializerType.SET, SetSerializerGenerator); diff --git a/javascript/packages/fury/lib/gen/typedArray.ts b/javascript/packages/fury/lib/gen/typedArray.ts index 57d8c0fa75..05631fc305 100644 --- a/javascript/packages/fury/lib/gen/typedArray.ts +++ b/javascript/packages/fury/lib/gen/typedArray.ts @@ -40,7 +40,7 @@ function build(inner: TypeDescription) { private innerGenerator() { const InnerGeneratorClass = CodegenRegistry.get(inner.type); if (!InnerGeneratorClass) { - throw new Error(`${inner.type} generator not exists`); + throw new Error(`${InternalSerializerType[inner.type]} generator not exists`); } return new InnerGeneratorClass(inner, this.builder, this.scope); } @@ -77,10 +77,11 @@ function build(inner: TypeDescription) { }; } -CodegenRegistry.register(InternalSerializerType.FURY_STRING_ARRAY, build(Type.string())); -CodegenRegistry.register(InternalSerializerType.FURY_PRIMITIVE_BOOL_ARRAY, build(Type.bool())); -CodegenRegistry.register(InternalSerializerType.FURY_PRIMITIVE_LONG_ARRAY, build(Type.int64())); -CodegenRegistry.register(InternalSerializerType.FURY_PRIMITIVE_INT_ARRAY, build(Type.int32())); -CodegenRegistry.register(InternalSerializerType.FURY_PRIMITIVE_FLOAT_ARRAY, build(Type.float())); -CodegenRegistry.register(InternalSerializerType.FURY_PRIMITIVE_DOUBLE_ARRAY, build(Type.double())); -CodegenRegistry.register(InternalSerializerType.FURY_PRIMITIVE_SHORT_ARRAY, build(Type.int16())); +CodegenRegistry.register(InternalSerializerType.BOOL_ARRAY, build(Type.bool())); +CodegenRegistry.register(InternalSerializerType.INT8_ARRAY, build(Type.int8())); +CodegenRegistry.register(InternalSerializerType.INT16_ARRAY, build(Type.int16())); +CodegenRegistry.register(InternalSerializerType.INT32_ARRAY, build(Type.int32())); +CodegenRegistry.register(InternalSerializerType.INT64_ARRAY, build(Type.int64())); +CodegenRegistry.register(InternalSerializerType.FLOAT16_ARRAY, build(Type.float16())); +CodegenRegistry.register(InternalSerializerType.FLOAT32_ARRAY, build(Type.float32())); +CodegenRegistry.register(InternalSerializerType.FLOAT64_ARRAY, build(Type.float64())); diff --git a/javascript/packages/fury/lib/meta.ts b/javascript/packages/fury/lib/meta.ts index 4c8234aea6..2e8978f946 100644 --- a/javascript/packages/fury/lib/meta.ts +++ b/javascript/packages/fury/lib/meta.ts @@ -34,77 +34,76 @@ export const getMeta = (description: TypeDescription, fury: Fury): Meta => { case InternalSerializerType.STRING: return { fixedSize: 8, - needToWriteRef: Boolean(fury.config.refTracking) && false, + needToWriteRef: false, type, }; case InternalSerializerType.ARRAY: return { fixedSize: 7, - needToWriteRef: Boolean(fury.config.refTracking) && true, + needToWriteRef: Boolean(fury.config.refTracking), type, }; case InternalSerializerType.TUPLE: return { fixedSize: 7, - needToWriteRef: Boolean(fury.config.refTracking) && true, + needToWriteRef: Boolean(fury.config.refTracking), type, }; case InternalSerializerType.MAP: return { fixedSize: 7, - needToWriteRef: Boolean(fury.config.refTracking) && true, + needToWriteRef: Boolean(fury.config.refTracking), type, }; case InternalSerializerType.BOOL: - case InternalSerializerType.UINT8: case InternalSerializerType.INT8: return { fixedSize: 4, - needToWriteRef: Boolean(fury.config.refTracking) && false, + needToWriteRef: false, type, }; - case InternalSerializerType.UINT16: case InternalSerializerType.INT16: + case InternalSerializerType.FLOAT16: return { fixedSize: 5, - needToWriteRef: Boolean(fury.config.refTracking) && false, + needToWriteRef: false, type, }; - case InternalSerializerType.UINT32: + case InternalSerializerType.VAR_INT32: case InternalSerializerType.INT32: - case InternalSerializerType.FLOAT: + case InternalSerializerType.FLOAT32: return { fixedSize: 7, - needToWriteRef: Boolean(fury.config.refTracking) && false, + needToWriteRef: false, type, }; - case InternalSerializerType.UINT64: + case InternalSerializerType.SLI_INT64: case InternalSerializerType.INT64: - case InternalSerializerType.DOUBLE: + case InternalSerializerType.FLOAT64: return { fixedSize: 11, - needToWriteRef: Boolean(fury.config.refTracking) && false, + needToWriteRef: false, type, }; case InternalSerializerType.BINARY: return { fixedSize: 8, - needToWriteRef: Boolean(fury.config.refTracking) && true, + needToWriteRef: Boolean(fury.config.refTracking), type, }; - case InternalSerializerType.DATE: + case InternalSerializerType.DURATION: return { fixedSize: 7, - needToWriteRef: Boolean(fury.config.refTracking) && false, + needToWriteRef: false, type, }; case InternalSerializerType.TIMESTAMP: return { fixedSize: 11, - needToWriteRef: Boolean(fury.config.refTracking) && false, + needToWriteRef: false, type, }; - case InternalSerializerType.FURY_TYPE_TAG: + case InternalSerializerType.OBJECT: { const options = (description).options; let fixedSize = ClassResolver.tagBuffer(options.tag).byteLength + 8; @@ -115,40 +114,41 @@ export const getMeta = (description: TypeDescription, fury: Fury): Meta => { } return { fixedSize, - needToWriteRef: Boolean(fury.config.refTracking) && true, + needToWriteRef: Boolean(fury.config.refTracking), type, }; } - case InternalSerializerType.FURY_SET: + case InternalSerializerType.SET: return { fixedSize: 7, - needToWriteRef: Boolean(fury.config.refTracking) && true, + needToWriteRef: Boolean(fury.config.refTracking), type, }; - case InternalSerializerType.FURY_PRIMITIVE_BOOL_ARRAY: - case InternalSerializerType.FURY_PRIMITIVE_SHORT_ARRAY: - case InternalSerializerType.FURY_PRIMITIVE_INT_ARRAY: - case InternalSerializerType.FURY_PRIMITIVE_LONG_ARRAY: - case InternalSerializerType.FURY_PRIMITIVE_FLOAT_ARRAY: - case InternalSerializerType.FURY_PRIMITIVE_DOUBLE_ARRAY: - case InternalSerializerType.FURY_STRING_ARRAY: + case InternalSerializerType.BOOL_ARRAY: + case InternalSerializerType.INT8_ARRAY: + case InternalSerializerType.INT16_ARRAY: + case InternalSerializerType.INT32_ARRAY: + case InternalSerializerType.INT64_ARRAY: + case InternalSerializerType.FLOAT16_ARRAY: + case InternalSerializerType.FLOAT32_ARRAY: + case InternalSerializerType.FLOAT64_ARRAY: return { fixedSize: 7, - needToWriteRef: Boolean(fury.config.refTracking) && true, + needToWriteRef: Boolean(fury.config.refTracking), type, }; case InternalSerializerType.ONEOF: case InternalSerializerType.ANY: return { fixedSize: 11, - needToWriteRef: Boolean(fury.config.refTracking) && true, + needToWriteRef: Boolean(fury.config.refTracking), type, }; case InternalSerializerType.ENUM: return { fixedSize: 7, - needToWriteRef: Boolean(fury.config.refTracking) && false, + needToWriteRef: false, type, }; default: diff --git a/javascript/packages/fury/lib/reader/index.ts b/javascript/packages/fury/lib/reader/index.ts index e0df600016..614eecbf51 100644 --- a/javascript/packages/fury/lib/reader/index.ts +++ b/javascript/packages/fury/lib/reader/index.ts @@ -90,7 +90,7 @@ export const BinaryReader = (config: Config) => { return result; } - function sliLong() { + function sliInt64() { const i = dataView.getUint32(cursor, true); if ((i & 0b1) != 0b1) { cursor += 4; @@ -100,13 +100,13 @@ export const BinaryReader = (config: Config) => { return varInt64(); } - function float() { + function float32() { const result = dataView.getFloat32(cursor, true); cursor += 4; return result; } - function double() { + function float64() { const result = dataView.getFloat64(cursor, true); cursor += 8; return result; @@ -298,6 +298,34 @@ export const BinaryReader = (config: Config) => { return (v >> 1n) ^ -(v & 1n); // zigZag decode } + function float16() { + const asUint16 = uint16(); + const sign = asUint16 >> 15; + const exponent = (asUint16 >> 10) & 0x1F; + const mantissa = asUint16 & 0x3FF; + + // IEEE 754-2008 + if (exponent === 0) { + if (mantissa === 0) { + // +-0 + return sign === 0 ? 0 : -0; + } else { + // Denormalized number + return (sign === 0 ? 1 : -1) * mantissa * 2 ** (1 - 15 - 10); + } + } else if (exponent === 31) { + if (mantissa === 0) { + // Infinity + return sign === 0 ? Infinity : -Infinity; + } else { + // NaN + return NaN; + } + } else { + // Normalized number + return (sign === 0 ? 1 : -1) * (1 + mantissa * 2 ** -10) * 2 ** (exponent - 15); + } + } return { getCursor: () => cursor, setCursor: (v: number) => (cursor = v), @@ -314,15 +342,16 @@ export const BinaryReader = (config: Config) => { stringUtf8, stringLatin1, stringOfVarUInt32, - double, - float, + float64, + float32, uint16, int16, uint64, skip, int64, - sliLong, + sliInt64, uint32, int32, + float16, }; }; diff --git a/javascript/packages/fury/lib/referenceResolver.ts b/javascript/packages/fury/lib/referenceResolver.ts index d826d724c4..cc27706f47 100644 --- a/javascript/packages/fury/lib/referenceResolver.ts +++ b/javascript/packages/fury/lib/referenceResolver.ts @@ -23,9 +23,10 @@ import { BinaryWriter, InternalSerializerType, } from "./type"; +import SerializerResolver from "./classResolver"; export const makeHead = (flag: RefFlags, type: InternalSerializerType) => { - return (((Math.floor(type) << 16) >>> 16) << 8) | ((flag << 24) >>> 24); + return (((SerializerResolver.getTypeIdByInternalSerializerType(type) << 16) >>> 16) << 8) | ((flag << 24) >>> 24); }; export const ReferenceResolver = ( diff --git a/javascript/packages/fury/lib/type.ts b/javascript/packages/fury/lib/type.ts index fd893af066..88881e6e70 100644 --- a/javascript/packages/fury/lib/type.ts +++ b/javascript/packages/fury/lib/type.ts @@ -25,36 +25,42 @@ export type BinaryWriter = ReturnType; export type BinaryReader = ReturnType; export enum InternalSerializerType { - STRING = 13, - ARRAY = 25, - TUPLE = 25.1, - MAP = 30, - BOOL = 1, - UINT8 = 2, - INT8 = 3, - UINT16 = 4, - INT16 = 5, - UINT32 = 6, - INT32 = 7, - UINT64 = 8, - INT64 = 9, - FLOAT = 11, - DOUBLE = 12, - BINARY = 14, - DATE = 16, - TIMESTAMP = 18, - ENUM = 19, // The cross-language enum has not yet been determined, this is not the final value, it will change later - FURY_TYPE_TAG = 256, - FURY_SET = 257, - FURY_PRIMITIVE_BOOL_ARRAY = 258, - FURY_PRIMITIVE_SHORT_ARRAY = 259, - FURY_PRIMITIVE_INT_ARRAY = 260, - FURY_PRIMITIVE_LONG_ARRAY = 261, - FURY_PRIMITIVE_FLOAT_ARRAY = 262, - FURY_PRIMITIVE_DOUBLE_ARRAY = 263, - FURY_STRING_ARRAY = 264, - ANY = -1, - ONEOF = -2, + // primitive type + BOOL, + INT8, + INT16, + INT32, + VAR_INT32, + INT64, + VAR_INT64, + SLI_INT64, + FLOAT16, + FLOAT32, + FLOAT64, + STRING, + ENUM, + LIST, + SET, + MAP, + DURATION, + TIMESTAMP, + DECIMAL, + BINARY, + ARRAY, + BOOL_ARRAY, + INT8_ARRAY, + INT16_ARRAY, + INT32_ARRAY, + INT64_ARRAY, + FLOAT16_ARRAY, + FLOAT32_ARRAY, + FLOAT64_ARRAY, + OBJECT, + + // alias type, only use by javascript + ANY, + ONEOF, + TUPLE, } export enum ConfigFlags { diff --git a/javascript/packages/fury/lib/writer.ts b/javascript/packages/fury/lib/writer/index.ts similarity index 95% rename from javascript/packages/fury/lib/writer.ts rename to javascript/packages/fury/lib/writer/index.ts index e329f69ed3..651e2d8c31 100644 --- a/javascript/packages/fury/lib/writer.ts +++ b/javascript/packages/fury/lib/writer/index.ts @@ -17,9 +17,10 @@ * under the License. */ -import { Config, HalfMaxInt32, HalfMinInt32, LATIN1, UTF8 } from "./type"; -import { PlatformBuffer, alloc, strByteLength } from "./platformBuffer"; -import { OwnershipError } from "./error"; +import { Config, HalfMaxInt32, HalfMinInt32, LATIN1, UTF8 } from "../type"; +import { PlatformBuffer, alloc, strByteLength } from "../platformBuffer"; +import { OwnershipError } from "../error"; +import { toFloat16 } from "./number"; const MAX_POOL_SIZE = 1024 * 1024 * 3; // 3MB @@ -102,7 +103,7 @@ export const BinaryWriter = (config: Config) => { cursor += 8; } - function sliLong(v: bigint | number) { + function sliInt64(v: bigint | number) { if (v <= HalfMaxInt32 && v >= HalfMinInt32) { // write: // 00xxx -> 0xxx @@ -120,12 +121,12 @@ export const BinaryWriter = (config: Config) => { } } - function float(v: number) { + function float32(v: number) { dataView.setFloat32(cursor, v, true); cursor += 4; } - function double(v: number) { + function float64(v: number) { dataView.setFloat64(cursor, v, true); cursor += 8; } @@ -301,6 +302,10 @@ export const BinaryWriter = (config: Config) => { }; } + function float16(value: number) { + uint16(toFloat16(value)); + } + function getCursor() { return cursor; } @@ -339,10 +344,11 @@ export const BinaryWriter = (config: Config) => { bufferWithoutMemCheck, uint64, buffer, - double, - float, + float16, + float64, + float32, int64, - sliLong, + sliInt64, uint32, int32, getCursor, diff --git a/javascript/packages/fury/lib/writer/number.ts b/javascript/packages/fury/lib/writer/number.ts new file mode 100644 index 0000000000..a9acab3c70 --- /dev/null +++ b/javascript/packages/fury/lib/writer/number.ts @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +const float32View = new Float32Array(1); +const int32View = new Int32Array(float32View.buffer); + +export function toFloat16(value: number) { + float32View[0] = value; + const floatValue = int32View[0]; + const sign = (floatValue >>> 16) & 0x8000; // sign only + const exponent = ((floatValue >>> 23) & 0xff) - 127; // extract exponent from floatValue + const significand = floatValue & 0x7fffff; // extract significand from floatValue + + if (exponent === 128) { // floatValue is NaN or Infinity + return sign | ((exponent === 128) ? 0x7c00 : 0x7fff); + } + + if (exponent > 15) { + return sign | 0x7c00; // return Infinity + } + + if (exponent < -14) { + return sign | 0x3ff; // returns ±max subnormal + } + + if (exponent <= 0) { + return sign | ((significand | 0x800000) >> (1 - exponent + 10)); + } + + return sign | ((exponent + 15) << 10) | (significand >> 13); +} diff --git a/javascript/test/array.test.ts b/javascript/test/array.test.ts index 6646e431b6..bee5b189ab 100644 --- a/javascript/test/array.test.ts +++ b/javascript/test/array.test.ts @@ -41,26 +41,23 @@ describe('array', () => { }); test('should typedarray work', () => { const description = { - type: InternalSerializerType.FURY_TYPE_TAG, + type: InternalSerializerType.OBJECT, options: { props: { a: { - type: InternalSerializerType.FURY_PRIMITIVE_BOOL_ARRAY, + type: InternalSerializerType.BOOL_ARRAY, }, a2: { - type: InternalSerializerType.FURY_PRIMITIVE_SHORT_ARRAY, + type: InternalSerializerType.INT16_ARRAY, }, a3: { - type: InternalSerializerType.FURY_PRIMITIVE_INT_ARRAY, + type: InternalSerializerType.INT32_ARRAY, }, a4: { - type: InternalSerializerType.FURY_PRIMITIVE_LONG_ARRAY, + type: InternalSerializerType.INT64_ARRAY, }, a6: { - type: InternalSerializerType.FURY_PRIMITIVE_DOUBLE_ARRAY, - }, - a7: { - type: InternalSerializerType.FURY_STRING_ARRAY + type: InternalSerializerType.FLOAT64_ARRAY, }, }, tag: "example.foo" @@ -74,7 +71,6 @@ describe('array', () => { a3: [3, 5, 76], a4: [634, 564, 76], a6: [234243.555, 55654.6786], - a7: ["hello", "world", "!"] }, serializer); const result = fury.deserialize( input @@ -86,66 +82,17 @@ describe('array', () => { a3: [3, 5, 76], a4: [634, 564, 76], a6: [234243.555, 55654.6786], - a7: ["hello", "world", "!"] - }) - }); - test('should string array work', () => { - const description = { - type: InternalSerializerType.FURY_TYPE_TAG, - options: { - props: { - a7: { - type: InternalSerializerType.FURY_STRING_ARRAY - }, - }, - tag: "example.foo" - } - }; - - const fury = new Fury({ refTracking: true }); - const serializer = fury.registerSerializer(description).serializer; - const input = fury.serialize({ - a7: ["hello", "world", "!"] - }, serializer); - const result = fury.deserialize( - input - ); - expect(result).toEqual({ - a7: ["hello", "world", "!"] - }) - }); - test('should string array work when latin1 enable', () => { - const description = { - type: InternalSerializerType.FURY_TYPE_TAG, - options: { - props: { - a7: { - type: InternalSerializerType.FURY_STRING_ARRAY - }, - }, - tag: "example.foo" - } - }; - - const fury = new Fury({ refTracking: true }); - const serializer = fury.registerSerializer(description).serializer; - const input = fury.serialize({ - a7: ["hello", "world", "!"] - }, serializer); - const result = fury.deserialize( - input - ); - expect(result).toEqual({ - a7: ["hello", "world", "!"] }) }); + + test('should floatarray work', () => { const description: ObjectTypeDescription = { - type: InternalSerializerType.FURY_TYPE_TAG, + type: InternalSerializerType.OBJECT, options: { props: { a5: { - type: InternalSerializerType.FURY_PRIMITIVE_FLOAT_ARRAY, + type: InternalSerializerType.FLOAT32_ARRAY, }, }, tag: "example.foo" diff --git a/javascript/test/binary.test.ts b/javascript/test/binary.test.ts index 7dd5962ec4..35ecab42ec 100644 --- a/javascript/test/binary.test.ts +++ b/javascript/test/binary.test.ts @@ -24,7 +24,7 @@ import { describe, expect, test } from '@jest/globals'; describe('binary', () => { test('should binary work', () => { const description = { - type: InternalSerializerType.FURY_TYPE_TAG, + type: InternalSerializerType.OBJECT, options: { props: { a: { diff --git a/javascript/test/datetime.test.ts b/javascript/test/datetime.test.ts index 46d4064191..09e3274372 100644 --- a/javascript/test/datetime.test.ts +++ b/javascript/test/datetime.test.ts @@ -34,7 +34,7 @@ describe('datetime', () => { test('should datetime work', () => { const description = Type.object("example.foo", { a: Type.timestamp(), - b: Type.date(), + b: Type.duration(), }) const fury = new Fury({ refTracking: true }); const serializer = fury.registerSerializer(description).serializer; diff --git a/javascript/test/fixtures/tuple.ts b/javascript/test/fixtures/tuple.ts index ae34c9f10d..ba7bd0dfdb 100644 --- a/javascript/test/fixtures/tuple.ts +++ b/javascript/test/fixtures/tuple.ts @@ -53,7 +53,7 @@ export const tupleType2 = Type.tuple( [ export const tupleType3 = Type.tuple([ Type.string(), Type.bool(), - Type.uint32(), + Type.int32(), Type.tuple([ Type.binary() ]) diff --git a/javascript/test/fury.test.ts b/javascript/test/fury.test.ts index c3723aa9c2..cb4a265c08 100644 --- a/javascript/test/fury.test.ts +++ b/javascript/test/fury.test.ts @@ -82,7 +82,7 @@ describe('fury', () => { // const description5 = Type.float() // testDescription(description5, 123.456) - const description6 = Type.double() + const description6 = Type.float64() testDescription(description6, 123.456789) const description7 = Type.binary() diff --git a/javascript/test/io.test.ts b/javascript/test/io.test.ts index 8d5edf9f7c..3e6e29e3f9 100644 --- a/javascript/test/io.test.ts +++ b/javascript/test/io.test.ts @@ -293,22 +293,22 @@ function num2Bin(num: number) { test('should float work', () => { const writer = BinaryWriter(config); - writer.float(10.01); + writer.float32(10.01); const ab = writer.dump(); expect(ab.byteLength).toBe(4); const reader = BinaryReader(config); reader.reset(ab); - expect(reader.float().toFixed(2)).toBe((10.01).toFixed(2)); + expect(reader.float32().toFixed(2)).toBe((10.01).toFixed(2)); }); - test('should double work', () => { + test('should float64 work', () => { const writer = BinaryWriter(config); - writer.double(10.01); + writer.float64(10.01); const ab = writer.dump(); expect(ab.byteLength).toBe(8); const reader = BinaryReader(config); reader.reset(ab); - expect(reader.double().toFixed(2)).toBe((10.01).toFixed(2)); + expect(reader.float64().toFixed(2)).toBe((10.01).toFixed(2)); }); test('should reserve work', () => { @@ -391,20 +391,20 @@ function num2Bin(num: number) { test('should silong work', () => { const writer = BinaryWriter(config); - writer.sliLong(2n ** 2n); + writer.sliInt64(2n ** 2n); const ab = writer.dump(); const reader = BinaryReader({}); reader.reset(ab) - expect(reader.sliLong()).toBe(2n ** 2n); + expect(reader.sliInt64()).toBe(2n ** 2n); }); test('should silong work', () => { const writer = BinaryWriter(config); - writer.sliLong(2n ** 62n); + writer.sliInt64(2n ** 62n); const ab = writer.dump(); const reader = BinaryReader({}); reader.reset(ab) - expect(reader.sliLong()).toBe(2n ** 62n); + expect(reader.sliInt64()).toBe(2n ** 62n); }); }); }) diff --git a/javascript/test/number.test.ts b/javascript/test/number.test.ts index 65db7df4cb..8cfed9509e 100644 --- a/javascript/test/number.test.ts +++ b/javascript/test/number.test.ts @@ -25,7 +25,7 @@ describe('number', () => { const fury = new Fury({ refTracking: true }); const serialize = fury.registerSerializer({ - type: InternalSerializerType.FURY_TYPE_TAG, + type: InternalSerializerType.OBJECT, options: { tag: "example.foo", props: { @@ -45,7 +45,7 @@ describe('number', () => { const fury = new Fury({ refTracking: true }); const serialize = fury.registerSerializer({ - type: InternalSerializerType.FURY_TYPE_TAG, + type: InternalSerializerType.OBJECT, options: { tag: "example.foo", props: { @@ -65,7 +65,7 @@ describe('number', () => { const fury = new Fury({ refTracking: true }); const serializer = fury.registerSerializer({ - type: InternalSerializerType.FURY_TYPE_TAG, + type: InternalSerializerType.OBJECT, options: { tag: "example.foo", props: { @@ -85,7 +85,7 @@ describe('number', () => { const fury = new Fury({ refTracking: true }); const serializer = fury.registerSerializer({ - type: InternalSerializerType.FURY_TYPE_TAG, + type: InternalSerializerType.OBJECT, options: { tag: "example.foo", props: { @@ -102,83 +102,17 @@ describe('number', () => { result.a = Number(result.a) expect(result).toEqual({ a: 1 }) }); - test('should u8 work', () => { - - const fury = new Fury({ refTracking: true }); - const description = Type.object("example.foo", { - a: Type.uint8() - }) - const serializer = fury.registerSerializer(description).serializer; - const input = fury.serialize({ a: 1 }, serializer); - const result = fury.deserialize( - input - ); - expect(result).toEqual({ a: 1 }) - }); - test('should u16 work', () => { - - const fury = new Fury({ refTracking: true }); - const description = Type.object("example.foo", { - a: Type.uint16(), - }) - const serializer = fury.registerSerializer(description).serializer; - const input = fury.serialize({ a: 1 }, serializer); - const result = fury.deserialize( - input - ); - expect(result).toEqual({ a: 1 }) - }); - test('should u32 work', () => { - - const fury = new Fury({ refTracking: true }); - const serializer = fury.registerSerializer({ - type: InternalSerializerType.FURY_TYPE_TAG, - options: { - tag: "example.foo", - props: { - a: { - type: InternalSerializerType.UINT32 - } - } - } - }).serializer; - const input = fury.serialize({ a: 1 }, serializer); - const result = fury.deserialize( - input - ); - expect(result).toEqual({ a: 1 }) - }); - test('should u64 work', () => { - - const fury = new Fury({ refTracking: true }); - const serializer = fury.registerSerializer({ - type: InternalSerializerType.FURY_TYPE_TAG, - options: { - tag: "example.foo", - props: { - a: { - type: InternalSerializerType.UINT64 - } - } - } - }).serializer; - const input = fury.serialize({ a: 1 }, serializer); - const result = fury.deserialize( - input - ); - result.a = Number(result.a) - expect(result).toEqual({ a: 1 }) - }); + test('should float work', () => { const fury = new Fury({ refTracking: true }); const serializer = fury.registerSerializer({ - type: InternalSerializerType.FURY_TYPE_TAG, + type: InternalSerializerType.OBJECT, options: { tag: "example.foo", props: { a: { - type: InternalSerializerType.FLOAT + type: InternalSerializerType.FLOAT32 } } } @@ -189,16 +123,16 @@ describe('number', () => { ); expect(result.a).toBeCloseTo(1.2) }); - test('should double work', () => { + test('should float64 work', () => { const fury = new Fury({ refTracking: true }); const serializer = fury.registerSerializer({ - type: InternalSerializerType.FURY_TYPE_TAG, + type: InternalSerializerType.OBJECT, options: { tag: "example.foo", props: { a: { - type: InternalSerializerType.DOUBLE + type: InternalSerializerType.FLOAT64 } } } diff --git a/javascript/test/object.test.ts b/javascript/test/object.test.ts index 4f9eb72075..ec2f7e4286 100644 --- a/javascript/test/object.test.ts +++ b/javascript/test/object.test.ts @@ -23,11 +23,11 @@ import { describe, expect, test } from '@jest/globals'; describe('object', () => { test('should object work', () => { const description = { - type: InternalSerializerType.FURY_TYPE_TAG as const, + type: InternalSerializerType.OBJECT as const, options: { props: { a: { - type: InternalSerializerType.FURY_TYPE_TAG as const, + type: InternalSerializerType.OBJECT as const, options: { tag: "example.bar", props: { @@ -54,11 +54,11 @@ describe('object', () => { test('should null value work', () => { const description = { - type: InternalSerializerType.FURY_TYPE_TAG as const, + type: InternalSerializerType.OBJECT as const, options: { props: { a: { - type: InternalSerializerType.FURY_TYPE_TAG as const, + type: InternalSerializerType.OBJECT as const, options: { tag: "example.bar", props: { @@ -87,8 +87,8 @@ describe('object', () => { a: Type.array(Type.object('example.bar', { b: Type.string(), c: Type.bool(), - d: Type.uint32(), - e: Type.uint64(), + d: Type.int32(), + e: Type.int64(), f: Type.binary(), })) }) @@ -105,11 +105,11 @@ describe('object', () => { test('should write tag and read tag work', () => { const description = { - type: InternalSerializerType.FURY_TYPE_TAG, + type: InternalSerializerType.OBJECT, options: { props: { a: { - type: InternalSerializerType.FURY_TYPE_TAG as const, + type: InternalSerializerType.OBJECT as const, options: { tag: "example.bar", props: { @@ -120,7 +120,7 @@ describe('object', () => { } }, a2: { - type: InternalSerializerType.FURY_TYPE_TAG as const, + type: InternalSerializerType.OBJECT as const, options: { tag: "example.bar", } @@ -141,11 +141,11 @@ describe('object', () => { test('should ciycle ref work', () => { const description = { - type: InternalSerializerType.FURY_TYPE_TAG, + type: InternalSerializerType.OBJECT, options: { props: { a: { - type: InternalSerializerType.FURY_TYPE_TAG as const, + type: InternalSerializerType.OBJECT as const, options: { tag: "example.bar", props: { @@ -156,7 +156,7 @@ describe('object', () => { } }, a2: { - type: InternalSerializerType.FURY_TYPE_TAG as const, + type: InternalSerializerType.OBJECT as const, options: { tag: "example.foo", } diff --git a/javascript/test/oneof.test.ts b/javascript/test/oneof.test.ts deleted file mode 100644 index 0cabf19030..0000000000 --- a/javascript/test/oneof.test.ts +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import Fury, { TypeDescription, InternalSerializerType, Type } from '../packages/fury/index'; -import { describe, expect, test } from '@jest/globals'; - -const oneOfThree = Type.oneof({ - option1: Type.string(), - option2: Type.object("foo", { - a: Type.int32() - }), - option3: Type.int32(), -}); - -describe('oneof', () => { - test('option1: should oneof work1', () => { - const fury = new Fury({ refTracking: true }); - const { serialize, deserialize } = fury.registerSerializer(oneOfThree); - const obj = { - option1: "hello" - } - const input = serialize(obj); - const result = deserialize( - input - ); - expect(result).toEqual(obj.option1) - }); - test('option2: should oneof work', () => { - const fury = new Fury({ refTracking: true }); - const { serialize, deserialize } = fury.registerSerializer(oneOfThree); - const obj = { - option2: { - a: 123 - } - } - const input = serialize(obj); - const result = deserialize( - input - ); - expect(result).toEqual(obj.option2) - }); - test('option3: should oneof work', () => { - const fury = new Fury({ refTracking: true }); - const { serialize, deserialize } = fury.registerSerializer(oneOfThree); - const obj = { - option3: 123 - } - const input = serialize(obj); - const result = deserialize( - input - ); - expect(result).toEqual(obj.option3) - }); - test('should nested oneof work1', () => { - const fury = new Fury({ refTracking: true }); - const { serialize, deserialize } = fury.registerSerializer(Type.object("foo2", { - f: oneOfThree - })); - const obj = { - option1: "hello" - } - const input = serialize({ - f: obj - }); - const result = deserialize( - input - ); - expect(result).toEqual({ - f: obj.option1 - }) - }); -}); - - diff --git a/javascript/test/protocol/struct.test.ts b/javascript/test/protocol/struct.test.ts index 0dd3141919..3c0f6fe400 100644 --- a/javascript/test/protocol/struct.test.ts +++ b/javascript/test/protocol/struct.test.ts @@ -26,8 +26,8 @@ describe('protocol', () => { const fury = new Fury({ refTracking: true }); const { serialize, deserialize } = fury.registerSerializer(Type.object("example.foo", { - foo: Type.any(), - bar: Type.any(), + foo: Type.string(), + bar: Type.int32(), map: Type.map(Type.any(), Type.any()), set: Type.set(Type.any()), list: Type.array(Type.any()), diff --git a/javascript/test/referenceResolve.test.ts b/javascript/test/referenceResolve.test.ts index 1a3af1e4d3..c4c2303564 100644 --- a/javascript/test/referenceResolve.test.ts +++ b/javascript/test/referenceResolve.test.ts @@ -24,6 +24,7 @@ import { makeHead } from '../packages/fury/lib/referenceResolver'; import { RefFlags } from '../packages/fury/lib/type'; import { BinaryWriter } from '../packages/fury/lib/writer'; import { BinaryReader } from '../packages/fury/lib/reader'; +import SerializerResolver from '../packages/fury/lib/classResolver'; describe('referenceResolve', () => { test('should write head work', () => { @@ -40,7 +41,7 @@ describe('referenceResolve', () => { const reader = BinaryReader({}); reader.reset(ab); expect(reader.int8()).toBe(RefFlags.NotNullValueFlag); - expect(reader.int16()).toBe(InternalSerializerType.STRING); + expect(reader.int16()).toBe(SerializerResolver.getTypeIdByInternalSerializerType(InternalSerializerType.STRING)); }); test('should make head work when flag is zero', () => { @@ -51,7 +52,7 @@ describe('referenceResolve', () => { const reader = BinaryReader({}); reader.reset(ab); expect(reader.int8()).toBe(RefFlags.RefValueFlag); - expect(reader.int16()).toBe(InternalSerializerType.STRING); + expect(reader.int16()).toBe(SerializerResolver.getTypeIdByInternalSerializerType(InternalSerializerType.STRING)); }); });