This is an automated email from the ASF dual-hosted git repository.

wangweipeng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-fury.git


The following commit(s) were added to refs/heads/main by this push:
     new e8d59d66 chore(javaScript):  enhancement code readability (#1505)
e8d59d66 is described below

commit e8d59d66eb76fde48dc5b30611e87c6e5226fad4
Author: weipeng <[email protected]>
AuthorDate: Sun Apr 14 19:30:44 2024 +0800

    chore(javaScript):  enhancement code readability (#1505)
    
    <!--
    **Thanks for contributing to Fury.**
    
    **If this is your first time opening a PR on fury, you can refer to
    
[CONTRIBUTING.md](https://github.com/apache/incubator-fury/blob/main/CONTRIBUTING.md).**
    
    Contribution Checklist
    
    - The **Apache Fury (incubating)** community has restrictions on the
    naming of pr titles. You can also find instructions in
    
[CONTRIBUTING.md](https://github.com/apache/incubator-fury/blob/main/CONTRIBUTING.md).
    
    - Fury has a strong focus on performance. If the PR you submit will have
    an impact on performance, please benchmark it first and provide the
    benchmark result here.
    -->
    
    ## What does this PR do?
    **Reduce the use of closures to enhance code readability.**
    We used closures extensively in the past, which could easily trigger
    Inline-Cache in `V8`, but this practice was detrimental to code
    readability. Now that the infrastructure is stable, we can replace them
    with classes to enhance readability without affecting performance.
    
    
    
    ## Does this PR introduce any user-facing change?
    There are no user-facing change.
    
    
    ## Benchmark
    | (index) | serialize | deserialize |
    |----------|------------|--------------|
    | fury     | 151        | 120          |
    | protobuf | 29         | 39           |
    | json     | 28         | 35           |
    
    ---------
    
    Co-authored-by: wangweipeng <weipeng>
---
 javascript/packages/fury/lib/classResolver.ts     |   7 +-
 javascript/packages/fury/lib/fury.ts              |  14 +-
 javascript/packages/fury/lib/gen/serializer.ts    |   5 +-
 javascript/packages/fury/lib/reader/index.ts      | 298 +++++++++---------
 javascript/packages/fury/lib/referenceResolver.ts |  62 ++--
 javascript/packages/fury/lib/type.ts              |   5 -
 javascript/packages/fury/lib/writer/index.ts      | 354 ++++++++++------------
 javascript/test/io.test.ts                        |  84 ++---
 javascript/test/reader.test.ts                    |   4 +-
 javascript/test/referenceResolve.test.ts          |  10 +-
 javascript/test/util.js                           |   6 +-
 javascript/test/writer.test.ts                    |   4 +-
 12 files changed, 400 insertions(+), 453 deletions(-)

diff --git a/javascript/packages/fury/lib/classResolver.ts 
b/javascript/packages/fury/lib/classResolver.ts
index 2156640b..c2d0f4c4 100644
--- a/javascript/packages/fury/lib/classResolver.ts
+++ b/javascript/packages/fury/lib/classResolver.ts
@@ -17,13 +17,14 @@
  * under the License.
  */
 
-import { InternalSerializerType, Serializer, BinaryReader, BinaryWriter as 
TBinaryWriter } from "./type";
+import { InternalSerializerType, Serializer } from "./type";
 import { fromString } from "./platformBuffer";
 import { x64hash128 } from "./murmurHash3";
 import { BinaryWriter } from "./writer";
 import { generateSerializer } from "./gen";
 import { Type, TypeDescription } from "./description";
 import Fury from "./fury";
+import { BinaryReader } from "./reader";
 
 const USESTRINGVALUE = 0;
 const USESTRINGID = 1;
@@ -157,7 +158,7 @@ export default class SerializerResolver {
   static tagBuffer(tag: string) {
     const tagBuffer = fromString(tag);
     const bufferLen = tagBuffer.byteLength;
-    const writer = BinaryWriter({});
+    const writer = new BinaryWriter({});
 
     let tagHash = x64hash128(tagBuffer, 47).getBigUint64(0);
     if (tagHash === 0n) {
@@ -177,7 +178,7 @@ export default class SerializerResolver {
     const fullBuffer = SerializerResolver.tagBuffer(tag);
 
     return {
-      write: (binaryWriter: TBinaryWriter) => {
+      write: (binaryWriter: BinaryWriter) => {
         const tagIndex = this.writeStringIndex[idx];
         if (tagIndex > -1) {
           // equivalent of: `uint8(USESTRINGID); int16(tagIndex)`
diff --git a/javascript/packages/fury/lib/fury.ts 
b/javascript/packages/fury/lib/fury.ts
index aa53e13a..2889ecc3 100644
--- a/javascript/packages/fury/lib/fury.ts
+++ b/javascript/packages/fury/lib/fury.ts
@@ -21,16 +21,16 @@ import ClassResolver from "./classResolver";
 import { BinaryWriter } from "./writer";
 import { BinaryReader } from "./reader";
 import { ReferenceResolver } from "./referenceResolver";
-import { ConfigFlags, Serializer, Config, Language, BinaryReader as 
BinaryReaderType, BinaryWriter as BinaryWriterType } from "./type";
+import { ConfigFlags, Serializer, Config, Language } from "./type";
 import { OwnershipError } from "./error";
 import { InputType, ResultType, TypeDescription } from "./description";
 import { generateSerializer, AnySerializer } from "./gen";
 
 export default class {
-  binaryReader: BinaryReaderType;
-  binaryWriter: BinaryWriterType;
+  binaryReader: BinaryReader;
+  binaryWriter: BinaryWriter;
   classResolver = new ClassResolver();
-  referenceResolver: ReturnType<typeof ReferenceResolver>;
+  referenceResolver: ReferenceResolver;
   anySerializer: AnySerializer;
 
   constructor(public config: Config = {
@@ -39,9 +39,9 @@ export default class {
     hooks: {
     },
   }) {
-    this.binaryReader = BinaryReader(config);
-    this.binaryWriter = BinaryWriter(config);
-    this.referenceResolver = ReferenceResolver(config, this.binaryWriter, 
this.binaryReader);
+    this.binaryReader = new BinaryReader(config);
+    this.binaryWriter = new BinaryWriter(config);
+    this.referenceResolver = new ReferenceResolver(this.binaryReader);
     this.classResolver.init(this);
     this.anySerializer = new AnySerializer(this);
   }
diff --git a/javascript/packages/fury/lib/gen/serializer.ts 
b/javascript/packages/fury/lib/gen/serializer.ts
index 6439aaad..3d8a067b 100644
--- a/javascript/packages/fury/lib/gen/serializer.ts
+++ b/javascript/packages/fury/lib/gen/serializer.ts
@@ -19,12 +19,15 @@
 
 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";
 
+export const makeHead = (flag: RefFlags, type: InternalSerializerType) => {
+  return (((SerializerResolver.getTypeIdByInternalSerializerType(type) << 16) 
>>> 16) << 8) | ((flag << 24) >>> 24);
+};
+
 export interface SerializerGenerator {
   toSerializer(): string;
   toWriteEmbed(accessor: string, excludeHead?: boolean): string;
diff --git a/javascript/packages/fury/lib/reader/index.ts 
b/javascript/packages/fury/lib/reader/index.ts
index 614eecbf..5731127a 100644
--- a/javascript/packages/fury/lib/reader/index.ts
+++ b/javascript/packages/fury/lib/reader/index.ts
@@ -22,175 +22,185 @@ import { isNodeEnv } from "../util";
 import { PlatformBuffer, alloc, fromUint8Array } from "../platformBuffer";
 import { readLatin1String } from "./string";
 
-export const BinaryReader = (config: Config) => {
-  const sliceStringEnable = isNodeEnv && config.useSliceString;
-  let cursor = 0;
-  let dataView!: DataView;
-  let buffer!: PlatformBuffer;
-  let bigString: string;
-  let byteLength: number;
-
-  const stringLatin1 = sliceStringEnable ? stringLatin1Fast : stringLatin1Slow;
-
-  function reset(ab: Uint8Array) {
-    buffer = fromUint8Array(ab);
-    byteLength = buffer.byteLength;
-    dataView = new DataView(buffer.buffer, buffer.byteOffset, byteLength);
-    if (sliceStringEnable) {
-      bigString = buffer.toString("latin1", 0, byteLength);
+export class BinaryReader {
+  private sliceStringEnable;
+  private cursor = 0;
+  private dataView!: DataView;
+  private platformBuffer!: PlatformBuffer;
+  private bigString = "";
+  private byteLength = 0;
+
+  constructor(config: Config) {
+    this.sliceStringEnable = isNodeEnv && config.useSliceString;
+  }
+
+  reset(ab: Uint8Array) {
+    this.platformBuffer = fromUint8Array(ab);
+    this.byteLength = this.platformBuffer.byteLength;
+    this.dataView = new DataView(this.platformBuffer.buffer, 
this.platformBuffer.byteOffset, this.byteLength);
+    if (this.sliceStringEnable) {
+      this.bigString = this.platformBuffer.toString("latin1", 0, 
this.byteLength);
     }
-    cursor = 0;
+    this.cursor = 0;
   }
 
-  function uint8() {
-    return dataView.getUint8(cursor++);
+  uint8() {
+    return this.dataView.getUint8(this.cursor++);
   }
 
-  function int8() {
-    return dataView.getInt8(cursor++);
+  int8() {
+    return this.dataView.getInt8(this.cursor++);
   }
 
-  function uint16() {
-    const result = dataView.getUint16(cursor, true);
-    cursor += 2;
+  uint16() {
+    const result = this.dataView.getUint16(this.cursor, true);
+    this.cursor += 2;
     return result;
   }
 
-  function int16() {
-    const result = dataView.getInt16(cursor, true);
-    cursor += 2;
+  int16() {
+    const result = this.dataView.getInt16(this.cursor, true);
+    this.cursor += 2;
     return result;
   }
 
-  function skip(len: number) {
-    cursor += len;
+  skip(len: number) {
+    this.cursor += len;
   }
 
-  function int32() {
-    const result = dataView.getInt32(cursor, true);
-    cursor += 4;
+  int32() {
+    const result = this.dataView.getInt32(this.cursor, true);
+    this.cursor += 4;
     return result;
   }
 
-  function uint32() {
-    const result = dataView.getUint32(cursor, true);
-    cursor += 4;
+  uint32() {
+    const result = this.dataView.getUint32(this.cursor, true);
+    this.cursor += 4;
     return result;
   }
 
-  function int64() {
-    const result = dataView.getBigInt64(cursor, true);
-    cursor += 8;
+  int64() {
+    const result = this.dataView.getBigInt64(this.cursor, true);
+    this.cursor += 8;
     return result;
   }
 
-  function uint64() {
-    const result = dataView.getBigUint64(cursor, true);
-    cursor += 8;
+  uint64() {
+    const result = this.dataView.getBigUint64(this.cursor, true);
+    this.cursor += 8;
     return result;
   }
 
-  function sliInt64() {
-    const i = dataView.getUint32(cursor, true);
+  sliInt64() {
+    const i = this.dataView.getUint32(this.cursor, true);
     if ((i & 0b1) != 0b1) {
-      cursor += 4;
+      this.cursor += 4;
       return BigInt(i >> 1);
     }
-    cursor += 1;
-    return varInt64();
+    this.cursor += 1;
+    return this.varInt64();
   }
 
-  function float32() {
-    const result = dataView.getFloat32(cursor, true);
-    cursor += 4;
+  float32() {
+    const result = this.dataView.getFloat32(this.cursor, true);
+    this.cursor += 4;
     return result;
   }
 
-  function float64() {
-    const result = dataView.getFloat64(cursor, true);
-    cursor += 8;
+  float64() {
+    const result = this.dataView.getFloat64(this.cursor, true);
+    this.cursor += 8;
     return result;
   }
 
-  function stringUtf8At(start: number, len: number) {
-    return buffer.toString("utf8", start, start + len);
+  stringUtf8At(start: number, len: number) {
+    return this.platformBuffer.toString("utf8", start, start + len);
   }
 
-  function stringUtf8(len: number) {
-    const result = buffer.toString("utf8", cursor, cursor + len);
-    cursor += len;
+  stringUtf8(len: number) {
+    const result = this.platformBuffer.toString("utf8", this.cursor, 
this.cursor + len);
+    this.cursor += len;
     return result;
   }
 
-  function stringOfVarUInt32() {
-    const isLatin1 = uint8() === LATIN1;
-    const len = varUInt32();
-    return isLatin1 ? stringLatin1(len) : stringUtf8(len);
+  stringOfVarUInt32() {
+    const isLatin1 = this.uint8() === LATIN1;
+    const len = this.varUInt32();
+    return isLatin1 ? this.stringLatin1(len) : this.stringUtf8(len);
+  }
+
+  stringLatin1(len: number) {
+    if (this.sliceStringEnable) {
+      return this.stringLatin1Fast(len);
+    }
+    return this.stringLatin1Slow(len);
   }
 
-  function stringLatin1Fast(len: number) {
-    const result = bigString.substring(cursor, cursor + len);
-    cursor += len;
+  private stringLatin1Fast(len: number) {
+    const result = this.bigString.substring(this.cursor, this.cursor + len);
+    this.cursor += len;
     return result;
   }
 
-  function stringLatin1Slow(len: number) {
-    const rawCursor = cursor;
-    cursor += len;
-    return readLatin1String(buffer, len, rawCursor);
+  private stringLatin1Slow(len: number) {
+    const rawCursor = this.cursor;
+    this.cursor += len;
+    return readLatin1String(this.platformBuffer, len, rawCursor);
   }
 
-  function binary(len: number) {
+  buffer(len: number) {
     const result = alloc(len);
-    buffer.copy(result, 0, cursor, cursor + len);
-    cursor += len;
+    this.platformBuffer.copy(result, 0, this.cursor, this.cursor + len);
+    this.cursor += len;
     return result;
   }
 
-  function bufferRef(len: number) {
-    const result = buffer.subarray(cursor, cursor + len);
-    cursor += len;
+  bufferRef(len: number) {
+    const result = this.platformBuffer.subarray(this.cursor, this.cursor + 
len);
+    this.cursor += len;
     return result;
   }
 
-  function varUInt32() {
+  varUInt32() {
     // Reduce memory reads as much as possible. Reading a uint32 at once is 
far faster than reading four uint8s separately.
-    if (byteLength - cursor >= 5) {
-      const u32 = dataView.getUint32(cursor++, true);
-      let result = u32 & 0x7f;
-      if ((u32 & 0x80) != 0) {
-        cursor++;
-        const b2 = u32 >> 8;
-        result |= (b2 & 0x7f) << 7;
-        if ((b2 & 0x80) != 0) {
-          cursor++;
-          const b3 = u32 >> 16;
-          result |= (b3 & 0x7f) << 14;
-          if ((b3 & 0x80) != 0) {
-            cursor++;
-            const b4 = u32 >> 24;
-            result |= (b4 & 0x7f) << 21;
-            if ((b4 & 0x80) != 0) {
-              result |= (uint8()) << 28;
+    if (this.byteLength - this.cursor >= 5) {
+      const fourByteValue = this.dataView.getUint32(this.cursor++, true);
+      // | 1bit + 7bits | 1bit + 7bits | 1bit + 7bits | 1bit + 7bits |
+      let result = fourByteValue & 0x7f;
+      if ((fourByteValue & 0x80) != 0) {
+        this.cursor++;
+        // 0x3f80: 0b1111111 << 7
+        result |= (fourByteValue >>> 1) & 0x3f80;
+        if ((fourByteValue & 0x8000) != 0) {
+          this.cursor++;
+          // 0x1fc000: 0b1111111 << 14
+          result |= (fourByteValue >>> 2) & 0x1fc000;
+          if ((fourByteValue & 0x800000) != 0) {
+            this.cursor++;
+            // 0xfe00000: 0b1111111 << 21
+            result |= (fourByteValue >>> 3) & 0xfe00000;
+            if ((fourByteValue & 0x80000000) != 0) {
+              result |= (this.uint8()) << 28;
             }
           }
         }
       }
       return result;
     }
-    let byte = uint8();
+    let byte = this.uint8();
     let result = byte & 0x7f;
     if ((byte & 0x80) != 0) {
-      byte = uint8();
+      byte = this.uint8();
       result |= (byte & 0x7f) << 7;
       if ((byte & 0x80) != 0) {
-        byte = uint8();
+        byte = this.uint8();
         result |= (byte & 0x7f) << 14;
         if ((byte & 0x80) != 0) {
-          byte = uint8();
+          byte = this.uint8();
           result |= (byte & 0x7f) << 21;
           if ((byte & 0x80) != 0) {
-            byte = uint8();
+            byte = this.uint8();
             result |= (byte) << 28;
           }
         }
@@ -199,43 +209,43 @@ export const BinaryReader = (config: Config) => {
     return result;
   }
 
-  function varInt32() {
-    const v = varUInt32();
+  varInt32() {
+    const v = this.varUInt32();
     return (v >> 1) ^ -(v & 1); // zigZag decode
   }
 
-  function bigUInt8() {
-    return BigInt(uint8() >>> 0);
+  bigUInt8() {
+    return BigInt(this.uint8() >>> 0);
   }
 
-  function varUInt64() {
+  varUInt64() {
     // Creating BigInts is too performance-intensive; we'll use uint32 instead.
-    if (byteLength - cursor < 8) {
-      let byte = bigUInt8();
+    if (this.byteLength - this.cursor < 8) {
+      let byte = this.bigUInt8();
       let result = byte & 0x7fn;
       if ((byte & 0x80n) != 0n) {
-        byte = bigUInt8();
+        byte = this.bigUInt8();
         result |= (byte & 0x7fn) << 7n;
         if ((byte & 0x80n) != 0n) {
-          byte = bigUInt8();
+          byte = this.bigUInt8();
           result |= (byte & 0x7fn) << 14n;
           if ((byte & 0x80n) != 0n) {
-            byte = bigUInt8();
+            byte = this.bigUInt8();
             result |= (byte & 0x7fn) << 21n;
             if ((byte & 0x80n) != 0n) {
-              byte = bigUInt8();
+              byte = this.bigUInt8();
               result |= (byte & 0x7fn) << 28n;
               if ((byte & 0x80n) != 0n) {
-                byte = bigUInt8();
+                byte = this.bigUInt8();
                 result |= (byte & 0x7fn) << 35n;
                 if ((byte & 0x80n) != 0n) {
-                  byte = bigUInt8();
+                  byte = this.bigUInt8();
                   result |= (byte & 0x7fn) << 42n;
                   if ((byte & 0x80n) != 0n) {
-                    byte = bigUInt8();
+                    byte = this.bigUInt8();
                     result |= (byte & 0x7fn) << 49n;
                     if ((byte & 0x80n) != 0n) {
-                      byte = bigUInt8();
+                      byte = this.bigUInt8();
                       result |= (byte) << 56n;
                     }
                   }
@@ -247,40 +257,40 @@ export const BinaryReader = (config: Config) => {
       }
       return result;
     }
-    const l32 = dataView.getUint32(cursor++, true);
+    const l32 = this.dataView.getUint32(this.cursor++, true);
     let byte = l32 & 0xff;
     let rl28 = byte & 0x7f;
     let rh28 = 0;
     if ((byte & 0x80) != 0) {
       byte = l32 & 0xff00 >> 8;
-      cursor++;
+      this.cursor++;
       rl28 |= (byte & 0x7f) << 7;
       if ((byte & 0x80) != 0) {
         byte = l32 & 0xff0000 >> 16;
-        cursor++;
+        this.cursor++;
         rl28 |= (byte & 0x7f) << 14;
         if ((byte & 0x80) != 0) {
           byte = l32 & 0xff000000 >> 24;
-          cursor++;
+          this.cursor++;
           rl28 |= (byte & 0x7f) << 21;
           if ((byte & 0x80) != 0) {
-            const h32 = dataView.getUint32(cursor++, true);
+            const h32 = this.dataView.getUint32(this.cursor++, true);
             byte = h32 & 0xff;
             rh28 |= (byte & 0x7f);
             if ((byte & 0x80) != 0) {
               byte = h32 & 0xff00 >> 8;
-              cursor++;
+              this.cursor++;
               rh28 |= (byte & 0x7f) << 7;
               if ((byte & 0x80) != 0) {
                 byte = h32 & 0xff0000 >> 16;
-                cursor++;
+                this.cursor++;
                 rh28 |= (byte & 0x7f) << 14;
                 if ((byte & 0x80) != 0) {
                   byte = h32 & 0xff000000 >> 24;
-                  cursor++;
+                  this.cursor++;
                   rh28 |= (byte & 0x7f) << 21;
                   if ((byte & 0x80) != 0) {
-                    return (BigInt(uint8()) << 56n) | BigInt(rh28) << 28n | 
BigInt(rl28);
+                    return (BigInt(this.uint8()) << 56n) | BigInt(rh28) << 28n 
| BigInt(rl28);
                   }
                 }
               }
@@ -293,13 +303,13 @@ export const BinaryReader = (config: Config) => {
     return BigInt(rh28) << 28n | BigInt(rl28);
   }
 
-  function varInt64() {
-    const v = varUInt64();
+  varInt64() {
+    const v = this.varUInt64();
     return (v >> 1n) ^ -(v & 1n); // zigZag decode
   }
 
-  function float16() {
-    const asUint16 = uint16();
+  float16() {
+    const asUint16 = this.uint16();
     const sign = asUint16 >> 15;
     const exponent = (asUint16 >> 10) & 0x1F;
     const mantissa = asUint16 & 0x3FF;
@@ -326,32 +336,12 @@ export const BinaryReader = (config: Config) => {
       return (sign === 0 ? 1 : -1) * (1 + mantissa * 2 ** -10) * 2 ** 
(exponent - 15);
     }
   }
-  return {
-    getCursor: () => cursor,
-    setCursor: (v: number) => (cursor = v),
-    varInt32,
-    varInt64,
-    varUInt32,
-    varUInt64,
-    int8,
-    buffer: binary,
-    bufferRef,
-    uint8,
-    reset,
-    stringUtf8At,
-    stringUtf8,
-    stringLatin1,
-    stringOfVarUInt32,
-    float64,
-    float32,
-    uint16,
-    int16,
-    uint64,
-    skip,
-    int64,
-    sliInt64,
-    uint32,
-    int32,
-    float16,
-  };
-};
+
+  getCursor() {
+    return this.cursor;
+  }
+
+  setCursor(v: number) {
+    this.cursor = v;
+  }
+}
diff --git a/javascript/packages/fury/lib/referenceResolver.ts 
b/javascript/packages/fury/lib/referenceResolver.ts
index cc27706f..b14f4b7a 100644
--- a/javascript/packages/fury/lib/referenceResolver.ts
+++ b/javascript/packages/fury/lib/referenceResolver.ts
@@ -19,61 +19,43 @@
 
 import {
   RefFlags,
-  BinaryReader,
-  BinaryWriter,
-  InternalSerializerType,
 } from "./type";
-import SerializerResolver from "./classResolver";
+import { BinaryReader } from "./reader";
 
-export const makeHead = (flag: RefFlags, type: InternalSerializerType) => {
-  return (((SerializerResolver.getTypeIdByInternalSerializerType(type) << 16) 
>>> 16) << 8) | ((flag << 24) >>> 24);
-};
+export class ReferenceResolver {
+  private readObjects: any[] = [];
+  private writeObjects: any[] = [];
 
-export const ReferenceResolver = (
-  config: {
-    refTracking?: boolean;
-  },
-  binaryWriter: BinaryWriter,
-  binaryReader: BinaryReader,
-) => {
-  let readObjects: any[] = [];
-  let writeObjects: any[] = [];
+  constructor(private binaryReader: BinaryReader) {
 
-  function reset() {
-    readObjects = [];
-    writeObjects = [];
   }
 
-  function getReadObject(refId: number) {
-    return readObjects[refId];
+  reset() {
+    this.readObjects = [];
+    this.writeObjects = [];
   }
 
-  function readRefFlag() {
-    return binaryReader.int8() as RefFlags;
+  getReadObject(refId: number) {
+    return this.readObjects[refId];
   }
 
-  function reference(object: any) {
-    readObjects.push(object);
+  readRefFlag() {
+    return this.binaryReader.int8() as RefFlags;
   }
 
-  function writeRef(object: any) {
-    writeObjects.push(object);
+  reference(object: any) {
+    this.readObjects.push(object);
   }
 
-  function existsWriteObject(obj: any) {
-    for (let index = 0; index < writeObjects.length; index++) {
-      if (writeObjects[index] === obj) {
+  writeRef(object: any) {
+    this.writeObjects.push(object);
+  }
+
+  existsWriteObject(obj: any) {
+    for (let index = 0; index < this.writeObjects.length; index++) {
+      if (this.writeObjects[index] === obj) {
         return index;
       }
     }
   }
-
-  return {
-    existsWriteObject,
-    writeRef,
-    reference,
-    readRefFlag,
-    getReadObject,
-    reset,
-  };
-};
+}
diff --git a/javascript/packages/fury/lib/type.ts 
b/javascript/packages/fury/lib/type.ts
index 88881e6e..9d044de7 100644
--- a/javascript/packages/fury/lib/type.ts
+++ b/javascript/packages/fury/lib/type.ts
@@ -17,13 +17,8 @@
  * under the License.
  */
 
-import type { BinaryWriter } from "./writer";
-import type { BinaryReader } from "./reader";
 import { Meta } from "./meta";
 
-export type BinaryWriter = ReturnType<typeof BinaryWriter>;
-export type BinaryReader = ReturnType<typeof BinaryReader>;
-
 export enum InternalSerializerType {
   // primitive type
   BOOL,
diff --git a/javascript/packages/fury/lib/writer/index.ts 
b/javascript/packages/fury/lib/writer/index.ts
index 651e2d8c..5f21afd9 100644
--- a/javascript/packages/fury/lib/writer/index.ts
+++ b/javascript/packages/fury/lib/writer/index.ts
@@ -24,86 +24,92 @@ import { toFloat16 } from "./number";
 
 const MAX_POOL_SIZE = 1024 * 1024 * 3; // 3MB
 
-export const BinaryWriter = (config: Config) => {
-  let cursor = 0;
-  let byteLength: number;
-  let arrayBuffer: PlatformBuffer;
-  let dataView: DataView;
-  let reserved = 0;
-  let locked = false;
-
-  function initPoll() {
-    byteLength = 1024 * 100;
-    arrayBuffer = alloc(byteLength);
-    dataView = new DataView(arrayBuffer.buffer, arrayBuffer.byteOffset);
-  }
-
-  initPoll();
-
-  function reserve(len: number) {
-    reserved += len;
-    if (byteLength - cursor <= reserved) {
-      const newAb = alloc(byteLength * 2 + len);
-      arrayBuffer.copy(newAb, 0);
-      arrayBuffer = newAb;
-      byteLength = arrayBuffer.byteLength;
-      dataView = new DataView(arrayBuffer.buffer, arrayBuffer.byteOffset);
+export class BinaryWriter {
+  private cursor = 0;
+  private byteLength = 0;
+  private platformBuffer!: PlatformBuffer;
+  private dataView!: DataView;
+  private reserved = 0;
+  private locked = false;
+  private config: Config;
+  private hpsEnable = false;
+
+  constructor(config: Config) {
+    this.initPoll();
+    this.config = config;
+    this.hpsEnable = Boolean(config?.hps);
+  }
+
+  private initPoll() {
+    this.byteLength = 1024 * 100;
+    this.platformBuffer = alloc(this.byteLength);
+    this.dataView = new DataView(this.platformBuffer.buffer, 
this.platformBuffer.byteOffset);
+  }
+
+  reserve(len: number) {
+    this.reserved += len;
+    if (this.byteLength - this.cursor <= this.reserved) {
+      const newAb = alloc(this.byteLength * 2 + len);
+      this.platformBuffer.copy(newAb, 0);
+      this.platformBuffer = newAb;
+      this.byteLength = this.platformBuffer.byteLength;
+      this.dataView = new DataView(this.platformBuffer.buffer, 
this.platformBuffer.byteOffset);
     }
   }
 
-  function reset() {
-    if (locked) {
+  reset() {
+    if (this.locked) {
       throw new OwnershipError("Ownership of writer was held by dumpAndOwn, 
but not released");
     }
-    cursor = 0;
-    reserved = 0;
+    this.cursor = 0;
+    this.reserved = 0;
   }
 
-  function uint8(v: number) {
-    dataView.setUint8(cursor, v);
-    cursor++;
+  uint8(v: number) {
+    this.dataView.setUint8(this.cursor, v);
+    this.cursor++;
   }
 
-  function int8(v: number) {
-    dataView.setInt8(cursor, v);
-    cursor++;
+  int8(v: number) {
+    this.dataView.setInt8(this.cursor, v);
+    this.cursor++;
   }
 
-  function int24(v: number) {
-    dataView.setUint32(cursor, v, true);
-    cursor += 3;
+  int24(v: number) {
+    this.dataView.setUint32(this.cursor, v, true);
+    this.cursor += 3;
   }
 
-  function uint16(v: number) {
-    dataView.setUint16(cursor, v, true);
-    cursor += 2;
+  uint16(v: number) {
+    this.dataView.setUint16(this.cursor, v, true);
+    this.cursor += 2;
   }
 
-  function int16(v: number) {
-    dataView.setInt16(cursor, v, true);
-    cursor += 2;
+  int16(v: number) {
+    this.dataView.setInt16(this.cursor, v, true);
+    this.cursor += 2;
   }
 
-  function skip(len: number) {
-    cursor += len;
+  skip(len: number) {
+    this.cursor += len;
   }
 
-  function int32(v: number) {
-    dataView.setInt32(cursor, v, true);
-    cursor += 4;
+  int32(v: number) {
+    this.dataView.setInt32(this.cursor, v, true);
+    this.cursor += 4;
   }
 
-  function uint32(v: number) {
-    dataView.setUint32(cursor, v, true);
-    cursor += 4;
+  uint32(v: number) {
+    this.dataView.setUint32(this.cursor, v, true);
+    this.cursor += 4;
   }
 
-  function int64(v: bigint) {
-    dataView.setBigInt64(cursor, v, true);
-    cursor += 8;
+  int64(v: bigint) {
+    this.dataView.setBigInt64(this.cursor, v, true);
+    this.cursor += 8;
   }
 
-  function sliInt64(v: bigint | number) {
+  sliInt64(v: bigint | number) {
     if (v <= HalfMaxInt32 && v >= HalfMinInt32) {
       // write:
       // 00xxx -> 0xxx
@@ -111,43 +117,43 @@ export const BinaryWriter = (config: Config) => {
       // read:
       // 0xxx -> 00xxx
       // 1xxx -> 11xxx
-      dataView.setUint32(cursor, Number(v) << 1, true);
-      cursor += 4;
+      this.dataView.setUint32(this.cursor, Number(v) << 1, true);
+      this.cursor += 4;
     } else {
       const BIG_LONG_FLAG = 0b1; // bit 0 set, means big long.
-      dataView.setUint8(cursor, BIG_LONG_FLAG);
-      cursor += 1;
-      varInt64(BigInt(v));
+      this.dataView.setUint8(this.cursor, BIG_LONG_FLAG);
+      this.cursor += 1;
+      this.varInt64(BigInt(v));
     }
   }
 
-  function float32(v: number) {
-    dataView.setFloat32(cursor, v, true);
-    cursor += 4;
+  float32(v: number) {
+    this.dataView.setFloat32(this.cursor, v, true);
+    this.cursor += 4;
   }
 
-  function float64(v: number) {
-    dataView.setFloat64(cursor, v, true);
-    cursor += 8;
+  float64(v: number) {
+    this.dataView.setFloat64(this.cursor, v, true);
+    this.cursor += 8;
   }
 
-  function buffer(v: Uint8Array) {
-    reserve(v.byteLength);
-    arrayBuffer.set(v, cursor);
-    cursor += v.byteLength;
+  buffer(v: Uint8Array) {
+    this.reserve(v.byteLength);
+    this.platformBuffer.set(v, this.cursor);
+    this.cursor += v.byteLength;
   }
 
-  function uint64(v: bigint) {
-    dataView.setBigUint64(cursor, v, true);
-    cursor += 8;
+  uint64(v: bigint) {
+    this.dataView.setBigUint64(this.cursor, v, true);
+    this.cursor += 8;
   }
 
-  function bufferWithoutMemCheck(bf: PlatformBuffer, byteLen: number) {
-    bf.copy(arrayBuffer, cursor);
-    cursor += byteLen;
+  bufferWithoutMemCheck(bf: PlatformBuffer, byteLen: number) {
+    bf.copy(this.platformBuffer, this.cursor);
+    this.cursor += byteLen;
   }
 
-  function fastWriteStringUtf8(string: string, buffer: Uint8Array, offset: 
number) {
+  fastWriteStringUtf8(string: string, buffer: Uint8Array, offset: number) {
     let c1: number;
     let c2: number;
     for (let i = 0; i < string.length; ++i) {
@@ -157,7 +163,7 @@ export const BinaryWriter = (config: Config) => {
       } else if (c1 < 2048) {
         const u1 = (c1 >> 6) | 192;
         const u2 = (c1 & 63) | 128;
-        dataView.setUint16(offset, (u1 << 8) | u2);
+        this.dataView.setUint16(offset, (u1 << 8) | u2);
         offset += 2;
       } else if (
         (c1 & 0xfc00) === 0xd800
@@ -169,190 +175,160 @@ export const BinaryWriter = (config: Config) => {
         const u2 = ((c1 >> 12) & 63) | 128;
         const u3 = ((c1 >> 6) & 63) | 128;
         const u4 = (c1 & 63) | 128;
-        dataView.setUint32(offset, (u1 << 24) | (u2 << 16) | (u3 << 8) | u4);
+        this.dataView.setUint32(offset, (u1 << 24) | (u2 << 16) | (u3 << 8) | 
u4);
         offset += 4;
       } else {
         const u1 = (c1 >> 12) | 224;
         const u2 = ((c1 >> 6) & 63) | 128;
-        dataView.setUint16(offset, (u1 << 8) | u2);
+        this.dataView.setUint16(offset, (u1 << 8) | u2);
         offset += 2;
         buffer[offset++] = (c1 & 63) | 128;
       }
     }
   }
 
-  function stringOfVarUInt32Fast() {
-    const { isLatin1: detectIsLatin1, stringCopy } = config!.hps!;
-    return function (v: string) {
-      const isLatin1 = detectIsLatin1(v);
-      const len = isLatin1 ? v.length : strByteLength(v);
-      dataView.setUint8(cursor++, isLatin1 ? LATIN1 : UTF8);
-      varUInt32(len);
-      reserve(len);
-      if (isLatin1) {
-        stringCopy(v, arrayBuffer, cursor);
+  stringOfVarUInt32Fast(v: string) {
+    const { isLatin1: detectIsLatin1, stringCopy } = this.config.hps!;
+    const isLatin1 = detectIsLatin1(v);
+    const len = isLatin1 ? v.length : strByteLength(v);
+    this.dataView.setUint8(this.cursor++, isLatin1 ? LATIN1 : UTF8);
+    this.varUInt32(len);
+    this.reserve(len);
+    if (isLatin1) {
+      stringCopy(v, this.platformBuffer, this.cursor);
+    } else {
+      if (len < 40) {
+        this.fastWriteStringUtf8(v, this.platformBuffer, this.cursor);
       } else {
-        if (len < 40) {
-          fastWriteStringUtf8(v, arrayBuffer, cursor);
-        } else {
-          arrayBuffer.write(v, cursor, "utf8");
-        }
+        this.platformBuffer.write(v, this.cursor, "utf8");
       }
-      cursor += len;
-    };
+    }
+    this.cursor += len;
   }
 
-  function stringOfVarUInt32Slow(v: string) {
+  stringOfVarUInt32Slow(v: string) {
     const len = strByteLength(v);
     const isLatin1 = len === v.length;
-    dataView.setUint8(cursor++, isLatin1 ? LATIN1 : UTF8);
-    varUInt32(len);
-    reserve(len);
+    this.dataView.setUint8(this.cursor++, isLatin1 ? LATIN1 : UTF8);
+    this.varUInt32(len);
+    this.reserve(len);
     if (isLatin1) {
       if (len < 40) {
         for (let index = 0; index < v.length; index++) {
-          arrayBuffer[cursor + index] = v.charCodeAt(index);
+          this.platformBuffer[this.cursor + index] = v.charCodeAt(index);
         }
       } else {
-        arrayBuffer.write(v, cursor, "latin1");
+        this.platformBuffer.write(v, this.cursor, "latin1");
       }
     } else {
       if (len < 40) {
-        fastWriteStringUtf8(v, arrayBuffer, cursor);
+        this.fastWriteStringUtf8(v, this.platformBuffer, this.cursor);
       } else {
-        arrayBuffer.write(v, cursor, "utf8");
+        this.platformBuffer.write(v, this.cursor, "utf8");
       }
     }
-    cursor += len;
+    this.cursor += len;
   }
 
-  function varInt32(v: number) {
-    return varUInt32((v << 1) ^ (v >> 31));
+  varInt32(v: number) {
+    return this.varUInt32((v << 1) ^ (v >> 31));
   }
 
-  function varUInt32(value: number) {
+  varUInt32(value: number) {
     value = (value >>> 0) & 0xFFFFFFFF; // keep only the lower 32 bits
 
     if (value >> 7 == 0) {
-      arrayBuffer[cursor++] = value;
+      this.platformBuffer[this.cursor++] = value;
       return;
     }
-    const rawCursor = cursor;
+    const rawCursor = this.cursor;
     let u32 = 0;
     if (value >> 14 == 0) {
       u32 = ((value & 0x7f | 0x80) << 24) | ((value >> 7) << 16);
-      cursor += 2;
+      this.cursor += 2;
     } else if (value >> 21 == 0) {
       u32 = ((value & 0x7f | 0x80) << 24) | ((value >> 7 & 0x7f | 0x80) << 16) 
| ((value >> 14) << 8);
-      cursor += 3;
+      this.cursor += 3;
     } else if (value >> 28 == 0) {
       u32 = ((value & 0x7f | 0x80) << 24) | ((value >> 7 & 0x7f | 0x80) << 16) 
| ((value >> 14 & 0x7f | 0x80) << 8) | (value >> 21);
-      cursor += 4;
+      this.cursor += 4;
     } else {
       u32 = ((value & 0x7f | 0x80) << 24) | ((value >> 7 & 0x7f | 0x80) << 16) 
| ((value >> 14 & 0x7f | 0x80) << 8) | (value >> 21 & 0x7f | 0x80);
-      arrayBuffer[rawCursor + 4] = value >> 28;
-      cursor += 5;
+      this.platformBuffer[rawCursor + 4] = value >> 28;
+      this.cursor += 5;
     }
-    dataView.setUint32(rawCursor, u32);
+    this.dataView.setUint32(rawCursor, u32);
   }
 
-  function varInt64(v: bigint) {
+  varInt64(v: bigint) {
     if (typeof v !== "bigint") {
       v = BigInt(v);
     }
-    return varUInt64((v << 1n) ^ (v >> 63n));
+    return this.varUInt64((v << 1n) ^ (v >> 63n));
   }
 
-  function varUInt64(val: bigint | number) {
+  varUInt64(val: bigint | number) {
     if (typeof val !== "bigint") {
       val = BigInt(val);
     }
     val = val & 0xFFFFFFFFFFFFFFFFn; // keep only the lower 64 bits
 
     while (val > 127) {
-      arrayBuffer[cursor++] = Number(val & 127n | 128n);
+      this.platformBuffer[this.cursor++] = Number(val & 127n | 128n);
       val >>= 7n;
     }
-    arrayBuffer[cursor++] = Number(val);
+    this.platformBuffer[this.cursor++] = Number(val);
     return;
   }
 
-  function tryFreePool() {
-    if (byteLength > MAX_POOL_SIZE) {
-      initPoll();
+  tryFreePool() {
+    if (this.byteLength > MAX_POOL_SIZE) {
+      this.initPoll();
     }
   }
 
-  function dump() {
-    const result = alloc(cursor);
-    arrayBuffer.copy(result, 0, 0, cursor);
-    tryFreePool();
+  dump() {
+    const result = alloc(this.cursor);
+    this.platformBuffer.copy(result, 0, 0, this.cursor);
+    this.tryFreePool();
     return result;
   }
 
-  function dumpAndOwn() {
-    locked = true;
+  dumpAndOwn() {
+    this.locked = true;
     return {
-      get() {
-        return arrayBuffer.subarray(0, cursor);
+      get: () => {
+        return this.platformBuffer.subarray(0, this.cursor);
       },
-      dispose() {
-        locked = false;
+      dispose: () => {
+        this.locked = false;
       },
     };
   }
 
-  function float16(value: number) {
-    uint16(toFloat16(value));
-  }
-
-  function getCursor() {
-    return cursor;
-  }
-
-  function setUint32Position(offset: number, v: number) {
-    dataView.setUint32(offset, v, true);
-  }
-
-  function getByteLen() {
-    return byteLength;
-  }
-
-  function getReserved() {
-    return reserved;
-  }
-
-  return {
-    skip,
-    getByteLen,
-    getReserved,
-    reset,
-    reserve,
-    uint16,
-    int8,
-    int24,
-    dump,
-    uint8,
-    int16,
-    varInt32,
-    varUInt32,
-    varUInt64,
-    varInt64,
-    stringOfVarUInt32: config && config.hps
-      ? stringOfVarUInt32Fast()
-      : stringOfVarUInt32Slow,
-    bufferWithoutMemCheck,
-    uint64,
-    buffer,
-    float16,
-    float64,
-    float32,
-    int64,
-    sliInt64,
-    uint32,
-    int32,
-    getCursor,
-    setUint32Position,
-    dumpAndOwn,
-  };
-};
+  float16(value: number) {
+    this.uint16(toFloat16(value));
+  }
+
+  getCursor() {
+    return this.cursor;
+  }
+
+  setUint32Position(offset: number, v: number) {
+    this.dataView.setUint32(offset, v, true);
+  }
+
+  getByteLen() {
+    return this.byteLength;
+  }
+
+  getReserved() {
+    return this.reserved;
+  }
+
+  stringOfVarUInt32(v: string) {
+    return this.hpsEnable
+      ? this.stringOfVarUInt32Fast(v)
+      : this.stringOfVarUInt32Slow(v);
+  }
+}
diff --git a/javascript/test/io.test.ts b/javascript/test/io.test.ts
index 3e6e29e3..84da04ab 100644
--- a/javascript/test/io.test.ts
+++ b/javascript/test/io.test.ts
@@ -35,7 +35,7 @@ function num2Bin(num: number) {
 ].forEach((config: Config) => {
     describe('writer', () => {
         test('should unsigned int work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             let len = 0;
             [
                 8,
@@ -86,7 +86,7 @@ function num2Bin(num: number) {
         });
 
         test('should int work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             let len = 0;
             [
                 8,
@@ -142,7 +142,7 @@ function num2Bin(num: number) {
                 4,
             ].forEach(x => {
                 {
-                    const writer = BinaryWriter(config);
+                    const writer = new BinaryWriter(config);
                     const value = (2 ** (x * 7)) - 1;
                     writer.varUInt32(value);
                     const ab = writer.dump();
@@ -151,13 +151,13 @@ function num2Bin(num: number) {
                         expect(num2Bin(ab[index])).toBe('11111111');
                     }
                     expect(num2Bin(ab[ab.byteLength - 1])).toBe('1111111');
-                    const reader = BinaryReader(config);
+                    const reader = new BinaryReader(config);
                     reader.reset(ab);
                     const vari32 = reader.varUInt32();
                     expect(vari32).toBe(value);
                 }
                 {
-                    const writer = BinaryWriter(config);
+                    const writer = new BinaryWriter(config);
                     const value = (2 ** (x * 7));
                     writer.varUInt32(value);
                     const ab = writer.dump();
@@ -166,7 +166,7 @@ function num2Bin(num: number) {
                         expect(num2Bin(ab[index])).toBe('10000000');
                     }
                     expect(num2Bin(ab[ab.byteLength - 1])).toBe('1');
-                    const reader = BinaryReader(config);
+                    const reader = new BinaryReader(config);
                     reader.reset(ab);
                     const vari32 = reader.varUInt32();
                     expect(vari32).toBe(value);
@@ -175,23 +175,23 @@ function num2Bin(num: number) {
         });
 
         test('should varInt32 work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             const value = -1;
             writer.varInt32(value);
             const ab = writer.dump();
             expect(ab.byteLength).toBe(1);
             expect(num2Bin(ab[0])).toBe('1');
-            const reader = BinaryReader(config);
+            const reader = new BinaryReader(config);
             reader.reset(ab);
             const vari32 = reader.varInt32();
             expect(vari32).toBe(value);
         });
 
         test('should short latin1 string work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             writer.stringOfVarUInt32("hello world");
             const ab = writer.dump();
-            const reader = BinaryReader(config);
+            const reader = new BinaryReader(config);
             reader.reset(ab);
             expect(reader.uint8()).toBe(0);
             const len = reader.varUInt32();
@@ -201,11 +201,11 @@ function num2Bin(num: number) {
         });
 
         test('should long latin1 string work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             const str = new Array(10).fill('hello world').join('');
             writer.stringOfVarUInt32(str);
             const ab = writer.dump();
-            const reader = BinaryReader(config);
+            const reader = new BinaryReader(config);
             reader.reset(ab);
             expect(reader.uint8()).toBe(0);
             const len = reader.varUInt32();
@@ -214,11 +214,11 @@ function num2Bin(num: number) {
         });
 
         test('should short utf8 string work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             const str = new Array(1).fill('hello 你好 😁').join('');
             writer.stringOfVarUInt32(str);
             const ab = writer.dump();
-            const reader = BinaryReader(config);
+            const reader = new BinaryReader(config);
 
             {
                 reader.reset(ab);
@@ -234,11 +234,11 @@ function num2Bin(num: number) {
         });
 
         test('should long utf8 string work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             const str = new Array(10).fill('hello 你好 😁').join('');
             writer.stringOfVarUInt32(str);
             const ab = writer.dump();
-            const reader = BinaryReader(config);
+            const reader = new BinaryReader(config);
             {
                 reader.reset(ab);
                 expect(reader.uint8()).toBe(1);
@@ -253,10 +253,10 @@ function num2Bin(num: number) {
         });
 
         test('should buffer work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             writer.buffer(new Uint8Array([1, 2, 3, 4, 5]));
             const ab = writer.dump();
-            const reader = BinaryReader(config);
+            const reader = new BinaryReader(config);
             reader.reset(ab);
             expect(ab.byteLength).toBe(5);
             expect(ab[0]).toBe(1);
@@ -267,10 +267,10 @@ function num2Bin(num: number) {
         });
 
         test('should bufferWithoutMemCheck work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             writer.bufferWithoutMemCheck(fromUint8Array(new Uint8Array([1, 2, 
3, 4, 5])), 5);
             const ab = writer.dump();
-            const reader = BinaryReader(config);
+            const reader = new BinaryReader(config);
             reader.reset(ab);
             expect(ab.byteLength).toBe(5);
             expect(ab[0]).toBe(1);
@@ -281,7 +281,7 @@ function num2Bin(num: number) {
         });
 
         test('should setUint32Position work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             writer.skip(10);
             writer.setUint32Position(0, 100);
             writer.setUint32Position(5, 100);
@@ -292,27 +292,27 @@ function num2Bin(num: number) {
         });
 
         test('should float work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             writer.float32(10.01);
             const ab = writer.dump();
             expect(ab.byteLength).toBe(4);
-            const reader = BinaryReader(config);
+            const reader = new BinaryReader(config);
             reader.reset(ab);
             expect(reader.float32().toFixed(2)).toBe((10.01).toFixed(2));
         });
 
         test('should float64 work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             writer.float64(10.01);
             const ab = writer.dump();
             expect(ab.byteLength).toBe(8);
-            const reader = BinaryReader(config);
+            const reader = new BinaryReader(config);
             reader.reset(ab);
             expect(reader.float64().toFixed(2)).toBe((10.01).toFixed(2));
         });
 
         test('should reserve work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             const byteLength = writer.getByteLen();
             const cursor = writer.getCursor();
             const reserved = writer.getReserved();
@@ -324,7 +324,7 @@ function num2Bin(num: number) {
         });
 
         test('should reserve work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             const byteLength = writer.getByteLen();
             const cursor = writer.getCursor();
             const reserved = writer.getReserved();
@@ -336,7 +336,7 @@ function num2Bin(num: number) {
         });
 
         test('should reset work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             writer.int16(100);
             writer.reset();
             expect(writer.getCursor()).toBe(0);
@@ -344,65 +344,65 @@ function num2Bin(num: number) {
         });
 
         test('should int24 work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             writer.int24( (20 << 8)  | 10);
             const ab = writer.dump();
-            const reader = BinaryReader({});
+            const reader = new BinaryReader({});
             reader.reset(ab)
             expect(reader.int8()).toBe(10);
             expect(reader.int16()).toBe(20);
         });
 
         test('should varUInt64 work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             writer.varUInt64(2n ** 2n);
             const ab = writer.dump();
-            const reader = BinaryReader({});
+            const reader = new BinaryReader({});
             reader.reset(ab)
             expect(reader.varUInt64()).toBe(2n ** 2n);
         });
 
         test('should varUInt64 work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             writer.varUInt64(2n ** 63n);
             const ab = writer.dump();
-            const reader = BinaryReader({});
+            const reader = new BinaryReader({});
             reader.reset(ab)
             expect(reader.varUInt64()).toBe(2n ** 63n);
         });
 
         test('should varInt64 work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             writer.varInt64(2n ** 2n);
             const ab = writer.dump();
-            const reader = BinaryReader({});
+            const reader = new BinaryReader({});
             reader.reset(ab)
             expect(reader.varInt64()).toBe(2n ** 2n);
         });
 
         test('should varInt64 work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             writer.varInt64(2n ** 62n);
             const ab = writer.dump();
-            const reader = BinaryReader({});
+            const reader = new BinaryReader({});
             reader.reset(ab)
             expect(reader.varInt64()).toBe(2n ** 62n);
         });
 
         test('should silong work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             writer.sliInt64(2n ** 2n);
             const ab = writer.dump();
-            const reader = BinaryReader({});
+            const reader = new BinaryReader({});
             reader.reset(ab)
             expect(reader.sliInt64()).toBe(2n ** 2n);
         });
 
         test('should silong work', () => {
-            const writer = BinaryWriter(config);
+            const writer = new BinaryWriter(config);
             writer.sliInt64(2n ** 62n);
             const ab = writer.dump();
-            const reader = BinaryReader({});
+            const reader = new BinaryReader({});
             reader.reset(ab)
             expect(reader.sliInt64()).toBe(2n ** 62n);
         });
diff --git a/javascript/test/reader.test.ts b/javascript/test/reader.test.ts
index d753aec3..28008ff1 100644
--- a/javascript/test/reader.test.ts
+++ b/javascript/test/reader.test.ts
@@ -25,7 +25,7 @@ import { describe, expect, test } from '@jest/globals';
 
 describe('writer', () => {
     test('should uint8 work', () => {
-        const writer = BinaryWriter({});
+        const writer = new BinaryWriter({});
         {
             writer.uint8(10);
             var ab = writer.dump();
@@ -55,7 +55,7 @@ describe('reader', () => {
         bb.copy(target, 0, 0, 5);
         expect([...target]).toEqual([ 104, 101, 108, 108, 111 ])
 
-        const reader = BinaryReader({});
+        const reader = new BinaryReader({});
 
         reader.reset(bb);
         const ref = reader.bufferRef(5);
diff --git a/javascript/test/referenceResolve.test.ts 
b/javascript/test/referenceResolve.test.ts
index c4c23035..b8d539a0 100644
--- a/javascript/test/referenceResolve.test.ts
+++ b/javascript/test/referenceResolve.test.ts
@@ -20,11 +20,11 @@
 import { fromUint8Array, alloc, BrowserBuffer } from 
'../packages/fury/lib/platformBuffer';
 import { describe, expect, test } from '@jest/globals';
 import Fury, { TypeDescription, InternalSerializerType } from 
'../packages/fury/index';
-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';
+import { makeHead } from '../packages/fury/lib/gen/serializer';
 
 describe('referenceResolve', () => {
     test('should write head work', () => {
@@ -35,10 +35,10 @@ describe('referenceResolve', () => {
 
     test('should make head work when flag positive', () => {
         const head = makeHead(RefFlags.NotNullValueFlag, 
InternalSerializerType.STRING);
-        const writer = BinaryWriter({});
+        const writer = new BinaryWriter({});
         writer.int24(head);
         const ab = writer.dump();
-        const reader = BinaryReader({});
+        const reader = new BinaryReader({});
         reader.reset(ab);
         expect(reader.int8()).toBe(RefFlags.NotNullValueFlag);
         
expect(reader.int16()).toBe(SerializerResolver.getTypeIdByInternalSerializerType(InternalSerializerType.STRING));
@@ -46,10 +46,10 @@ describe('referenceResolve', () => {
 
     test('should make head work when flag is zero', () => {
         const head = makeHead(RefFlags.RefValueFlag, 
InternalSerializerType.STRING);
-        const writer = BinaryWriter({});
+        const writer = new BinaryWriter({});
         writer.int24(head);
         const ab = writer.dump();
-        const reader = BinaryReader({});
+        const reader = new BinaryReader({});
         reader.reset(ab);
         expect(reader.int8()).toBe(RefFlags.RefValueFlag);
         
expect(reader.int16()).toBe(SerializerResolver.getTypeIdByInternalSerializerType(InternalSerializerType.STRING));
diff --git a/javascript/test/util.js b/javascript/test/util.js
index 3afb9490..12efe989 100644
--- a/javascript/test/util.js
+++ b/javascript/test/util.js
@@ -38,7 +38,7 @@ const mockData2Description = (data, tag) => {
     }
     if (data instanceof Date) {
         return {
-            type: InternalSerializerType.TIMESTAMP,
+            type: InternalSerializerType.DURATION,
             label: 'timestamp'
         }
     }
@@ -50,7 +50,7 @@ const mockData2Description = (data, tag) => {
     }
     if (data instanceof Set) {
         return {
-            type: InternalSerializerType.FURY_SET,
+            type: InternalSerializerType.SET,
             label: "set",
             options: {
                 key: mockData2Description([...data.values()][0], tag),
@@ -87,7 +87,7 @@ const mockData2Description = (data, tag) => {
     }
     if (typeof data === 'object') {
         return {
-            type: InternalSerializerType.FURY_TYPE_TAG,
+            type: InternalSerializerType.OBJECT,
             label: "object",
             options: {
                 props: Object.fromEntries(Object.entries(data).map(([key, 
value]) => {
diff --git a/javascript/test/writer.test.ts b/javascript/test/writer.test.ts
index 71d11c84..6fa348c3 100644
--- a/javascript/test/writer.test.ts
+++ b/javascript/test/writer.test.ts
@@ -23,7 +23,7 @@ import { describe, expect, test } from '@jest/globals';
 
 describe('writer', () => {
     test('should dumpOwn dispose work', () => {
-        const writer = BinaryWriter({});
+        const writer = new BinaryWriter({});
         {
             writer.uint8(256);
             const { get, dispose } = writer.dumpAndOwn();
@@ -46,7 +46,7 @@ describe('writer', () => {
     });
 
     test('should dumpOwn work', () => {
-        const writer = BinaryWriter({});
+        const writer = new BinaryWriter({});
         {
             writer.uint8(256);
             const { get } = writer.dumpAndOwn();


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to