Skip to content

Commit

Permalink
feat: remove node specific dependency on Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
wikiwong committed Jul 20, 2023
1 parent daf8b9e commit 34e454e
Show file tree
Hide file tree
Showing 6 changed files with 7,834 additions and 8,419 deletions.
4 changes: 2 additions & 2 deletions packages/helper-buffer/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function makeBuffer(...splitedBytes: Array<Array<Byte>>): Array<Byte> {
return new Uint8Array(bytes).buffer;
}

export function fromHexdump(str: string): Buffer {
export function fromHexdump(str: string): Uint8Array {
let lines = str.split("\n");

// remove any leading left whitespace
Expand All @@ -63,5 +63,5 @@ export function fromHexdump(str: string): Buffer {
return acc;
}, []);

return Buffer.from(bytes);
return new Uint8Array(bytes);
}
4 changes: 2 additions & 2 deletions packages/ieee754/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ export function encodeF64(v: number): Array<number> {
}

export function decodeF32(bytes: Array<Byte>): number {
const buffer = Buffer.from(bytes);
const buffer = new Uint8Array(bytes);

return read(buffer, 0, true, SINGLE_PRECISION_MANTISSA, NUMBER_OF_BYTE_F32);
}

export function decodeF64(bytes: Array<Byte>): number {
const buffer = Buffer.from(bytes);
const buffer = new Uint8Array(bytes);

return read(buffer, 0, true, DOUBLE_PRECISION_MANTISSA, NUMBER_OF_BYTE_F64);
}
14 changes: 7 additions & 7 deletions packages/leb128/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ export const MAX_NUMBER_OF_BYTE_U32 = 5;
*/
export const MAX_NUMBER_OF_BYTE_U64 = 10;

