|
1 | | -import * as hostShim from "./automate_packed/host_shim.mjs"; |
2 | | - |
3 | | -function createFallbackWasiImports(memoryRef = { current: null }, outputCallback = () => {}) { |
4 | | - const textDecoder = new TextDecoder("utf-8"); |
5 | | - let stdoutBuffer = ""; |
6 | | - const writeOutput = () => { |
7 | | - let newlineIndex; |
8 | | - while ((newlineIndex = stdoutBuffer.indexOf("\n")) !== -1) { |
9 | | - outputCallback(stdoutBuffer.slice(0, newlineIndex)); |
10 | | - stdoutBuffer = stdoutBuffer.slice(newlineIndex + 1); |
11 | | - } |
12 | | - }; |
13 | | - |
14 | | - return { |
15 | | - wasi_snapshot_preview1: { |
16 | | - fd_write(fd, iovsPtr, iovsLen, nwrittenPtr) { |
17 | | - if (fd !== 1 && fd !== 2) return 8; |
18 | | - const memory = memoryRef.current; |
19 | | - if (!memory) return 8; |
20 | | - const view = new DataView(memory.buffer); |
21 | | - let written = 0; |
22 | | - for (let index = 0; index < iovsLen; index += 1) { |
23 | | - const ptr = view.getUint32(iovsPtr + index * 8, true); |
24 | | - const len = view.getUint32(iovsPtr + index * 8 + 4, true); |
25 | | - stdoutBuffer += textDecoder.decode(new Uint8Array(memory.buffer, ptr, len)); |
26 | | - written += len; |
27 | | - } |
28 | | - writeOutput(); |
29 | | - view.setUint32(nwrittenPtr, written, true); |
30 | | - return 0; |
31 | | - }, |
32 | | - fd_read(_fd, _iovsPtr, _iovsLen, nreadPtr) { |
33 | | - const memory = memoryRef.current; |
34 | | - if (memory) { |
35 | | - new DataView(memory.buffer).setUint32(nreadPtr, 0, true); |
36 | | - } |
37 | | - return 0; |
38 | | - }, |
39 | | - args_sizes_get(argcPtr, argvBufSizePtr) { |
40 | | - const memory = memoryRef.current; |
41 | | - if (!memory) return 8; |
42 | | - const view = new DataView(memory.buffer); |
43 | | - view.setUint32(argcPtr, 0, true); |
44 | | - view.setUint32(argvBufSizePtr, 0, true); |
45 | | - return 0; |
46 | | - }, |
47 | | - args_get() { |
48 | | - return 0; |
49 | | - }, |
50 | | - }, |
51 | | - memoryRef, |
52 | | - }; |
53 | | -} |
54 | | - |
55 | | -function createFallbackDomImports(memoryRef = { current: null }, _exportsRef = { current: null }, outputCallback = () => {}) { |
56 | | - const readString = (ptr, len) => { |
57 | | - const memory = memoryRef.current; |
58 | | - if (!memory || ptr <= 0 || len <= 0) return ""; |
59 | | - return new TextDecoder("utf-8").decode(new Uint8Array(memory.buffer, ptr, len)); |
60 | | - }; |
61 | | - |
62 | | - return { |
63 | | - env: { |
64 | | - print_str(ptr, len) { |
65 | | - outputCallback(readString(ptr, len)); |
66 | | - }, |
67 | | - print_num(value) { |
68 | | - outputCallback(String(value)); |
69 | | - }, |
70 | | - print(value) { |
71 | | - outputCallback(String(value)); |
72 | | - }, |
73 | | - }, |
74 | | - }; |
75 | | -} |
76 | | - |
77 | | -function createDefaultImport(entry) { |
78 | | - if (entry.kind === "function") { |
79 | | - return () => 0; |
80 | | - } |
81 | | - if (entry.kind === "memory") { |
82 | | - return new WebAssembly.Memory({ initial: 16 }); |
83 | | - } |
84 | | - if (entry.kind === "table") { |
85 | | - return new WebAssembly.Table({ initial: 0, element: "anyfunc" }); |
86 | | - } |
87 | | - if (entry.kind === "global") { |
88 | | - return new WebAssembly.Global({ value: "i32", mutable: true }, 0); |
89 | | - } |
90 | | - return undefined; |
91 | | -} |
92 | | - |
93 | | -function completeImports(module, imports) { |
94 | | - for (const entry of WebAssembly.Module.imports(module)) { |
95 | | - if (!imports[entry.module]) { |
96 | | - imports[entry.module] = {}; |
97 | | - } |
98 | | - if (imports[entry.module][entry.name] === undefined) { |
99 | | - imports[entry.module][entry.name] = createDefaultImport(entry); |
100 | | - } |
101 | | - } |
102 | | - return imports; |
103 | | -} |
| 1 | +import { createDomImports, createWasiImports } from "./automate_packed/host_shim.mjs"; |
104 | 2 |
|
105 | 3 | export async function loadAutomaginariumPacked(options = {}) { |
106 | 4 | const wasmUrl = options.wasmUrl || new URL("./automate_packed/module.wasm", import.meta.url); |
107 | 5 | const memoryRef = { current: null }; |
108 | 6 | const exportsRef = { current: null }; |
109 | | - const createWasiImports = hostShim.createWasiImports || createFallbackWasiImports; |
110 | | - const createDomImports = hostShim.createDomImports || createFallbackDomImports; |
111 | | - const baseImports = { |
| 7 | + const imports = { |
112 | 8 | ...createWasiImports(memoryRef, options.outputCallback || (() => {})), |
113 | | - ...createDomImports(memoryRef, exportsRef, options.outputCallback || (() => {})), |
| 9 | + ...createDomImports(memoryRef, exportsRef), |
114 | 10 | }; |
115 | 11 |
|
116 | | - let module; |
| 12 | + let instance; |
117 | 13 | if (options.bytes) { |
118 | | - module = await WebAssembly.compile(options.bytes); |
| 14 | + const result = await WebAssembly.instantiate(options.bytes, imports); |
| 15 | + instance = result.instance; |
| 16 | + } else if (typeof WebAssembly.instantiateStreaming === "function") { |
| 17 | + const result = await WebAssembly.instantiateStreaming(fetch(wasmUrl), imports); |
| 18 | + instance = result.instance; |
119 | 19 | } else { |
120 | 20 | const response = await fetch(wasmUrl); |
121 | 21 | const bytes = await response.arrayBuffer(); |
122 | | - module = await WebAssembly.compile(bytes); |
| 22 | + const result = await WebAssembly.instantiate(bytes, imports); |
| 23 | + instance = result.instance; |
123 | 24 | } |
124 | 25 |
|
125 | | - const imports = completeImports(module, baseImports); |
126 | | - const instance = await WebAssembly.instantiate(module, imports); |
127 | 26 | exportsRef.current = instance.exports; |
128 | 27 | memoryRef.current = instance.exports.memory || memoryRef.current; |
129 | 28 | return instance.exports; |
|
0 commit comments