Skip to content

Commit c3a162d

Browse files
authored
[Feature] Implement Buffer.alloc and Buffer.allocUnsafe, add tests (#4)
* [Feature] Implement Buffer.alloc and Buffer.allocUnsafe, add tests * [Tests] Add Actual and Expected message * [Tests] Add todos to implement when Try/Catch is supported * [Cleanup] import Uint8Array manually to avoid @ts-ignore
1 parent ff5a10c commit c3a162d

File tree

6 files changed

+62
-5
lines changed

6 files changed

+62
-5
lines changed

assembly/buffer/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
1+
import { BLOCK_MAXSIZE } from "rt/common";
2+
import { E_INVALIDLENGTH, E_INDEXOUTOFRANGE } from "util/error";
3+
import { Uint8Array } from "typedarray";
4+
15
export class Buffer extends Uint8Array {
26
constructor(size: i32) {
37
super(size);
48
}
9+
10+
public static alloc(size: i32): Buffer {
11+
return new Buffer(size);
12+
}
13+
14+
@unsafe public static allocUnsafe(size: i32): Buffer {
15+
// Node throws an error if size is less than 0
16+
if (u32(size) > BLOCK_MAXSIZE) throw new RangeError(E_INVALIDLENGTH);
17+
let buffer = __alloc(size, idof<ArrayBuffer>());
18+
// This retains the pointer to the result Buffer.
19+
let result = changetype<Buffer>(__alloc(offsetof<Buffer>(), idof<Buffer>()));
20+
result.data = changetype<ArrayBuffer>(buffer);
21+
result.dataStart = changetype<usize>(buffer);
22+
result.dataLength = size;
23+
return result;
24+
}
525
}

assembly/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
/// <reference path="../node_modules/assemblyscript/std/assembly/rt/index.d.ts" />
2+
13
export { Buffer } from "./buffer";

assembly/node.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
declare class Buffer extends Uint8Array {
2-
2+
/** This method allocates a new Buffer of indicated size. All of the data is zeroed. */
3+
static alloc(size: i32): Buffer;
4+
/** This method allocates a new Buffer of indicated size. This is unsafe because the data is not zeroed. */
5+
static allocUnsafe(size: i32): Buffer;
36
}

tests/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.wat

tests/buffer.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* });
1010
* });
1111
*/
12+
import { BLOCK_MAXSIZE } from "rt/common";
1213

1314
describe("buffer", () => {
1415
test("#constructor", () => {
@@ -17,5 +18,28 @@ describe("buffer", () => {
1718
let myBuffer = new Buffer(10);
1819
expect<ArrayBuffer>(myBuffer.buffer).toBeTruthy();
1920
expect<ArrayBuffer>(myBuffer.buffer).toHaveLength(10);
21+
// TODO: expectFn(() => { new Buffer(-1); }).toThrow();
22+
// TODO: expectFn(() => { new Buffer(BLOCK_MAXSIZE + 1); }).toThrow();
23+
});
24+
25+
test("#alloc", () => {
26+
expect<Buffer>(Buffer.alloc(10)).toBeTruthy();
27+
expect<Buffer>(Buffer.alloc(10)).toHaveLength(10);
28+
let buff = Buffer.alloc(100);
29+
for (let i = 0; i < buff.length; i++) expect<u8>(buff[i]).toBe(0);
30+
expect<ArrayBuffer>(buff.buffer).not.toBeNull();
31+
expect<u32>(buff.byteLength).toBe(100);
32+
// TODO: expectFn(() => { Buffer.alloc(-1); }).toThrow();
33+
// TODO: expectFn(() => { Buffer.alloc(BLOCK_MAXSIZE + 1); }).toThrow();
34+
});
35+
36+
test("#allocUnsafe", () => {
37+
expect<Buffer>(Buffer.allocUnsafe(10)).toBeTruthy();
38+
expect<Buffer>(Buffer.allocUnsafe(10)).toHaveLength(10);
39+
let buff = Buffer.allocUnsafe(100);
40+
expect<ArrayBuffer>(buff.buffer).not.toBeNull();
41+
expect<u32>(buff.byteLength).toBe(100);
42+
// TODO: expectFn(() => { Buffer.allocUnsafe(-1); }).toThrow();
43+
// TODO: expectFn(() => { Buffer.allocUnsafe(BLOCK_MAXSIZE + 1); }).toThrow();
2044
});
2145
});

tests/node.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ class Reporter extends EmptyReporter {
3737
}
3838

3939
onTestFinish(group, test) {
40-
if (test.pass) process.stdout.write("Test : " + group.name + " -> " + test.name + " ✔ PASS");
41-
else process.stdout.write("Test : " + group.name + " -> " + test.name + " ❌ FAIL");
42-
process.stdout.write("\n");
40+
if (test.pass) process.stdout.write("Test : " + group.name + " -> " + test.name + " ✔ PASS\n");
41+
else process.stdout.write("Test : " + group.name + " -> " + test.name + " ❌ FAIL\n");
42+
43+
if (!test.pass) {
44+
process.stdout.write("Actual : " + test.actual.message + "\n");
45+
process.stdout.write("Expected : " + test.expected.message + "\n");
46+
}
4347

4448
if (test.logs.length > 0) {
4549
test.logs.forEach((e, i) => {
@@ -49,7 +53,10 @@ class Reporter extends EmptyReporter {
4953
process.stdout.write("\n");
5054
}
5155
}
52-
onFinish() {
56+
onFinish(context) {
57+
const passed = context.testGroups.filter(e => !e.pass).length === 0;
58+
if (passed) process.stdout.write("Suite : ✔ PASS");
59+
else process.stdout.write("Suite : ❌ FAIL");
5360
process.stdout.write("\n");
5461
}
5562
}

0 commit comments

Comments
 (0)