Skip to content

[Feature] Implement Buffer.alloc and Buffer.allocUnsafe, add tests #4

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

Merged
merged 4 commits into from
Jul 18, 2019
Merged
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
20 changes: 20 additions & 0 deletions assembly/buffer/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import { BLOCK_MAXSIZE } from "rt/common";
import { E_INVALIDLENGTH, E_INDEXOUTOFRANGE } from "util/error";
import { Uint8Array } from "typedarray";

export class Buffer extends Uint8Array {
constructor(size: i32) {
super(size);
}

public static alloc(size: i32): Buffer {
return new Buffer(size);
}

@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);
let buffer = __alloc(size, idof<ArrayBuffer>());
// This retains the pointer to the result Buffer.
let result = changetype<Buffer>(__alloc(offsetof<Buffer>(), idof<Buffer>()));
result.data = changetype<ArrayBuffer>(buffer);
result.dataStart = changetype<usize>(buffer);
result.dataLength = size;
return result;
}
}
2 changes: 2 additions & 0 deletions assembly/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/// <reference path="../node_modules/assemblyscript/std/assembly/rt/index.d.ts" />

export { Buffer } from "./buffer";
5 changes: 4 additions & 1 deletion assembly/node.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
declare class Buffer extends Uint8Array {

/** This method allocates a new Buffer of indicated size. All of the data is zeroed. */
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;
}
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.wat
24 changes: 24 additions & 0 deletions tests/buffer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* });
* });
*/
import { BLOCK_MAXSIZE } from "rt/common";

describe("buffer", () => {
test("#constructor", () => {
Expand All @@ -17,5 +18,28 @@ describe("buffer", () => {
let myBuffer = new Buffer(10);
expect<ArrayBuffer>(myBuffer.buffer).toBeTruthy();
expect<ArrayBuffer>(myBuffer.buffer).toHaveLength(10);
// TODO: expectFn(() => { new Buffer(-1); }).toThrow();
// TODO: expectFn(() => { new Buffer(BLOCK_MAXSIZE + 1); }).toThrow();
});

test("#alloc", () => {
expect<Buffer>(Buffer.alloc(10)).toBeTruthy();
expect<Buffer>(Buffer.alloc(10)).toHaveLength(10);
let buff = Buffer.alloc(100);
for (let i = 0; i < buff.length; i++) expect<u8>(buff[i]).toBe(0);
expect<ArrayBuffer>(buff.buffer).not.toBeNull();
expect<u32>(buff.byteLength).toBe(100);
// TODO: expectFn(() => { Buffer.alloc(-1); }).toThrow();
// TODO: expectFn(() => { Buffer.alloc(BLOCK_MAXSIZE + 1); }).toThrow();
});

test("#allocUnsafe", () => {
expect<Buffer>(Buffer.allocUnsafe(10)).toBeTruthy();
expect<Buffer>(Buffer.allocUnsafe(10)).toHaveLength(10);
let buff = Buffer.allocUnsafe(100);
expect<ArrayBuffer>(buff.buffer).not.toBeNull();
expect<u32>(buff.byteLength).toBe(100);
// TODO: expectFn(() => { Buffer.allocUnsafe(-1); }).toThrow();
// TODO: expectFn(() => { Buffer.allocUnsafe(BLOCK_MAXSIZE + 1); }).toThrow();
});
});
15 changes: 11 additions & 4 deletions tests/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ class Reporter extends EmptyReporter {
}

onTestFinish(group, test) {
if (test.pass) process.stdout.write("Test : " + group.name + " -> " + test.name + " ✔ PASS");
else process.stdout.write("Test : " + group.name + " -> " + test.name + " ❌ FAIL");
process.stdout.write("\n");
if (test.pass) process.stdout.write("Test : " + group.name + " -> " + test.name + " ✔ PASS\n");
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.logs.length > 0) {
test.logs.forEach((e, i) => {
Expand All @@ -49,7 +53,10 @@ class Reporter extends EmptyReporter {
process.stdout.write("\n");
}
}
onFinish() {
onFinish(context) {
const passed = context.testGroups.filter(e => !e.pass).length === 0;
if (passed) process.stdout.write("Suite : ✔ PASS");
else process.stdout.write("Suite : ❌ FAIL");
process.stdout.write("\n");
}
}
Expand Down