Skip to content

Commit 68ce55a

Browse files
Update GH action
1 parent ecffdf0 commit 68ce55a

2 files changed

Lines changed: 37 additions & 13 deletions

File tree

public/generated/automate_packed_runtime.mjs

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,49 @@
1-
import { createWasiImports } from "./automate_packed/host_shim.mjs";
1+
function createImportValue(entry, memoryRef, outputCallback) {
2+
if (entry.kind === "function") {
3+
return () => 0;
4+
}
5+
if (entry.kind === "memory") {
6+
const memory = new WebAssembly.Memory({ initial: 16 });
7+
memoryRef.current = memory;
8+
return memory;
9+
}
10+
if (entry.kind === "table") {
11+
return new WebAssembly.Table({ initial: 0, element: "anyfunc" });
12+
}
13+
if (entry.kind === "global") {
14+
return new WebAssembly.Global({ value: "i32", mutable: true }, 0);
15+
}
16+
outputCallback(`Unsupported WASM import kind: ${entry.kind}`);
17+
return undefined;
18+
}
19+
20+
function createImportObject(module, memoryRef, outputCallback) {
21+
const imports = {};
22+
for (const entry of WebAssembly.Module.imports(module)) {
23+
if (!imports[entry.module]) {
24+
imports[entry.module] = {};
25+
}
26+
imports[entry.module][entry.name] = createImportValue(entry, memoryRef, outputCallback);
27+
}
28+
return imports;
29+
}
230

331
export async function loadAutomaginariumPacked(options = {}) {
432
const wasmUrl = options.wasmUrl || new URL("./automate_packed/module.wasm", import.meta.url);
533
const memoryRef = { current: null };
6-
const imports = {
7-
...createWasiImports(memoryRef, options.outputCallback || (() => {})),
8-
};
34+
const outputCallback = options.outputCallback || (() => {});
935

10-
let instance;
36+
let module;
1137
if (options.bytes) {
12-
const result = await WebAssembly.instantiate(options.bytes, imports);
13-
instance = result.instance;
14-
} else if (typeof WebAssembly.instantiateStreaming === "function") {
15-
const result = await WebAssembly.instantiateStreaming(fetch(wasmUrl), imports);
16-
instance = result.instance;
38+
module = await WebAssembly.compile(options.bytes);
1739
} else {
1840
const response = await fetch(wasmUrl);
1941
const bytes = await response.arrayBuffer();
20-
const result = await WebAssembly.instantiate(bytes, imports);
21-
instance = result.instance;
42+
module = await WebAssembly.compile(bytes);
2243
}
2344

45+
const imports = createImportObject(module, memoryRef, outputCallback);
46+
const instance = await WebAssembly.instantiate(module, imports);
2447
memoryRef.current = instance.exports.memory || memoryRef.current;
2548
return instance.exports;
2649
}

tests/stage5-static.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ function testRuntimeLoaderIsCommitted() {
3232
assert(fs.existsSync(path.join(root, "public/generated/automate_packed_runtime.mjs")));
3333
const loader = read("public/generated/automate_packed_runtime.mjs");
3434
assert(loader.includes("./automate_packed/module.wasm"));
35-
assert(loader.includes("./automate_packed/host_shim.mjs"));
35+
assert(loader.includes("WebAssembly.Module.imports"));
36+
assert(loader.includes("createImportObject"));
3637
}
3738

3839
testPagesEntrypoints();

0 commit comments

Comments
 (0)