Skip to content

Commit dac67cc

Browse files
authored
* working on unwind support * start working on unwind support now that it can compile * finished unwind impl, need to migrate example * working on tests and migration guide * got wasm-pack to run successfully * setting up testing harness with nodejs worker threads * working on async main * async main implementation + docs * working on setting up test harness * working on setting up playwright for browser testing * playwright tests framework ok * fix a few harness issues * disable workflows during draft * panic=abort can also terminate reliably * web and nodejs target support * add deno harness and working on bundlers * bundler harness * added available_parallelism test, of course not supported * add arc_atomic test and simplify test harness * improve example and test structure and temporary disable bun tests due to seg fault * mutex example * mutex poison example and test * add test selection cli * remove some dependencies by inlining implementation * working on changing ThreadProc to work with non Send futures * async thread example * omg async panic works * finally got to the bottom of async panic * example website somewhat works * nit touches for the website * setup cloudflare pages * bug fix and setup wrangler * setup docs package * working on docs * migrate all docs * intro page of the doc * add workflow for github pages * clean up files and publish github pages once * fix doc build command in ci * fix other ci * documentation audit * documentation audit pass * ran test pass with node 26 * fixes after final PR audit
1 parent 4878f51 commit dac67cc

180 files changed

Lines changed: 13042 additions & 2315 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.cargo/config.toml‎

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
[target.wasm32-unknown-unknown]
2-
rustflags = [
3-
"-C", "target-feature=+atomics",
4-
"-Clink-args=--shared-memory",
5-
"-Clink-args=--import-memory",
6-
"-Clink-args=--max-memory=1073741824",
7-
"-Clink-args=--export=__wasm_init_tls",
8-
"-Clink-args=--export=__tls_size",
9-
"-Clink-args=--export=__tls_align",
10-
"-Clink-args=--export=__tls_base",
11-
]
12-
# Ciantic: Tested +simd128 22.7.2021, didn't work! Got some wasm-opt problems.
13-
# 2024-10-01 - It now works, but threading works without it. So probably best to wait for it to stabilize.
14-
# 2025-06-12 - mutable-globals is enabled by default, and bulk-memory is enabled by default on Rust 1.87+
15-
# 2025-10-02 - rust now requires extra -Clink-args to enable shared-memory, see https://github.com/rust-lang/rust/pull/147225
16-
17-
[unstable]
18-
build-std = ["panic_abort", "std"]
19-
1+
# this is mainly for rust-analyzer
2+
[env]
3+
WASM_BINDGEN_USE_JS_SYS="1"

‎.github/workflows/docs.yml‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Docs
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
docs:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: Pistonight/mono-dev/actions/setup@main
16+
with:
17+
ecma-pnpm: true
18+
tool-mdbook: true
19+
- run: task build-doc
20+
- uses: actions/upload-pages-artifact@v5
21+
if: github.event_name != 'pull_request'
22+
with:
23+
path: packages/docs/book
24+
retention-days: 3
25+
26+
deploy-docs:
27+
name: deploy-docs
28+
needs:
29+
- docs
30+
if: github.event_name != 'pull_request'
31+
permissions:
32+
pages: write
33+
id-token: write
34+
environment:
35+
name: github-pages
36+
url: ${{ steps.deployment.outputs.page_url }}
37+
runs-on: ubuntu-latest
38+
steps:
39+
- id: deployment
40+
uses: actions/deploy-pages@v5

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ node_modules
55
!/src/js/createDispatcher.min.js
66
/mono-dev
77
rust-toolchain
8+
.task
9+
.cert

