Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 37 additions & 0 deletions assembly/buffer/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BLOCK_MAXSIZE } from "rt/common";
import { E_INVALIDLENGTH, E_INDEXOUTOFRANGE } from "util/error";
import { Uint8Array } from "typedarray";
import { Array } from "array";

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

public static concat<T>(items: T, length: i32): Buffer {
if (!isArray<T>()) {
ERROR("Buffer.concat<T> must accept an Array where T extends Array<Uint8Array | Buffer>");
}
assert(unchecked(items[0]) instanceof Uint8Array); // Can this be a static check?

let size: usize = 0;
let itemCount = usize(items.length);
let itemsDataStart = items.dataStart;

for (let i: usize = 0; i < itemCount; i++) {
let item = load<usize>(itemsDataStart + (i << alignof<usize>()));
if (item == 0) continue;
size += <usize>load<u32>(item, offsetof<Uint8Array>("dataLength"));
}
size = min<usize>(usize(length), size);
assert(items);

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

result.data = changetype<ArrayBuffer>(buffer);
result.dataStart = changetype<usize>(buffer);
let start: usize = result.dataStart;
for (let i: usize = 0; i < itemCount && size > 0; i++) {
let item = load<usize>(itemsDataStart + (i << alignof<usize>()));
if (item == 0) continue;
let count = min<u32>(size, <usize>load<u32>(item, offsetof<Uint8Array>("dataLength")));
memory.copy(start, load<usize>(item, offsetof<Uint8Array>("dataStart")), count);
start += count;
size -= count;
}

return result;
}

@unsafe public static allocUnsafe(size: i32): Buffer {
// Node throws an error if size is less than 0
if (u32(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,4 +3,6 @@ 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<T extends Uint8Array[]>(list: T, totalLength: i32): Buffer;
}
25 changes: 25 additions & 0 deletions tests/buffer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,29 @@ describe("buffer", () => {
// TODO: expectFn(() => { Buffer.allocUnsafe(-1); }).toThrow();
// TODO: expectFn(() => { Buffer.allocUnsafe(BLOCK_MAXSIZE + 1); }).toThrow();
});

test("#concat", () => {
let list: Buffer[] = new Array<Buffer>(0);
for (let i = 0; i < 5; i++) {
let buff = Buffer.alloc(5 - i);
for (let j = 0; j < (5 - i); j++) {
buff[j] = u8(i);
}
list.push(buff);
}
let actual: Buffer = Buffer.concat<Buffer[]>(list, 15);

let expected: Buffer = Buffer.alloc(15);
expected[5] = 1;
expected[6] = 1;
expected[7] = 1;
expected[8] = 1;
expected[9] = 2;
expected[10] = 2;
expected[11] = 2;
expected[12] = 3;
expected[13] = 3;
expected[14] = 4;
expect<ArrayBuffer>(actual.buffer).toStrictEqual(expected.buffer);
});
});
7 changes: 4 additions & 3 deletions tests/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ class Reporter extends EmptyReporter {
else process.stdout.write("Test : " + group.name + " -> " + test.name + " ❌ FAIL\n");

if (!test.pass) {
process.stdout.write("Actual : " + test.actual.message + "\n");
process.stdout.write("Expected : " + test.expected.message + "\n");
if (test.actual) process.stdout.write("Actual : " + test.actual.message + "\n");
if (test.expected) process.stdout.write("Expected : " + test.expected.message + "\n");
}

if (test.logs.length > 0) {
test.logs.forEach((e, i) => {
if (i > 0) process.stdout.write("\n");
process.stdout.write("Log : " + e.value);
if (e.bytes) process.stdout.write("Log : " + Buffer.from(e.bytes).toString("hex"));
else process.stdout.write("Log : " + e.value);
});
process.stdout.write("\n");
}
Expand Down