Skip to content

Commit daf9dc1

Browse files
committed
[Chore] merge master
2 parents 62b4dc9 + 22e7456 commit daf9dc1

File tree

6 files changed

+126
-61
lines changed

6 files changed

+126
-61
lines changed

assembly/buffer/index.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ export class Buffer extends Uint8Array {
2929
// @ts-ignore: AssemblyScript treats this statement correctly
3030
if (value instanceof String[]) {
3131
let length = value.length;
32-
log<i32>(length);
3332
let buffer = __alloc(length, idof<ArrayBuffer>());
3433
let sourceStart = value.dataStart;
3534
for (let i = 0; i < length; i++) {
36-
let str = changetype<string>(load<usize>(sourceStart + (usize(i) << alignof<usize>())));
35+
let str = changetype<string>(load<usize>(sourceStart + (<usize>i << alignof<usize>())));
3736
let value = parseFloat(str); // parseFloat is still naive
3837
store<u8>(buffer + usize(i), isFinite<f64>(value) ? u8(value) : u8(0));
3938
}
@@ -72,8 +71,32 @@ export class Buffer extends Uint8Array {
7271
result.dataStart = buffer;
7372
result.dataLength = u32(length);
7473
return result;
75-
7674
}
7775
ERROR("Cannot call Buffer.from<T>() where T is not a string, Buffer, ArrayBuffer, Array, or Array-like Object.");
7876
}
77+
public static isBuffer<T>(value: T): bool {
78+
return value instanceof Buffer;
79+
}
80+
81+
readUInt8(offset: i32 = 0): u8 {
82+
if(<u32>offset >= this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
83+
return load<u8>(this.dataStart + usize(offset));
84+
}
85+
86+
writeUInt8(value: u8, offset: i32 = 0): i32 {
87+
if(<u32>offset >= this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
88+
store<u8>(this.dataStart + offset, value);
89+
return offset + 1;
90+
}
91+
92+
writeInt8(value: i8, offset: i32 = 0): i32 {
93+
if(<u32>offset >= this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
94+
store<i8>(this.dataStart + offset, value);
95+
return offset + 1;
96+
}
97+
98+
readInt8(offset: i32 = 0): i8 {
99+
if(<u32>offset >= this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
100+
return load<i8>(this.dataStart + usize(offset));
101+
}
79102
}

assembly/node.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,14 @@ declare class Buffer extends Uint8Array {
55
static allocUnsafe(size: i32): Buffer;
66
/** This method creates a Buffer from the given reference. This method is naive and defaults to utf8 encoding for strings. */
77
static from<T>(value: T): Buffer;
8+
/** This method asserts a value is a Buffer object via `value instanceof Buffer`. */
9+
static isBuffer<T>(value: T): bool;
10+
/** Reads an unsigned integer at the designated offset. */
11+
readUInt8(offset?: i32): u8;
12+
/** Writes an inputted u8 value to the buffer, at the desired offset. */
13+
writeUInt8(value:u8, offset?:i32): i32;
14+
/** Writes an inputted value to the buffer, at the desired offset. */
15+
writeInt8(value:i8, offset?:i32): i32;
16+
/** Reads a signed integer at the designated offset. */
17+
readInt8(offset?: i32): i8;
818
}

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: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe("buffer", () => {
3434
expect<Buffer>(Buffer.alloc(10)).toHaveLength(10);
3535
let buff = Buffer.alloc(100);
3636
for (let i = 0; i < buff.length; i++) expect<u8>(buff[i]).toBe(0);
37-
expect<ArrayBuffer>(buff.buffer).not.toBeNull();
37+
expect<ArrayBuffer | null>(buff.buffer).not.toBeNull();
3838
expect<u32>(buff.byteLength).toBe(100);
3939
// TODO: expectFn(() => { Buffer.alloc(-1); }).toThrow();
4040
// TODO: expectFn(() => { Buffer.alloc(BLOCK_MAXSIZE + 1); }).toThrow();
@@ -44,13 +44,12 @@ describe("buffer", () => {
4444
expect<Buffer>(Buffer.allocUnsafe(10)).toBeTruthy();
4545
expect<Buffer>(Buffer.allocUnsafe(10)).toHaveLength(10);
4646
let buff = Buffer.allocUnsafe(100);
47-
expect<ArrayBuffer>(buff.buffer).not.toBeNull();
47+
expect<ArrayBuffer | null>(buff.buffer).not.toBeNull();
4848
expect<u32>(buff.byteLength).toBe(100);
4949
// TODO: expectFn(() => { Buffer.allocUnsafe(-1); }).toThrow();
5050
// TODO: expectFn(() => { Buffer.allocUnsafe(BLOCK_MAXSIZE + 1); }).toThrow();
5151
});
5252

53-
5453
/**
5554
* This specification is a tradeoff, because Buffer.from() takes _many_ parameters.
5655
* Instead, the only common parameter is the first one, which results in Buffer.from
@@ -92,4 +91,66 @@ describe("buffer", () => {
9291
let strArrayActual = Buffer.from<Array<String>>(stringValues);
9392
expect<ArrayBuffer>(strArrayActual.buffer).toStrictEqual(strArrayExpected.buffer);
9493
});
94+
95+
test("#isBuffer", () => {
96+
let a = "";
97+
let b = new Uint8Array(0);
98+
let c = 0;
99+
let d = 1.1;
100+
let e = new Buffer(0);
101+
expect<bool>(Buffer.isBuffer<string>(a)).toBeFalsy();
102+
expect<bool>(Buffer.isBuffer<Uint8Array>(b)).toBeFalsy();
103+
expect<bool>(Buffer.isBuffer<i32>(c)).toBeFalsy();
104+
expect<bool>(Buffer.isBuffer<f64>(d)).toBeFalsy();
105+
expect<bool>(Buffer.isBuffer<Buffer>(e)).toBeTruthy();
106+
// null checks are done by the compiler explicitly at runtime
107+
expect<bool>(Buffer.isBuffer<Buffer | null>(null)).toBeFalsy();
108+
});
109+
110+
test("#readUInt8", () => {
111+
let buff = new Buffer(10);
112+
buff[0] = -2;
113+
buff[9] = 47;
114+
// Testing casting between u8 and i8.
115+
expect<u8>(buff.readUInt8(0)).toBe(254);
116+
expect<u8>(buff.readUInt8()).toBe(254);
117+
// Testing offset
118+
expect<u8>(buff.readUInt8(9)).toBe(47);
119+
// TODO:
120+
// expectFn(() => {
121+
// let newBuff = new Buffer(1);
122+
// newBuff.readUInt8(5);
123+
// }).toThrow();
124+
});
125+
126+
test("#writeUInt8", () => {
127+
let buff = new Buffer(5);
128+
expect<i32>(buff.writeUInt8(4)).toBe(1);
129+
expect<i32>(buff.writeUInt8(252,4)).toBe(5);
130+
expect<u8>(buff[0]).toBe(4);
131+
expect<u8>(buff[4]).toBe(252);
132+
});
133+
134+
test("#writeInt8", () => {
135+
let buff = new Buffer(5);
136+
expect<i32>(buff.writeInt8(9)).toBe(1);
137+
expect<i32>(buff.writeInt8(-3,4)).toBe(5);
138+
expect<i8>(buff[0]).toBe(9);
139+
expect<i8>(buff[4]).toBe(-3);
140+
});
141+
142+
test("#readInt8", () => {
143+
let buff = new Buffer(10);
144+
buff[0] = 5;
145+
buff[9] = 255;
146+
expect<i8>(buff.readInt8(0)).toBe(5);
147+
expect<i8>(buff.readInt8()).toBe(5);
148+
// Testing offset, and casting between u8 and i8.
149+
expect<i8>(buff.readInt8(9)).toBe(-1);
150+
// TODO:
151+
// expectFn(() => {
152+
// let newBuff = new Buffer(1);
153+
// newBuff.readInt8(5);
154+
// }).toThrow();
155+
});
95156
});

tests/node.js

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { TestContext, EmptyReporter } = require("@as-pect/core");
1+
const { TestContext, VerboseReporter } = require("@as-pect/core");
22
const { instantiateBuffer } = require("assemblyscript/lib/loader");
33
const glob = require("glob");
44
const { main } = require("assemblyscript/cli/asc");
@@ -7,7 +7,7 @@ const path = require("path");
77
const fs = require("fs");
88
const Wasi = require("wasi");
99
const wasi = new Wasi({});
10-
const diff = require("diff");
10+
let pass = true;
1111

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

30-
class Reporter extends EmptyReporter {
31-
onGroupFinish(group) {
32-
if (group.name) {
33-
if (group.pass) process.stdout.write("Group : " + group.name + " -> ✔ PASS");
34-
else process.stdout.write("Group : " + group.name + " -> ❌ FAIL");
35-
process.stdout.write("\n");
36-
}
37-
}
38-
39-
onTestFinish(group, test) {
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-
}
47-
48-
if (test.logs.length > 0) {
49-
test.logs.forEach((e, i) => {
50-
if (i > 0) process.stdout.write("\n");
51-
process.stdout.write("Log : " + e.value);
52-
});
53-
process.stdout.write("\n");
54-
}
55-
}
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");
60-
process.stdout.write("\n");
61-
}
62-
}
63-
64-
const reporter = new Reporter();
30+
const reporter = new VerboseReporter();
6531

6632
function relativeFromCwd(location) {
6733
return path.relative(process.cwd(), location);
@@ -139,6 +105,14 @@ for (const file of files) {
139105
}
140106

141107
function runTest(file, type, binary, wat) {
108+
const watPath = path.join(path.dirname(file), path.basename(file, ".ts"))
109+
+ "." + type + ".wat";
110+
111+
// should not block testing
112+
fs.writeFile(watPath, wat, (err) => {
113+
if (err) console.warn(err);
114+
});
115+
142116
const context = new TestContext({
143117
fileName: file,
144118
reporter,
@@ -152,4 +126,8 @@ function runTest(file, type, binary, wat) {
152126
wasi.setMemory(wasm.memory);
153127
wasi.view = new DataView(wasm.memory.buffer);
154128
context.run(wasm);
129+
130+
if (!context.pass) pass = false;
155131
}
132+
133+
process.exit(pass ? 0 : 1);

0 commit comments

Comments
 (0)