From 2237dd3d215848e11529b1b29ca8e9062093aacb Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Tue, 12 Sep 2023 18:42:46 +0800 Subject: [PATCH 01/13] Add a version of wasmer.sh --- examples/wasmer.sh/.gitignore | 1 + examples/wasmer.sh/index.html | 15 +++++++++ examples/wasmer.sh/index.ts | 60 +++++++++++++++++++++++++++++++++ examples/wasmer.sh/package.json | 18 ++++++++++ lib.ts | 2 ++ package.json | 7 ++-- 6 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 examples/wasmer.sh/.gitignore create mode 100644 examples/wasmer.sh/index.html create mode 100644 examples/wasmer.sh/index.ts create mode 100644 examples/wasmer.sh/package.json diff --git a/examples/wasmer.sh/.gitignore b/examples/wasmer.sh/.gitignore new file mode 100644 index 00000000..95d00675 --- /dev/null +++ b/examples/wasmer.sh/.gitignore @@ -0,0 +1 @@ +.parcel-cache/ diff --git a/examples/wasmer.sh/index.html b/examples/wasmer.sh/index.html new file mode 100644 index 00000000..60a4ab65 --- /dev/null +++ b/examples/wasmer.sh/index.html @@ -0,0 +1,15 @@ + + + + + + + Python REPL + + + +
+ + + + diff --git a/examples/wasmer.sh/index.ts b/examples/wasmer.sh/index.ts new file mode 100644 index 00000000..58ae5b2d --- /dev/null +++ b/examples/wasmer.sh/index.ts @@ -0,0 +1,60 @@ +import { Terminal } from "xterm"; +import init, { SpawnConfig, Wasmer } from "@wasmer/wasix"; + +const encoder = new TextEncoder(); + +async function main() { + const packageName = "sharrattj/bash"; + const args: string[] = []; + const uses: string[] = []; + + const term = new Terminal(); + + const element = document.getElementById("app")!; + term.open(element); + + term.writeln("Starting..."); + + await init(); + + const wasmer = new Wasmer(); + + while (true) { + await runInstance(term, wasmer, packageName, { args }); + } +} + +async function runInstance(term: Terminal, wasmer: Wasmer, packageName: string, config: SpawnConfig) { + const instance = await wasmer.spawn(packageName, config); + + const stdin: WritableStreamDefaultWriter = instance.stdin!.getWriter(); + term.onData(line => { stdin.write(encoder.encode(line)); }); + + const stdout: ReadableStreamDefaultReader = instance.stdout.getReader(); + copyStream(stdout, line => term.write(line)); + + const stderr: ReadableStreamDefaultReader = instance.stderr.getReader(); + copyStream(stderr, line => term.write(line)); + + const { code } = await instance.wait(); + + if (code != 0) { + term.writeln(`\nExit code: ${code}`); + } +} + +async function copyStream(reader: ReadableStreamDefaultReader, cb: (line: string) => void) { + const decoder = new TextDecoder("utf-8"); + + while(true) { + const {done, value} = await reader.read(); + + if (done || !value) { + break; + } + const chunk = decoder.decode(value); + cb(chunk); + } +} + +main(); diff --git a/examples/wasmer.sh/package.json b/examples/wasmer.sh/package.json new file mode 100644 index 00000000..cd6e9936 --- /dev/null +++ b/examples/wasmer.sh/package.json @@ -0,0 +1,18 @@ +{ + "name": "@wasmer/shell", + "version": "1.0.0", + "description": "A Bash terminal in your browser, powered by WebAssembly and WASIX.", + "source": "index.html", + "scripts": { + "dev": "parcel", + "build": "parcel build" + }, + "dependencies": { + "@wasmer/wasix": "file:../..", + "xterm": "^5.3.0" + }, + "devDependencies": { + "parcel": "^2.9.3" + }, + "browserslist": "> 0.5%, last 2 versions, not dead" +} diff --git a/lib.ts b/lib.ts index c6029501..5a83b140 100644 --- a/lib.ts +++ b/lib.ts @@ -1,2 +1,4 @@ // @deno-types="./pkg/wasmer_wasix_js.d.ts" export * from "./pkg/wasmer_wasix_js"; +import init from "./pkg/wasmer_wasix_js"; +export default init; diff --git a/package.json b/package.json index b9d90d63..3a3399af 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "@wasmer/wasi", - "version": "1.2.2", + "name": "@wasmer/wasix", + "version": "0.1.0", "main": "dist/Library.cjs.min.js", "module": "dist/Library.esm.min.js", "unpkg": "dist/Library.umd.min.js", @@ -8,7 +8,8 @@ "keywords": [ "webassembly", "wasm", - "wasi" + "wasi", + "wasix" ], "description": "Isomorphic Javascript library for interacting with WASI Modules in Node.js and the Browser.", "author": "Wasmer Engineering Team ", From 1869b2c4bce672dd43cadd0c91a2e50fa3934efd Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Wed, 27 Sep 2023 16:46:39 +0800 Subject: [PATCH 02/13] Update how we fetch python.webc from the registry --- tests/integration.test.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/integration.test.ts b/tests/integration.test.ts index b029554b..43a0f5b8 100644 --- a/tests/integration.test.ts +++ b/tests/integration.test.ts @@ -3,7 +3,7 @@ import init, { Runtime, run, wat2wasm, Wasmer, Container } from "../pkg/wasmer_w const encoder = new TextEncoder(); const decoder = new TextDecoder("utf-8"); -const wasmerPython = "https://storage.googleapis.com/wapm-registry-prod/packages/_/python/python-0.1.0.webc"; +const wasmerPython = "https://wasmer.io/python/python@0.1.0"; before(async () => { await init(); @@ -114,7 +114,9 @@ async function readToEnd(stream: ReadableStream): Promise { } async function getPython(): Promise<{container: Container, python: Uint8Array, module: WebAssembly.Module}> { - const response = await fetch(wasmerPython); + const response = await fetch(wasmerPython, { + headers: { "Accept": "application/webc" } + }); const raw = await response.arrayBuffer(); const container = new Container(new Uint8Array(raw)); const python = container.get_atom("python"); From 3c40adcd00c8ff0360f34408bf9d7c6e1ae466f4 Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Wed, 27 Sep 2023 16:49:21 +0800 Subject: [PATCH 03/13] Fixing how we import init() from @wasmer/wasix --- examples/wasmer.sh/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/wasmer.sh/index.ts b/examples/wasmer.sh/index.ts index 58ae5b2d..56715bf5 100644 --- a/examples/wasmer.sh/index.ts +++ b/examples/wasmer.sh/index.ts @@ -1,5 +1,5 @@ +import { SpawnConfig, Wasmer, init } from "@wasmer/wasix"; import { Terminal } from "xterm"; -import init, { SpawnConfig, Wasmer } from "@wasmer/wasix"; const encoder = new TextEncoder(); From e619914c4df432c883bb6089838bc1567b2ba92f Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Wed, 27 Sep 2023 16:50:19 +0800 Subject: [PATCH 04/13] Revert the "lib.ts" changes and inject node polyfills while building --- lib.ts | 87 +++++++++++++++++++++++++++++++++++++++++++++-- package.json | 3 +- rollup.config.mjs | 4 ++- 3 files changed, 89 insertions(+), 5 deletions(-) diff --git a/lib.ts b/lib.ts index 5a83b140..7ae3e35a 100644 --- a/lib.ts +++ b/lib.ts @@ -1,4 +1,85 @@ -// @deno-types="./pkg/wasmer_wasix_js.d.ts" +import { Buffer } from "buffer"; export * from "./pkg/wasmer_wasix_js"; -import init from "./pkg/wasmer_wasix_js"; -export default init; +import load from "./pkg/wasmer_wasix_js"; +import wasm_bytes from "./pkg/wasmer_wasix_js_bg.wasm"; + +interface MimeBuffer extends Buffer { + type: string; + typeFull: string; + charset: string; +} + +/** + * Returns a `Buffer` instance from the given data URI `uri`. + * + * @param {String} uri Data URI to turn into a Buffer instance + * @returns {Buffer} Buffer instance from Data URI + * @api public + */ +function dataUriToBuffer(uri: string): MimeBuffer { + console.log(uri, Buffer); + if (!/^data:/i.test(uri)) { + throw new TypeError( + '`uri` does not appear to be a Data URI (must begin with "data:")' + ); + } + + // strip newlines + uri = uri.replace(/\r?\n/g, ''); + + // split the URI up into the "metadata" and the "data" portions + const firstComma = uri.indexOf(','); + if (firstComma === -1 || firstComma <= 4) { + throw new TypeError('malformed data: URI'); + } + + // remove the "data:" scheme and parse the metadata + const meta = uri.substring(5, firstComma).split(';'); + + let charset = ''; + let base64 = false; + const type = meta[0] || 'text/plain'; + let typeFull = type; + for (let i = 1; i < meta.length; i++) { + if (meta[i] === 'base64') { + base64 = true; + } else { + typeFull += `;${ meta[i]}`; + if (meta[i].indexOf('charset=') === 0) { + charset = meta[i].substring(8); + } + } + } + // defaults to US-ASCII only if type is not provided + if (!meta[0] && !charset.length) { + typeFull += ';charset=US-ASCII'; + charset = 'US-ASCII'; + } + + // get the encoded data portion and decode URI-encoded chars + const encoding = base64 ? 'base64' : 'ascii'; + const data = unescape(uri.substring(firstComma + 1)); + const buffer = Buffer.from(data, encoding) as MimeBuffer; + + // set `.type` and `.typeFull` properties to MIME type + buffer.type = type; + buffer.typeFull = typeFull; + + // set the `.charset` property + buffer.charset = charset; + + return buffer; +} + +export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module; + +let inited: Promise | null = null; +export const init = async (input?: InitInput | Promise, force?: boolean) => { + if (inited === null || force === true) { + if (!input) { + input = await WebAssembly.compile(dataUriToBuffer(wasm_bytes as any as string)); + } + inited = load(input); + } + await inited; +} diff --git a/package.json b/package.json index 3a3399af..ef402cbc 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@wasmer/wasix", "version": "0.1.0", "main": "dist/Library.cjs.min.js", - "module": "dist/Library.esm.min.js", + "module": "dist/Library.esm.js", "unpkg": "dist/Library.umd.min.js", "types": "dist/lib.d.ts", "keywords": [ @@ -45,6 +45,7 @@ "rimraf": "~3.0.2", "rollup": "~3.5.1", "rollup-plugin-dts": "^5.0.0", + "rollup-plugin-node-polyfills": "^0.2.1", "rollup-plugin-typescript2": "^0.34.1", "ts-loader": "^9.2.6", "tslib": "^2.3.1", diff --git a/rollup.config.mjs b/rollup.config.mjs index a6e48995..9310d91c 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -9,6 +9,7 @@ // ], // }; +import nodePolyfills from 'rollup-plugin-node-polyfills'; import terser from '@rollup/plugin-terser'; import pkg from './package.json' assert { type: 'json' }; import dts from "rollup-plugin-dts"; @@ -68,8 +69,9 @@ const makeConfig = (env = 'development') => { typescript(), url({ include: ['**/*.wasm'], - limit: 1 * 1024 * 1024, + limit: 100 * 1000 * 1000, }), + nodePolyfills(), ] }; From 8d3cabcead068a391e8544e1b75f72b53a307eab Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Wed, 27 Sep 2023 16:50:30 +0800 Subject: [PATCH 05/13] Wire up builds for the example --- examples/wasmer.sh/package.json | 13 +++++++++---- examples/wasmer.sh/rollup.config.mjs | 29 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 examples/wasmer.sh/rollup.config.mjs diff --git a/examples/wasmer.sh/package.json b/examples/wasmer.sh/package.json index cd6e9936..ec501263 100644 --- a/examples/wasmer.sh/package.json +++ b/examples/wasmer.sh/package.json @@ -2,17 +2,22 @@ "name": "@wasmer/shell", "version": "1.0.0", "description": "A Bash terminal in your browser, powered by WebAssembly and WASIX.", - "source": "index.html", "scripts": { - "dev": "parcel", - "build": "parcel build" + "dev": "rollup -c -w", + "build": "rollup -c" }, "dependencies": { "@wasmer/wasix": "file:../..", "xterm": "^5.3.0" }, "devDependencies": { - "parcel": "^2.9.3" + "@rollup/plugin-commonjs": "^25.0.4", + "@rollup/plugin-node-resolve": "^15.2.1", + "@rollup/plugin-typescript": "^11.1.4", + "@rollup/plugin-url": "^8.0.1", + "@web/rollup-plugin-html": "^2.0.1", + "rollup": "^3.29.3", + "rollup-plugin-serve": "^2.0.2" }, "browserslist": "> 0.5%, last 2 versions, not dead" } diff --git a/examples/wasmer.sh/rollup.config.mjs b/examples/wasmer.sh/rollup.config.mjs new file mode 100644 index 00000000..728eabdd --- /dev/null +++ b/examples/wasmer.sh/rollup.config.mjs @@ -0,0 +1,29 @@ +import { rollupPluginHTML as html } from "@web/rollup-plugin-html"; +import typescript from "@rollup/plugin-typescript"; +import commonjs from "@rollup/plugin-commonjs"; +import { nodeResolve } from "@rollup/plugin-node-resolve"; +import url from "@rollup/plugin-url"; +import serve from "rollup-plugin-serve"; + +export default function configure() { + const config = { + input: "index.html", + output: { dir: "dist" }, + plugins: [ + html(), + typescript(), + nodeResolve(), + commonjs(), + url({ + include: ["**/*.wasm"], + limit: 1 * 1024 * 1024, + }), + ], + }; + + if (process.env.ROLLUP_WATCH) { + config.plugins.push(serve("dist")); + } + + return config; +} From 096fc0e503efcb4d3098997fb33d4c3e3177d43a Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Wed, 27 Sep 2023 16:53:28 +0800 Subject: [PATCH 06/13] Wire up CORS headers for the dev server --- examples/wasmer.sh/rollup.config.mjs | 8 +++++++- lib.ts | 1 - 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/examples/wasmer.sh/rollup.config.mjs b/examples/wasmer.sh/rollup.config.mjs index 728eabdd..2dca6409 100644 --- a/examples/wasmer.sh/rollup.config.mjs +++ b/examples/wasmer.sh/rollup.config.mjs @@ -22,7 +22,13 @@ export default function configure() { }; if (process.env.ROLLUP_WATCH) { - config.plugins.push(serve("dist")); + config.plugins.push(serve({ + contentBase: "dist", + headers: { + "Cross-Origin-Opener-Policy": "same-origin", + "Cross-Origin-Embedder-Policy": "require-corp", + } + })); } return config; diff --git a/lib.ts b/lib.ts index 7ae3e35a..1e26f166 100644 --- a/lib.ts +++ b/lib.ts @@ -17,7 +17,6 @@ interface MimeBuffer extends Buffer { * @api public */ function dataUriToBuffer(uri: string): MimeBuffer { - console.log(uri, Buffer); if (!/^data:/i.test(uri)) { throw new TypeError( '`uri` does not appear to be a Data URI (must begin with "data:")' From 9a88e018aa981095af2af0305ab916b80c0395a9 Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Fri, 29 Sep 2023 13:36:09 +0800 Subject: [PATCH 07/13] Expose init() as a named export rather than the default --- examples/wasmer.sh/index.ts | 8 +++++--- examples/wasmer.sh/package.json | 2 +- lib.ts | 12 +++++++++++- package.json | 2 +- tests/integration.test.ts | 2 +- 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/examples/wasmer.sh/index.ts b/examples/wasmer.sh/index.ts index 56715bf5..24ae2564 100644 --- a/examples/wasmer.sh/index.ts +++ b/examples/wasmer.sh/index.ts @@ -4,6 +4,9 @@ import { Terminal } from "xterm"; const encoder = new TextEncoder(); async function main() { + console.log("Initializing"); + await init(); + const packageName = "sharrattj/bash"; const args: string[] = []; const uses: string[] = []; @@ -11,12 +14,11 @@ async function main() { const term = new Terminal(); const element = document.getElementById("app")!; + console.log(element, element.clientWidth, element.clientHeight); term.open(element); term.writeln("Starting..."); - await init(); - const wasmer = new Wasmer(); while (true) { @@ -57,4 +59,4 @@ async function copyStream(reader: ReadableStreamDefaultReader, cb: ( } } -main(); +addEventListener("DOMContentLoaded", () => main()); diff --git a/examples/wasmer.sh/package.json b/examples/wasmer.sh/package.json index ec501263..e33faa33 100644 --- a/examples/wasmer.sh/package.json +++ b/examples/wasmer.sh/package.json @@ -8,7 +8,7 @@ }, "dependencies": { "@wasmer/wasix": "file:../..", - "xterm": "^5.3.0" + "xterm": "5.0.x" }, "devDependencies": { "@rollup/plugin-commonjs": "^25.0.4", diff --git a/lib.ts b/lib.ts index 1e26f166..ca8db9c7 100644 --- a/lib.ts +++ b/lib.ts @@ -1,6 +1,7 @@ import { Buffer } from "buffer"; export * from "./pkg/wasmer_wasix_js"; -import load from "./pkg/wasmer_wasix_js"; +// @ts-ignore +import load, { WorkerState } from "./pkg/wasmer_wasix_js"; import wasm_bytes from "./pkg/wasmer_wasix_js_bg.wasm"; interface MimeBuffer extends Buffer { @@ -73,6 +74,10 @@ function dataUriToBuffer(uri: string): MimeBuffer { export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module; let inited: Promise | null = null; + +/** + * Initialize the underlying WebAssembly module. +*/ export const init = async (input?: InitInput | Promise, force?: boolean) => { if (inited === null || force === true) { if (!input) { @@ -82,3 +87,8 @@ export const init = async (input?: InitInput | Promise, force?: boole } await inited; } + +// HACK: We save these to the global scope because it's the most reliable way to +// make sure worker.js gets access to them. Normal exports are removed when +// using a bundler. +(globalThis as any)["__WASMER_INTERNALS__"] = { WorkerState, init }; diff --git a/package.json b/package.json index ef402cbc..6d831c37 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@wasmer/wasix", "version": "0.1.0", "main": "dist/Library.cjs.min.js", - "module": "dist/Library.esm.js", + "module": "dist/Library.esm.min.js", "unpkg": "dist/Library.umd.min.js", "types": "dist/lib.d.ts", "keywords": [ diff --git a/tests/integration.test.ts b/tests/integration.test.ts index 43a0f5b8..6c1699b0 100644 --- a/tests/integration.test.ts +++ b/tests/integration.test.ts @@ -1,5 +1,5 @@ import { expect } from '@esm-bundle/chai'; -import init, { Runtime, run, wat2wasm, Wasmer, Container } from "../pkg/wasmer_wasix_js"; +import { Runtime, run, wat2wasm, Wasmer, Container, init } from ".."; const encoder = new TextEncoder(); const decoder = new TextDecoder("utf-8"); From 4297baf81b5bcb6310021e5ad18a1c77e5c520e9 Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Fri, 29 Sep 2023 13:36:32 +0800 Subject: [PATCH 08/13] Re-working the worker.js script --- src/tasks/worker.js | 21 +++++++++++++-------- src/tasks/worker.rs | 28 ++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/tasks/worker.js b/src/tasks/worker.js index d9288653..7d16b738 100644 --- a/src/tasks/worker.js +++ b/src/tasks/worker.js @@ -2,24 +2,29 @@ Error.stackTraceLimit = 50; globalThis.onerror = console.error; let pendingMessages = []; +let worker = undefined; let handleMessage = async data => { - // We start off by buffering up all messages until we finish initializing. - pendingMessages.push(data); + if (worker) { + await worker.handle(data); + } else { + // We start off by buffering up all messages until we finish initializing. + pendingMessages.push(data); + } }; globalThis.onmessage = async ev => { if (ev.data.type == "init") { const { memory, module, id } = ev.data; - const { default: init, WorkerState } = await import("$IMPORT_META_URL"); + // Note: This populates global variables as a side-effect + await import("$IMPORT_META_URL"); + const { init, WorkerState } = globalThis["__WASMER_INTERNALS__"]; await init(module, memory); - const worker = new WorkerState(id); + worker = new WorkerState(id); - // Now that we're initialized, we can switch over to the "real" handler - // function and handle any buffered messages - handleMessage = msg => worker.handle(msg); + // Now that we're initialized, we need to handle any buffered messages for (const msg of pendingMessages.splice(0, pendingMessages.length)) { - await handleMessage(msg); + await worker.handle(msg); } } else { // Handle the message like normal. diff --git a/src/tasks/worker.rs b/src/tasks/worker.rs index 90ea9237..c425210d 100644 --- a/src/tasks/worker.rs +++ b/src/tasks/worker.rs @@ -11,14 +11,17 @@ pub struct WorkerState { } impl WorkerState { - fn emit(&self, msg: WorkerMessage) -> Result<(), Error> { - let scope: DedicatedWorkerGlobalScope = js_sys::global().dyn_into().unwrap(); + fn busy(&self) -> impl Drop { + struct BusyGuard; + impl Drop for BusyGuard { + fn drop(&mut self) { + let _ = emit(WorkerMessage::MarkIdle); + } + } - let value = - serde_wasm_bindgen::to_value(&msg).map_err(|e| crate::utils::js_error(e.into()))?; - scope.post_message(&value).map_err(crate::utils::js_error)?; + let _ = emit(WorkerMessage::MarkBusy); - Ok(()) + BusyGuard } } @@ -37,9 +40,8 @@ impl WorkerState { match msg { PostMessagePayload::SpawnAsync(thunk) => thunk().await, PostMessagePayload::SpawnBlocking(thunk) => { - self.emit(WorkerMessage::MarkBusy)?; + let _guard = self.busy(); thunk(); - self.emit(WorkerMessage::MarkIdle)?; } PostMessagePayload::CacheModule { hash, .. } => { tracing::warn!(%hash, "XXX Caching module"); @@ -62,3 +64,13 @@ pub(crate) enum WorkerMessage { /// Mark this worker as idle. MarkIdle, } + +/// Send a message to the scheduler. +fn emit(msg: WorkerMessage) -> Result<(), Error> { + let scope: DedicatedWorkerGlobalScope = js_sys::global().dyn_into().unwrap(); + + let value = serde_wasm_bindgen::to_value(&msg).map_err(|e| crate::utils::js_error(e.into()))?; + scope.post_message(&value).map_err(crate::utils::js_error)?; + + Ok(()) +} From 94f2e10da793e4ba8c47acabcd782d754625bf2a Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Mon, 2 Oct 2023 14:59:29 +0800 Subject: [PATCH 09/13] Found a bug in the init() glue function where we don't allow reusing linear memory --- lib.ts | 4 ++-- src/tasks/worker.js | 2 +- src/tasks/worker.rs | 4 +++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib.ts b/lib.ts index ca8db9c7..7d71ed2d 100644 --- a/lib.ts +++ b/lib.ts @@ -78,12 +78,12 @@ let inited: Promise | null = null; /** * Initialize the underlying WebAssembly module. */ -export const init = async (input?: InitInput | Promise, force?: boolean) => { +export const init = async (input?: InitInput | Promise, maybe_memory?: WebAssembly.Memory, force?: boolean) => { if (inited === null || force === true) { if (!input) { input = await WebAssembly.compile(dataUriToBuffer(wasm_bytes as any as string)); } - inited = load(input); + inited = load(input, maybe_memory); } await inited; } diff --git a/src/tasks/worker.js b/src/tasks/worker.js index 7d16b738..951206e8 100644 --- a/src/tasks/worker.js +++ b/src/tasks/worker.js @@ -15,7 +15,7 @@ let handleMessage = async data => { globalThis.onmessage = async ev => { if (ev.data.type == "init") { const { memory, module, id } = ev.data; - // Note: This populates global variables as a side-effect + // HACK: This populates global variables as a side-effect await import("$IMPORT_META_URL"); const { init, WorkerState } = globalThis["__WASMER_INTERNALS__"]; await init(module, memory); diff --git a/src/tasks/worker.rs b/src/tasks/worker.rs index c425210d..9d75673d 100644 --- a/src/tasks/worker.rs +++ b/src/tasks/worker.rs @@ -38,7 +38,9 @@ impl WorkerState { tracing::trace!(?msg, "Handling a message"); match msg { - PostMessagePayload::SpawnAsync(thunk) => thunk().await, + PostMessagePayload::SpawnAsync(thunk) => { + thunk().await; + } PostMessagePayload::SpawnBlocking(thunk) => { let _guard = self.busy(); thunk(); From b963e244ca20f49d53708677783ff952902f68cf Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Tue, 3 Oct 2023 16:17:25 +0800 Subject: [PATCH 10/13] Create a JS Error directly instead of sub-classing it --- src/utils.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 3e7c581e..903c8d48 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -70,10 +70,17 @@ impl From for JsValue { match error { Error::JavaScript(e) => e, Error::Rust(error) => { - let custom = js_sys::Object::new(); + let message = error.to_string(); + let js_error = js_sys::Error::new(&message); let _ = js_sys::Reflect::set( - &custom, + &js_error, + &JsString::from("message"), + &JsString::from(error.to_string()), + ); + + let _ = js_sys::Reflect::set( + &js_error, &JsString::from("detailedMessage"), &JsString::from(format!("{error:?}")), ); @@ -81,12 +88,9 @@ impl From for JsValue { let causes: js_sys::Array = std::iter::successors(error.source(), |e| e.source()) .map(|e| JsString::from(e.to_string())) .collect(); - let _ = js_sys::Reflect::set(&custom, &JsString::from("causes"), &causes); - - let error_prototype = js_sys::Error::new(&error.to_string()); - let _ = js_sys::Reflect::set_prototype_of(&custom, &error_prototype); + let _ = js_sys::Reflect::set(&js_error, &JsString::from("causes"), &causes); - custom.into() + js_error.into() } } } From e6c94b5e93da3a0e820f2811f3f600c2c2a68001 Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Tue, 3 Oct 2023 16:19:11 +0800 Subject: [PATCH 11/13] Bump wasmer versions --- Cargo.lock | 256 +++++++++++++++++++++++++---------------------------- Cargo.toml | 8 +- 2 files changed, 127 insertions(+), 137 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2b6c320c..c219cdd2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,9 +30,9 @@ dependencies = [ [[package]] name = "aho-corasick" -version = "1.0.4" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6748e8def348ed4d14996fa801f4122cd763fff530258cdc03f64b25f89d3a5a" +checksum = "ea5d730647d4fadd988536d06fecce94b7b4f2a7efdae548f1cf4b63205518ab" dependencies = [ "memchr", ] @@ -72,7 +72,7 @@ checksum = "bc00ceb34980c03614e35a3a4e218276a0a824e911d07651cd0d858a51e8c0f0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.37", ] [[package]] @@ -107,9 +107,9 @@ dependencies = [ [[package]] name = "base64" -version = "0.21.3" +version = "0.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "414dcefbc63d77c526a76b3afcf6fbb9b5e2791c19c3aa2297733208750c6e53" +checksum = "9ba43ea6f343b788c8764558649e08df62f86c6ef251fdaeb1ffd010a9ae50a2" [[package]] name = "bincode" @@ -155,9 +155,9 @@ dependencies = [ [[package]] name = "bumpalo" -version = "3.13.0" +version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3e2c3daef883ecc1b5d58c15adae93470a91d425f3532ba1695849656af3fc1" +checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" [[package]] name = "bytecheck" @@ -189,9 +189,9 @@ checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" [[package]] name = "bytes" -version = "1.4.0" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" +checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" dependencies = [ "serde", ] @@ -219,16 +219,16 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.26" +version = "0.4.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec837a71355b28f6556dbd569b37b3f363091c0bd4b2e735674521b4c5fd9bc5" +checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38" dependencies = [ "android-tzdata", "iana-time-zone", "js-sys", "num-traits", "wasm-bindgen", - "winapi", + "windows-targets", ] [[package]] @@ -363,7 +363,7 @@ dependencies = [ "ident_case", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.37", ] [[package]] @@ -385,17 +385,17 @@ checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ "darling_core 0.20.3", "quote", - "syn 2.0.29", + "syn 2.0.37", ] [[package]] name = "dashmap" -version = "5.5.1" +version = "5.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "edd72493923899c6f10c641bdbdeddc7183d6396641d99c1a0d1597f37f92e28" +checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" dependencies = [ "cfg-if 1.0.0", - "hashbrown 0.14.0", + "hashbrown 0.14.1", "lock_api", "once_cell", "parking_lot_core", @@ -461,9 +461,9 @@ dependencies = [ [[package]] name = "educe" -version = "0.4.22" +version = "0.4.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "079044df30bb07de7d846d41a184c4b00e66ebdac93ee459253474f3a47e50ae" +checksum = "0f0042ff8246a363dbe77d2ceedb073339e85a804b9a47636c6e016a9a32c05f" dependencies = [ "enum-ordinalize", "proc-macro2", @@ -493,15 +493,15 @@ dependencies = [ [[package]] name = "enum-ordinalize" -version = "3.1.13" +version = "3.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e4f76552f53cefc9a7f64987c3701b99d982f7690606fd67de1d09712fbf52f1" +checksum = "1bf1fa3f06bbff1ea5b1a9c7b14aa992a39657db60a2759457328d7e058f49ee" dependencies = [ "num-bigint", "num-traits", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.37", ] [[package]] @@ -522,7 +522,7 @@ dependencies = [ "darling 0.20.3", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.37", ] [[package]] @@ -533,9 +533,9 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "errno" -version = "0.3.2" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b30f669a7961ef1631673d2766cc92f52d64f7ef354d4fe0ddfd30ed52f0f4f" +checksum = "add4f07d43996f76ef320709726a556a9d4f965d9410d8d0271132d2f8293480" dependencies = [ "errno-dragonfly", "libc", @@ -554,9 +554,9 @@ dependencies = [ [[package]] name = "fastrand" -version = "2.0.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" +checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" [[package]] name = "filetime" @@ -663,7 +663,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.37", ] [[package]] @@ -751,9 +751,9 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.14.0" +version = "0.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a" +checksum = "7dfda62a12f55daeae5015f81b0baea145391cb4520f86c248fc615d72640d12" [[package]] name = "heapless" @@ -852,12 +852,12 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.0.0" +version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5477fe2230a79769d8dc68e0eabf5437907c0457a5614a9e8dddb67f65eb65d" +checksum = "8adf3ddd720272c6ea8bf59463c04e0f93d0bbf7c5439b691bca2987e0270897" dependencies = [ "equivalent", - "hashbrown 0.14.0", + "hashbrown 0.14.1", ] [[package]] @@ -910,9 +910,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.147" +version = "0.2.148" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" +checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b" [[package]] name = "linked-hash-map" @@ -931,9 +931,9 @@ dependencies = [ [[package]] name = "linux-raw-sys" -version = "0.4.5" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57bcfdad1b858c2db7c38303a6d2ad4dfaf5eb53dfeb0910128b2c26d6158503" +checksum = "3852614a3bd9ca9804678ba6be5e3b8ce76dfc902cae004e3e0c44051b6e88db" [[package]] name = "lock_api" @@ -971,9 +971,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.6.0" +version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76fc44e2588d5b436dbc3c6cf62aef290f90dab6235744a93dfe1cc18f451e2c" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" [[package]] name = "memmap2" @@ -1086,9 +1086,9 @@ dependencies = [ [[package]] name = "object" -version = "0.32.0" +version = "0.32.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77ac5bbd07aea88c60a577a1ce218075ffd59208b2d7ca97adf9bfc5aeb21ebe" +checksum = "9cf5f9dd3933bd50a9e1f149ec995f39ae2c496d31fd772c1fd45ebc27e902b0" dependencies = [ "memchr", ] @@ -1137,7 +1137,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9" dependencies = [ "fixedbitset", - "indexmap 2.0.0", + "indexmap 2.0.2", ] [[package]] @@ -1157,7 +1157,7 @@ checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.37", ] [[package]] @@ -1214,9 +1214,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.66" +version = "1.0.67" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" +checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328" dependencies = [ "unicode-ident", ] @@ -1308,13 +1308,13 @@ dependencies = [ [[package]] name = "regex" -version = "1.9.4" +version = "1.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12de2eff854e5fa4b1295edd650e227e9d8fb0c9e90b12e7f36d6a6811791a29" +checksum = "ebee201405406dbf528b8b672104ae6d6d63e6d118cb10e4d51abbc7b58044ff" dependencies = [ "aho-corasick", "memchr", - "regex-automata 0.3.7", + "regex-automata 0.3.9", "regex-syntax 0.7.5", ] @@ -1329,9 +1329,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.3.7" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49530408a136e16e5b486e883fbb6ba058e8e4e8ae6621a77b048b314336e629" +checksum = "59b23e92ee4318893fa3fe3e6fb365258efbfe6ac6ab30f090cdcbb7aa37efa9" dependencies = [ "aho-corasick", "memchr", @@ -1364,9 +1364,9 @@ dependencies = [ [[package]] name = "rend" -version = "0.4.0" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "581008d2099240d37fb08d77ad713bcaec2c4d89d50b5b21a8bb1996bbab68ab" +checksum = "a2571463863a6bd50c32f94402933f03457a3fbaf697a707c5be741e459f08fd" dependencies = [ "bytecheck", ] @@ -1423,9 +1423,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.9" +version = "0.38.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9bfe0f2582b4931a45d1fa608f8a8722e8b3c7ac54dd6d5f3b3212791fedef49" +checksum = "d2f9da0cbd88f9f09e7814e388301c8414c51c62aa6ce1e4b5c551d49d96e531" dependencies = [ "bitflags 2.4.0", "errno", @@ -1475,9 +1475,9 @@ checksum = "4c309e515543e67811222dbc9e3dd7e1056279b782e1dacffe4242b718734fb6" [[package]] name = "semver" -version = "1.0.18" +version = "1.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0293b4b29daaf487284529cc2f5675b8e57c61f70167ba415a463651fd6a918" +checksum = "ad977052201c6de01a8ef2aa3378c4bd23217a056337d1d6da40468d267a4fb0" dependencies = [ "serde", ] @@ -1531,14 +1531,14 @@ checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.37", ] [[package]] name = "serde_json" -version = "1.0.105" +version = "1.0.107" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "693151e1ac27563d6dbcec9dee9fbd5da8539b20fa14ad3752b2e6d363ace360" +checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65" dependencies = [ "itoa", "ryu", @@ -1572,7 +1572,7 @@ version = "0.9.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a49e178e4452f45cb61d0cd8cebc1b0fafd3e41929e996cef79aa3aca91f574" dependencies = [ - "indexmap 2.0.0", + "indexmap 2.0.2", "itoa", "ryu", "serde", @@ -1581,9 +1581,9 @@ dependencies = [ [[package]] name = "sha2" -version = "0.10.7" +version = "0.10.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "479fb9d862239e610720565ca91403019f2f00410f1864c5aa7479b950a76ed8" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" dependencies = [ "cfg-if 1.0.0", "cpufeatures", @@ -1592,9 +1592,9 @@ dependencies = [ [[package]] name = "sharded-slab" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6805d8ff0f66aa61fb79a97a51ba210dcae753a797336dea8a36a3168196fab" +checksum = "c1b21f559e07218024e7e9f90f96f601825397de0e25420135f7f952453fed0b" dependencies = [ "lazy_static", ] @@ -1626,9 +1626,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.11.0" +version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62bb4feee49fdd9f707ef802e22365a35de4b7b299de4763d44bfea899442ff9" +checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a" [[package]] name = "spin" @@ -1664,9 +1664,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.29" +version = "2.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c324c494eba9d92503e6f1ef2e6df781e78f6a7705a0202d9801b198807d518a" +checksum = "7303ef2c05cd654186cb250d29049a24840ca25d2747c25c0381c8d9e2f582e8" dependencies = [ "proc-macro2", "quote", @@ -1730,22 +1730,22 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.47" +version = "1.0.49" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97a802ec30afc17eee47b2855fc72e0c4cd62be9b4efe6591edde0ec5bd68d8f" +checksum = "1177e8c6d7ede7afde3585fd2513e611227efd6481bd78d2e82ba1ce16557ed4" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.47" +version = "1.0.49" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bb623b56e39ab7dcd4b1b98bb6c8f8d907ed255b18de254088016b27a8ee19b" +checksum = "10712f02019e9288794769fba95cd6847df9874d49d871d062172f9dd41bc4cc" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.37", ] [[package]] @@ -1760,9 +1760,9 @@ dependencies = [ [[package]] name = "time" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17f6bb557fd245c28e6411aa56b6403c689ad95061f50e4be16c274e70a17e48" +checksum = "426f806f4089c493dcac0d24c29c01e2c38baf8e30f1b716ee37e83d200b18fe" dependencies = [ "deranged", "itoa", @@ -1773,15 +1773,15 @@ dependencies = [ [[package]] name = "time-core" -version = "0.1.1" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7300fbefb4dadc1af235a9cef3737cea692a9d97e1b9cbcd4ebdae6f8868e6fb" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" [[package]] name = "time-macros" -version = "0.2.14" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a942f44339478ef67935ab2bbaec2fb0322496cf3cbe84b261e06ac3814c572" +checksum = "4ad70d68dba9e1f8aceda7aa6711965dfec1cac869f311a51bd08b3a2ccbce20" dependencies = [ "time-core", ] @@ -1821,7 +1821,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.37", ] [[package]] @@ -1864,9 +1864,9 @@ dependencies = [ [[package]] name = "toml" -version = "0.7.6" +version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c17e963a819c331dcacd7ab957d80bc2b9a9c1e71c804826d2f283dd65306542" +checksum = "dd79e69d3b627db300ff956027cc6c3798cef26d22526befdfcd12feeb6d2257" dependencies = [ "serde", "serde_spanned", @@ -1885,11 +1885,11 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.19.14" +version = "0.19.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8123f27e969974a3dfba720fdb560be359f57b44302d280ba72e76a74480e8a" +checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" dependencies = [ - "indexmap 2.0.0", + "indexmap 2.0.2", "serde", "serde_spanned", "toml_datetime", @@ -1917,7 +1917,7 @@ checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.37", ] [[package]] @@ -1982,9 +1982,9 @@ dependencies = [ [[package]] name = "typenum" -version = "1.16.0" +version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" [[package]] name = "unicase" @@ -2003,9 +2003,9 @@ checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460" [[package]] name = "unicode-ident" -version = "1.0.11" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "unicode-normalization" @@ -2024,9 +2024,9 @@ checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" [[package]] name = "unicode-width" -version = "0.1.10" +version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" +checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" [[package]] name = "unicode-xid" @@ -2078,8 +2078,7 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "virtual-fs" -version = "0.8.0" -source = "git+https://github.com/wasmerio/wasmer?branch=wasmer-js-fixes#ab71f9eda50e8fbe82f7ebd8e33d6b9592e919a8" +version = "0.9.0" dependencies = [ "anyhow", "async-trait", @@ -2100,8 +2099,7 @@ dependencies = [ [[package]] name = "virtual-mio" -version = "0.2.0" -source = "git+https://github.com/wasmerio/wasmer?branch=wasmer-js-fixes#ab71f9eda50e8fbe82f7ebd8e33d6b9592e919a8" +version = "0.3.0" dependencies = [ "async-trait", "bytes", @@ -2114,8 +2112,7 @@ dependencies = [ [[package]] name = "virtual-net" -version = "0.5.0" -source = "git+https://github.com/wasmerio/wasmer?branch=wasmer-js-fixes#ab71f9eda50e8fbe82f7ebd8e33d6b9592e919a8" +version = "0.6.1" dependencies = [ "anyhow", "async-trait", @@ -2201,8 +2198,7 @@ dependencies = [ [[package]] name = "wai-bindgen-wasmer" -version = "0.13.0" -source = "git+https://github.com/wasmerio/wasmer?branch=wasmer-js-fixes#ab71f9eda50e8fbe82f7ebd8e33d6b9592e919a8" +version = "0.14.0" dependencies = [ "anyhow", "bitflags 1.3.2", @@ -2240,15 +2236,15 @@ dependencies = [ [[package]] name = "waker-fn" -version = "1.1.0" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" +checksum = "f3c4517f54858c779bbcbf228f4fca63d121bf85fbecb2dc578cdf4a39395690" [[package]] name = "walkdir" -version = "2.3.3" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36df944cda56c7d8d8b7496af378e6b16de9284591917d307c9b4d313c44e698" +checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee" dependencies = [ "same-file", "winapi-util", @@ -2281,7 +2277,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.37", "wasm-bindgen-shared", ] @@ -2338,7 +2334,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.37", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -2375,17 +2371,16 @@ dependencies = [ [[package]] name = "wasm-encoder" -version = "0.32.0" +version = "0.33.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ba64e81215916eaeb48fee292f29401d69235d62d8b8fd92a7b2844ec5ae5f7" +checksum = "34180c89672b3e4825c3a8db4b61a674f1447afd5fe2445b2d22c3d8b6ea086c" dependencies = [ "leb128", ] [[package]] name = "wasmer" -version = "4.2.0" -source = "git+https://github.com/wasmerio/wasmer?branch=wasmer-js-fixes#ab71f9eda50e8fbe82f7ebd8e33d6b9592e919a8" +version = "4.2.1" dependencies = [ "bytes", "cfg-if 1.0.0", @@ -2400,6 +2395,7 @@ dependencies = [ "target-lexicon", "thiserror", "wasm-bindgen", + "wasm-bindgen-downcast", "wasmer-compiler", "wasmer-derive", "wasmer-types", @@ -2412,8 +2408,7 @@ dependencies = [ [[package]] name = "wasmer-compiler" -version = "4.2.0" -source = "git+https://github.com/wasmerio/wasmer?branch=wasmer-js-fixes#ab71f9eda50e8fbe82f7ebd8e33d6b9592e919a8" +version = "4.2.1" dependencies = [ "backtrace", "bytes", @@ -2437,8 +2432,7 @@ dependencies = [ [[package]] name = "wasmer-derive" -version = "4.2.0" -source = "git+https://github.com/wasmerio/wasmer?branch=wasmer-js-fixes#ab71f9eda50e8fbe82f7ebd8e33d6b9592e919a8" +version = "4.2.1" dependencies = [ "proc-macro-error", "proc-macro2", @@ -2448,9 +2442,9 @@ dependencies = [ [[package]] name = "wasmer-toml" -version = "0.7.0" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d79d9e87af8aea672134da379ccef76e659b7bc316a10b7e51d30177515c0199" +checksum = "80dd00e4ae6e2f13c1fba9c8fd49d2567985c8099f9c9920aa4bb922c59e4f54" dependencies = [ "anyhow", "derive_builder", @@ -2466,8 +2460,7 @@ dependencies = [ [[package]] name = "wasmer-types" -version = "4.2.0" -source = "git+https://github.com/wasmerio/wasmer?branch=wasmer-js-fixes#ab71f9eda50e8fbe82f7ebd8e33d6b9592e919a8" +version = "4.2.1" dependencies = [ "bytecheck", "enum-iterator", @@ -2482,8 +2475,7 @@ dependencies = [ [[package]] name = "wasmer-vm" -version = "4.2.0" -source = "git+https://github.com/wasmerio/wasmer?branch=wasmer-js-fixes#ab71f9eda50e8fbe82f7ebd8e33d6b9592e919a8" +version = "4.2.1" dependencies = [ "backtrace", "cc", @@ -2509,8 +2501,7 @@ dependencies = [ [[package]] name = "wasmer-wasix" -version = "0.13.0" -source = "git+https://github.com/wasmerio/wasmer?branch=wasmer-js-fixes#ab71f9eda50e8fbe82f7ebd8e33d6b9592e919a8" +version = "0.14.0" dependencies = [ "anyhow", "async-trait", @@ -2605,8 +2596,7 @@ dependencies = [ [[package]] name = "wasmer-wasix-types" -version = "0.13.0" -source = "git+https://github.com/wasmerio/wasmer?branch=wasmer-js-fixes#ab71f9eda50e8fbe82f7ebd8e33d6b9592e919a8" +version = "0.14.0" dependencies = [ "anyhow", "bitflags 1.3.2", @@ -2644,9 +2634,9 @@ dependencies = [ [[package]] name = "wast" -version = "64.0.0" +version = "65.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a259b226fd6910225aa7baeba82f9d9933b6d00f2ce1b49b80fa4214328237cc" +checksum = "a55a88724cf8c2c0ebbf32c8e8f4ac0d6aa7ba6d73a1cfd94b254aa8f894317e" dependencies = [ "leb128", "memchr", @@ -2656,9 +2646,9 @@ dependencies = [ [[package]] name = "wat" -version = "1.0.71" +version = "1.0.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53253d920ab413fca1c7dc2161d601c79b4fdf631d0ba51dd4343bf9b556c3f6" +checksum = "d83e1a8d86d008adc7bafa5cf4332d448699a08fcf2a715a71fbb75e2c5ca188" dependencies = [ "wast", ] @@ -2675,9 +2665,9 @@ dependencies = [ [[package]] name = "webc" -version = "5.3.0" +version = "5.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5c35d27cb4c7898571b5f25036ead587736ffb371261f9e928a28edee7abf9d" +checksum = "4b56acc943f6b80cc2842231f34f99a02cd406896a23f3c6dacd8130c24ab3d1" dependencies = [ "anyhow", "base64", @@ -2699,7 +2689,7 @@ dependencies = [ "tar", "tempfile", "thiserror", - "toml 0.7.6", + "toml 0.7.8", "url", "walkdir", "wasmer-toml", @@ -2741,9 +2731,9 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" dependencies = [ "winapi", ] diff --git a/Cargo.toml b/Cargo.toml index 2bbea326..b3bd14fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,14 +27,14 @@ tracing = { version = "0.1", features = ["log", "release_max_level_info"] } tracing-futures = { version = "0.2" } tracing-wasm = { version = "0.2" } url = "2.4.0" -virtual-net = { version = "0.5.0", default-features = false, features = ["remote"] } -virtual-fs = { version = "0.8.0", default-features = false } +virtual-net = { version = "0.6.0", default-features = false, features = ["remote"] } +virtual-fs = { version = "0.9.0", default-features = false } wasm-bindgen = { version = "0.2" } wasm-bindgen-downcast = "0.1" wasm-bindgen-futures = "0.4" wasm-bindgen-test = "0.3.37" -wasmer = { version = "4.1", default-features = false, features = ["js", "js-default"] } -wasmer-wasix = { version = "0.13", default-features = false, features = ["js", "js-default"] } +wasmer = { version = "4.2", default-features = false, features = ["js", "js-default"] } +wasmer-wasix = { version = "0.14", default-features = false, features = ["js", "js-default"] } wee_alloc = { version = "0.4", optional = true } webc = "5.3.0" shared-buffer = "0.1.3" From faa0322aedb5b2e3157d07da5847d5296887fa4b Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Wed, 4 Oct 2023 15:51:27 +0800 Subject: [PATCH 12/13] Added support for "uses" --- Cargo.lock | 1 - Cargo.toml | 12 +++++--- examples/wasmer.sh/index.ts | 20 ++++++++----- examples/wasmer.sh/package.json | 3 +- examples/wasmer.sh/rollup.config.mjs | 2 ++ src/facade.rs | 42 ++++++++++++++++++++++++++-- src/run.rs | 37 ++++++------------------ src/tasks/thread_pool.rs | 4 +-- src/utils.rs | 36 ++++++++++++++++++++++++ tests/integration.test.ts | 25 +++++++++++++++-- 10 files changed, 134 insertions(+), 48 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c219cdd2..5286e58e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2395,7 +2395,6 @@ dependencies = [ "target-lexicon", "thiserror", "wasm-bindgen", - "wasm-bindgen-downcast", "wasmer-compiler", "wasmer-derive", "wasmer-types", diff --git a/Cargo.toml b/Cargo.toml index b3bd14fa..d45acd09 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -99,7 +99,11 @@ dwarf-debug-info = false wasm-opt = ["--enable-threads", "--enable-bulk-memory", "-Oz"] [patch.crates-io] -virtual-net = { git = "https://github.com/wasmerio/wasmer", branch = "wasmer-js-fixes" } -virtual-fs = { git = "https://github.com/wasmerio/wasmer", branch = "wasmer-js-fixes" } -wasmer-wasix = { git = "https://github.com/wasmerio/wasmer", branch = "wasmer-js-fixes" } -wasmer = { git = "https://github.com/wasmerio/wasmer", branch = "wasmer-js-fixes" } +# virtual-net = { git = "https://github.com/wasmerio/wasmer", branch = "wasmer-js-fixes" } +# virtual-fs = { git = "https://github.com/wasmerio/wasmer", branch = "wasmer-js-fixes" } +# wasmer-wasix = { git = "https://github.com/wasmerio/wasmer", branch = "wasmer-js-fixes" } +# wasmer = { git = "https://github.com/wasmerio/wasmer", branch = "wasmer-js-fixes" } +virtual-net = { path = "../wasmer/lib/virtual-net" } +virtual-fs = { path = "../wasmer/lib/virtual-fs" } +wasmer-wasix = { path = "../wasmer/lib/wasix" } +wasmer = { path = "../wasmer/lib/api" } diff --git a/examples/wasmer.sh/index.ts b/examples/wasmer.sh/index.ts index 24ae2564..5de5a2c7 100644 --- a/examples/wasmer.sh/index.ts +++ b/examples/wasmer.sh/index.ts @@ -1,33 +1,39 @@ +import "xterm/css/xterm.css"; + import { SpawnConfig, Wasmer, init } from "@wasmer/wasix"; import { Terminal } from "xterm"; const encoder = new TextEncoder(); +const packageName = "sharrattj/bash"; +const args: string[] = []; +const uses: string[] = ["sharrattj/coreutils"]; + async function main() { console.log("Initializing"); await init(); - const packageName = "sharrattj/bash"; - const args: string[] = []; - const uses: string[] = []; - const term = new Terminal(); const element = document.getElementById("app")!; - console.log(element, element.clientWidth, element.clientHeight); term.open(element); - + term.onResize(console.log); term.writeln("Starting..."); + term.onData(console.error); + + console.log("Starting instance"); const wasmer = new Wasmer(); while (true) { - await runInstance(term, wasmer, packageName, { args }); + await runInstance(term, wasmer, packageName, { args, uses }); + console.log("Rebooting..."); } } async function runInstance(term: Terminal, wasmer: Wasmer, packageName: string, config: SpawnConfig) { const instance = await wasmer.spawn(packageName, config); + term.clear(); const stdin: WritableStreamDefaultWriter = instance.stdin!.getWriter(); term.onData(line => { stdin.write(encoder.encode(line)); }); diff --git a/examples/wasmer.sh/package.json b/examples/wasmer.sh/package.json index e33faa33..3093536e 100644 --- a/examples/wasmer.sh/package.json +++ b/examples/wasmer.sh/package.json @@ -8,7 +8,7 @@ }, "dependencies": { "@wasmer/wasix": "file:../..", - "xterm": "5.0.x" + "xterm": "4.19" }, "devDependencies": { "@rollup/plugin-commonjs": "^25.0.4", @@ -17,6 +17,7 @@ "@rollup/plugin-url": "^8.0.1", "@web/rollup-plugin-html": "^2.0.1", "rollup": "^3.29.3", + "rollup-plugin-import-css": "^3.3.4", "rollup-plugin-serve": "^2.0.2" }, "browserslist": "> 0.5%, last 2 versions, not dead" diff --git a/examples/wasmer.sh/rollup.config.mjs b/examples/wasmer.sh/rollup.config.mjs index 2dca6409..893dcec9 100644 --- a/examples/wasmer.sh/rollup.config.mjs +++ b/examples/wasmer.sh/rollup.config.mjs @@ -4,6 +4,7 @@ import commonjs from "@rollup/plugin-commonjs"; import { nodeResolve } from "@rollup/plugin-node-resolve"; import url from "@rollup/plugin-url"; import serve from "rollup-plugin-serve"; +import css from "rollup-plugin-import-css"; export default function configure() { const config = { @@ -18,6 +19,7 @@ export default function configure() { include: ["**/*.wasm"], limit: 1 * 1024 * 1024, }), + css(), ], }; diff --git a/src/facade.rs b/src/facade.rs index 240df157..3432e510 100644 --- a/src/facade.rs +++ b/src/facade.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use anyhow::Context; -use futures::channel::oneshot; +use futures::{channel::oneshot, TryStreamExt}; use js_sys::JsString; use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; use wasmer_wasix::{ @@ -72,7 +72,7 @@ impl Wasmer { let tasks = Arc::clone(runtime.task_manager()); let mut runner = WasiRunner::new(); - let (stdin, stdout, stderr) = config.configure_runner(&mut runner)?; + let (stdin, stdout, stderr) = config.configure_runner(&mut runner, &runtime).await?; tracing::debug!(%specifier, %command_name, "Starting the WASI runner"); @@ -102,12 +102,15 @@ extern "C" { #[wasm_bindgen(method, getter)] fn command(this: &SpawnConfig) -> JsValue; + #[wasm_bindgen(method, getter)] + fn uses(this: &SpawnConfig) -> Option; } impl SpawnConfig { - pub(crate) fn configure_runner( + pub(crate) async fn configure_runner( &self, runner: &mut WasiRunner, + runtime: &Runtime, ) -> Result< ( Option, @@ -122,6 +125,12 @@ impl SpawnConfig { let env = self.parse_env()?; runner.set_envs(env); + if let Some(uses) = self.uses() { + let uses = crate::utils::js_string_array(uses)?; + let packages = load_injected_packages(uses, runtime).await?; + runner.add_injected_packages(packages); + } + let stdin = match self.read_stdin() { Some(stdin) => { let f = virtual_fs::StaticFile::new(stdin.into()); @@ -145,6 +154,29 @@ impl SpawnConfig { } } +#[tracing::instrument(level = "debug", skip_all)] +async fn load_injected_packages( + packages: Vec, + runtime: &Runtime, +) -> Result, Error> { + let futures: futures::stream::FuturesOrdered<_> = packages + .into_iter() + .map(|pkg| async move { load_package(&pkg, runtime).await }) + .collect(); + + let packages = futures.try_collect().await?; + + Ok(packages) +} + +#[tracing::instrument(level = "debug", skip(runtime))] +async fn load_package(pkg: &str, runtime: &Runtime) -> Result { + let specifier: PackageSpecifier = pkg.parse()?; + let pkg = BinaryPackage::from_registry(&specifier, runtime).await?; + + Ok(pkg) +} + #[wasm_bindgen(typescript_custom_section)] const SPAWN_CONFIG_TYPE_DEFINITION: &'static str = r#" /** @@ -156,6 +188,10 @@ export type SpawnConfig = RunConfig & { * defined). */ command?: string; + /** + * Packages that should also be loaded into the WASIX environment. + */ + uses?: string[]; } "#; diff --git a/src/run.rs b/src/run.rs index 7efdbabb..c41d0a06 100644 --- a/src/run.rs +++ b/src/run.rs @@ -1,7 +1,7 @@ use std::{collections::BTreeMap, sync::Arc}; use futures::channel::oneshot; -use js_sys::{Array, JsString, TypeError}; +use js_sys::Array; use wasm_bindgen::{prelude::wasm_bindgen, JsCast, JsValue}; use wasmer_wasix::{Runtime as _, WasiEnvBuilder}; @@ -126,39 +126,20 @@ impl RunConfig { } pub(crate) fn parse_args(&self) -> Result, Error> { - let mut parsed = Vec::new(); - - if let Some(args) = self.args() { - for arg in args { - match arg.dyn_into::() { - Ok(arg) => parsed.push(String::from(arg)), - Err(_) => { - return Err(Error::js(TypeError::new("Arguments must be strings"))); - } - } - } + match self.args() { + Some(args) => crate::utils::js_string_array(args), + None => Ok(Vec::new()), } - - Ok(parsed) } pub(crate) fn parse_env(&self) -> Result, Error> { - let mut parsed = BTreeMap::new(); - - if let Some(env) = self.env().dyn_ref() { - for (key, value) in crate::utils::object_entries(env)? { - let key: String = key.into(); - let value: String = value - .dyn_into::() - .map_err(|_| { - Error::js(TypeError::new("Environment variables must be strings")) - })? - .into(); - parsed.insert(key, value); + match self.env().dyn_ref() { + Some(env) => { + let vars = crate::utils::js_record_of_strings(env)?; + Ok(vars.into_iter().collect()) } + None => Ok(BTreeMap::new()), } - - Ok(parsed) } pub(crate) fn read_stdin(&self) -> Option> { diff --git a/src/tasks/thread_pool.rs b/src/tasks/thread_pool.rs index 0c36ab69..94af4062 100644 --- a/src/tasks/thread_pool.rs +++ b/src/tasks/thread_pool.rs @@ -84,8 +84,8 @@ impl VirtualTaskManager for ThreadPool { /// Starts an asynchronous task will will run on a dedicated thread /// pulled from the worker pool that has a stateful thread local variable /// It is ok for this task to block execution and any async futures within its scope - fn task_wasm(&self, task: TaskWasm) -> Result<(), WasiThreadError> { - todo!(); + fn task_wasm(&self, _task: TaskWasm) -> Result<(), WasiThreadError> { + todo!("TaskWasm isn't implemented"); } /// Starts an asynchronous task will will run on a dedicated thread diff --git a/src/utils.rs b/src/utils.rs index 903c8d48..5fb7f9ba 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -156,3 +156,39 @@ pub(crate) fn current_module() -> js_sys::WebAssembly::Module { // 1: `wasm_bindgen::module` is currently only supported with `--target no-modules` and `--tar get web` wasm_bindgen::module().dyn_into().unwrap() } + +pub(crate) fn js_string_array(array: js_sys::Array) -> Result, Error> { + let mut parsed = Vec::new(); + + for arg in array { + match arg.dyn_into::() { + Ok(arg) => parsed.push(String::from(arg)), + Err(_) => { + return Err(Error::js(js_sys::TypeError::new( + "Expected an array of strings", + ))); + } + } + } + + Ok(parsed) +} + +pub(crate) fn js_record_of_strings(obj: &js_sys::Object) -> Result, Error> { + let mut parsed = Vec::new(); + + for (key, value) in crate::utils::object_entries(obj)? { + let key: String = key.into(); + let value: String = value + .dyn_into::() + .map_err(|_| { + Error::js(js_sys::TypeError::new( + "Expected an object mapping strings to strings", + )) + })? + .into(); + parsed.push((key, value)); + } + + Ok(parsed) +} diff --git a/tests/integration.test.ts b/tests/integration.test.ts index 6c1699b0..4023c85e 100644 --- a/tests/integration.test.ts +++ b/tests/integration.test.ts @@ -69,6 +69,10 @@ describe("Wasmer.spawn", function() { args: ["-c", "import sys; sys.exit(42)"], }); const output = await instance.wait(); + console.log({ + stdout: decoder.decode(output.stdout), + stderr: decoder.decode(output.stderr), + }); expect(output.code).to.equal(42); expect(output.ok).to.be.false; @@ -94,9 +98,22 @@ describe("Wasmer.spawn", function() { expect(output.ok).to.be.true; expect(await stdout).to.equal("2\n"); }); + + it("can start run a bash session", async () => { + const wasmer = new Wasmer(); + + // First, start python up in the background + const instance = await wasmer.spawn("sharrattj/bash", { + uses: ["sharrattj/coreutils"], + }); + // Then, send the command to the REPL + const stdin = instance.stdin!.getWriter(); + await stdin.write(encoder.encode("1 + 1\n")); + }); }); -async function readToEnd(stream: ReadableStream): Promise { + +async function readUntil(stream: ReadableStream, predicate: (chunk: ReadableStreamReadResult) => boolean): Promise { let reader = stream.getReader(); let pieces: string[] =[]; let chunk: ReadableStreamReadResult; @@ -108,11 +125,15 @@ async function readToEnd(stream: ReadableStream): Promise { const sentence = decoder.decode(chunk.value); pieces.push(sentence); } - } while(!chunk.done); + } while(predicate(chunk)); return pieces.join(""); } +async function readToEnd(stream: ReadableStream): Promise { + return await readUntil(stream, chunk => !chunk.done); +} + async function getPython(): Promise<{container: Container, python: Uint8Array, module: WebAssembly.Module}> { const response = await fetch(wasmerPython, { headers: { "Accept": "application/webc" } From e62c2f08ea70ab84222efd5ffb46b92bf7cbfc21 Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Tue, 10 Oct 2023 23:47:14 +0800 Subject: [PATCH 13/13] Make sure to use the wasmer::Module From implementation which goes through our wasm-types-polyfill --- Cargo.lock | 33 +++++++++++++++++---------------- Cargo.toml | 4 ++-- examples/wasmer.sh/index.ts | 8 +++----- package.json | 2 +- src/lib.rs | 9 +++++++-- src/run.rs | 1 + src/runtime.rs | 12 +++++++++++- src/tasks/scheduler.rs | 1 + src/tasks/worker.js | 2 ++ 9 files changed, 45 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5286e58e..34a62932 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1423,9 +1423,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.15" +version = "0.38.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2f9da0cbd88f9f09e7814e388301c8414c51c62aa6ce1e4b5c551d49d96e531" +checksum = "f25469e9ae0f3d0047ca8b93fc56843f38e6774f0914a107ff8b41be8be8e0b7" dependencies = [ "bitflags 2.4.0", "errno", @@ -1592,9 +1592,9 @@ dependencies = [ [[package]] name = "sharded-slab" -version = "0.1.6" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1b21f559e07218024e7e9f90f96f601825397de0e25420135f7f952453fed0b" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" dependencies = [ "lazy_static", ] @@ -2198,7 +2198,7 @@ dependencies = [ [[package]] name = "wai-bindgen-wasmer" -version = "0.14.0" +version = "0.15.0" dependencies = [ "anyhow", "bitflags 1.3.2", @@ -2380,7 +2380,7 @@ dependencies = [ [[package]] name = "wasmer" -version = "4.2.1" +version = "4.2.2" dependencies = [ "bytes", "cfg-if 1.0.0", @@ -2394,6 +2394,7 @@ dependencies = [ "shared-buffer", "target-lexicon", "thiserror", + "tracing", "wasm-bindgen", "wasmer-compiler", "wasmer-derive", @@ -2407,7 +2408,7 @@ dependencies = [ [[package]] name = "wasmer-compiler" -version = "4.2.1" +version = "4.2.2" dependencies = [ "backtrace", "bytes", @@ -2431,7 +2432,7 @@ dependencies = [ [[package]] name = "wasmer-derive" -version = "4.2.1" +version = "4.2.2" dependencies = [ "proc-macro-error", "proc-macro2", @@ -2459,7 +2460,7 @@ dependencies = [ [[package]] name = "wasmer-types" -version = "4.2.1" +version = "4.2.2" dependencies = [ "bytecheck", "enum-iterator", @@ -2474,7 +2475,7 @@ dependencies = [ [[package]] name = "wasmer-vm" -version = "4.2.1" +version = "4.2.2" dependencies = [ "backtrace", "cc", @@ -2500,7 +2501,7 @@ dependencies = [ [[package]] name = "wasmer-wasix" -version = "0.14.0" +version = "0.15.0" dependencies = [ "anyhow", "async-trait", @@ -2595,7 +2596,7 @@ dependencies = [ [[package]] name = "wasmer-wasix-types" -version = "0.14.0" +version = "0.15.0" dependencies = [ "anyhow", "bitflags 1.3.2", @@ -2633,9 +2634,9 @@ dependencies = [ [[package]] name = "wast" -version = "65.0.2" +version = "66.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a55a88724cf8c2c0ebbf32c8e8f4ac0d6aa7ba6d73a1cfd94b254aa8f894317e" +checksum = "0da7529bb848d58ab8bf32230fc065b363baee2bd338d5e58c589a1e7d83ad07" dependencies = [ "leb128", "memchr", @@ -2645,9 +2646,9 @@ dependencies = [ [[package]] name = "wat" -version = "1.0.74" +version = "1.0.75" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d83e1a8d86d008adc7bafa5cf4332d448699a08fcf2a715a71fbb75e2c5ca188" +checksum = "4780374047c65b6b6e86019093fe80c18b66825eb684df778a4e068282a780e7" dependencies = [ "wast", ] diff --git a/Cargo.toml b/Cargo.toml index d45acd09..4ee32a8b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,8 +33,8 @@ wasm-bindgen = { version = "0.2" } wasm-bindgen-downcast = "0.1" wasm-bindgen-futures = "0.4" wasm-bindgen-test = "0.3.37" -wasmer = { version = "4.2", default-features = false, features = ["js", "js-default"] } -wasmer-wasix = { version = "0.14", default-features = false, features = ["js", "js-default"] } +wasmer = { version = "4.2.2", default-features = false, features = ["js", "js-default", "tracing", "wasm-types-polyfill"] } +wasmer-wasix = { version = "0.15", default-features = false, features = ["js", "js-default"] } wee_alloc = { version = "0.4", optional = true } webc = "5.3.0" shared-buffer = "0.1.3" diff --git a/examples/wasmer.sh/index.ts b/examples/wasmer.sh/index.ts index 5de5a2c7..1a13012b 100644 --- a/examples/wasmer.sh/index.ts +++ b/examples/wasmer.sh/index.ts @@ -17,17 +17,15 @@ async function main() { const element = document.getElementById("app")!; term.open(element); - term.onResize(console.log); - term.writeln("Starting..."); - term.onData(console.error); - - console.log("Starting instance"); + term.writeln("Starting..."); const wasmer = new Wasmer(); while (true) { + console.log("Starting instance"); await runInstance(term, wasmer, packageName, { args, uses }); console.log("Rebooting..."); + term.writeln("Rebooting..."); } } diff --git a/package.json b/package.json index 6d831c37..4e5ef30b 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "access": "public" }, "scripts": { - "build": "wasm-pack build --release --target=web --weak-refs && wasm-opt pkg/wasmer_wasix_js_bg.wasm -O2 -o pkg/wasmer_wasix_js_bg.wasm && wasm-strip pkg/wasmer_wasix_js_bg.wasm && rollup -c --environment BUILD:production", + "build": "wasm-pack build --release --target=web --weak-refs && wasm-opt pkg/wasmer_wasix_js_bg.wasm -O2 -o pkg/wasmer_wasix_js_bg.wasm && rollup -c --environment BUILD:production", "build:dev": "wasm-pack build --dev --target=web --weak-refs && rollup -c --environment BUILD:development", "dev": "rollup -c -w", "test": "web-test-runner 'tests/**/*.test.ts' --node-resolve --esbuild-target auto --config ./web-dev-server.config.mjs", diff --git a/src/lib.rs b/src/lib.rs index 54037984..321990b4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,8 +27,13 @@ use js_sys::{JsString, Uint8Array}; use tracing_subscriber::{prelude::__tracing_subscriber_SubscriberExt, EnvFilter}; use wasm_bindgen::prelude::wasm_bindgen; -pub(crate) const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "-", env!("CARGO_PKG_VERSION")); -const RUST_LOG: &[&str] = &["warn", "wasmer_wasix=info", "wasmer_wasix_js=debug"]; +pub(crate) const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); +const RUST_LOG: &[&str] = &[ + "info", + "wasmer_wasix=debug", + "wasmer_wasix_js=debug", + "wasmer=debug", +]; #[wasm_bindgen] pub fn wat2wasm(wat: JsString) -> Result { diff --git a/src/run.rs b/src/run.rs index c41d0a06..dec41a75 100644 --- a/src/run.rs +++ b/src/run.rs @@ -28,6 +28,7 @@ pub fn run( let (stdin, stdout, stderr) = config.configure_builder(&mut builder)?; let (sender, receiver) = oneshot::channel(); + let module = wasmer::Module::from(wasm_module); // Note: The WasiEnvBuilder::run() method blocks, so we need to run it on diff --git a/src/runtime.rs b/src/runtime.rs index ea7c33ae..7ebbbbc3 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -119,7 +119,17 @@ impl wasmer_wasix::runtime::Runtime for Runtime { fn load_module_sync(&self, wasm: &[u8]) -> Result { let wasm = unsafe { js_sys::Uint8Array::view(wasm) }; let module = js_sys::WebAssembly::Module::new(&wasm).map_err(crate::utils::js_error)?; - Ok(module.into()) + // Note: We need to use this From impl because it will use the + // wasm-types-polyfill to parse the *.wasm file's import section. + // + // The browser doesn't give you any way to inspect the imports at the + // moment, so without the polyfill we'll always assume the module wants + // a minimum of 1 page of memory. This causes modules that want more + // memory by default (e.g. sharrattj/bash) to fail with an instantiation + // error. + // + // https://github.com/wasmerio/wasmer/blob/8ec4f1d76062e2a612ac2f70f4a73eaf59f8fe9f/lib/api/src/js/module.rs#L323-L328 + Ok(wasmer::Module::from((module, wasm.to_vec()))) } fn tty(&self) -> Option<&(dyn wasmer_wasix::os::TtyBridge + Send + Sync)> { diff --git a/src/tasks/scheduler.rs b/src/tasks/scheduler.rs index cb1d70a6..c39cadc4 100644 --- a/src/tasks/scheduler.rs +++ b/src/tasks/scheduler.rs @@ -221,6 +221,7 @@ pub(crate) enum SchedulerMessage { msg: WorkerMessage, }, /// Tell all workers to cache a WebAssembly module. + #[allow(dead_code)] CacheModule { hash: WebcHash, module: wasmer::Module, diff --git a/src/tasks/worker.js b/src/tasks/worker.js index 951206e8..1d51ffb9 100644 --- a/src/tasks/worker.js +++ b/src/tasks/worker.js @@ -13,6 +13,8 @@ let handleMessage = async data => { }; globalThis.onmessage = async ev => { + console.log(globalThis.name, ev.data); + if (ev.data.type == "init") { const { memory, module, id } = ev.data; // HACK: This populates global variables as a side-effect