]> git.proxmox.com Git - rustc.git/blob - vendor/wasm-bindgen/tests/wasm/import_class.rs
New upstream version 1.70.0+dfsg2
[rustc.git] / vendor / wasm-bindgen / tests / wasm / import_class.rs
1 //! dox
2
3 #![deny(missing_docs)] // test that documenting public bindings is enough
4
5 use wasm_bindgen::prelude::*;
6 use wasm_bindgen_test::*;
7
8 #[wasm_bindgen(module = "tests/wasm/import_class.js")]
9 extern "C" {
10 fn math_log(f: f64) -> f64;
11
12 #[wasm_bindgen(js_namespace = StaticFunction)]
13 fn bar() -> u32;
14
15 #[derive(Clone)]
16 type Construct;
17 #[wasm_bindgen(js_namespace = Construct)]
18 fn create() -> Construct;
19 #[wasm_bindgen(method)]
20 fn get_internal_string(this: &Construct) -> String;
21 #[wasm_bindgen(method)]
22 fn append_to_internal_string(this: &Construct, s: &str);
23 #[wasm_bindgen(method)]
24 fn assert_internal_string(this: &Construct, s: &str);
25
26 type NewConstructors;
27 #[wasm_bindgen(constructor)]
28 fn new(arg: i32) -> NewConstructors;
29 #[wasm_bindgen(method)]
30 fn get(this: &NewConstructors) -> i32;
31
32 #[wasm_bindgen(js_name = default)]
33 type RenamedTypes;
34 #[wasm_bindgen(constructor, js_class = default)]
35 fn new(arg: i32) -> RenamedTypes;
36 #[wasm_bindgen(method, js_class = default)]
37 fn get(this: &RenamedTypes) -> i32;
38
39 fn switch_methods_a();
40 fn switch_methods_b();
41 type SwitchMethods;
42 #[wasm_bindgen(constructor)]
43 #[wasm_bindgen(final)]
44 fn new() -> SwitchMethods;
45 #[wasm_bindgen(js_namespace = SwitchMethods, final)]
46 fn a();
47 fn switch_methods_called() -> bool;
48 #[wasm_bindgen(method, final)]
49 fn b(this: &SwitchMethods);
50
51 type Properties;
52 #[wasm_bindgen(constructor)]
53 fn new() -> Properties;
54 #[wasm_bindgen(getter, method)]
55 fn a(this: &Properties) -> i32;
56 #[wasm_bindgen(setter, method)]
57 fn set_a(this: &Properties, a: i32);
58
59 type RenameProperties;
60 #[wasm_bindgen(constructor)]
61 fn new() -> RenameProperties;
62 #[wasm_bindgen(getter = a, method)]
63 fn test(this: &RenameProperties) -> i32;
64 #[wasm_bindgen(getter, method, js_name = a)]
65 fn test2(this: &RenameProperties) -> i32;
66 #[wasm_bindgen(setter = a, method)]
67 fn another(this: &RenameProperties, a: i32);
68 #[wasm_bindgen(setter, method, js_name = a)]
69 fn another2(this: &RenameProperties, a: i32);
70
71 /// dox
72 pub type AssertImportDenyDocsWorks;
73 /// dox
74 #[wasm_bindgen(constructor)]
75 pub fn new() -> AssertImportDenyDocsWorks;
76 /// dox
77 #[wasm_bindgen(getter = a, method)]
78 pub fn test(this: &AssertImportDenyDocsWorks) -> i32;
79 /// dox
80 pub fn foo();
81
82 pub type Options;
83 #[wasm_bindgen(constructor)]
84 fn new() -> Options;
85 fn take_none(val: Option<Options>);
86 fn take_some(val: Option<Options>);
87 fn return_null() -> Option<Options>;
88 fn return_undefined() -> Option<Options>;
89 fn return_some() -> Option<Options>;
90 fn run_rust_option_tests();
91
92 type CatchConstructors;
93 #[wasm_bindgen(constructor, catch)]
94 fn new(x: u32) -> Result<CatchConstructors, JsValue>;
95
96 type StaticStructural;
97 #[wasm_bindgen(static_method_of = StaticStructural, structural)]
98 fn static_structural(a: u32) -> u32;
99
100 #[derive(Clone)]
101 type InnerClass;
102 #[wasm_bindgen(js_namespace = ["nestedNamespace", "InnerClass"])]
103 fn inner_static_function(a: u32) -> u32;
104 #[wasm_bindgen(js_namespace = ["nestedNamespace", "InnerClass"])]
105 fn create_inner_instance() -> InnerClass;
106 #[wasm_bindgen(method)]
107 fn get_internal_int(this: &InnerClass) -> u32;
108 #[wasm_bindgen(method)]
109 fn append_to_internal_int(this: &InnerClass, i: u32);
110 #[wasm_bindgen(method)]
111 fn assert_internal_int(this: &InnerClass, i: u32);
112 }
113
114 #[wasm_bindgen]
115 extern "C" {
116 #[wasm_bindgen(js_namespace = Math)]
117 fn random() -> f64;
118 #[wasm_bindgen(js_namespace = Math)]
119 fn log(a: f64) -> f64;
120 }
121
122 #[wasm_bindgen_test]
123 fn simple() {
124 random();
125 assert_eq!(log(1.0), math_log(1.0));
126 }
127
128 #[wasm_bindgen_test]
129 fn import_class() {
130 assert_eq!(bar(), 2);
131 }
132
133 #[wasm_bindgen_test]
134 fn construct() {
135 let f = Construct::create();
136 assert_eq!(f.get_internal_string(), "this");
137 assert_eq!(f.clone().get_internal_string(), "this");
138 f.append_to_internal_string(" foo");
139 f.assert_internal_string("this foo");
140 }
141
142 #[wasm_bindgen_test]
143 fn new_constructors() {
144 let f = NewConstructors::new(1);
145 assert_eq!(f.get(), 2);
146 }
147
148 #[wasm_bindgen_test]
149 fn rename_type() {
150 let f = RenamedTypes::new(1);
151 assert_eq!(f.get(), 2);
152 }
153
154 #[wasm_bindgen_test]
155 #[cfg(ignored)] // TODO: fix this before landing
156 fn switch_methods() {
157 assert!(!switch_methods_called());
158 SwitchMethods::a();
159 assert!(switch_methods_called());
160
161 switch_methods_a();
162
163 assert!(!switch_methods_called());
164 SwitchMethods::a();
165 assert!(switch_methods_called());
166
167 assert!(!switch_methods_called());
168 SwitchMethods::new().b();
169 assert!(switch_methods_called());
170
171 switch_methods_b();
172
173 assert!(!switch_methods_called());
174 SwitchMethods::new().b();
175 assert!(!switch_methods_called());
176 }
177
178 #[wasm_bindgen_test]
179 fn properties() {
180 let a = Properties::new();
181 assert_eq!(a.a(), 1);
182 a.set_a(2);
183 assert_eq!(a.a(), 2);
184 }
185
186 #[wasm_bindgen_test]
187 fn rename_setter_getter() {
188 let x: fn() -> RenameProperties = RenameProperties::new;
189 let a = x();
190 assert_eq!(a.test(), 1);
191 a.another(2);
192 assert_eq!(a.test(), 2);
193 a.another2(3);
194 assert_eq!(a.test2(), 3);
195 }
196
197 /// dox
198 #[wasm_bindgen]
199 pub struct AssertDenyDocsWorks {
200 /// dox
201 pub a: u32,
202 _b: i64,
203 }
204
205 /// dox
206 #[wasm_bindgen]
207 pub fn assert_deny_docs_works() {}
208
209 #[wasm_bindgen_test]
210 fn options() {
211 take_none(None);
212 take_some(Some(Options::new()));
213 assert!(return_null().is_none());
214 assert!(return_undefined().is_none());
215 assert!(return_some().is_some());
216 run_rust_option_tests();
217 }
218
219 /// doc
220 #[wasm_bindgen]
221 pub fn rust_take_none(a: Option<Options>) {
222 assert!(a.is_none());
223 }
224
225 /// doc
226 #[wasm_bindgen]
227 pub fn rust_take_some(a: Option<Options>) {
228 assert!(a.is_some());
229 }
230
231 /// doc
232 #[wasm_bindgen]
233 pub fn rust_return_none() -> Option<Options> {
234 None
235 }
236
237 /// doc
238 #[wasm_bindgen]
239 pub fn rust_return_some() -> Option<Options> {
240 Some(Options::new())
241 }
242
243 #[wasm_bindgen_test]
244 fn catch_constructors() {
245 assert!(CatchConstructors::new(0).is_err());
246 assert!(CatchConstructors::new(1).is_ok());
247 }
248
249 #[wasm_bindgen_test]
250 fn static_structural() {
251 assert_eq!(StaticStructural::static_structural(30), 33);
252 }
253
254 #[wasm_bindgen_test]
255 fn nested_namespace() {
256 assert_eq!(InnerClass::inner_static_function(15), 20);
257
258 let f = InnerClass::create_inner_instance();
259 assert_eq!(f.get_internal_int(), 3);
260 assert_eq!(f.clone().get_internal_int(), 3);
261 f.append_to_internal_int(5);
262 f.assert_internal_int(8);
263 }