Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ libc = "0.2.26"

[target.'cfg(target_os = "hermit")'.dependencies]
hermit-abi = "0.3.0"

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = { version = "0.3.0", features = [
"WorkerNavigator",
"DedicatedWorkerGlobalScope",
], optional = true }

[features]
wasm-bindgen-support = ["web-sys"]
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,22 @@ fn get_num_cpus() -> usize {
unsafe { hermit_abi::get_processor_count() }
}

#[cfg(feature = "wasm-bindgen-support")]
extern crate web_sys;

#[cfg(feature = "wasm-bindgen-support")]
fn get_num_cpus() -> usize {
use web_sys::{wasm_bindgen::JsCast, DedicatedWorkerGlobalScope};

let global = web_sys::js_sys::global();

// DedicatedWorkerGlobalScope is not always the correct type
// but the underlying global will always have access to `navigator`
// either way and this was the simplest way to work with `web_sys`.
let global = global.unchecked_into::<DedicatedWorkerGlobalScope>();
global.navigator().hardware_concurrency() as usize
}

#[cfg(not(any(
target_os = "nacl",
target_os = "macos",
Expand All @@ -449,6 +465,7 @@ fn get_num_cpus() -> usize {
target_os = "netbsd",
target_os = "haiku",
target_os = "hermit",
feature = "wasm-bindgen-support",
windows,
)))]
fn get_num_cpus() -> usize {
Expand Down