export function decodeInt64(encodedBuffer: Buffer, index: number): any {
export function decodeInt64(encodedBuffer: Uint8Array, index: number): any {
return leb.decodeInt64(encodedBuffer, index);
}

export function decodeUInt64(encodedBuffer: Buffer, index: number): any {
export function decodeUInt64(encodedBuffer: Uint8Array, index: number): any {
return leb.decodeUInt64(encodedBuffer, index);
}

export function decodeInt32(encodedBuffer: Buffer, index: number): any {
export function decodeInt32(encodedBuffer: Uint8Array, index: number): any {
return leb.decodeInt32(encodedBuffer, index);
}

export function decodeUInt32(encodedBuffer: Buffer, index: number): any {
export function decodeUInt32(encodedBuffer: Uint8Array, index: number): any {
return leb.decodeUInt32(encodedBuffer, index);
}

export function encodeU32(v: number): Buffer {
export function encodeU32(v: number): Uint8Array {
return leb.encodeUInt32(v);
}

export function encodeI32(v: number): Buffer {
export function encodeI32(v: number): Uint8Array {
return leb.encodeInt32(v);
}

export function encodeI64(v: number): Buffer {
export function encodeI64(v: number): Uint8Array {
return leb.encodeInt64(v);
}
16 changes: 8 additions & 8 deletions packages/leb128/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,43 @@ describe("LEB128", () => {
let u32;

it("1 byte", () => {
u32 = decodeUInt32(Buffer.from([0x00]));
u32 = decodeUInt32(new Uint8Array([0x00]));
assert.equal(u32.value, 0);
assert.equal(u32.nextIndex, 1);

u32 = decodeUInt32(Buffer.from([0x08]));
u32 = decodeUInt32(new Uint8Array([0x08]));
assert.equal(u32.value, 8);
assert.equal(u32.nextIndex, 1);
});

it("2 byte", () => {
u32 = decodeUInt32(Buffer.from([0x80, 0x7f]));
u32 = decodeUInt32(new Uint8Array([0x80, 0x7f]));
assert.equal(u32.value, 16256);
assert.equal(u32.nextIndex, 2);
});

it("3 byte", () => {
u32 = decodeUInt32(Buffer.from([0xe5, 0x8e, 0x26]));
u32 = decodeUInt32(new Uint8Array([0xe5, 0x8e, 0x26]));
assert.equal(u32.value, 624485);
assert.equal(u32.nextIndex, 3);
});

it("4 byte", () => {
u32 = decodeUInt32(Buffer.from([0x80, 0x80, 0x80, 0x4f]));
u32 = decodeUInt32(new Uint8Array([0x80, 0x80, 0x80, 0x4f]));
assert.equal(u32.value, 165675008);
assert.equal(u32.nextIndex, 4);
});

it("5 byte", () => {
u32 = decodeUInt32(Buffer.from([0x89, 0x80, 0x80, 0x80, 0x00]));
u32 = decodeUInt32(new Uint8Array([0x89, 0x80, 0x80, 0x80, 0x00]));
assert.equal(u32.value, 9);
assert.equal(u32.nextIndex, 5);
});
});

it("should decode number where |n| > 2^53", () => {
const u64 = decodeInt64(
Buffer.from([0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x7f])
new Uint8Array([0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x7f])
);

assert.typeOf(u64.value, "object");
Expand All @@ -54,7 +54,7 @@ describe("LEB128", () => {
});

it("should decode -1 to i64", () => {
const u64 = decodeInt64(Buffer.from([0x7f]));
const u64 = decodeInt64(new Uint8Array([0x7f]));

assert.typeOf(u64.value, "object");
assert.equal(u64.nextIndex, 1);
Expand Down
20 changes: 10 additions & 10 deletions packages/wasm-parser/src/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export function decode(ab: ArrayBuffer, opts: DecoderOpts): Program {
*/
function readU32(): Decoded32 {
const bytes = readBytes(MAX_NUMBER_OF_BYTE_U32);
const buffer = Buffer.from(bytes);
const buffer = new Uint8Array(bytes);

return decodeUInt32(buffer);
}
Expand All @@ -254,7 +254,7 @@ export function decode(ab: ArrayBuffer, opts: DecoderOpts): Program {
// where 32 bits = max 4 bytes

const bytes = readBytes(4);
const buffer = Buffer.from(bytes);
const buffer = new Uint8Array(bytes);

return decodeUInt32(buffer);
}
Expand All @@ -263,7 +263,7 @@ export function decode(ab: ArrayBuffer, opts: DecoderOpts): Program {
// where 7 bits = max 1 bytes

const bytes = readBytes(1);
const buffer = Buffer.from(bytes);
const buffer = new Uint8Array(bytes);

return decodeUInt32(buffer);
}
Expand All @@ -273,7 +273,7 @@ export function decode(ab: ArrayBuffer, opts: DecoderOpts): Program {
*/
function read32(): Decoded32 {
const bytes = readBytes(MAX_NUMBER_OF_BYTE_U32);
const buffer = Buffer.from(bytes);
const buffer = new Uint8Array(bytes);

return decodeInt32(buffer);
}
Expand All @@ -283,14 +283,14 @@ export function decode(ab: ArrayBuffer, opts: DecoderOpts): Program {
*/
function read64(): Decoded64 {
const bytes = readBytes(MAX_NUMBER_OF_BYTE_U64);
const buffer = Buffer.from(bytes);
const buffer = new Uint8Array(bytes);

return decodeInt64(buffer);
}

function readU64(): Decoded64 {
const bytes = readBytes(MAX_NUMBER_OF_BYTE_U64);
const buffer = Buffer.from(bytes);
const buffer = new Uint8Array(bytes);

return decodeUInt64(buffer);
}
Expand Down Expand Up @@ -1861,10 +1861,10 @@ export function decode(ab: ArrayBuffer, opts: DecoderOpts): Program {

dumpSep(
"ignore custom " +
JSON.stringify(sectionName.value) +
" section (" +
remainingBytes +
" bytes)"
JSON.stringify(sectionName.value) +
" section (" +
remainingBytes +
" bytes)"
);
}

Expand Down
Loading

0 comments on commit 34e454e

Please sign in to comment.