From 52da98ecc4bbed978fb7982f97e650671ede754e Mon Sep 17 00:00:00 2001 From: Samuel Maier Date: Sun, 4 Aug 2024 11:42:19 +0200 Subject: [PATCH 1/7] Add example for wasm_threads usage --- examples/wasm_threads/.cargo/config.toml | 8 + examples/wasm_threads/Cargo.lock | 300 ++++++++++++++++++++++ examples/wasm_threads/Cargo.toml | 10 + examples/wasm_threads/README.md | 32 +++ examples/wasm_threads/Trunk.toml | 3 + examples/wasm_threads/assets/_headers | 10 + examples/wasm_threads/index.html | 21 ++ examples/wasm_threads/rust-toolchain.toml | 6 + examples/wasm_threads/src/main.rs | 77 ++++++ 9 files changed, 467 insertions(+) create mode 100644 examples/wasm_threads/.cargo/config.toml create mode 100644 examples/wasm_threads/Cargo.lock create mode 100644 examples/wasm_threads/Cargo.toml create mode 100644 examples/wasm_threads/README.md create mode 100644 examples/wasm_threads/Trunk.toml create mode 100644 examples/wasm_threads/assets/_headers create mode 100644 examples/wasm_threads/index.html create mode 100644 examples/wasm_threads/rust-toolchain.toml create mode 100644 examples/wasm_threads/src/main.rs diff --git a/examples/wasm_threads/.cargo/config.toml b/examples/wasm_threads/.cargo/config.toml new file mode 100644 index 00000000..898afc89 --- /dev/null +++ b/examples/wasm_threads/.cargo/config.toml @@ -0,0 +1,8 @@ +# Translating complex cargo invocation into this config file, so that trunk will use the same setup +# https://github.com/chemicstry/wasm_thread/blob/main/build_wasm.sh + +[target.wasm32-unknown-unknown] +rustflags = ["-C", "target-feature=+atomics,+bulk-memory,+mutable-globals"] + +[unstable] +build-std = ["std,panic_abort"] diff --git a/examples/wasm_threads/Cargo.lock b/examples/wasm_threads/Cargo.lock new file mode 100644 index 00000000..c192ea64 --- /dev/null +++ b/examples/wasm_threads/Cargo.lock @@ -0,0 +1,300 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "console_error_panic_hook" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" +dependencies = [ + "cfg-if", + "wasm-bindgen", +] + +[[package]] +name = "console_log" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be8aed40e4edbf4d3b4431ab260b63fdc40f5780a4766824329ea0f1eefe3c0f" +dependencies = [ + "log", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "futures" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" + +[[package]] +name = "futures-executor" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" + +[[package]] +name = "futures-macro" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" + +[[package]] +name = "futures-task" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" + +[[package]] +name = "futures-util" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "js-sys" +version = "0.3.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "pin-project-lite" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + +[[package]] +name = "syn" +version = "2.0.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "wasm-bindgen" +version = "0.2.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" + +[[package]] +name = "wasm_thread" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22bc95de7e96023c115820c1b87504cdd316ed958d2dfb97dca502cc6e5772a1" +dependencies = [ + "futures", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "wasm_threads" +version = "0.1.0" +dependencies = [ + "console_error_panic_hook", + "console_log", + "log", + "wasm_thread", +] + +[[package]] +name = "web-sys" +version = "0.3.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" +dependencies = [ + "js-sys", + "wasm-bindgen", +] diff --git a/examples/wasm_threads/Cargo.toml b/examples/wasm_threads/Cargo.toml new file mode 100644 index 00000000..b7091a6f --- /dev/null +++ b/examples/wasm_threads/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "wasm_threads" +version = "0.1.0" +edition = "2021" + +[dependencies] +console_error_panic_hook = "0.1.7" +console_log = { version = "1.0.0", features = ["color"] } +log = "0.4.22" +wasm_thread = "0.3.0" diff --git a/examples/wasm_threads/README.md b/examples/wasm_threads/README.md new file mode 100644 index 00000000..5dd6ceb5 --- /dev/null +++ b/examples/wasm_threads/README.md @@ -0,0 +1,32 @@ +# Support workers with shared memory and one wasm binary + +This is a port of [wasm_threads `simple.rs` example](https://github.com/chemicstry/wasm_thread/tree/main?tab=readme-ov-file#simple). + +It should also work similarly with `wasm-bindgen-rayon` and `wasm-mt`. + +## Limitations + +It has a few considerable advantages over the `webworker*` examples, but also considerable disadvantages. + +For starters, `trunk serve` won't work currently, as it doesn't do Cross Site Isolation (setting 2 headers), which is [required for this approach to workers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements). +`sfz` works, with `cargo install sfz` and run `sfz dist --coi` in the root directory of this example, then click on index.html in the website directory. + +These same headers are also required during deployment. Github pages does not allow setting headers, and alternatives such as using `` did not work in my testing, so these sites can't be deployed like that. Cloudflare Pages is a free alternative that allows setting headers that worked for me. + +Then it also requires nightly Rust, because [the standard library needs to be rebuild](https://github.com/RReverser/wasm-bindgen-rayon?tab=readme-ov-file#building-rust-code). + +Additional limitations are listed [here](https://github.com/chemicstry/wasm_thread/tree/7bc7dccebf2d96775b60bf0fda6b4173aa993aff?tab=readme-ov-file#notes-on-wasm-limitations). + +## Advantages + +* code sharing + * improves dev experience + * also means that the WASM binary will be shared, which can in the extreme case half the size of the website. +* shared memory between threads + * can be a huge performance win + +## Notes on applying this + +Note that this requires the [toolchain file](./rust-toolchain.toml) and the [cargo config](.cargo/config.toml). + +The `_headers` file and its copy in `index.html` is simply an example of how to set the headers using Cloudflare Pages. diff --git a/examples/wasm_threads/Trunk.toml b/examples/wasm_threads/Trunk.toml new file mode 100644 index 00000000..842cc2f7 --- /dev/null +++ b/examples/wasm_threads/Trunk.toml @@ -0,0 +1,3 @@ +[build] +target = "index.html" +dist = "dist" diff --git a/examples/wasm_threads/assets/_headers b/examples/wasm_threads/assets/_headers new file mode 100644 index 00000000..8be94abc --- /dev/null +++ b/examples/wasm_threads/assets/_headers @@ -0,0 +1,10 @@ +# Sets headers on cloudflare pages as required for wasm_threads, because they rely on SharedArrayBuffer. +# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements +# https://developers.cloudflare.com/pages/configuration/headers/ +/* + # Alternatively `credentialless` also works + # MDN: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Embedder-Policy + cross-origin-embedder-policy: require-corp + cross-origin-opener-policy: same-origin + # not strictly required, just allows you to load assets from the same... subdomain IIRC. + cross-origin-resource-policy: same-site diff --git a/examples/wasm_threads/index.html b/examples/wasm_threads/index.html new file mode 100644 index 00000000..aad30d32 --- /dev/null +++ b/examples/wasm_threads/index.html @@ -0,0 +1,21 @@ + + + + + + + Trunk | wasm_threads + + + + + + + + diff --git a/examples/wasm_threads/rust-toolchain.toml b/examples/wasm_threads/rust-toolchain.toml new file mode 100644 index 00000000..aba5b4a1 --- /dev/null +++ b/examples/wasm_threads/rust-toolchain.toml @@ -0,0 +1,6 @@ +# Before upgrading check that everything is available on all tier1 targets here: +# https://rust-lang.github.io/rustup-components-history +[toolchain] +channel = "nightly-2024-08-02" +targets = ["wasm32-unknown-unknown"] +components = ["rust-src", "rustfmt", "clippy"] diff --git a/examples/wasm_threads/src/main.rs b/examples/wasm_threads/src/main.rs new file mode 100644 index 00000000..600d6d17 --- /dev/null +++ b/examples/wasm_threads/src/main.rs @@ -0,0 +1,77 @@ +use std::time::Duration; + +use wasm_thread as thread; + +fn main() { + #[cfg(target_arch = "wasm32")] + { + console_log::init().unwrap(); + console_error_panic_hook::set_once(); + } + + #[cfg(not(target_arch = "wasm32"))] + env_logger::init_from_env(env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info")); + + log::info!("Available parallelism: {:?}", thread::available_parallelism()); + + let mut threads = vec![]; + + for _ in 0..2 { + threads.push(thread::spawn(|| { + for i in 1..3 { + log::info!("hi number {} from the spawned thread {:?}!", i, thread::current().id()); + thread::sleep(Duration::from_millis(1)); + } + })); + } + + for i in 1..3 { + log::info!("hi number {} from the main thread {:?}!", i, thread::current().id()); + } + + // It's not possible to do a scope on the main thread, because blocking waits are not supported, but we can use + // scope inside web workers. + threads.push(thread::spawn(|| { + log::info!("Start scope test on thread {:?}", thread::current().id()); + + let mut a = vec![1, 2, 3]; + let mut x = 0; + + thread::scope(|s| { + let handle = s.spawn(|| { + log::info!("hello from the first scoped thread {:?}", thread::current().id()); + // We can borrow `a` here. + log::info!("a = {:?}", &a); + // Return a subslice of borrowed `a` + &a[0..2] + }); + + // Wait for the returned value from first thread + log::info!("a[0..2] = {:?}", handle.join().unwrap()); + + s.spawn(|| { + log::info!("hello from the second scoped thread {:?}", thread::current().id()); + // We can even mutably borrow `x` here, + // because no other threads are using it. + x += a[0] + a[2]; + }); + + log::info!( + "Hello from scope \"main\" thread {:?} inside scope.", + thread::current().id() + ); + }); + + // After the scope, we can modify and access our variables again: + a.push(4); + assert_eq!(x, a.len()); + log::info!("Scope done x = {}, a.len() = {}", x, a.len()); + })); + + // Wait for all threads, otherwise program exits before threads finish execution. + // We can't do blocking join on wasm main thread though, but the browser window will continue running. + #[cfg(not(target_arch = "wasm32"))] + for handle in threads { + handle.join().unwrap(); + } +} From c129c5953a73a9be721e085ca12840fbe1c7dcbe Mon Sep 17 00:00:00 2001 From: Samuel Maier Date: Mon, 5 Aug 2024 12:30:55 +0200 Subject: [PATCH 2/7] Add headers for trunk serve --- examples/wasm_threads/Trunk.toml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/examples/wasm_threads/Trunk.toml b/examples/wasm_threads/Trunk.toml index 842cc2f7..8d2ce960 100644 --- a/examples/wasm_threads/Trunk.toml +++ b/examples/wasm_threads/Trunk.toml @@ -1,3 +1,9 @@ [build] target = "index.html" dist = "dist" + +[serve.headers] +# see ./assets/_headers for more documentation +"cross-origin-embedder-policy"= "require-corp" +"cross-origin-opener-policy"= "same-origin" +"cross-origin-resource-policy"= "same-site" From fb244becf5a8a433fc5e9e792db33ab5490ad562 Mon Sep 17 00:00:00 2001 From: Samuel Maier Date: Mon, 5 Aug 2024 12:31:09 +0200 Subject: [PATCH 3/7] Update and improve documentation --- examples/wasm_threads/README.md | 17 +++++++++++++---- examples/wasm_threads/index.html | 1 + 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/examples/wasm_threads/README.md b/examples/wasm_threads/README.md index 5dd6ceb5..8cfb0cb4 100644 --- a/examples/wasm_threads/README.md +++ b/examples/wasm_threads/README.md @@ -2,20 +2,22 @@ This is a port of [wasm_threads `simple.rs` example](https://github.com/chemicstry/wasm_thread/tree/main?tab=readme-ov-file#simple). -It should also work similarly with `wasm-bindgen-rayon` and `wasm-mt`. +It should also work similarly with `wasm-bindgen-rayon` and other packages that use SharedArrayBuffer. + +An explanation of that approach is described [here](https://rustwasm.github.io/wasm-bindgen/examples/raytrace.html) ## Limitations It has a few considerable advantages over the `webworker*` examples, but also considerable disadvantages. -For starters, `trunk serve` won't work currently, as it doesn't do Cross Site Isolation (setting 2 headers), which is [required for this approach to workers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements). -`sfz` works, with `cargo install sfz` and run `sfz dist --coi` in the root directory of this example, then click on index.html in the website directory. +For starters, this needs Cross Site Isolation (setting 2 headers), which is [required for this approach to workers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements). +We've added them in the trunk config. These same headers are also required during deployment. Github pages does not allow setting headers, and alternatives such as using `` did not work in my testing, so these sites can't be deployed like that. Cloudflare Pages is a free alternative that allows setting headers that worked for me. Then it also requires nightly Rust, because [the standard library needs to be rebuild](https://github.com/RReverser/wasm-bindgen-rayon?tab=readme-ov-file#building-rust-code). -Additional limitations are listed [here](https://github.com/chemicstry/wasm_thread/tree/7bc7dccebf2d96775b60bf0fda6b4173aa993aff?tab=readme-ov-file#notes-on-wasm-limitations). +Additional limitations are listed [here](https://rustwasm.github.io/wasm-bindgen/examples/raytrace.html#caveats) (some of them might be solved or worked around in libraries). Specifically for `wasm_thread` limitations are explained in the comments in the source code. ## Advantages @@ -30,3 +32,10 @@ Additional limitations are listed [here](https://github.com/chemicstry/wasm_thre Note that this requires the [toolchain file](./rust-toolchain.toml) and the [cargo config](.cargo/config.toml). The `_headers` file and its copy in `index.html` is simply an example of how to set the headers using Cloudflare Pages. + +If you get errors such as + +> [Firefox] The WebAssembly.Memory object cannot be serialized. The Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy HTTP headers can be used to enable this. +> [Chrome] SharedArrayBuffer transfer requires self.crossOriginIsolated. + +Then the headers did not set correctly. You can check the response headers on the `/` file in the network tab of the browser developer tools. diff --git a/examples/wasm_threads/index.html b/examples/wasm_threads/index.html index aad30d32..4e0f3890 100644 --- a/examples/wasm_threads/index.html +++ b/examples/wasm_threads/index.html @@ -17,5 +17,6 @@ data-bindgen-target="web" /> +

