Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Implement] Buffer.concat #5

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions assembly/buffer/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { BLOCK_MAXSIZE, BLOCK, BLOCK_OVERHEAD } from "rt/common";
import { E_INVALIDLENGTH, E_INDEXOUTOFRANGE } from "util/error";
import { Uint8Array } from "typedarray";
import { Array } from "array";

export class Buffer extends Uint8Array {
[key: number]: u8;

constructor(size: i32) {
super(size);
}
Expand All @@ -13,6 +12,48 @@ export class Buffer extends Uint8Array {
return new Buffer(size);
}

public static concat<T extends Uint8Array>(items: Array<T>, length: i32 = i32.MAX_VALUE): Buffer {
if (items.length == 0) return new Buffer(0);
if (length < 0) throw new Error(E_INDEXOUTOFRANGE);

let size: usize = 0;
let itemCount = <usize>items.length;
let itemsDataStart = items.dataStart;

// loop over each item and increase the size of the resulting buffer
for (let i: usize = 0; i < itemCount; i++) {
let item = load<usize>(itemsDataStart + (i << alignof<usize>()));
// check for nulls
if (item == 0) continue;
size += <usize>load<u32>(item, offsetof<Buffer>("byteLength"));
}

// account for passed concat buffer length
size = min(<usize>length, size);

let arrayBuffer = __alloc(size, idof<ArrayBuffer>());
let result = __alloc(offsetof<Buffer>(), idof<Buffer>());

// assemble the Buffer
store<usize>(result, __retain(arrayBuffer), offsetof<Buffer>("buffer"));
store<usize>(result, arrayBuffer, offsetof<Buffer>("dataStart"));
store<u32>(result, <u32>size, offsetof<Buffer>("byteLength"));

let start = arrayBuffer;
for (let i: usize = 0; i32(i < itemCount) & i32(size > 0); i++) {
let item = load<usize>(itemsDataStart + (i << alignof<usize>()));
// if Buffer is null, continue
if (item == 0) continue;
let count = min(size, <usize>load<u32>(item, offsetof<Buffer>("byteLength")));
memory.copy(start, load<usize>(item, offsetof<Buffer>("dataStart")), count);
start += count;
size -= count;
}

// return and retain
return changetype<Buffer>(result);
}

@unsafe static allocUnsafe(size: i32): Buffer {
// range must be valid
if (<usize>size > BLOCK_MAXSIZE) throw new RangeError(E_INVALIDLENGTH);
Expand Down
2 changes: 2 additions & 0 deletions assembly/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ declare class Buffer extends Uint8Array {
static alloc(size: i32): Buffer;
/** This method allocates a new Buffer of indicated size. This is unsafe because the data is not zeroed. */
static allocUnsafe(size: i32): Buffer;
/** This method concatenates an array of `U extends Uint8Array` objects. */
static concat(list: Buffer[], totalLength?: i32): Buffer;
/** This method asserts a value is a Buffer object via `value instanceof Buffer`. */
static isBuffer<T>(value: T): bool;
/** Reads a signed integer at the designated offset. */
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"devDependencies": {
"@as-pect/core": "^3.1.0-beta.3",
"assemblyscript": "0.9.3",
"assemblyscript": "0.9.4",
"glob": "^7.1.6"
},
"scripts": {
Expand Down
19 changes: 17 additions & 2 deletions tests/buffer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe("buffer", () => {
expect(Buffer.alloc(10)).toBeTruthy();
expect(Buffer.alloc(10)).toHaveLength(10);
let buff = Buffer.alloc(100);
for (let i = 0; i < buff.length; i++) expect<u8>(buff[i]).toBe(0);
for (let i = 0; i < buff.length; i++) expect(buff[i]).toBe(0);
expect(buff.buffer).not.toBeNull();
expect(buff.byteLength).toBe(100);
expect(() => { Buffer.alloc(-1); }).toThrow();
Expand All @@ -55,6 +55,21 @@ describe("buffer", () => {
// expect(() => { Buffer.allocUnsafe(BLOCK_MAXSIZE + 1); }).toThrow();
});

test("#concat", () => {
let actual = Buffer.concat([
create<Buffer>([0, 0, 0, 0, 0]),
create<Buffer>([1, 1, 1, 1]),
create<Buffer>([2, 2, 2]),
create<Buffer>([3, 3]),
create<Buffer>([4]),
create<Buffer>([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
], 15);

let expected = create<Buffer>([0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4]);

expect(actual).toStrictEqual(expected);
});

test("#isBuffer", () => {
let a = "";
let b = new Uint8Array(0);
Expand Down Expand Up @@ -252,7 +267,7 @@ describe("buffer", () => {
expect(buff.writeInt32LE(-559038737)).toBe(4);
expect(buff.writeInt32LE(283033613,4)).toBe(8);
let result = create<Buffer>([0xEF,0xBE,0xAD,0xDE,0x0d,0xc0,0xde,0x10]);
expect<Buffer>(buff).toStrictEqual(result);
expect(buff).toStrictEqual(result);
expect(() => {
let newBuff = new Buffer(1);
newBuff.writeInt32LE(0);
Expand Down