ironshield_wasm/lib.rs
1//! IronShield WebAssembly bindings
2
3// Suppress warnings from wasm-bindgen internals during ABI transition
4#![allow(wasm_c_abi)]
5
6mod wasm_compat;
7
8use wasm_bindgen::prelude::*;
9
10/// Support for threading in WASM
11#[cfg(all(feature = "threading", not(feature = "no-threading")))]
12use wasm_bindgen_rayon::init_thread_pool;
13
14#[cfg(all(feature = "threading", not(feature = "no-threading")))]
15use wasm_bindgen_futures::JsFuture;
16
17/// JavaScript-compatible solution result for IronShield challenges
18///
19/// * `solution_str`: String representation of the solution nonce
20/// to avoid JavaScript BigInt precision issues.
21/// * `solution`: Original numeric value for compatibility.
22/// * `challenge_signature_hex`: Challenge signature preserved from the
23/// original challenge.
24
25/// Initializes WebAssembly thread pool for parallel proof-of-work
26///
27/// # Arguments
28/// * `num_threads`: Number of worker threads to spawn
29///
30/// # Note
31/// Only available when compiled with a "threading" feature flag
32#[wasm_bindgen]
33#[cfg(all(feature = "threading", not(feature = "no-threading")))]
34pub async fn init_threads(num_threads: usize) -> Result<(), JsValue> {
35 // Create a shared memory thread pool for parallel processing
36 let promise: js_sys::Promise = init_thread_pool(num_threads);
37 JsFuture::from(promise).await.map(|_| ()).map_err(|e: JsValue| e)
38}
39
40/// Checks if threading is available in the current build.
41///
42/// # Returns
43/// `bool`: `true` if compiled with a "threading" feature, `false` otherwise.
44#[wasm_bindgen]
45pub extern "C" fn are_threads_supported() -> bool {
46 #[cfg(all(feature = "threading", not(feature = "no-threading")))]
47 return true;
48
49 #[cfg(not(all(feature = "threading", not(feature = "no-threading"))))]
50 return false;
51}