theweipeng commented on code in PR #1505:
URL: https://github.com/apache/incubator-fury/pull/1505#discussion_r1564570879


##########
javascript/packages/fury/lib/reader/index.ts:
##########
@@ -22,175 +22,184 @@ 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);
+    if (this.byteLength - this.cursor >= 5) {

Review Comment:
   Good idea. I will change it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to