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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//! Select functions (with rust-native parameter types) from the emscripten [`console.h`] [header file].
//!
//! [`console.h`]: https://github.com/emscripten-core/emscripten/blob/main/site/source/docs/api_reference/console.h.rst
//! [header file]: https://github.com/emscripten-core/emscripten/blob/main/system/include/emscripten/console.h

use std::ffi::CString;

use emscripten_functions_sys::console;

/// Prints the given string using the [`console.log()`] JS function.
///
/// [`console.log()`]: https://developer.mozilla.org/en-US/docs/Web/API/console/log
///
/// # Arguments
/// * `string` - The string to print.
///
/// # Examples
/// ```rust
/// log("Hello, world!");
/// log(format!("0.1 + 0.2 = {}", 0.1 + 0.2));
/// ```
pub fn log<T>(string: T)
where
    T: AsRef<str>,
{
    let cstring = CString::new(string.as_ref()).unwrap();
    unsafe {
        console::emscripten_console_log(cstring.as_ptr());
    }
}

/// Prints the given string using the [`console.warn()`] JS function.
///
/// [`console.warn()`]: https://developer.mozilla.org/en-US/docs/Web/API/console/warn
///
/// # Arguments
/// * `string` - The string to print.
///
/// # Examples
/// ```rust
/// warn("Hello, world!");
/// warn(format!("0.1 + 0.2 = {}", 0.1 + 0.2));
/// ```
pub fn warn<T>(string: T)
where
    T: AsRef<str>,
{
    let cstring = CString::new(string.as_ref()).unwrap();
    unsafe {
        console::emscripten_console_warn(cstring.as_ptr());
    }
}

/// Prints the given string using the [`console.error()`] function.
///
/// [`console.error()`]: https://developer.mozilla.org/en-US/docs/Web/API/console/error
///
/// # Arguments
/// * `string` - The string to print.
///
/// # Examples
/// ```rust
/// error("Hello, world!");
/// error(format!("0.1 + 0.2 = {}", 0.1 + 0.2));
/// ```
pub fn error<T>(string: T)
where
    T: AsRef<str>,
{
    let cstring = CString::new(string.as_ref()).unwrap();
    unsafe {
        console::emscripten_console_error(cstring.as_ptr());
    }
}

/// Prints the given string using the emscripten-defined `out()` JS function.
///
/// # Arguments
/// * `string` - The string to print.
///
/// # Examples
/// ```rust
/// out("Hello, world!");
/// out(format!("0.1 + 0.2 = {}", 0.1 + 0.2));
/// ```
pub fn out<T>(string: T)
where
    T: AsRef<str>,
{
    let cstring = CString::new(string.as_ref()).unwrap();
    unsafe {
        console::emscripten_out(cstring.as_ptr());
    }
}

/// Prints the given string using the emscripten-defined `err()` JS function.
///
/// # Arguments
/// * `string` - The string to print.
///
/// # Examples
/// ```rust
/// err("Hello, world!");
/// err(format!("0.1 + 0.2 = {}", 0.1 + 0.2));
/// ```
pub fn err<T>(string: T)
where
    T: AsRef<str>,
{
    let cstring = CString::new(string.as_ref()).unwrap();
    unsafe {
        console::emscripten_err(cstring.as_ptr());
    }
}

/// Prints the given string using the emscripten-defined `dbg()` JS function.
///
/// # Arguments
/// * `string` - The string to print.
///
/// # Examples
/// ```rust
/// dbg("Hello, world!");
/// dbg(format!("0.1 + 0.2 = {}", 0.1 + 0.2));
/// ```
pub fn dbg<T>(string: T)
where
    T: AsRef<str>,
{
    let cstring = CString::new(string.as_ref()).unwrap();
    unsafe {
        console::emscripten_dbg(cstring.as_ptr());
    }
}