Debian Codegraph @

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

raw
1 use wasm_bindgen::prelude::*;
2 use wasm_bindgen_test::*;
3
4 #[wasm_bindgen(module = "tests/wasm/imports.js")]
5 extern "C" {
6 fn test_simple();
7
8 fn simple_foo(s: &str);
9 fn simple_another(a: u32) -> i32;
10 fn simple_take_and_return_bool(a: bool) -> bool;
11 fn simple_return_object() -> JsValue;
12 #[allow(dead_code)]
13 fn missing_symbol(s: &str);
14 fn return_string() -> String;
15 fn take_and_ret_string(s: String) -> String;
16 #[wasm_bindgen(js_name = take_and_ret_string)]
17 fn take_and_ret_string2(s: &str) -> String;
18
19 fn exceptions_throw();
20 #[wasm_bindgen(catch)]
21 fn exceptions_throw2() -> Result<(), JsValue>;
22 fn test_exception_propagates();
23
24 fn assert_valid_error(val: JsValue);
25
26 static IMPORT: JsValue;
27
28 #[wasm_bindgen(js_name = return_three)]
29 fn rust_name_for_return_three() -> u32;
30
31 fn underscore(_: u8);
32
33 #[wasm_bindgen(js_name = pub)]
34 fn js_function_named_rust_keyword() -> u32;
35
36 #[allow(non_camel_case_types)]
37 type bar;
38 #[wasm_bindgen(js_namespace = bar, js_name = foo)]
39 static FOO: JsValue;
40
41 fn take_custom_type(f: CustomType) -> CustomType;
42 fn touch_custom_type();
43 fn custom_type_return_2() -> CustomType;
44 #[wasm_bindgen(js_name = interpret_2_as_custom_type)]
45 fn js_interpret_2_as_custom_type();
46
47 #[wasm_bindgen(js_name = "baz$")]
48 fn renamed_with_dollar_sign();
49 #[wasm_bindgen(js_name = "$foo")]
50 static RENAMED: JsValue;
51
52 fn unused_import();
53 fn assert_dead_import_not_generated();
54 fn should_call_undefined_functions() -> bool;
55
56 type StaticMethodCheck;
57 #[wasm_bindgen(static_method_of = StaticMethodCheck)]
58 fn static_method_of_right_this();
59
60 static STATIC_STRING: String;
61
62 #[derive(Clone)]
63 type PassOutOptionUndefined;
64 fn get_some_val() -> PassOutOptionUndefined;
65 #[wasm_bindgen(js_name = "receive_undefined")]
66 fn receive_undefined_ref(arg: Option<&PassOutOptionUndefined>);
67 #[wasm_bindgen(js_name = "receive_undefined")]
68 fn receive_undefined_owned(arg: Option<PassOutOptionUndefined>);
69 #[wasm_bindgen(js_name = "receive_some")]
70 fn receive_some_ref(arg: Option<&PassOutOptionUndefined>);
71 #[wasm_bindgen(js_name = "receive_some")]
72 fn receive_some_owned(arg: Option<PassOutOptionUndefined>);
73
74 #[wasm_bindgen(js_namespace = Math)]
75 fn func_from_module_math(a: i32) -> i32;
76
77 #[wasm_bindgen(js_namespace = Number)]
78 fn func_from_module_number() -> f64;
79
80 #[wasm_bindgen(js_name = "same_name_from_import")]
81 fn same_name_from_import_1(s: i32) -> i32;
82
83 #[wasm_bindgen(js_namespace = same_js_namespace_from_module)]
84 fn func_from_module_1_same_js_namespace(s: i32) -> i32;
85
86 #[wasm_bindgen(js_name = "kebab-case")]
87 fn kebab_case() -> u32;
88
89 #[wasm_bindgen(js_name = "\"string'literal\nbreakers\r")]
90 fn string_literal_breakers() -> u32;
91 }
92
93 #[wasm_bindgen(module = "tests/wasm/imports_2.js")]
94 extern "C" {
95 #[wasm_bindgen(js_name = "same_name_from_import")]
96 fn same_name_from_import_2(s: i32) -> i32;
97
98 #[wasm_bindgen(js_namespace = same_js_namespace_from_module)]
99 fn func_from_module_2_same_js_namespace(s: i32) -> i32;
100 }
101
102 #[wasm_bindgen]
103 extern "C" {
104 fn parseInt(a: &str) -> u32;
105
106 #[wasm_bindgen(js_namespace = Math, js_name = "sqrt")]
107 fn func_from_global_math(s: f64) -> f64;
108
109 type Number;
110 #[wasm_bindgen(getter, static_method_of = Number, js_name = "NAN")]
111 fn static_getter_from_global_number() -> f64;
112 }
113
114 #[wasm_bindgen_test]
115 fn simple() {
116 test_simple();
117 }
118
119 #[wasm_bindgen]
120 pub fn simple_take_str(s: &str) {
121 simple_foo(s);
122 }
123
124 #[wasm_bindgen]
125 pub fn simple_another_thunk(a: u32) -> i32 {
126 simple_another(a)
127 }
128
129 #[wasm_bindgen]
130 pub fn simple_bool_thunk(a: bool) -> bool {
131 simple_take_and_return_bool(a)
132 }
133
134 #[wasm_bindgen]
135 pub fn simple_get_the_object() -> JsValue {
136 simple_return_object()
137 }
138
139 #[wasm_bindgen_test]
140 fn string_ret() {
141 assert_eq!(return_string(), "bar");
142 }
143
144 #[wasm_bindgen_test]
145 fn strings() {
146 assert_eq!(take_and_ret_string(String::from("a")), "ab");
147 assert_eq!(take_and_ret_string2("b"), "bb");
148 }
149
150 #[wasm_bindgen_test]
151 fn exceptions() {
152 test_exception_propagates();
153 assert!(exceptions_throw2().is_err());
154 }
155
156 #[wasm_bindgen]
157 pub fn exceptions_propagate() {
158 exceptions_throw();
159 }
160
161 #[wasm_bindgen_test]
162 fn exn_caught() {
163 assert_valid_error(exceptions_throw2().unwrap_err());
164 }
165
166 #[wasm_bindgen_test]
167 fn free_imports() {
168 assert_eq!(parseInt("3"), 3);
169 }
170
171 #[wasm_bindgen_test]
172 fn import_a_field() {
173 assert_eq!(IMPORT.as_f64(), Some(1.0));
174 }
175
176 #[wasm_bindgen_test]
177 fn rename() {
178 assert_eq!(rust_name_for_return_three(), 3);
179 }
180
181 #[wasm_bindgen_test]
182 fn underscore_pattern() {
183 underscore(2);
184 }
185
186 #[wasm_bindgen_test]
187 fn rust_keyword() {
188 assert_eq!(js_function_named_rust_keyword(), 2);
189 }
190
191 #[wasm_bindgen_test]
192 fn rust_keyword2() {
193 assert_eq!(FOO.as_f64(), Some(3.0));
194 }
195
196 #[wasm_bindgen_test]
197 fn custom_type() {
198 take_custom_type(CustomType(()));
199 touch_custom_type();
200 js_interpret_2_as_custom_type();
201 }
202
203 #[wasm_bindgen]
204 pub fn interpret_2_as_custom_type() {
205 custom_type_return_2();
206 }
207
208 #[wasm_bindgen]
209 pub struct CustomType(());
210
211 #[wasm_bindgen]
212 impl CustomType {
213 pub fn touch(&self) {
214 panic!()
215 }
216 }
217
218 #[wasm_bindgen_test]
219 fn rename_with_string() {
220 renamed_with_dollar_sign();
221 }
222
223 #[wasm_bindgen_test]
224 fn rename_static_with_string() {
225 assert_eq!(RENAMED.as_f64(), Some(1.0));
226 }
227
228 #[wasm_bindgen_test]
229 fn dead_imports_not_generated() {
230 assert_dead_import_not_generated();
231 }
232
233 #[wasm_bindgen_test]
234 fn import_inside_function_works() {
235 #[wasm_bindgen(module = "tests/wasm/imports.js")]
236 extern "C" {
237 fn import_inside_function_works();
238 }
239 import_inside_function_works();
240 }
241
242 #[wasm_bindgen_test]
243 fn private_module_imports_work() {
244 private::foo();
245 }
246
247 mod private {
248 use wasm_bindgen::prelude::*;
249
250 pub fn foo() {
251 #[wasm_bindgen(module = "tests/wasm/imports.js")]
252 extern "C" {
253 fn import_inside_private_module();
254 }
255 import_inside_private_module();
256 }
257 }
258
259 #[wasm_bindgen]
260 extern "C" {
261 fn something_not_defined_in_the_environment();
262
263 type TypeThatIsNotDefined;
264 #[wasm_bindgen(constructor)]
265 fn new() -> TypeThatIsNotDefined;
266 #[wasm_bindgen(method)]
267 fn method(this: &TypeThatIsNotDefined);
268 #[wasm_bindgen(method, getter)]
269 fn property(this: &TypeThatIsNotDefined) -> u32;
270 #[wasm_bindgen(method, setter)]
271 fn set_property(this: &TypeThatIsNotDefined, val: u32);
272 }
273
274 #[wasm_bindgen_test]
275 fn undefined_function_is_ok() {
276 if !should_call_undefined_functions() {
277 return;
278 }
279 something_not_defined_in_the_environment();
280
281 let x = TypeThatIsNotDefined::new();
282 x.method();
283 x.set_property(x.property());
284 }
285
286 #[wasm_bindgen_test]
287 fn static_string_ok() {
288 assert_eq!(*STATIC_STRING, "x");
289 }
290
291 #[wasm_bindgen_test]
292 fn static_method_of_has_right_this() {
293 StaticMethodCheck::static_method_of_right_this();
294 }
295
296 #[wasm_bindgen_test]
297 fn pass_out_options_as_undefined() {
298 receive_undefined_ref(None);
299 receive_undefined_ref(None);
300 receive_undefined_owned(None);
301 receive_undefined_owned(None);
302
303 let v = get_some_val();
304 receive_some_ref(Some(&v));
305 receive_some_ref(Some(&v));
306 receive_some_owned(Some(v.clone()));
307 receive_some_owned(Some(v));
308 }
309
310 #[wasm_bindgen_test]
311 fn func_from_global_and_module_same_js_namespace() {
312 assert_eq!(func_from_global_math(4.0), 2.0);
313 assert_eq!(func_from_module_math(2), 4);
314 }
315 #[wasm_bindgen_test]
316 fn getter_from_global_and_module_same_name() {
317 assert!(Number::static_getter_from_global_number().is_nan());
318 assert_eq!(func_from_module_number(), 3.0);
319 }
320 #[wasm_bindgen_test]
321 fn func_from_two_modules_same_js_name() {
322 assert_eq!(same_name_from_import_1(1), 3);
323 assert_eq!(same_name_from_import_2(1), 4);
324 }
325
326 #[wasm_bindgen_test]
327 fn func_from_two_modules_same_js_namespace() {
328 assert_eq!(func_from_module_1_same_js_namespace(2), 10);
329 assert_eq!(func_from_module_2_same_js_namespace(2), 12);
330 }
331
332 #[wasm_bindgen_test]
333 fn invalid_idents() {
334 assert_eq!(kebab_case(), 42);
335 assert_eq!(string_literal_breakers(), 42);
336 }