Skip to content

Commit

Permalink
Add shared memory support to entrypoint scripts (#486)
Browse files Browse the repository at this point in the history
* Add shared memory support

* Update wasm-imports-parser to fix buffer source issue
  • Loading branch information
kateinoigakukun committed Jun 13, 2024
1 parent 1b7daf2 commit 078b352
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
8 changes: 4 additions & 4 deletions Sources/CartonKit/Server/StaticArchive.swift

Large diffs are not rendered by default.

24 changes: 22 additions & 2 deletions entrypoint/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}

Expand Down
17 changes: 15 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}

0 comments on commit 078b352

Please sign in to comment.