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

Major refactor and cleanup #14

Merged
merged 24 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
28 changes: 28 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
on: [push, pull_request]

jobs:
test:
# Prevent running double CI when pull request is on the same repository
if: |
(github.event_name != 'pull_request')
|| (github.event.pull_request.head.repo.id != github.event.pull_request.base.repo.id)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Install
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

- name: Rustfmt
run: cargo fmt --all --check

- name: Test native
run: cargo test

- name: Test wasm
run: ./test_wasm.sh

- name: Build docs
env:
RUSTDOCFLAGS: "-Dwarnings"
run: cargo doc --no-deps --lib --target wasm32-unknown-unknown
chemicstry marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Cargo.lock
/examples/target
/examples-wasm-pack/target
.vscode/
!.vscode/settings.json
23 changes: 19 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,34 @@ categories = ["concurrency", "wasm"]
readme = "README.md"

[features]
default = ["es_modules"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only problem I had with the PR, I needed to update { default-features = false }

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought module support is now common enough to be default. I will bump the version of course.

es_modules = []

[dependencies]
wasm-bindgen = "0.2"
web-sys = { version = "0.3", features = ["Blob", "DedicatedWorkerGlobalScope", "MessageEvent", "Url", "Worker", "WorkerType", "WorkerOptions"]}
web-sys = { version = "0.3", features = [
"Blob",
"DedicatedWorkerGlobalScope",
"MessageEvent",
"Url",
"Worker",
"WorkerType",
"WorkerOptions",
"Window",
"Navigator",
"WorkerNavigator",
] }
js-sys = "0.3"
futures = "0.3"
async-channel = "1.4"

[dev-dependencies]
log = "0.4"
env_logger = "0.7"
env_logger = "0.10"
wasm-bindgen-test = "0.3"

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
console_log = { version = "0.2", features = ["color"] }
console_log = { version = "1.0", features = ["color"] }
console_error_panic_hook = "0.1"

[package.metadata.docs.rs]
targets = ["wasm32-unknown-unknown"]
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,22 @@ For a higher-level threading solution, see [wasm-bindgen-rayon](https://github.c

#### wasm-bindgen

- Install nightly toolchain and dependencies:
```bash
rustup toolchain install nightly
rustup component add rust-src --toolchain nightly
cargo install wasm-bindgen-cli
```
- Build with `./build_wasm.sh` (bash) or `./build_wasm.ps1` (PowerShell). This custom build step is required because prebuilt standard library does not have support for atomics yet. Read more about this [here](https://rustwasm.github.io/2018/10/24/multithreading-rust-and-wasm.html).
- Serve `examples` directory over HTTP with cross-origin isolation enabled and open `simple.html` in the browser. Inspect console output. You can use `cargo install sfz` as a basic HTTP server and serve with `sfz examples --coi`.

#### wasm-pack

- Build with `./examples-wasm-pack/web-build.sh` for an example targeting `web`, and `./examples/wasm-pack/web-build-no-module.sh` for an example targeting `no-modules`.
- Install `wasm-pack`:
```bash
cargo install wasm-pack
```
- Build with `./examples-wasm-pack/web-build.sh` for an example targeting `web`, and `./examples-wasm-pack/web-build-no-module.sh` for an example targeting `no-modules`.
- Serve `./examples-wasm-pack/module` or `./examples-wasm-pack/no-module`, respectively, over HTTP and open `simple.html` in browser. Inspect console output.

### Example output
Expand Down
2 changes: 1 addition & 1 deletion build_wasm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ RUSTFLAGS='-C target-feature=+atomics,+bulk-memory,+mutable-globals' \
wasm-bindgen \
target/wasm32-unknown-unknown/release/examples/simple.wasm \
--out-dir ./examples/target/ \
--target no-modules
--target web
4 changes: 2 additions & 2 deletions examples-wasm-pack/.cargo/Config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
target = "wasm32-unknown-unknown"

[target.'cfg(target_arch = "wasm32")']
rustflags = ["-C", "target-feature=+simd128,+atomics,+bulk-memory,+mutable-globals"]
rustflags = ["-C", "target-feature=+atomics,+bulk-memory,+mutable-globals"]

[unstable]
build-std = ["panic_abort", "std"]
build-std = ["panic_abort", "std"]
9 changes: 2 additions & 7 deletions examples-wasm-pack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@ edition = "2021"
crate-type = ["cdylib", "rlib"]

[dependencies]
wasm_thread = { path = "../." }
wasm_thread = { path = "../", default-features = false }
log = "0.4"
env_logger = "0.7"
wasm-bindgen = "0.2"
web-sys = { version = "0.3", features = ["Blob", "DedicatedWorkerGlobalScope", "MessageEvent", "Url", "Worker", "WorkerType", "WorkerOptions"]}
js-sys = "0.3"
futures = "0.3"
async-channel = "1.4"
console_log = { version = "0.2", features = ["color"] }
console_log = { version = "1.0", features = ["color"] }
console_error_panic_hook = "0.1"
26 changes: 13 additions & 13 deletions examples-wasm-pack/module/simple.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<script type="module">
import init, {run} from "./target/simple.js";
init()
.then(() => {
run()
});
</script>
</body>
</html>

<head>
<meta charset="UTF-8" />
</head>

<body>
<script type="module">
import init from "./target/simple.js";
init();
</script>
</body>

</html>
25 changes: 13 additions & 12 deletions examples-wasm-pack/no-module/simple.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<script src="./target/simple.js"></script>
<script type="text/javascript">
wasm_bindgen('./target/simple_bg.wasm').then((wasm) => {
wasm.run();
});
</script>
</body>
</html>

<head>
<meta charset="UTF-8" />
</head>

<body>
<script src="./target/simple.js"></script>
<script type="text/javascript">
wasm_bindgen('./target/simple_bg.wasm');
</script>
</body>

</html>
41 changes: 6 additions & 35 deletions examples-wasm-pack/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,23 @@
use std::time::Duration;

#[cfg(not(target_arch = "wasm32"))]
use std::thread;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
use wasm_thread as thread;

#[cfg(target_arch = "wasm32")]
mod wasm {
use crate::main;
use wasm_bindgen::prelude::*;

// Prevent `wasm_bindgen` from autostarting main on all spawned threads
#[wasm_bindgen(start)]
pub fn dummy_main() {}

// Export explicit run function to start main
#[wasm_bindgen]
pub fn run() {
console_log::init().unwrap();
console_error_panic_hook::set_once();
main();
}
}

#[wasm_bindgen(start)]
fn main() {
#[cfg(not(target_arch = "wasm32"))]
env_logger::init_from_env(
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
);
console_log::init().unwrap();
console_error_panic_hook::set_once();

for _ in 0..2 {
thread::spawn(|| {
for i in 1..3 {
log::info!(
"hi number {} from the spawned thread {:?}!",
i,
thread::current().id()
);
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()
);
log::info!("hi number {} from the main thread {:?}!", i, thread::current().id());
}
}
1 change: 0 additions & 1 deletion examples-wasm-pack/web-build-no-module.ps1
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
$env:RUSTFLAGS="-C target-feature=+atomics,+bulk-memory,+mutable-globals"
wasm-pack build --dev --out-dir ./no-module/target --target no-modules
3 changes: 1 addition & 2 deletions examples-wasm-pack/web-build-no-module.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/bin/sh

RUSTFLAGS="-C target-feature=+atomics,+bulk-memory,+mutable-globals" \
wasm-pack build --dev --out-dir ./no-module/target --target no-modules
wasm-pack build --dev --out-dir ./no-module/target --target no-modules
1 change: 0 additions & 1 deletion examples-wasm-pack/web-build.ps1
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
$env:RUSTFLAGS="-C target-feature=+atomics,+bulk-memory,+mutable-globals"
wasm-pack build --dev --out-dir ./module/target --target web --features wasm_thread/es_modules
3 changes: 1 addition & 2 deletions examples-wasm-pack/web-build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/bin/sh

RUSTFLAGS="-C target-feature=+atomics,+bulk-memory,+mutable-globals" \
wasm-pack build --dev --out-dir ./module/target --target web --features wasm_thread/es_modules
wasm-pack build --dev --out-dir ./module/target --target web --features wasm_thread/es_modules
25 changes: 13 additions & 12 deletions examples/simple.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<script src="./target/simple.js"></script>
<script type="text/javascript">
wasm_bindgen('./target/simple_bg.wasm').then((wasm) => {
wasm.run();
});
</script>
</body>
</html>

<head>
<meta charset="UTF-8" />
</head>

<body>
<script type="module">
import init from "./target/simple.js";
init();
</script>
</body>

</html>
Loading