Skip to content

Commit

Permalink
Add support for multi-threading in Node.js (#4318)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored Dec 5, 2024
1 parent 89f8a42 commit 94b2dc6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,16 @@ jobs:
test_threads:
name: "Run wasm-bindgen crate tests with multithreading"
runs-on: ubuntu-latest
env:
WASM_BINDGEN_SPLIT_LINKED_MODULES: 1
steps:
- uses: actions/checkout@v4
- run: rustup default nightly-2024-07-06
- run: rustup target add wasm32-unknown-unknown
- run: rustup component add rust-src
# Note: we only run the browser tests here, because wasm-bindgen doesn't support threading in Node yet.
- run: |
RUSTFLAGS='-C target-feature=+atomics,+bulk-memory,+mutable-globals' \
cargo test --target wasm32-unknown-unknown --test headless -Z build-std=std,panic_abort
cargo test --target wasm32-unknown-unknown -Z build-std=std,panic_abort
# I don't know why this is failing so comment this out for now, but ideally
# this would be figured out at some point and solved.
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

## Unreleased

### Added

* Add support for multi-threading in Node.js.
[#4318](https://github.com/rustwasm/wasm-bindgen/pull/4318)

### Changed

* Add clear error message to communicate new feature resolver version requirements.
Expand Down
12 changes: 12 additions & 0 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ impl<'a> Context<'a> {
// With normal CommonJS node we need to defer requiring the wasm
// until the end so most of our own exports are hooked up
OutputMode::Node { module: false } => {
self.nodejs_memory();

js.push_str(&self.generate_node_imports());

js.push_str("let wasm;\n");
Expand Down Expand Up @@ -483,6 +485,8 @@ impl<'a> Context<'a> {
// and let the bundler/runtime take care of it.
// With Node we manually read the Wasm file from the filesystem and instantiate it.
OutputMode::Bundler { .. } | OutputMode::Node { module: true } => {
self.nodejs_memory();

for (id, js) in iter_by_import(&self.wasm_import_definitions, self.module) {
let import = self.module.imports.get_mut(*id);
import.module = format!("./{}_bg.js", module_name);
Expand Down Expand Up @@ -1005,6 +1009,14 @@ __wbg_set_wasm(wasm);"
Ok((js, ts))
}

fn nodejs_memory(&mut self) {
if let Some(mem) = self.module.memories.iter_mut().next() {
if let Some(id) = mem.import.take() {
self.module.imports.delete(id);
}
}
}

fn write_classes(&mut self) -> Result<(), Error> {
for (class, exports) in self.exported_classes.take().unwrap() {
self.write_class(&class, &exports)?;
Expand Down

0 comments on commit 94b2dc6

Please sign in to comment.