Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import memory and enable module() for Node.js #4349

Merged
merged 1 commit into from
Dec 15, 2024

Conversation

daxpedda
Copy link
Collaborator

Revert #4318 in favor of actually importing the memory. This is getting us closer to actually be able to support multi-threading on Node.js.

I briefly attempted to get this into the bundler target as well. But quickly encountered webpack/webpack#8157.

@daxpedda
Copy link
Collaborator Author

daxpedda commented Dec 10, 2024

Alright, I confirmed that multi-threading actually works with Node.js and WBG. Its a bit messed up because of two things:

  • Node.js doesn't actually support the Web Worker API, but has its own custom one: worker_threads. See Support Web Workers nodejs/node#43583.
  • For some reason I'm not aware of and totally lack the context, we rename the module of all our internal imports to the name of the JS shim file. I'm afraid to change anything there because I don't know how people are deploying Node.js stuff. Definitely not impossible to work around right now.

Here is some sample code for the interested:

use js_sys::{Array, Object};
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::{JsCast, JsValue};
use web_sys::console;

fn main() {
    let url = META.with(Meta::url);
    let import_module = format!("./{}", url.rsplit_once('/').unwrap().1);
    let script = include_str!("worker.js").replacen("@shim.js", &url, 1);
    let script = script.replacen("@import_module", &import_module, 2);
    let options = WorkerOptions::new();
    options.set_eval(true);
    options.set_worker_data(&Array::of2(
        &wasm_bindgen::module(),
        &wasm_bindgen::memory(),
    ));

    Worker::new(script, &options).unwrap();
}

#[wasm_bindgen]
pub fn worker_entry() {
    console::log_1(&"Hello from Worker!".into());
}

#[wasm_bindgen]
extern "C" {
    type Meta;

    #[wasm_bindgen(thread_local_v2, js_namespace = import, js_name = meta)]
    static META: Meta;

    #[wasm_bindgen(method, getter)]
    fn url(this: &Meta) -> String;
}

#[wasm_bindgen(module = "node:worker_threads")]
extern "C" {
    type Worker;

    #[wasm_bindgen(constructor, catch)]
    fn new(filename: String, options: &WorkerOptions) -> Result<Worker, JsValue>;

    type WorkerOptions;

    #[wasm_bindgen(method, setter, js_name = eval)]
    fn set_eval(this: &WorkerOptions, value: bool);

    #[wasm_bindgen(method, setter, js_name = workerData)]
    fn set_worker_data(this: &WorkerOptions, value: &JsValue);
}

impl WorkerOptions {
    fn new() -> Self {
        Object::new().unchecked_into()
    }
}
// `worker.js` file.
import { workerData } from 'node:worker_threads'
import * as import0 from '@shim.js'

const [module, memory] = workerData

const imports = { '@import_module': import0, wbg: { memory: memory } }
const wasmInstance = new WebAssembly.Instance(module, imports)
const wasm = wasmInstance.exports

imports['@import_module'].__wbg_set_wasm(wasm, module)
wasm.__wbindgen_start()

wasm.worker_entry()

@daxpedda daxpedda force-pushed the memory-module-nodejs branch 2 times, most recently from 0168d14 to 007573b Compare December 10, 2024 14:51
@daxpedda daxpedda force-pushed the memory-module-nodejs branch from 007573b to b909d44 Compare December 15, 2024 09:13
@daxpedda daxpedda merged commit 237babb into rustwasm:main Dec 15, 2024
67 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant