Skip to content

Commit fe0cf50

Browse files
committed
[Chore] Merge master
2 parents 10a5c54 + 22e7456 commit fe0cf50

6 files changed

Lines changed: 39 additions & 20 deletions

File tree

assembly/buffer/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export class Buffer extends Uint8Array {
2727
return result;
2828
}
2929

30+
public static isBuffer<T>(value: T): bool {
31+
return value instanceof Buffer;
32+
}
33+
3034
readUInt8(offset: i32 = 0): u8 {
3135
if(<u32>offset >= this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
3236
return load<u8>(this.dataStart + usize(offset));

assembly/node.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ declare class Buffer extends Uint8Array {
33
static alloc(size: i32): Buffer;
44
/** This method allocates a new Buffer of indicated size. This is unsafe because the data is not zeroed. */
55
static allocUnsafe(size: i32): Buffer;
6+
/** This method asserts a value is a Buffer object via `value instanceof Buffer`. */
7+
static isBuffer<T>(value: T): bool;
68
/** Reads an unsigned integer at the designated offset. */
79
readUInt8(offset?: i32): u8;
810
/** Writes an inputted u8 value to the buffer, at the desired offset. */

package-lock.json

Lines changed: 9 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
"url": "https://github.com/AssemblyScript/node/issues"
1313
},
1414
"devDependencies": {
15-
"@as-pect/core": "^2.2.0",
16-
"assemblyscript": "github:assemblyscript/assemblyscript#7c775d1bccbe08fec5d820b9d53ae44ff6bd1e49",
17-
"diff": "^4.0.1",
15+
"@as-pect/core": "^2.3.1",
16+
"assemblyscript": "github:assemblyscript/assemblyscript",
1817
"glob": "^7.1.4",
1918
"wasi": "github:devsnek/node-wasi"
2019
},

tests/buffer.spec.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe("buffer", () => {
2828
expect<Buffer>(Buffer.alloc(10)).toHaveLength(10);
2929
let buff = Buffer.alloc(100);
3030
for (let i = 0; i < buff.length; i++) expect<u8>(buff[i]).toBe(0);
31-
expect<ArrayBuffer>(buff.buffer).not.toBeNull();
31+
expect<ArrayBuffer | null>(buff.buffer).not.toBeNull();
3232
expect<u32>(buff.byteLength).toBe(100);
3333
// TODO: expectFn(() => { Buffer.alloc(-1); }).toThrow();
3434
// TODO: expectFn(() => { Buffer.alloc(BLOCK_MAXSIZE + 1); }).toThrow();
@@ -38,12 +38,27 @@ describe("buffer", () => {
3838
expect<Buffer>(Buffer.allocUnsafe(10)).toBeTruthy();
3939
expect<Buffer>(Buffer.allocUnsafe(10)).toHaveLength(10);
4040
let buff = Buffer.allocUnsafe(100);
41-
expect<ArrayBuffer>(buff.buffer).not.toBeNull();
41+
expect<ArrayBuffer | null>(buff.buffer).not.toBeNull();
4242
expect<u32>(buff.byteLength).toBe(100);
4343
// TODO: expectFn(() => { Buffer.allocUnsafe(-1); }).toThrow();
4444
// TODO: expectFn(() => { Buffer.allocUnsafe(BLOCK_MAXSIZE + 1); }).toThrow();
4545
});
4646

47+
test("#isBuffer", () => {
48+
let a = "";
49+
let b = new Uint8Array(0);
50+
let c = 0;
51+
let d = 1.1;
52+
let e = new Buffer(0);
53+
expect<bool>(Buffer.isBuffer<string>(a)).toBeFalsy();
54+
expect<bool>(Buffer.isBuffer<Uint8Array>(b)).toBeFalsy();
55+
expect<bool>(Buffer.isBuffer<i32>(c)).toBeFalsy();
56+
expect<bool>(Buffer.isBuffer<f64>(d)).toBeFalsy();
57+
expect<bool>(Buffer.isBuffer<Buffer>(e)).toBeTruthy();
58+
// null checks are done by the compiler explicitly at runtime
59+
expect<bool>(Buffer.isBuffer<Buffer | null>(null)).toBeFalsy();
60+
});
61+
4762
test("#readUInt8", () => {
4863
let buff = new Buffer(10);
4964
buff[0] = -2;

tests/node.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const path = require("path");
77
const fs = require("fs");
88
const Wasi = require("wasi");
99
const wasi = new Wasi({});
10+
let pass = true;
1011

1112
const options = parse(process.argv.slice(2), {
1213
"help": {
@@ -125,4 +126,8 @@ function runTest(file, type, binary, wat) {
125126
wasi.setMemory(wasm.memory);
126127
wasi.view = new DataView(wasm.memory.buffer);
127128
context.run(wasm);
129+
130+
if (!context.pass) pass = false;
128131
}
132+
133+
process.exit(pass ? 0 : 1);

0 commit comments

Comments
 (0)