Skip to content

Commit 315ad51

Browse files
committed
Add comments and cleanup
1 parent aef51b5 commit 315ad51

File tree

9 files changed

+82
-32
lines changed

9 files changed

+82
-32
lines changed

lib/asbind-instance/asbind-instance.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export default class AsbindInstance {
6868
}
6969
if (
7070
!WebAssembly.Module.exports(this.module).find(
71-
exp => exp.name === "__asbind_entryfile_flag"
71+
exp => exp.name === "__asbind_String_ID"
7272
)
7373
) {
7474
throw new Error(

lib/asbind-instance/bind-function.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ function putString(asbindInstance, value, typeName) {
2929
return asbindInstance.exports.__newString(value);
3030
}
3131

32-
function getArrayBuffer(asbindInstance, value, typeName) {
33-
return asbindInstance.exports.__getArray(value);
34-
}
35-
3632
function getArrayBufferView(asbindInstance, value, typeName) {
3733
return asbindInstance.exports[
3834
`__get${normalizeArrayBufferViewTypeName(typeName)}View`
@@ -53,7 +49,6 @@ const ascToJsConverters = new Map([
5349
[/(i|u)(8|16|32)/, nop],
5450
[/f(32|64)/, nop],
5551
[/[sS]tring/, getString],
56-
[/ArrayBuffer/, getArrayBuffer],
5752
[/(Ui|I)nt(8|16|32)Array/, getArrayBufferView],
5853
[/Big(Ui|I)nt64Array/, getArrayBufferView],
5954
[/Uint8ClampedArray/, getArrayBufferView],
@@ -74,7 +69,6 @@ const jsToAscConverters = new Map([
7469
[/(i|u)(8|16|32)/, nop],
7570
[/f(32|64)/, nop],
7671
[/[sS]tring/, putString],
77-
[/ArrayBuffer/, putArrayBuffer],
7872
[/(Ui|I)nt(8|16|32)Array/, putArrayBuffer],
7973
[/Big(Ui|I)nt64Array/, putArrayBuffer],
8074
[/Uint8ClampedArray/, putArrayBuffer],

test/test-runner.html

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8" />
3+
<title>Mocha Tests</title>
4+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
5+
<link rel="stylesheet" href="/node_modules/mocha/mocha.css" />
6+
<div id="mocha"></div>
7+
8+
<script src="/node_modules/mocha/mocha.js"></script>
9+
<script src="/dist/as-bind.iife.js"></script>
10+
<script>
11+
self.suitePaths = [];
12+
function runScript(src) {
13+
return new Promise(resolve => {
14+
const script = document.createElement("script");
15+
script.src = src;
16+
script.onload = resolve;
17+
document.body.append(script);
18+
});
19+
}
20+
function runInlineScript(src) {
21+
const script = document.createElement("script");
22+
script.innerHTML = src;
23+
document.body.append(script);
24+
}
25+
26+
self.AsBind = AsBindIIFE;
27+
self.assert = console.assert.bind(console);
28+
mocha.setup({
29+
ui: "bdd",
30+
reporter: "json"
31+
});
32+
mocha.rootHooks({
33+
async beforeEach() {
34+
// This is ugly. The Mocha Context (`this`) knows the `currentTest`, but not the *path*
35+
// of the current test suite, which we need to load the corresponding .wasm file.
36+
// So I added a bit of code that pushes the suite paths into the `suitePaths` array in the same
37+
// order as they are run.
38+
const relativeTestPath =
39+
suitePaths[
40+
this.currentTest.parent.parent.suites.indexOf(this.currentTest.parent)
41+
];
42+
const testPath = new URL(relativeTestPath, location.href);
43+
const wasmPath = new URL("./asc.wasm", testPath);
44+
this.rawModule = await fetch(wasmPath);
45+
}
46+
});
47+
</script>

test/test-runner.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,16 @@ const asc = require("assemblyscript/cli/asc");
1010

1111
globalThis.AsBind = require("../dist/as-bind.cjs.js");
1212

13-
// Just used for syntax highlighting
14-
const html = String.raw;
15-
1613
async function main() {
1714
process.chdir(__dirname);
1815
await asc.ready;
1916

2017
await compileAllAsc();
2118

22-
if ((await runTestsInNode()) > 0) {
19+
if ((await getNumFailingTestsInNode()) > 0) {
2320
process.exit(1);
2421
}
25-
if ((await runTestsInPuppeteer()) > 0) {
22+
if ((await getNumFailingTestsInPuppeteer()) > 0) {
2623
process.exit(1);
2724
}
2825
console.log("Passed node and browser tests");
@@ -36,6 +33,8 @@ async function compileAllAsc() {
3633
for (const ascFile of ascFiles) {
3734
console.log(`Compiling ${ascFile}...`);
3835
await asc.main([
36+
"--runtime",
37+
"stub",
3938
"--exportRuntime",
4039
"--transform",
4140
transformFile,
@@ -46,7 +45,7 @@ async function compileAllAsc() {
4645
}
4746
}
4847

49-
async function runTestsInNode() {
48+
async function getNumFailingTestsInNode() {
5049
const mocha = new Mocha();
5150

5251
const testFiles = await glob("./tests/**/test.js");
@@ -71,7 +70,7 @@ async function runMochaAsync(mocha) {
7170
return new Promise(resolve => mocha.run(resolve));
7271
}
7372

74-
async function extractMochaStatDump(msg) {
73+
async function maybeExtractMochaStatsDump(msg) {
7574
if (msg.args().length == 1 && msg.text().startsWith("{")) {
7675
const arg = await msg.args()[0].jsonValue();
7776
let obj;
@@ -86,28 +85,31 @@ async function extractMochaStatDump(msg) {
8685
}
8786
}
8887

89-
const PORT = 50123;
90-
const OPEN_DEVTOOLS = false;
91-
async function runTestsInPuppeteer() {
88+
const PORT = process.env.PORT ?? 50123;
89+
const OPEN_DEVTOOLS = !!process.env.OPEN_DEVTOOLS;
90+
91+
async function getNumFailingTestsInPuppeteer() {
9292
const testFiles = await glob("./tests/**/test.js");
9393
const browser = await pptr.launch({
9494
devtools: OPEN_DEVTOOLS
9595
});
9696
const page = await browser.newPage();
97+
98+
// Mocha’s JSON reporter doesn’t really give you access to the JSON report,
99+
// ironically. So I have to intercept console.log()s and detect which
100+
// one is the JSON resport string. `result` will contain the parsed JSON.
97101
let result;
98102
page.on("console", async msg => {
99-
const maybeResult = await extractMochaStatDump(msg);
103+
const maybeResult = await maybeExtractMochaStatsDump(msg);
100104
if (maybeResult) {
101105
result = maybeResult;
102106
return;
103107
}
104-
// Otherwise you can forward the log while debugging
105-
// console.log("Browser log:", msg.text());
106108
});
107109

108110
if (OPEN_DEVTOOLS) {
109-
// If we want DevTools open, wait for a second here so it can load
110-
// and `debugger` statements are effective.
111+
// If we want DevTools open, wait for a second here so DevTools can load.
112+
// Otherwise we might run past `debugger` statements.
111113
await new Promise(resolve => setTimeout(resolve, 1000));
112114
}
113115
const app = Express();
@@ -116,12 +118,16 @@ async function runTestsInPuppeteer() {
116118
await page.goto(`http://localhost:${PORT}/test/test-runner.html`);
117119
const numFailures = await page.evaluate(async testFiles => {
118120
for (const testFile of testFiles) {
121+
// Register the test
119122
await runScript(`/test/${testFile}`);
123+
// Save the test’s path. See `test-runner.html` for an explanation.
120124
runInlineScript(`
121125
suitePaths.push(${JSON.stringify(testFile)});
122126
`);
123127
}
124128
const script = document.createElement("script");
129+
// Create a promise that resolves once mocha is done running.
130+
// This way we can block this `evaluate` call until mocha is done.
125131
script.innerHTML = `
126132
self.mochaRun = new Promise(resolve => mocha.run(resolve));`;
127133
document.body.append(script);

test/tests/arraybufferview/asc.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
export function exported(a: Uint8Array, b: Uint8Array): Uint8Array {
2-
const intermediate = imported(a, b);
1+
export function swapAndPad(a: Uint8Array, b: Uint8Array): Uint8Array {
2+
const intermediate = swappedConcat(a, b);
33
const result = new Uint8Array(intermediate.length + 2);
44
result.set(intermediate, 1);
55
result[0] = 255;
66
result[result.length - 1] = 255;
77
return result;
88
}
99

10-
declare function imported(a: Uint8Array, b: Uint8Array): Uint8Array;
10+
declare function swappedConcat(a: Uint8Array, b: Uint8Array): Uint8Array;

test/tests/arraybufferview/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ describe("as-bind", function() {
22
it("should handle Uint8Arrays", async function() {
33
const instance = await AsBind.instantiate(this.rawModule, {
44
asc: {
5-
imported(a, b) {
5+
swappedConcat(a, b) {
66
const result = new Uint8Array(a.length + b.length);
77
result.set(b, 0);
88
result.set(a, b.length);
@@ -12,7 +12,7 @@ describe("as-bind", function() {
1212
});
1313
assert(
1414
instance.exports
15-
.exported(new Uint8Array([1, 2, 3]), new Uint8Array([10, 11, 12]))
15+
.swapAndPad(new Uint8Array([1, 2, 3]), new Uint8Array([10, 11, 12]))
1616
.join(",") === new Uint8Array([255, 10, 11, 12, 1, 2, 3, 255]).join(",")
1717
);
1818
});

test/tests/strings/asc.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export function exported(a: string, b: string): string {
2-
return "!" + imported(a, b) + "!";
1+
export function swapAndPad(a: string, b: string): string {
2+
return "!" + swappedConcat(a, b) + "!";
33
}
44

5-
declare function imported(a: string, b: string): string;
5+
declare function swappedConcat(a: string, b: string): string;

test/tests/strings/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ describe("as-bind", function() {
22
it("should handle strings", async function() {
33
const instance = await AsBind.instantiate(this.rawModule, {
44
asc: {
5-
imported(a, b) {
5+
swappedConcat(a, b) {
66
return b + a;
77
}
88
}
99
});
10-
assert(instance.exports.exported("a", "b") === "!ba!");
10+
assert(instance.exports.swapAndPad("a", "b") === "!ba!");
1111
});
1212
});

test/tests/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../node_modules/assemblyscript/std/assembly.json"
3+
}

0 commit comments

Comments
 (0)