Skip to content

Commit

Permalink
Add comments and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
surma committed Mar 14, 2021
1 parent aef51b5 commit 315ad51
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/asbind-instance/asbind-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class AsbindInstance {
}
if (
!WebAssembly.Module.exports(this.module).find(
exp => exp.name === "__asbind_entryfile_flag"
exp => exp.name === "__asbind_String_ID"
)
) {
throw new Error(
Expand Down
6 changes: 0 additions & 6 deletions lib/asbind-instance/bind-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ function putString(asbindInstance, value, typeName) {
return asbindInstance.exports.__newString(value);
}

function getArrayBuffer(asbindInstance, value, typeName) {
return asbindInstance.exports.__getArray(value);
}

function getArrayBufferView(asbindInstance, value, typeName) {
return asbindInstance.exports[
`__get${normalizeArrayBufferViewTypeName(typeName)}View`
Expand All @@ -53,7 +49,6 @@ const ascToJsConverters = new Map([
[/(i|u)(8|16|32)/, nop],
[/f(32|64)/, nop],
[/[sS]tring/, getString],
[/ArrayBuffer/, getArrayBuffer],
[/(Ui|I)nt(8|16|32)Array/, getArrayBufferView],
[/Big(Ui|I)nt64Array/, getArrayBufferView],
[/Uint8ClampedArray/, getArrayBufferView],
Expand All @@ -74,7 +69,6 @@ const jsToAscConverters = new Map([
[/(i|u)(8|16|32)/, nop],
[/f(32|64)/, nop],
[/[sS]tring/, putString],
[/ArrayBuffer/, putArrayBuffer],
[/(Ui|I)nt(8|16|32)Array/, putArrayBuffer],
[/Big(Ui|I)nt64Array/, putArrayBuffer],
[/Uint8ClampedArray/, putArrayBuffer],
Expand Down
47 changes: 47 additions & 0 deletions test/test-runner.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!DOCTYPE html>
<meta charset="utf-8" />
<title>Mocha Tests</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="/node_modules/mocha/mocha.css" />
<div id="mocha"></div>

<script src="/node_modules/mocha/mocha.js"></script>
<script src="/dist/as-bind.iife.js"></script>
<script>
self.suitePaths = [];
function runScript(src) {
return new Promise(resolve => {
const script = document.createElement("script");
script.src = src;
script.onload = resolve;
document.body.append(script);
});
}
function runInlineScript(src) {
const script = document.createElement("script");
script.innerHTML = src;
document.body.append(script);
}

self.AsBind = AsBindIIFE;
self.assert = console.assert.bind(console);
mocha.setup({
ui: "bdd",
reporter: "json"
});
mocha.rootHooks({
async beforeEach() {
// This is ugly. The Mocha Context (`this`) knows the `currentTest`, but not the *path*
// of the current test suite, which we need to load the corresponding .wasm file.
// So I added a bit of code that pushes the suite paths into the `suitePaths` array in the same
// order as they are run.
const relativeTestPath =
suitePaths[
this.currentTest.parent.parent.suites.indexOf(this.currentTest.parent)
];
const testPath = new URL(relativeTestPath, location.href);
const wasmPath = new URL("./asc.wasm", testPath);
this.rawModule = await fetch(wasmPath);
}
});
</script>
36 changes: 21 additions & 15 deletions test/test-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@ const asc = require("assemblyscript/cli/asc");

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

// Just used for syntax highlighting
const html = String.raw;

async function main() {
process.chdir(__dirname);
await asc.ready;

await compileAllAsc();

if ((await runTestsInNode()) > 0) {
if ((await getNumFailingTestsInNode()) > 0) {
process.exit(1);
}
if ((await runTestsInPuppeteer()) > 0) {
if ((await getNumFailingTestsInPuppeteer()) > 0) {
process.exit(1);
}
console.log("Passed node and browser tests");
Expand All @@ -36,6 +33,8 @@ async function compileAllAsc() {
for (const ascFile of ascFiles) {
console.log(`Compiling ${ascFile}...`);
await asc.main([
"--runtime",
"stub",
"--exportRuntime",
"--transform",
transformFile,
Expand All @@ -46,7 +45,7 @@ async function compileAllAsc() {
}
}

async function runTestsInNode() {
async function getNumFailingTestsInNode() {
const mocha = new Mocha();

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

async function extractMochaStatDump(msg) {
async function maybeExtractMochaStatsDump(msg) {
if (msg.args().length == 1 && msg.text().startsWith("{")) {
const arg = await msg.args()[0].jsonValue();
let obj;
Expand All @@ -86,28 +85,31 @@ async function extractMochaStatDump(msg) {
}
}

const PORT = 50123;
const OPEN_DEVTOOLS = false;
async function runTestsInPuppeteer() {
const PORT = process.env.PORT ?? 50123;
const OPEN_DEVTOOLS = !!process.env.OPEN_DEVTOOLS;

async function getNumFailingTestsInPuppeteer() {
const testFiles = await glob("./tests/**/test.js");
const browser = await pptr.launch({
devtools: OPEN_DEVTOOLS
});
const page = await browser.newPage();

// Mocha鈥檚 JSON reporter doesn鈥檛 really give you access to the JSON report,
// ironically. So I have to intercept console.log()s and detect which
// one is the JSON resport string. `result` will contain the parsed JSON.
let result;
page.on("console", async msg => {
const maybeResult = await extractMochaStatDump(msg);
const maybeResult = await maybeExtractMochaStatsDump(msg);
if (maybeResult) {
result = maybeResult;
return;
}
// Otherwise you can forward the log while debugging
// console.log("Browser log:", msg.text());
});

if (OPEN_DEVTOOLS) {
// If we want DevTools open, wait for a second here so it can load
// and `debugger` statements are effective.
// If we want DevTools open, wait for a second here so DevTools can load.
// Otherwise we might run past `debugger` statements.
await new Promise(resolve => setTimeout(resolve, 1000));
}
const app = Express();
Expand All @@ -116,12 +118,16 @@ async function runTestsInPuppeteer() {
await page.goto(`http://localhost:${PORT}/test/test-runner.html`);
const numFailures = await page.evaluate(async testFiles => {
for (const testFile of testFiles) {
// Register the test
await runScript(`/test/${testFile}`);
// Save the test鈥檚 path. See `test-runner.html` for an explanation.
runInlineScript(`
suitePaths.push(${JSON.stringify(testFile)});
`);
}
const script = document.createElement("script");
// Create a promise that resolves once mocha is done running.
// This way we can block this `evaluate` call until mocha is done.
script.innerHTML = `
self.mochaRun = new Promise(resolve => mocha.run(resolve));`;
document.body.append(script);
Expand Down
6 changes: 3 additions & 3 deletions test/tests/arraybufferview/asc.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export function exported(a: Uint8Array, b: Uint8Array): Uint8Array {
const intermediate = imported(a, b);
export function swapAndPad(a: Uint8Array, b: Uint8Array): Uint8Array {
const intermediate = swappedConcat(a, b);
const result = new Uint8Array(intermediate.length + 2);
result.set(intermediate, 1);
result[0] = 255;
result[result.length - 1] = 255;
return result;
}

declare function imported(a: Uint8Array, b: Uint8Array): Uint8Array;
declare function swappedConcat(a: Uint8Array, b: Uint8Array): Uint8Array;
4 changes: 2 additions & 2 deletions test/tests/arraybufferview/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ describe("as-bind", function() {
it("should handle Uint8Arrays", async function() {
const instance = await AsBind.instantiate(this.rawModule, {
asc: {
imported(a, b) {
swappedConcat(a, b) {
const result = new Uint8Array(a.length + b.length);
result.set(b, 0);
result.set(a, b.length);
Expand All @@ -12,7 +12,7 @@ describe("as-bind", function() {
});
assert(
instance.exports
.exported(new Uint8Array([1, 2, 3]), new Uint8Array([10, 11, 12]))
.swapAndPad(new Uint8Array([1, 2, 3]), new Uint8Array([10, 11, 12]))
.join(",") === new Uint8Array([255, 10, 11, 12, 1, 2, 3, 255]).join(",")
);
});
Expand Down
6 changes: 3 additions & 3 deletions test/tests/strings/asc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function exported(a: string, b: string): string {
return "!" + imported(a, b) + "!";
export function swapAndPad(a: string, b: string): string {
return "!" + swappedConcat(a, b) + "!";
}

declare function imported(a: string, b: string): string;
declare function swappedConcat(a: string, b: string): string;
4 changes: 2 additions & 2 deletions test/tests/strings/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ describe("as-bind", function() {
it("should handle strings", async function() {
const instance = await AsBind.instantiate(this.rawModule, {
asc: {
imported(a, b) {
swappedConcat(a, b) {
return b + a;
}
}
});
assert(instance.exports.exported("a", "b") === "!ba!");
assert(instance.exports.swapAndPad("a", "b") === "!ba!");
});
});
3 changes: 3 additions & 0 deletions test/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../node_modules/assemblyscript/std/assembly.json"
}

0 comments on commit 315ad51

Please sign in to comment.