Debian Codegraph @

.cargo/vendor/wasm-bindgen/tests/wasm/futures.rs

raw
1 use wasm_bindgen::prelude::*;
2 use wasm_bindgen_test::*;
3
4 #[rustfmt::skip]
5 #[wasm_bindgen(module = "tests/wasm/futures.js")]
6 extern "C" {
7 #[wasm_bindgen(catch)]
8 async fn call_exports() -> Result<JsValue, JsValue>;
9
10 async fn call_promise() -> JsValue;
11 #[wasm_bindgen(catch)]
12 async fn call_promise_ok() -> Result<JsValue, JsValue>;
13 #[wasm_bindgen(catch)]
14 async fn call_promise_err() -> Result<JsValue, JsValue>;
15
16 #[wasm_bindgen]
17 async fn call_promise_unit();
18 #[wasm_bindgen(catch)]
19 async fn call_promise_ok_unit() -> Result<(), JsValue>;
20 #[wasm_bindgen(catch)]
21 async fn call_promise_err_unit() -> Result<(), JsValue>;
22 }
23
24 #[wasm_bindgen_test]
25 async fn smoke() {
26 call_exports().await.unwrap();
27 }
28
29 #[wasm_bindgen]
30 pub async fn async_do_nothing() {}
31
32 #[wasm_bindgen]
33 pub async fn async_return_1() -> JsValue {
34 1.into()
35 }
36
37 #[wasm_bindgen]
38 pub async fn async_return_2() -> u32 {
39 2
40 }
41
42 #[wasm_bindgen]
43 pub async fn async_nothing_again() -> Result<(), JsValue> {
44 Ok(())
45 }
46
47 #[wasm_bindgen]
48 pub async fn async_return_3() -> Result<u32, JsValue> {
49 Ok(3)
50 }
51
52 #[wasm_bindgen]
53 pub async fn async_return_4() -> Result<JsValue, JsValue> {
54 Ok(4.into())
55 }
56
57 #[wasm_bindgen]
58 pub struct AsyncCustomReturn {
59 pub val: u32,
60 }
61
62 #[wasm_bindgen]
63 pub async fn async_return_5() -> AsyncCustomReturn {
64 AsyncCustomReturn { val: 5 }
65 }
66
67 #[wasm_bindgen]
68 pub async fn async_return_6() -> Result<AsyncCustomReturn, JsValue> {
69 Ok(AsyncCustomReturn { val: 6 })
70 }
71
72 #[wasm_bindgen]
73 pub async fn async_throw_7() -> Result<AsyncCustomReturn, JsValue> {
74 Err(7.into())
75 }
76
77 #[wasm_bindgen]
78 pub async fn async_throw_custom() -> Result<AsyncCustomReturn, JsValue> {
79 Err(AsyncCustomReturn { val: 8 }.into())
80 }
81
82 #[wasm_bindgen]
83 pub async fn async_throw_message() -> Result<(), JsValue> {
84 Err(js_sys::Error::new("async message").into())
85 }
86
87 #[wasm_bindgen]
88 pub async fn async_throw_jserror() -> Result<AsyncCustomReturn, JsError> {
89 Err(JsError::new("async message"))
90 }
91
92 pub struct AsyncCustomError {
93 pub val: JsValue,
94 }
95
96 impl From<AsyncCustomError> for JsValue {
97 fn from(e: AsyncCustomError) -> Self {
98 e.val
99 }
100 }
101
102 #[wasm_bindgen]
103 pub async fn async_throw_custom_error() -> Result<AsyncCustomReturn, AsyncCustomError> {
104 Err(AsyncCustomError {
105 val: JsValue::from("custom error"),
106 })
107 }
108
109 #[wasm_bindgen]
110 pub async fn async_take_reference(x: &str) -> String {
111 format!("Hi, {x}!")
112 }
113
114 #[wasm_bindgen]
115 pub struct AsyncStruct;
116
117 #[wasm_bindgen]
118 impl AsyncStruct {
119 #[wasm_bindgen(constructor)]
120 pub async fn new() -> AsyncStruct {
121 AsyncStruct
122 }
123
124 pub async fn method(&self) -> u32 {
125 42
126 }
127 }
128
129 #[wasm_bindgen]
130 pub async fn async_take_js_reference(x: &JsValue) {
131 assert_eq!(*x, 42);
132 }
133
134 #[wasm_bindgen]
135 pub async fn async_take_mut_slice(x: &mut [i32]) {
136 x.fill(42);
137 }
138
139 #[wasm_bindgen_test]
140 async fn test_promise() {
141 assert_eq!(call_promise().await.as_string(), Some(String::from("ok")))
142 }
143
144 #[wasm_bindgen_test]
145 async fn test_promise_ok() {
146 assert_eq!(
147 call_promise_ok().await.map(|j| j.as_string()),
148 Ok(Some(String::from("ok")))
149 )
150 }
151
152 #[wasm_bindgen_test]
153 async fn test_promise_err() {
154 assert_eq!(
155 call_promise_err().await.map_err(|j| j.as_string()),
156 Err(Some(String::from("error")))
157 )
158 }
159
160 #[wasm_bindgen_test]
161 async fn test_promise_unit() {
162 call_promise_unit().await
163 }
164
165 #[wasm_bindgen_test]
166 async fn test_promise_ok_unit() {
167 call_promise_ok_unit().await.unwrap()
168 }
169
170 #[wasm_bindgen_test]
171 async fn test_promise_err_unit() {
172 assert_eq!(
173 call_promise_err_unit().await.map_err(|j| j.as_string()),
174 Err::<(), _>(Some(String::from("error")))
175 )
176 }