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

Fix wasm32 compile errors #75

Merged
merged 4 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ jobs:
- uses: actions/checkout@v4
- name: Install Rust
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
- run: rustup target add wasm32-unknown-unknown
- run: cargo build --all --all-features --all-targets
if: startsWith(matrix.rust, 'nightly')
- name: Run cargo check (without dev-dependencies to catch missing feature flags)
if: startsWith(matrix.rust, 'nightly')
run: cargo check -Z features=dev_dep
- run: cargo test
- run: cargo check --all --all-features --target wasm32-unknown-unknown

msrv:
runs-on: ubuntu-latest
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ fastrand = "2.0.0"
futures-lite = { version = "2.0.0", default-features = false }
slab = "0.4.4"

[target.'cfg(target_family = "wasm")'.dependencies]
js-sys = "0.3.65"
wasm-bindgen = "0.2.88"

notgull marked this conversation as resolved.
Show resolved Hide resolved
[dev-dependencies]
async-channel = "2.0.0"
async-io = "2.1.0"
Expand Down
13 changes: 12 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,18 @@ impl<'a> Executor<'a> {

/// Returns a reference to the inner state.
fn state(&self) -> &Arc<State> {
self.state.get_or_init_blocking(|| Arc::new(State::new()))
#[cfg(not(target_family = "wasm"))]
{
return self.state.get_or_init_blocking(|| Arc::new(State::new()));
}

// Some projects use this on WASM for some reason. In this case get_or_init_blocking
// doesn't work. Just poll the future once and panic if there is contention.
#[cfg(target_family = "wasm")]
future::block_on(future::poll_once(
self.state.get_or_init(|| async { Arc::new(State::new()) }),
))
.expect("encountered contention on WASM")
}
}

Expand Down
33 changes: 33 additions & 0 deletions src/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//! WebAssembly implementation of the executor.
notgull marked this conversation as resolved.
Show resolved Hide resolved

use std::collections::VecDeque;
use std::sync::{Arc, Mutex};

use async_task::{Runnable, Task};
use js_sys::Promise;
use wasm_bindgen::prelude::*;

/// The state of the executor.
pub(super) struct State {

}

impl State {
/// Create a new, empty executor state.
#[inline]
pub(crate) fn new() -> Self {

}
}

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen]
fn queueMicrotask(closure: &Closure<dyn FnMut(JsValue)>);

type Global;

#[wasm_bindgen(method, getter, js_name = queueMicrotask)]
fn hasQueueMicrotask(this: &Global) -> JsValue;
}

Loading