50 lines
1.7 KiB
Rust
50 lines
1.7 KiB
Rust
//! various utilities for usage with webassembly and webgl.
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
/// set panic hook to the console_error_panic_hook crate. this allows for better debugging, but it
|
|
/// will result in a higher code size. requires the console_error_panic_hook feature.
|
|
pub fn set_panic_hook() {
|
|
#[cfg(feature = "console_error_panic_hook")]
|
|
console_error_panic_hook::set_once();
|
|
}
|
|
|
|
/// prints an error message via alert(). the error message is provided as a JsValue in order to be
|
|
/// compatible with errors thrown by the javascript runtime.
|
|
pub fn alert_err(msg: &JsValue) {
|
|
let mut s = String::from("an error occured! --- ");
|
|
s.push_str(
|
|
&msg.as_string()
|
|
.unwrap_or_else(|| "(unknown error message)".to_string()),
|
|
);
|
|
web_sys::window().unwrap().alert_with_message(&s).unwrap();
|
|
}
|
|
|
|
/// create a javascript Float32Array from a regular rust array
|
|
#[macro_export]
|
|
macro_rules! f32_array {
|
|
($arr: expr) => {{
|
|
let memory_buffer = wasm_bindgen::memory()
|
|
.dyn_into::<WebAssembly::Memory>()?
|
|
.buffer();
|
|
let arr_location = $arr.as_ptr() as u32 / 4;
|
|
let array = js_sys::Float32Array::new(&memory_buffer)
|
|
.subarray(arr_location, arr_location + $arr.len() as u32);
|
|
array
|
|
}};
|
|
}
|
|
|
|
/// create a javascript Uint16Array from a regular rust array
|
|
#[macro_export]
|
|
macro_rules! u16_array {
|
|
($arr: expr) => {{
|
|
let memory_buffer = wasm_bindgen::memory()
|
|
.dyn_into::<WebAssembly::Memory>()?
|
|
.buffer();
|
|
let arr_location = $arr.as_ptr() as u32 / 2;
|
|
let array = js_sys::Uint16Array::new(&memory_buffer)
|
|
.subarray(arr_location, arr_location + $arr.len() as u32);
|
|
array
|
|
}};
|
|
}
|