Add shared memory support to entrypoint scripts (#486)

* Add shared memory support

* Update wasm-imports-parser to fix buffer source issue
This commit is contained in:
Yuta Saito 2024-06-14 01:10:48 +09:00 committed by GitHub
parent 1b7daf2c92
commit 078b3520a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 44 additions and 10 deletions

File diff suppressed because one or more lines are too long

View File

@ -14,6 +14,12 @@
import { WASI, File, OpenFile, ConsoleStdout, PreopenDirectory } from "@bjorn3/browser_wasi_shim";
import type { SwiftRuntime, SwiftRuntimeConstructor } from "./JavaScriptKit_JavaScriptKit.resources/Runtime";
import { polyfill as polyfillWebAssemblyTypeReflection } from "wasm-imports-parser/polyfill";
import type { ImportEntry } from "wasm-imports-parser";
// Apply polyfill for WebAssembly Type Reflection JS API to inspect imported memory info.
// https://github.com/WebAssembly/js-types/blob/main/proposals/js-types/Overview.md
globalThis.WebAssembly = polyfillWebAssemblyTypeReflection(globalThis.WebAssembly);
export class LineDecoder {
constructor(onLine: (line: string) => void) {
@ -111,14 +117,28 @@ export const WasmRunner = (rawOptions: Options, SwiftRuntime: SwiftRuntimeConstr
}
}
for (const importEntry of WebAssembly.Module.imports(module)) {
for (const _importEntry of WebAssembly.Module.imports(module)) {
const importEntry = _importEntry as ImportEntry;
if (!importObject[importEntry.module]) {
importObject[importEntry.module] = {};
}
if (importEntry.kind == "function" && !importObject[importEntry.module][importEntry.name]) {
// Skip if the import is already provided
if (importObject[importEntry.module][importEntry.name]) {
continue;
}
if (importEntry.kind == "function") {
importObject[importEntry.module][importEntry.name] = () => {
throw new Error(`Imported function ${importEntry.module}.${importEntry.name} not implemented`);
}
} else if (importEntry.kind == "memory" && importEntry.module == "env" && importEntry.name == "memory") {
// Create a new WebAssembly.Memory instance with the same descriptor as the imported memory
const type = importEntry.type
const descriptor: WebAssembly.MemoryDescriptor = {
initial: type.minimum,
maximum: type.maximum,
shared: type.shared,
}
importObject[importEntry.module][importEntry.name] = new WebAssembly.Memory(descriptor);
}
}

17
package-lock.json generated
View File

@ -15,7 +15,8 @@
"esbuild": "^0.14.38",
"npm-run-all": "^4.1.5",
"reconnecting-websocket": "^4.4.0",
"typescript": "^5.4.5"
"typescript": "^5.4.5",
"wasm-imports-parser": "^1.0.2"
}
},
"node_modules/@bjorn3/browser_wasi_shim": {
@ -1280,6 +1281,12 @@
"spdx-expression-parse": "^3.0.0"
}
},
"node_modules/wasm-imports-parser": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wasm-imports-parser/-/wasm-imports-parser-1.0.2.tgz",
"integrity": "sha512-ujgghmN0M5oFT7ZWAmgt102rh5SIHpMMnZxB07zG2yUGfrC38voOkfcmKxS5pX2VcrkD8xNI0kZg70eFeZawWw==",
"dev": true
},
"node_modules/which": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz",
@ -2160,6 +2167,12 @@
"spdx-expression-parse": "^3.0.0"
}
},
"wasm-imports-parser": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wasm-imports-parser/-/wasm-imports-parser-1.0.2.tgz",
"integrity": "sha512-ujgghmN0M5oFT7ZWAmgt102rh5SIHpMMnZxB07zG2yUGfrC38voOkfcmKxS5pX2VcrkD8xNI0kZg70eFeZawWw==",
"dev": true
},
"which": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz",
@ -2183,4 +2196,4 @@
}
}
}
}
}

View File

@ -27,6 +27,7 @@
"esbuild": "^0.14.38",
"npm-run-all": "^4.1.5",
"reconnecting-websocket": "^4.4.0",
"typescript": "^5.4.5"
"typescript": "^5.4.5",
"wasm-imports-parser": "1.0.2"
}
}
}