Skip to content

Commit

Permalink
Merge pull request #14 from chemicstry/refactor
Browse files Browse the repository at this point in the history
Major refactor and cleanup
  • Loading branch information
chemicstry authored Mar 17, 2024
2 parents 0751fa2 + 2ea79c4 commit cd6487e
Show file tree
Hide file tree
Showing 30 changed files with 1,164 additions and 589 deletions.
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
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
27 changes: 22 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasm_thread"
version = "0.2.0"
version = "0.3.0"
authors = ["Jurgis Balciunas <[email protected]>"]
edition = "2018"
description = "An std thread replacement for wasm32 target"
Expand All @@ -13,19 +13,36 @@ categories = ["concurrency", "wasm"]
readme = "README.md"

[features]
default = ["es_modules"]
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"
async-channel = "2.2"
wasm-bindgen-futures = "0.4"

[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"
24 changes: 12 additions & 12 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>

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

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

</html>
23 changes: 12 additions & 11 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>

<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
23 changes: 12 additions & 11 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>

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

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

</html>
Loading

0 comments on commit cd6487e

Please sign in to comment.