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
52
53
54
55
56
57
58
#![no_std]
use js_ffi::*;

pub struct Console {
    fn_log: JSInvoker,
    fn_clear: JSInvoker,
    fn_error: JSInvoker,
    fn_warning: JSInvoker,
    fn_time: JSInvoker,
    fn_time_end: JSInvoker,
}

impl Default for Console {
    fn default() -> Self {
        Console {
            fn_log: js!(console.log),
            fn_clear: js!(console.clear),
            fn_error: js!(console.error),
            fn_warning: js!(console.warn),
            fn_time: js!(console.time),
            fn_time_end: js!(console.timeEnd),
        }
    }
}

impl Console {
    pub fn clear(&self) {
        self.fn_clear.invoke_0();
    }

    pub fn log(&self, msg: &str) {
        self.fn_log.invoke_1(JSString::from(msg));
    }

    pub fn warning(&self, msg: &str) {
        self.fn_warning.invoke_1(JSString::from(msg));
    }

    pub fn error(&self, msg: &str) {
        self.fn_error.invoke_1(JSString::from(msg));
    }

    pub fn time(&self, label: Option<&str>) {
        if label.is_none() {
            self.fn_time.invoke_0();
        } else {
            self.fn_time.invoke_1(JSString::from(label.unwrap()));
        }
    }

    pub fn time_end(&self, label: Option<&str>) {
        if label.is_none() {
            self.fn_time_end.invoke_0();
        } else {
            self.fn_time_end.invoke_1(JSString::from(label.unwrap()));
        }
    }
}