1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use js_sys::{Function, Object};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen (extends = Object, js_name = nw)]
#[derive(Debug, Clone)]
pub type Nw;
#[wasm_bindgen(js_namespace = console)]
pub fn error(s: &str);
}
pub fn try_nw() -> Result<Nw, JsValue> {
let nw_opt = Function::new_no_args("return this.nw").call0(&JsValue::undefined());
match nw_opt {
Ok(value) => {
if value.is_undefined() {
error("NW not found");
Err(value)
} else {
let nw_ns: Nw = value.into();
Ok(nw_ns)
}
}
Err(err) => {
error(&format!("NW not found, error: {:?}", err));
Err(err)
}
}
}
pub fn is_nw() -> bool {
try_nw().is_ok()
}