Skip to content

Commit 3b10d79

Browse files
authored
Merge pull request #45 from torch2424/entry-file-check
Added a check that the AssemblyScript Wasm Module was build with as-bind
2 parents b6edf7a + 5dc202f commit 3b10d79

File tree

6 files changed

+50
-2
lines changed

6 files changed

+50
-2
lines changed

lib/asbind-instance/asbind-instance.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ export default class AsbindInstance {
8282
// Set our unbound exports
8383
this.unboundExports = unboundExports;
8484

85+
// Ensure that the AS Wasm was built with the as-bind entryfile
86+
if (
87+
!this.unboundExports.__asbind_entryfile_flag ||
88+
this.unboundExports.__asbind_entryfile_flag === 0
89+
) {
90+
throw new Error(
91+
"as-bind: The instantiated AssemblyScript wasm module was not built with the as-bind entryfile. Please see the as-bind documentation (Quick Start), and rebuild your AssemblyScript wasm module."
92+
);
93+
}
94+
8595
// Wrap appropriate the appropriate export functions
8696
this.exports = {};
8797
Object.keys(this.unboundExports).forEach(unboundExportKey => {

lib/assembly/as-bind.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// Neccessary bootstrapping for asbind
22

3+
// Flag for the entryfile check
4+
export const __asbind_entryfile_flag = 1;
5+
36
// Strings
47
export const __asbind_String_ID = idof<String>();
58

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
"lint": "prettier --write **/*.js **/*.ts **/*.json !build/**/* !dist/**/*",
1414
"lint:ci": "prettier --check **/*.js **/*.ts **/*.json !build/**/* !dist/**/*",
1515
"lib:watch": "chokidar --initial \"lib/**/*\" -c \"run-s lib:wasm:build lib:js:build:dev test\"",
16-
"lib:test:watch": "chokidar \"test/**/*.js\" \"test/**/*.ts\" -c \"run-s lib:test:build test\"",
16+
"lib:test:watch": "chokidar \"test/**/*.js\" \"test/**/*.ts\" -c \"run-s lib:test:build lib:test:build test\"",
1717
"lib:wasm:build": "run-s lib:wasm:build:debug lib:wasm:build:optimized lib:wasm:build:cp",
1818
"lib:wasm:build:debug": "asc lib/assembly/as-bind.ts -b dist/as-bind.debug.wasm -t dist/as-bind.debug.wat --sourceMap --debug --runtime full",
1919
"lib:wasm:build:optimized": "asc lib/assembly/as-bind.ts -b dist/as-bind.wasm --sourceMap dist/as-bind.wasm.map -t dist/as-bind.wat --runtime full -O3",
2020
"lib:wasm:build:cp": "cpy 'lib/assembly/**/*' dist",
2121
"lib:js:build": "rollup -c --environment LIB,PROD && cpy 'lib/as-bind.d.ts' dist",
2222
"lib:js:build:dev": "rollup -c --environment LIB,DEV",
23-
"lib:test:build": "asc lib/assembly/as-bind.ts test/assembly/test.ts -b test/assembly/test.wasm --runtime full --debug",
23+
"lib:test:build": "run-s lib:test:build:entry lib:test:build:no-entry",
24+
"lib:test:build:entry": "asc lib/assembly/as-bind.ts test/assembly/test.ts -b test/assembly/test.wasm --runtime full --debug",
25+
"lib:test:build:no-entry": "asc test/assembly/test.ts -b test/assembly/test-no-entry.wasm --runtime full --debug",
2426
"lib:deploy": "npm run build && np",
2527
"md:build": "run-s md:wasm:build md:ts:build md:js:build",
2628
"premd:dev": "npm run md:build",

test/assembly/test-no-entry.wasm

17.5 KB
Binary file not shown.

test/assembly/test.wasm

3.46 KB
Binary file not shown.

test/test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ const assert = require("assert");
33
const { AsBind } = require("../dist/as-bind.cjs");
44

55
const wasmBytes = new Uint8Array(fs.readFileSync("./test/assembly/test.wasm"));
6+
const wasmBytesNoEntry = new Uint8Array(
7+
fs.readFileSync("./test/assembly/test-no-entry.wasm")
8+
);
69

710
describe("asbind", () => {
811
const baseImportObject = {
@@ -36,6 +39,36 @@ describe("asbind", () => {
3639
});
3740

3841
describe("instantiation", () => {
42+
it("should error when instantiating a module that does not include the as-bind entryfile", async () => {
43+
const wasmModule = await WebAssembly.compile(wasmBytesNoEntry);
44+
45+
let didInstantiate = false;
46+
try {
47+
await AsBind.instantiate(wasmModule, baseImportObject);
48+
didInstantiate = true;
49+
} catch (err) {
50+
// Hooray! We caught the base instantiation
51+
console.log("Expected Test Error: ", err.message);
52+
}
53+
if (didInstantiate) {
54+
throw new Error("Was able to instantiate the incorrectly built test");
55+
}
56+
57+
let didInstantiateSync = false;
58+
try {
59+
AsBind.instantiateSync(wasmModule, baseImportObject);
60+
didInstantiateSync = true;
61+
} catch (err) {
62+
// Hooray! We caught the base instantiation
63+
console.log("Expected Test Error: ", err.message);
64+
}
65+
if (didInstantiateSync) {
66+
throw new Error(
67+
"Was able to instantiateSync the incorrectly built test"
68+
);
69+
}
70+
});
71+
3972
it("should instantiate WebAssembly.Module", async () => {
4073
const wasmModule = await WebAssembly.compile(wasmBytes);
4174
asbindInstance = await AsBind.instantiate(wasmModule, baseImportObject);

0 commit comments

Comments
 (0)