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:
parent
1b7daf2c92
commit
078b3520a6
File diff suppressed because one or more lines are too long
|
@ -14,6 +14,12 @@
|
||||||
|
|
||||||
import { WASI, File, OpenFile, ConsoleStdout, PreopenDirectory } from "@bjorn3/browser_wasi_shim";
|
import { WASI, File, OpenFile, ConsoleStdout, PreopenDirectory } from "@bjorn3/browser_wasi_shim";
|
||||||
import type { SwiftRuntime, SwiftRuntimeConstructor } from "./JavaScriptKit_JavaScriptKit.resources/Runtime";
|
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 {
|
export class LineDecoder {
|
||||||
constructor(onLine: (line: string) => void) {
|
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]) {
|
if (!importObject[importEntry.module]) {
|
||||||
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] = () => {
|
importObject[importEntry.module][importEntry.name] = () => {
|
||||||
throw new Error(`Imported function ${importEntry.module}.${importEntry.name} not implemented`);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,8 @@
|
||||||
"esbuild": "^0.14.38",
|
"esbuild": "^0.14.38",
|
||||||
"npm-run-all": "^4.1.5",
|
"npm-run-all": "^4.1.5",
|
||||||
"reconnecting-websocket": "^4.4.0",
|
"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": {
|
"node_modules/@bjorn3/browser_wasi_shim": {
|
||||||
|
@ -1280,6 +1281,12 @@
|
||||||
"spdx-expression-parse": "^3.0.0"
|
"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": {
|
"node_modules/which": {
|
||||||
"version": "1.3.1",
|
"version": "1.3.1",
|
||||||
"resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz",
|
"resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz",
|
||||||
|
@ -2160,6 +2167,12 @@
|
||||||
"spdx-expression-parse": "^3.0.0"
|
"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": {
|
"which": {
|
||||||
"version": "1.3.1",
|
"version": "1.3.1",
|
||||||
"resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz",
|
"resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz",
|
||||||
|
@ -2183,4 +2196,4 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
"esbuild": "^0.14.38",
|
"esbuild": "^0.14.38",
|
||||||
"npm-run-all": "^4.1.5",
|
"npm-run-all": "^4.1.5",
|
||||||
"reconnecting-websocket": "^4.4.0",
|
"reconnecting-websocket": "^4.4.0",
|
||||||
"typescript": "^5.4.5"
|
"typescript": "^5.4.5",
|
||||||
|
"wasm-imports-parser": "1.0.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue