diff --git a/assembly/buffer/index.ts b/assembly/buffer/index.ts index 98c0f4e..66866bf 100644 --- a/assembly/buffer/index.ts +++ b/assembly/buffer/index.ts @@ -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()); + // This retains the pointer to the result Buffer. + let result = changetype(__alloc(offsetof(), idof())); + result.data = changetype(buffer); + result.dataStart = changetype(buffer); + result.dataLength = size; + return result; + } } diff --git a/assembly/index.ts b/assembly/index.ts index 780f216..93246c5 100644 --- a/assembly/index.ts +++ b/assembly/index.ts @@ -1 +1,3 @@ +/// + export { Buffer } from "./buffer"; diff --git a/assembly/node.d.ts b/assembly/node.d.ts index e18c3e1..80075a8 100644 --- a/assembly/node.d.ts +++ b/assembly/node.d.ts @@ -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; } diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..21868a6 --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1 @@ +*.wat \ No newline at end of file diff --git a/tests/buffer.spec.ts b/tests/buffer.spec.ts index 1c7b4e4..1596845 100644 --- a/tests/buffer.spec.ts +++ b/tests/buffer.spec.ts @@ -9,6 +9,7 @@ * }); * }); */ +import { BLOCK_MAXSIZE } from "rt/common"; describe("buffer", () => { test("#constructor", () => { @@ -17,5 +18,28 @@ describe("buffer", () => { let myBuffer = new Buffer(10); expect(myBuffer.buffer).toBeTruthy(); expect(myBuffer.buffer).toHaveLength(10); + // TODO: expectFn(() => { new Buffer(-1); }).toThrow(); + // TODO: expectFn(() => { new Buffer(BLOCK_MAXSIZE + 1); }).toThrow(); + }); + + test("#alloc", () => { + 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(buff[i]).toBe(0); + expect(buff.buffer).not.toBeNull(); + expect(buff.byteLength).toBe(100); + // TODO: expectFn(() => { Buffer.alloc(-1); }).toThrow(); + // TODO: expectFn(() => { Buffer.alloc(BLOCK_MAXSIZE + 1); }).toThrow(); + }); + + test("#allocUnsafe", () => { + expect(Buffer.allocUnsafe(10)).toBeTruthy(); + expect(Buffer.allocUnsafe(10)).toHaveLength(10); + let buff = Buffer.allocUnsafe(100); + expect(buff.buffer).not.toBeNull(); + expect(buff.byteLength).toBe(100); + // TODO: expectFn(() => { Buffer.allocUnsafe(-1); }).toThrow(); + // TODO: expectFn(() => { Buffer.allocUnsafe(BLOCK_MAXSIZE + 1); }).toThrow(); }); }); diff --git a/tests/node.js b/tests/node.js index 35cf77c..5117ef9 100644 --- a/tests/node.js +++ b/tests/node.js @@ -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) => { @@ -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"); } }