Skip to content

Commit

Permalink
[JavaScript] Add more methods for BinaryReader (#1231)
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain authored Dec 15, 2023
1 parent d7cf9d4 commit 308b825
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 27 deletions.
15 changes: 4 additions & 11 deletions javascript/packages/fury/lib/internalSerializer/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,19 @@
* limitations under the License.
*/

import { Fury, LATIN1 } from "../type";
import { Fury } from "../type";
import { InternalSerializerType, RefFlags } from "../type";



export default (fury: Fury) => {
const { binaryReader, binaryWriter, referenceResolver } = fury;
const { stringOfVarInt32: writeStringOfVarInt32, int8 } = binaryWriter
const { varInt32: readVarInt32, stringUtf8: readStringUtf8, uint8: readUInt8, stringLatin1, } = binaryReader;
const { stringOfVarInt32: readStringOfVarInt32 } = binaryReader;

return {
...referenceResolver.deref(fury.config.useLatin1 ? () => {
const type = readUInt8();
const len = readVarInt32();
const result = type === LATIN1 ? stringLatin1(len) : readStringUtf8(len);
return result;
} : () => {
const len = readVarInt32();
const result = readStringUtf8(len);
return result;
...referenceResolver.deref(() => {
return readStringOfVarInt32();
}),
write: referenceResolver.withNotNullableWriter(InternalSerializerType.STRING, "", (v: string) => {
writeStringOfVarInt32(v);
Expand Down
21 changes: 19 additions & 2 deletions javascript/packages/fury/lib/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { Config } from "./type";
import { Config, LATIN1 } from "./type";
import { isNodeEnv } from "./util";
import { PlatformBuffer, alloc, fromUint8Array } from './platformBuffer';

Expand All @@ -24,6 +24,9 @@ export const BinaryReader = (config: Config) => {
let dataView!: DataView;
let buffer!: PlatformBuffer;
let bigString: string;

const stringLatin1 = sliceStringEnable ? stringLatin1Fast : stringLatin1Slow;

function reset(ab: Uint8Array) {
buffer = fromUint8Array(ab);
dataView = new DataView(buffer.buffer, buffer.byteOffset);
Expand Down Expand Up @@ -99,6 +102,12 @@ export const BinaryReader = (config: Config) => {
return result;
}

function stringOfVarInt32() {
const useLatin1 = config.useLatin1 ? uint8() === LATIN1 : false;
const len = varInt32();
return useLatin1 ? stringLatin1(len) : stringUtf8(len);
}

function stringLatin1Fast(len: number) {
const result = bigString.substring(cursor, cursor + len);
cursor += len;
Expand All @@ -118,6 +127,12 @@ export const BinaryReader = (config: Config) => {
return result;
}

function bufferRef(len: number) {
const result = buffer.subarray(cursor, cursor + len);
cursor += len;
return result;
}

function varInt32() {
let byte_ = int8();
let result = byte_ & 0x7f;
Expand Down Expand Up @@ -146,10 +161,12 @@ export const BinaryReader = (config: Config) => {
varInt32,
int8,
buffer: binary,
bufferRef,
uint8,
reset,
stringUtf8,
stringLatin1: sliceStringEnable ? stringLatin1Fast : stringLatin1Slow,
stringLatin1,
stringOfVarInt32,
double,
float,
uint16,
Expand Down
39 changes: 25 additions & 14 deletions javascript/test/io.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,20 @@ function num2Bin(num: number) {
writer.stringOfVarInt32(str);
const ab = writer.dump();
const reader = BinaryReader(config);
reader.reset(ab);
if (config.useLatin1) {
expect(reader.uint8()).toBe(1);

{
reader.reset(ab);
if (config.useLatin1) {
expect(reader.uint8()).toBe(1);
}
const len = reader.varInt32();
expect(len).toBe(17);
expect(reader.stringUtf8(len)).toBe(str);
}
{
reader.reset(ab);
expect(reader.stringOfVarInt32()).toBe(str);
}
const len = reader.varInt32();
expect(len).toBe(17);
expect(reader.stringUtf8(len)).toBe(str);
});

test('should long utf8 string work', () => {
Expand All @@ -228,13 +235,19 @@ function num2Bin(num: number) {
writer.stringOfVarInt32(str);
const ab = writer.dump();
const reader = BinaryReader(config);
reader.reset(ab);
if (config.useLatin1) {
expect(reader.uint8()).toBe(1);
{
reader.reset(ab);
if (config.useLatin1) {
expect(reader.uint8()).toBe(1);
}
const len = reader.varInt32();
expect(len).toBe(170);
expect(reader.stringUtf8(len)).toBe(str);
}
{
reader.reset(ab);
expect(reader.stringOfVarInt32()).toBe(str);
}
const len = reader.varInt32();
expect(len).toBe(170);
expect(reader.stringUtf8(len)).toBe(str);
});

test('should buffer work', () => {
Expand Down Expand Up @@ -339,5 +352,3 @@ function num2Bin(num: number) {
});
});
})


20 changes: 20 additions & 0 deletions javascript/test/reader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import { alloc } from '@furyjs/fury/lib/platformBuffer';
import { BinaryReader } from '@furyjs/fury/lib/reader';
import { Config } from '@furyjs/fury/lib/type';
import { BinaryWriter } from '@furyjs/fury/lib/writer';
import { describe, expect, test } from '@jest/globals';
Expand Down Expand Up @@ -55,3 +57,21 @@ const hps = process.env.enableHps ? require('@furyjs/hps') : null;
})


describe('reader', () => {

test('should bufferRef work', () => {
const bb = alloc(100);
bb.latin1Write("hello", 0);
const target = new Uint8Array(5);
bb.copy(target, 0, 0, 5);
expect([...target]).toEqual([ 104, 101, 108, 108, 111 ])

const reader = BinaryReader({});

reader.reset(bb);
const ref = reader.bufferRef(5);
ref[0] = 0;
bb.copy(target, 0, 0, 5);
expect([...target]).toEqual([ 0, 101, 108, 108, 111 ])
})
})

0 comments on commit 308b825

Please sign in to comment.