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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
#[rustfmt::skip]
#[wasm_bindgen(module = "tests/wasm/futures.js")]
extern "C" {
#[wasm_bindgen(catch)]
async fn call_exports() -> Result<JsValue, JsValue>;
async fn call_promise() -> JsValue;
#[wasm_bindgen(catch)]
async fn call_promise_ok() -> Result<JsValue, JsValue>;
#[wasm_bindgen(catch)]
async fn call_promise_err() -> Result<JsValue, JsValue>;
#[wasm_bindgen]
async fn call_promise_unit();
#[wasm_bindgen(catch)]
async fn call_promise_ok_unit() -> Result<(), JsValue>;
#[wasm_bindgen(catch)]
async fn call_promise_err_unit() -> Result<(), JsValue>;
}
#[wasm_bindgen_test]
async fn smoke() {
call_exports().await.unwrap();
}
#[wasm_bindgen]
pub async fn async_do_nothing() {}
#[wasm_bindgen]
pub async fn async_return_1() -> JsValue {
1.into()
}
#[wasm_bindgen]
pub async fn async_return_2() -> u32 {
2
}
#[wasm_bindgen]
pub async fn async_nothing_again() -> Result<(), JsValue> {
Ok(())
}
#[wasm_bindgen]
pub async fn async_return_3() -> Result<u32, JsValue> {
Ok(3)
}
#[wasm_bindgen]
pub async fn async_return_4() -> Result<JsValue, JsValue> {
Ok(4.into())
}
#[wasm_bindgen]
pub struct AsyncCustomReturn {
pub val: u32,
}
#[wasm_bindgen]
pub async fn async_return_5() -> AsyncCustomReturn {
AsyncCustomReturn { val: 5 }
}
#[wasm_bindgen]
pub async fn async_return_6() -> Result<AsyncCustomReturn, JsValue> {
Ok(AsyncCustomReturn { val: 6 })
}
#[wasm_bindgen]
pub async fn async_throw_7() -> Result<AsyncCustomReturn, JsValue> {
Err(7.into())
}
#[wasm_bindgen]
pub async fn async_throw_custom() -> Result<AsyncCustomReturn, JsValue> {
Err(AsyncCustomReturn { val: 8 }.into())
}
#[wasm_bindgen]
pub async fn async_throw_message() -> Result<(), JsValue> {
Err(js_sys::Error::new("async message").into())
}
#[wasm_bindgen]
pub async fn async_throw_jserror() -> Result<AsyncCustomReturn, JsError> {
Err(JsError::new("async message"))
}
pub struct AsyncCustomError {
pub val: JsValue,
}
impl Into<JsValue> for AsyncCustomError {
fn into(self) -> JsValue {
self.val
}
}
#[wasm_bindgen]
pub async fn async_throw_custom_error() -> Result<AsyncCustomReturn, AsyncCustomError> {
Err(AsyncCustomError {
val: JsValue::from("custom error"),
})
}
#[wasm_bindgen]
pub async fn async_take_reference(x: &str) -> String {
format!("Hi, {x}!")
}
#[wasm_bindgen]
pub struct AsyncStruct;
#[wasm_bindgen]
impl AsyncStruct {
#[wasm_bindgen(constructor)]
pub async fn new() -> AsyncStruct {
AsyncStruct
}
pub async fn method(&self) -> u32 {
42
}
}
#[wasm_bindgen_test]
async fn test_promise() {
assert_eq!(call_promise().await.as_string(), Some(String::from("ok")))
}
#[wasm_bindgen_test]
async fn test_promise_ok() {
assert_eq!(
call_promise_ok().await.map(|j| j.as_string()),
Ok(Some(String::from("ok")))
)
}
#[wasm_bindgen_test]
async fn test_promise_err() {
assert_eq!(
call_promise_err().await.map_err(|j| j.as_string()),
Err(Some(String::from("error")))
)
}
#[wasm_bindgen_test]
async fn test_promise_unit() {
call_promise_unit().await
}
#[wasm_bindgen_test]
async fn test_promise_ok_unit() {
call_promise_ok_unit().await.unwrap()
}
#[wasm_bindgen_test]
async fn test_promise_err_unit() {
assert_eq!(
call_promise_err_unit().await.map_err(|j| j.as_string()),
Err::<(), _>(Some(String::from("error")))
)
}
|