Skip to content

Commit 94b2dc6

Browse files
authored
Add support for multi-threading in Node.js (#4318)
1 parent 89f8a42 commit 94b2dc6

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

.github/workflows/main.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,16 @@ jobs:
206206
test_threads:
207207
name: "Run wasm-bindgen crate tests with multithreading"
208208
runs-on: ubuntu-latest
209+
env:
210+
WASM_BINDGEN_SPLIT_LINKED_MODULES: 1
209211
steps:
210212
- uses: actions/checkout@v4
211213
- run: rustup default nightly-2024-07-06
212214
- run: rustup target add wasm32-unknown-unknown
213215
- run: rustup component add rust-src
214-
# Note: we only run the browser tests here, because wasm-bindgen doesn't support threading in Node yet.
215216
- run: |
216217
RUSTFLAGS='-C target-feature=+atomics,+bulk-memory,+mutable-globals' \
217-
cargo test --target wasm32-unknown-unknown --test headless -Z build-std=std,panic_abort
218+
cargo test --target wasm32-unknown-unknown -Z build-std=std,panic_abort
218219
219220
# I don't know why this is failing so comment this out for now, but ideally
220221
# this would be figured out at some point and solved.

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
## Unreleased
55

6+
### Added
7+
8+
* Add support for multi-threading in Node.js.
9+
[#4318](https://github.com/rustwasm/wasm-bindgen/pull/4318)
10+
611
### Changed
712

813
* Add clear error message to communicate new feature resolver version requirements.

crates/cli-support/src/js/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,8 @@ impl<'a> Context<'a> {
440440
// With normal CommonJS node we need to defer requiring the wasm
441441
// until the end so most of our own exports are hooked up
442442
OutputMode::Node { module: false } => {
443+
self.nodejs_memory();
444+
443445
js.push_str(&self.generate_node_imports());
444446

445447
js.push_str("let wasm;\n");
@@ -483,6 +485,8 @@ impl<'a> Context<'a> {
483485
// and let the bundler/runtime take care of it.
484486
// With Node we manually read the Wasm file from the filesystem and instantiate it.
485487
OutputMode::Bundler { .. } | OutputMode::Node { module: true } => {
488+
self.nodejs_memory();
489+
486490
for (id, js) in iter_by_import(&self.wasm_import_definitions, self.module) {
487491
let import = self.module.imports.get_mut(*id);
488492
import.module = format!("./{}_bg.js", module_name);
@@ -1005,6 +1009,14 @@ __wbg_set_wasm(wasm);"
10051009
Ok((js, ts))
10061010
}
10071011

1012+
fn nodejs_memory(&mut self) {
1013+
if let Some(mem) = self.module.memories.iter_mut().next() {
1014+
if let Some(id) = mem.import.take() {
1015+
self.module.imports.delete(id);
1016+
}
1017+
}
1018+
}
1019+
10081020
fn write_classes(&mut self) -> Result<(), Error> {
10091021
for (class, exports) in self.exported_classes.take().unwrap() {
10101022
self.write_class(&class, &exports)?;

0 commit comments

Comments
 (0)