‎.prettierignore‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎Cargo.toml‎

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,19 @@
1-
[package]
2-
name = "wasm-bindgen-spawn"
3-
version = "0.0.5"
4-
edition = "2024"
5-
description = "Web Worker Multithreading library for wasm-bindgen the uses shared memory"
6-
repository = "https://github.com/Pistonite/wasm-bindgen-spawn"
7-
license = "MIT"
8-
authors = ["Pistonight <pistonknight@outlook.com>"]
9-
keywords = ["WASM", "threads", "atomics", "wasm-bindgen", "shared-memory"]
10-
categories = ["web-programming", "wasm"]
11-
include = [
12-
"/.cargo/config.toml",
13-
"/.gitignore",
14-
"src/js/createDispatcher.min.js",
15-
"src/**/*.rs",
16-
"Cargo.toml",
17-
"/LICENSE",
18-
"/README.md",
1+
[workspace]
2+
resolver = "2"
3+
members = [
4+
"packages/example",
5+
"packages/lib",
196
]
207

21-
[dependencies]
22-
js-sys = "0.3.83"
23-
oneshot = { version = "0.1.11", default-features = false, features = ["std"] }
24-
static_assertions = "1.1.0"
25-
thiserror = "2.0.17"
26-
wasm-bindgen = "0.2.106"
27-
wasm-bindgen-futures = { version = "0.4.56", optional = true }
8+
[workspace.dependencies]
9+
# If versions are changed please also update docs/src/support.md
10+
wasm-bindgen = "0.2.127"
11+
js-sys = "0.3.104"
12+
2813

29-
[features]
30-
default = ["async"]
31-
async = ["wasm-bindgen-futures"]
14+
anyhow = "1.0.104"
15+
console_error_panic_hook = "0.1.7"
16+
oneshot = { version = "0.2.1", default-features = false }
17+
tokio = "1.53.1"
18+
static_assertions = "1.1.0"
19+
thiserror = "2.0.20"

‎README.md‎

Lines changed: 9 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# wasm-bindgen-spawn
22

3-
![Build Badge](https://img.shields.io/github/check-runs/Pistonite/wasm-bindgen-spawn/main)
43
![Version Badge](https://img.shields.io/crates/v/wasm-bindgen-spawn)
54
![License Badge](https://img.shields.io/github/license/Pistonite/wasm-bindgen-spawn)
65
![Issue Badge](https://img.shields.io/github/issues/Pistonite/wasm-bindgen-spawn)
@@ -9,10 +8,10 @@ A Web Worker based multithreading library for Rust and WebAssembly.
98

109
This uses the WebAssembly [threads proposal](https://github.com/WebAssembly/threads/blob/master/proposals/threads/Overview.md)
1110
and shared memory to communicate between workers (once they are started), instead of `postMessage`.
12-
The threads proposal is currently in [phase 4](https://webassembly.org/features/) and available in Chrome, Firefox, Safari and Node.js
11+
The threads proposal is currently in [phase 4](https://webassembly.org/features/) and available in all major browsers and runtimes.
1312

1413
At the current stage, this is the closest thing to `std::thread::spawn`
15-
that "Just Works" for `wasm32-unknown-unknown` target. You can:
14+
that "Just Works" for `wasm32-unknown-unknown` target. For example you can:
1615
- Spawn a thread with a Rust closure
1716
- Join a thread
1817
- Send data between threads using channels
@@ -23,73 +22,12 @@ will remain on version `0.0.x` until all features required are in stable Rust,
2322
standardized in WASM, and baseline widely available across browsers.
2423

2524
## Examples
26-
The [`examples`](https://github.com/Pistonite/wasm-bindgen-spawn/tree/main/example) directory
27-
on GitHub contains a full example using Vite. Check out the [live demo](https://wasm-bindgen-spawn.pistonite.org)
25+
The [Playground](https://wbgspawn-playground.pistonite.dev) has runnable examples and links to their source code on GitHub.
2826

29-
See [ThreadCreator](https://docs.rs/wasm-bindgen-spawn/latest/wasm_bindgen_spawn/struct.ThreadCreator.html) for the main API.
27+
## Documentation
28+
- For setup, tutorial, and concepts, please refer to [the book](https://wbgspawn.pistonite.dev).
29+
- For technical reference, refer to [API documentation on docs.rs](https://docs.rs/wasm-bindgen-spawn).
30+
- There is also a [blog post](https://wbgspawn.psitonite.dev/design.html) I wrote when I first made this library
3031

31-
## Background/Design
32-
I wrote a blog on how and why this library is designed this way,
33-
and what the limitations are. You can read it [here](https://pistonite.github.io/wasm-bindgen-spawn).
34-
35-
## Requirements
36-
37-
### Cross-Origin Isolation
38-
39-
You can read more about this in the [web dev article](https://web.dev/articles/coop-coep). Long story short:
40-
- This is required for `SharedArrayBuffer`
41-
- This is to mitigate Spectre-like attacks
42-
43-
To get started, the server that serves the main document must send these headers:
44-
```
45-
Cross-Origin-Embedder-Policy: require-corp
46-
Cross-Origin-Opener-Policy: same-origin
47-
```
48-
49-
You can check if the document is in a cross-origin isolated context by running this in the console:
50-
```javascript
51-
self.crossOriginIsolated
52-
```
53-
54-
Read the full article for more details on the implications of Cross-Origin Isolation.
55-
56-
### Rust Nightly and `target-feature`
57-
1. Create a `rust-toolchain` file (no extensions) and put `nightly` in it, to use the nightly toolchain
58-
```sh
59-
echo "nightly" > rust-toolchain
60-
```
61-
2. Add the following to `.cargo/config.toml`
62-
```toml
63-
[target.wasm32-unknown-unknown]
64-
rustflags = [
65-
"-C", "target-feature=+atomics",
66-
"-Clink-args=--shared-memory",
67-
"-Clink-args=--import-memory",
68-
"-Clink-args=--max-memory=1073741824",
69-
"-Clink-args=--export=__wasm_init_tls",
70-
"-Clink-args=--export=__tls_size",
71-
"-Clink-args=--export=__tls_align",
72-
"-Clink-args=--export=__tls_base",
73-
]
74-
# You also need `bulk-memory` for Rust < 1.87. For 1.87+ it's enabled by default
75-
# rustflags = ["-C", "target-feature=+atomics,+bulk-memory"]
76-
77-
[unstable]
78-
build-std = ["panic_abort", "std"]
79-
```
80-
81-
### `wasm-pack` Target
82-
Currently, this library only supports the `no-modules` target:
83-
```sh
84-
wasm-pack build -t no-modules
85-
```
86-
87-
### WASM in Web Worker
88-
Since the main thread in web cannot block, you must use blocking operations in a
89-
web worker, this include:
90-
1. Calling `join` on a JoinHandle
91-
2. Calling `recv` on a Receiver in the `std::sync` library or `oneshot` library.
92-
93-
The [example](https://github.com/Pistonite/wasm-bindgen-spawn/tree/main/example) shows how to put the WASM module in the worker. You can
94-
then use some kind of RPC with `postMessage` to communicate between the main thread and the worker.
95-
This is probably something you have to do anyway to avoid the heavy, multithreaded computation freezing the UI.
32+
## Contributing
33+
Contribution is accepted. Please refer to the [Contribution Guidelines](https://wbgspawn.pistonite.dev/contributing.html)

‎Taskfile.yml‎

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,73 @@
11
version: '3'
22

33
includes:
4-
example:
5-
aliases: [ex]
6-
taskfile: ./example
7-
dir: ./example
4+
common:
5+
taskfile: ./node_modules/mono-dev/task/common.yaml
6+
flatten: true
7+
optional: true
88

99
cargo:
10-
taskfile: ./mono-dev/task/cargo.yaml
10+
taskfile: ./node_modules/mono-dev/task/cargo.yaml
1111
internal: true
1212
optional: true
1313

14+
disp: { taskfile: packages/dispatcher, dir: packages/dispatcher, internal: true }
15+
example: { taskfile: packages/example, dir: packages/example, internal: true }
16+
lib: { taskfile: packages/lib, dir: packages/lib, internal: true }
17+
test: { taskfile: packages/test, dir: packages/test, internal: true }
18+
website: { taskfile: packages/website, dir: packages/website, internal: true }
19+
docs: { taskfile: packages/docs, dir: packages/docs, internal: true }
20+
21+
1422
tasks:
1523
install-cargo-extra-tools:
1624
aliases: [icets]
1725
cmds:
18-
- cargo install txtpp
19-
26+
# requires edge wasm-pack for a newer version of binaryen
27+
- cargo install wasm-pack --git https://github.com/wasm-bindgen/wasm-pack --branch master
28+
- cargo binstall shwoop mdbook
2029
install:
2130
cmds:
22-
- rm -rf mono-dev
23-
- git clone https://github.com/Pistonight/mono-dev --depth 1
24-
- echo "nightly" > example/rust-toolchain
31+
- pnpm install
32+
- task: docs:install
2533
- echo "nightly" > rust-toolchain
2634

27-
build:
28-
desc: Incrementally dev build
29-
cmds:
30-
- txtpp src/js/createDispatcher.min.js
31-
- cd example && pnpm run wasm
32-
3335
check:
34-
desc: Check for issues
35-
cmds:
36-
- task: cargo:clippy-all
37-
- task: cargo:fmt-check
36+
- task: disp:check
37+
- task: disp:build
38+
- task: lib:check
39+
- task: example:check
40+
- task: test:check
41+
- task: website:check
3842

3943
fix:
40-
desc: Fix issues
44+
- task: disp:fix
45+
- task: test:fix
46+
- task: website:fix
47+
- cargo fmt
48+
49+
build:
50+
desc: Ensure everything builds
4151
cmds:
42-
- task: cargo:fmt-fix
43-
- txtpp src -r
52+
- task: build-test
53+
- task: build-web
54+
- task: build-doc
55+
56+
build-doc:
57+
- task: docs:install
58+
- task: docs:build
59+
60+
build-test:
61+
- task: test:build
62+
63+
build-web:
64+
- task: website:build
65+
66+
clean:
67+
- rm -rf target node_modules
68+
- rm -rf packages/build-*/target
69+
- rm -rf packages/dispatcher/node_modules packages/dispatcher/.task
70+
- rm -rf packages/dispatcher/node_modules packages/dispatcher/.task
71+
- rm -rf packages/lib/.task packages/lib/target
72+
- rm -rf packages/test/.task packages/test/target packages/test/node_modules
73+
- rm -rf packages/website/.task packages/website/dist packages/website/node_modules

‎example/.cargo/config.toml‎

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)