See the console for the thread output

From 42d6bfd235f571add33ad4044938630b445dd565 Mon Sep 17 00:00:00 2001 From: Samuel Maier Date: Mon, 5 Aug 2024 12:40:46 +0200 Subject: [PATCH 4/7] Add warning about problematic dependencies --- examples/wasm_threads/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/wasm_threads/README.md b/examples/wasm_threads/README.md index 8cfb0cb4..e1beab5b 100644 --- a/examples/wasm_threads/README.md +++ b/examples/wasm_threads/README.md @@ -17,6 +17,8 @@ These same headers are also required during deployment. Github pages does not al Then it also requires nightly Rust, because [the standard library needs to be rebuild](https://github.com/RReverser/wasm-bindgen-rayon?tab=readme-ov-file#building-rust-code). +Some libraries might not work correctly like that, since they are written under the assumption of the historially single threaded wasm32 runtimes. `wgpu` has the `fragile-send-sync-non-atomic-wasm` flag, which if set will not work with this. Eg. `egui` sets this flag currently, though it can be removed with a manual, not well tested [patch](https://github.com/9SMTM6/egui/commit/11b00084e34c8b0ff40bac82274291dff64c26db). + Additional limitations are listed [here](https://rustwasm.github.io/wasm-bindgen/examples/raytrace.html#caveats) (some of them might be solved or worked around in libraries). Specifically for `wasm_thread` limitations are explained in the comments in the source code. ## Advantages From 2a28a230ffdea1cb69aceaf49bbee304d063abd5 Mon Sep 17 00:00:00 2001 From: Samuel Maier Date: Mon, 5 Aug 2024 12:42:07 +0200 Subject: [PATCH 5/7] Fix readme rendering --- examples/wasm_threads/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/wasm_threads/README.md b/examples/wasm_threads/README.md index e1beab5b..761e6317 100644 --- a/examples/wasm_threads/README.md +++ b/examples/wasm_threads/README.md @@ -38,6 +38,7 @@ The `_headers` file and its copy in `index.html` is simply an example of how to If you get errors such as > [Firefox] The WebAssembly.Memory object cannot be serialized. The Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy HTTP headers can be used to enable this. + > [Chrome] SharedArrayBuffer transfer requires self.crossOriginIsolated. Then the headers did not set correctly. You can check the response headers on the `/` file in the network tab of the browser developer tools. From 4646b54b7e428b0fb8b835ab2b4b1d9bf051f160 Mon Sep 17 00:00:00 2001 From: Samuel Maier Date: Mon, 5 Aug 2024 13:03:48 +0200 Subject: [PATCH 6/7] Add another note about rust-analyzer --- .gitignore | 1 + examples/wasm_threads/.vscode/settings.json | 7 +++++++ examples/wasm_threads/README.md | 4 ++++ 3 files changed, 12 insertions(+) create mode 100644 examples/wasm_threads/.vscode/settings.json diff --git a/.gitignore b/.gitignore index 27ffee1e..8835c04a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ target dist site/public/* .vscode +!examples/wasm_threads/.vscode .idea/ .DS_Store /vendor diff --git a/examples/wasm_threads/.vscode/settings.json b/examples/wasm_threads/.vscode/settings.json new file mode 100644 index 00000000..6b2178fe --- /dev/null +++ b/examples/wasm_threads/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + // The build-std flag requires an explicit target. + // This breaks the usual rust-analyzer setup. + // But setting the target manually for the workspace fixes that. + // "rust-analyzer.cargo.target": "x86_64-unknown-linux-gnu", + "rust-analyzer.cargo.target": "wasm32-unknown-unknown", +} \ No newline at end of file diff --git a/examples/wasm_threads/README.md b/examples/wasm_threads/README.md index 761e6317..ca90a605 100644 --- a/examples/wasm_threads/README.md +++ b/examples/wasm_threads/README.md @@ -42,3 +42,7 @@ If you get errors such as > [Chrome] SharedArrayBuffer transfer requires self.crossOriginIsolated. Then the headers did not set correctly. You can check the response headers on the `/` file in the network tab of the browser developer tools. + +## Using rust-analyzer + +Since we use the build-std flag in the toolchain file, and that requires an explicit target to be set for compilation etc., this will break rust-analyzer in many setups. This can be solved by specifying an explicit target for the workspace, such as with the provided [config file for vscode](./.vscode/settings.json). From 4d415129e13d856602102c4cb25063fdfbf022e2 Mon Sep 17 00:00:00 2001 From: Samuel Maier Date: Mon, 5 Aug 2024 14:08:08 +0200 Subject: [PATCH 7/7] Add example to tests --- .github/workflows/ci.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index cb3b8dc9..15958088 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -175,6 +175,7 @@ jobs: - webworker - webworker-gloo - webworker-module + - wasm_threads - yew - yew-tailwindcss - yew-tls