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

Add support for compiling with atomics for Node.js #4318

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading