Skip to content

Commit a6f1dcb

Browse files
committed
Test harness running in node
1 parent 527ca58 commit a6f1dcb

File tree

8 files changed

+59
-16
lines changed

8 files changed

+59
-16
lines changed

lib/asbind-instance/asbind-instance.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ async function compileStreaming(source) {
3030
if (WebAssembly.compileStreaming) {
3131
return WebAssembly.compileStreaming(source);
3232
}
33-
source = await Promise.resolve(souce);
34-
if (source instanceof Response) {
33+
source = await Promise.resolve(source);
34+
if (typeof Response === "object" && source instanceof Response) {
3535
source = await source.arrayBuffer();
3636
}
3737
return WebAssembly.compile(source);

test/.gitignore

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

test/test-runner.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
const asc = require("assemblyscript/cli/asc");
21
const { promisify } = require("util");
3-
const glob = promisify(require("glob"));
2+
const fs = require("fs/promises");
3+
44
const Mocha = require("mocha");
5+
const assert = require("assert");
6+
const glob = promisify(require("glob"));
7+
8+
const asc = require("assemblyscript/cli/asc");
9+
10+
globalThis.AsBind = require("../dist/as-bind.cjs.js");
511

612
async function main() {
713
await asc.ready;
@@ -31,20 +37,29 @@ async function compileAllAsc() {
3137
async function runTestsInNode() {
3238
const mocha = new Mocha();
3339

34-
mocha.globalSetup(() => {
35-
console.log("GLOBAL SETUP");
36-
});
37-
3840
const testFiles = await glob("./tests/**/test.js");
3941
for (const testFile of testFiles) {
4042
mocha.addFile(testFile);
4143
}
4244

45+
mocha.globalSetup(() => {
46+
this.assert = assert;
47+
});
48+
49+
mocha.rootHooks({
50+
async beforeEach() {
51+
const { file } = this.currentTest;
52+
const wasmFile = file.replace(/test\.js$/, "asc.wasm");
53+
this.rawModule = await fs.readFile(wasmFile);
54+
}
55+
});
56+
4357
const failures = await runMochaAsync(mocha);
4458
console.log({ failures });
4559
}
4660

47-
function runMochaAsync(mocha) {
61+
async function runMochaAsync(mocha) {
62+
await mocha.loadFilesAsync();
4863
return new Promise(resolve => {
4964
const runner = mocha.run(resolve);
5065
});

test/tests/arraybufferview/asc.wasm

-11 KB
Binary file not shown.

test/tests/arraybufferview/test.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
describe("as-bind", function() {
2-
it("should handle ArrayBufferViews", function(done) {
3-
console.log("AAAH2");
4-
done("NAH2");
2+
it("should handle Uint8Arrays", async function() {
3+
const instance = await AsBind.instantiate(this.rawModule, {
4+
asc: {
5+
imported(a, b) {
6+
const result = new Uint8Array(a.length + b.length);
7+
result.set(b, 0);
8+
result.set(a, b.length);
9+
return result;
10+
}
11+
}
12+
});
13+
assert.strictEqual(
14+
instance.exports
15+
.exported(new Uint8Array([1, 2, 3]), new Uint8Array([10, 11, 12]))
16+
.join(","),
17+
new Uint8Array([255, 10, 11, 12, 1, 2, 3, 255]).join(",")
18+
);
519
});
620
});

test/tests/strings/asc.wasm

-10.4 KB
Binary file not shown.

test/tests/strings/test.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
describe("as-bind", function() {
2-
it("should handle strings", function(done) {
3-
console.log("AAAH");
4-
done("NAH");
2+
it("should handle strings", async function() {
3+
const instance = await AsBind.instantiate(this.rawModule, {
4+
asc: {
5+
imported(a, b) {
6+
return b + a;
7+
}
8+
}
9+
});
10+
assert.strictEqual(instance.exports.exported("a", "b"), "!ba!");
511
});
612
});

transform.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,14 @@ class AsBindTransform extends Transform {
6161
);
6262
const importedFunctions = {};
6363
for (const importedFunction of flatImportedFunctions) {
64-
const moduleName = containingModule(importedFunction).internalName;
64+
// To know under what module name an imported function will be expected,
65+
// we have to find the containing module of the given function, take the
66+
// internal name (which is effectively the file path without extension)
67+
// and only take the part after the last `/`
68+
// (i.e. the file name without extension).
69+
const moduleName = containingModule(importedFunction)
70+
.internalName.split("/")
71+
.slice(-1)[0];
6572
if (!importedFunctions.hasOwnProperty(moduleName)) {
6673
importedFunctions[moduleName] = {};
6774
}

0 commit comments

Comments
 (0)