Skip to content
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
6 changes: 5 additions & 1 deletion assembly/buffer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export class Buffer extends Uint8Array {
return result;
}

public static isBuffer<T>(value: T): bool {
return value instanceof Buffer;
}

readUInt8(offset: i32 = 0): u8 {
if(<u32>offset >= this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
return load<u8>(this.dataStart + usize(offset));
Expand All @@ -39,7 +43,7 @@ export class Buffer extends Uint8Array {
store<i8>(this.dataStart + offset, value);
return offset + 1;
}

readInt8(offset: i32 = 0): i8 {
if(<u32>offset >= this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
return load<i8>(this.dataStart + usize(offset));
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 asserts a value is a Buffer object via `value instanceof Buffer`. */
static isBuffer<T>(value: T): bool;
/** Reads an unsigned integer at the designated offset. */
readUInt8(offset?: i32): u8;
/** Writes an inputted u8 value to the buffer, at the desired offset. */
Expand Down
24 changes: 9 additions & 15 deletions package-lock.json

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

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
"url": "https://github.com/AssemblyScript/node/issues"
},
"devDependencies": {
"@as-pect/core": "^2.2.0",
"assemblyscript": "github:assemblyscript/assemblyscript#7c775d1bccbe08fec5d820b9d53ae44ff6bd1e49",
"diff": "^4.0.1",
"@as-pect/core": "^2.3.1",
"assemblyscript": "github:assemblyscript/assemblyscript",
"glob": "^7.1.4",
"wasi": "github:devsnek/node-wasi"
},
Expand Down
27 changes: 21 additions & 6 deletions tests/buffer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("buffer", () => {
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<ArrayBuffer | null>(buff.buffer).not.toBeNull();
expect<u32>(buff.byteLength).toBe(100);
// TODO: expectFn(() => { Buffer.alloc(-1); }).toThrow();
// TODO: expectFn(() => { Buffer.alloc(BLOCK_MAXSIZE + 1); }).toThrow();
Expand All @@ -37,12 +37,27 @@ describe("buffer", () => {
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<ArrayBuffer | null>(buff.buffer).not.toBeNull();
expect<u32>(buff.byteLength).toBe(100);
// TODO: expectFn(() => { Buffer.allocUnsafe(-1); }).toThrow();
// TODO: expectFn(() => { Buffer.allocUnsafe(BLOCK_MAXSIZE + 1); }).toThrow();
});

test("#isBuffer", () => {
let a = "";
let b = new Uint8Array(0);
let c = 0;
let d = 1.1;
let e = new Buffer(0);
expect<bool>(Buffer.isBuffer<string>(a)).toBeFalsy();
expect<bool>(Buffer.isBuffer<Uint8Array>(b)).toBeFalsy();
expect<bool>(Buffer.isBuffer<i32>(c)).toBeFalsy();
expect<bool>(Buffer.isBuffer<f64>(d)).toBeFalsy();
expect<bool>(Buffer.isBuffer<Buffer>(e)).toBeTruthy();
// null checks are done by the compiler explicitly at runtime
expect<bool>(Buffer.isBuffer<Buffer | null>(null)).toBeFalsy();
});

test("#readUInt8", () => {
let buff = new Buffer(10);
buff[0] = -2;
Expand All @@ -53,10 +68,10 @@ describe("buffer", () => {
// Testing offset
expect<u8>(buff.readUInt8(9)).toBe(47);
// TODO:
// expectFn(() => {
// expectFn(() => {
// let newBuff = new Buffer(1);
// newBuff.readUInt8(5);
// }).toThrow();
// }).toThrow();
});

test("#writeUInt8", () => {
Expand All @@ -65,7 +80,7 @@ describe("buffer", () => {
expect<i32>(buff.writeUInt8(252,4)).toBe(5);
expect<u8>(buff[0]).toBe(4);
expect<u8>(buff[4]).toBe(252);
});
});

test("#writeInt8", () => {
let buff = new Buffer(5);
Expand All @@ -84,7 +99,7 @@ describe("buffer", () => {
// Testing offset, and casting between u8 and i8.
expect<i8>(buff.readInt8(9)).toBe(-1);
// TODO:
// expectFn(() => {
// expectFn(() => {
// let newBuff = new Buffer(1);
// newBuff.readInt8(5);
// }).toThrow();
Expand Down
44 changes: 7 additions & 37 deletions tests/node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { TestContext, EmptyReporter } = require("@as-pect/core");
const { TestContext, VerboseReporter } = require("@as-pect/core");
const { instantiateBuffer } = require("assemblyscript/lib/loader");
const glob = require("glob");
const { main } = require("assemblyscript/cli/asc");
Expand All @@ -7,7 +7,7 @@ const path = require("path");
const fs = require("fs");
const Wasi = require("wasi");
const wasi = new Wasi({});
const diff = require("diff");
let pass = true;

const options = parse(process.argv.slice(2), {
"help": {
Expand All @@ -27,41 +27,7 @@ if (options.unknown.length > 1) {
process.exit(1);
}

class Reporter extends EmptyReporter {
onGroupFinish(group) {
if (group.name) {
if (group.pass) process.stdout.write("Group : " + group.name + " -> ✔ PASS");
else process.stdout.write("Group : " + group.name + " -> ❌ FAIL");
process.stdout.write("\n");
}
}

onTestFinish(group, test) {
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) => {
if (i > 0) process.stdout.write("\n");
process.stdout.write("Log : " + e.value);
});
process.stdout.write("\n");
}
}
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");
}
}

const reporter = new Reporter();
const reporter = new VerboseReporter();

function relativeFromCwd(location) {
return path.relative(process.cwd(), location);
Expand Down Expand Up @@ -160,4 +126,8 @@ function runTest(file, type, binary, wat) {
wasi.setMemory(wasm.memory);
wasi.view = new DataView(wasm.memory.buffer);
context.run(wasm);

if (!context.pass) pass = false;
}

process.exit(pass ? 0 : 1);