File: import_class.rs

package info (click to toggle)
rustc-web 1.78.0%2Bdfsg1-2~deb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,245,420 kB
  • sloc: xml: 147,985; javascript: 18,022; sh: 11,083; python: 10,265; ansic: 6,172; cpp: 5,023; asm: 4,390; makefile: 4,269
file content (284 lines) | stat: -rw-r--r-- 7,690 bytes parent folder | download | duplicates (3)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//! dox

#![deny(missing_docs)] // test that documenting public bindings is enough
#![allow(clippy::redundant_clone)] // test specifically with cloned objects

use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;

#[wasm_bindgen(module = "tests/wasm/import_class.js")]
extern "C" {
    fn math_log(f: f64) -> f64;

    #[wasm_bindgen(js_namespace = StaticFunction)]
    fn bar() -> u32;

    #[derive(Clone)]
    type Construct;
    #[wasm_bindgen(js_namespace = Construct)]
    fn create() -> Construct;
    #[wasm_bindgen(method)]
    fn get_internal_string(this: &Construct) -> String;
    #[wasm_bindgen(method)]
    fn append_to_internal_string(this: &Construct, s: &str);
    #[wasm_bindgen(method)]
    fn assert_internal_string(this: &Construct, s: &str);

    #[wasm_bindgen(method, js_name = "kebab-case")]
    fn kebab_case(this: &Construct) -> u32;
    #[wasm_bindgen(method, getter, js_name = "kebab-case-val")]
    fn kebab_case_val(this: &Construct) -> u32;
    #[wasm_bindgen(method, setter, js_name = "kebab-case-val")]
    fn set_kebab_case_val(this: &Construct, val: u32);
    #[wasm_bindgen(static_method_of = Construct, js_name = "static-kebab-case")]
    fn static_kebab_case() -> u32;
    #[wasm_bindgen(static_method_of = Construct, getter, js_name = "static-kebab-case-val")]
    fn static_kebab_case_val() -> u32;
    #[wasm_bindgen(static_method_of = Construct, setter, js_name = "static-kebab-case-val")]
    fn set_static_kebab_case_val(val: u32);

    type NewConstructors;
    #[wasm_bindgen(constructor)]
    fn new(arg: i32) -> NewConstructors;
    #[wasm_bindgen(method)]
    fn get(this: &NewConstructors) -> i32;

    #[wasm_bindgen(js_name = default)]
    type RenamedTypes;
    #[wasm_bindgen(constructor, js_class = default)]
    fn new(arg: i32) -> RenamedTypes;
    #[wasm_bindgen(method, js_class = default)]
    fn get(this: &RenamedTypes) -> i32;

    fn switch_methods_a();
    fn switch_methods_b();
    type SwitchMethods;
    #[wasm_bindgen(constructor)]
    #[wasm_bindgen(final)]
    fn new() -> SwitchMethods;
    #[wasm_bindgen(js_namespace = SwitchMethods, final)]
    fn a();
    fn switch_methods_called() -> bool;
    #[wasm_bindgen(method, final)]
    fn b(this: &SwitchMethods);

    type Properties;
    #[wasm_bindgen(constructor)]
    fn new() -> Properties;
    #[wasm_bindgen(getter, method)]
    fn a(this: &Properties) -> i32;
    #[wasm_bindgen(setter, method)]
    fn set_a(this: &Properties, a: i32);

    type RenameProperties;
    #[wasm_bindgen(constructor)]
    fn new() -> RenameProperties;
    #[wasm_bindgen(getter = a, method)]
    fn test(this: &RenameProperties) -> i32;
    #[wasm_bindgen(getter, method, js_name = a)]
    fn test2(this: &RenameProperties) -> i32;
    #[wasm_bindgen(setter = a, method)]
    fn another(this: &RenameProperties, a: i32);
    #[wasm_bindgen(setter, method, js_name = a)]
    fn another2(this: &RenameProperties, a: i32);

    /// dox
    pub type AssertImportDenyDocsWorks;
    /// dox
    #[wasm_bindgen(constructor)]
    pub fn new() -> AssertImportDenyDocsWorks;
    /// dox
    #[wasm_bindgen(getter = a, method)]
    pub fn test(this: &AssertImportDenyDocsWorks) -> i32;
    /// dox
    pub fn foo();

    pub type Options;
    #[wasm_bindgen(constructor)]
    fn new() -> Options;
    fn take_none(val: Option<Options>);
    fn take_some(val: Option<Options>);
    fn return_null() -> Option<Options>;
    fn return_undefined() -> Option<Options>;
    fn return_some() -> Option<Options>;
    fn run_rust_option_tests();

    type CatchConstructors;
    #[wasm_bindgen(constructor, catch)]
    fn new(x: u32) -> Result<CatchConstructors, JsValue>;

    type StaticStructural;
    #[wasm_bindgen(static_method_of = StaticStructural, structural)]
    fn static_structural(a: u32) -> u32;

    #[derive(Clone)]
    type InnerClass;
    #[wasm_bindgen(js_namespace = ["nestedNamespace", "InnerClass"])]
    fn inner_static_function(a: u32) -> u32;
    #[wasm_bindgen(js_namespace = ["nestedNamespace", "InnerClass"])]
    fn create_inner_instance() -> InnerClass;
    #[wasm_bindgen(method)]
    fn get_internal_int(this: &InnerClass) -> u32;
    #[wasm_bindgen(method)]
    fn append_to_internal_int(this: &InnerClass, i: u32);
    #[wasm_bindgen(method)]
    fn assert_internal_int(this: &InnerClass, i: u32);
}

#[wasm_bindgen(js_namespace = Math)]
extern "C" {
    #[wasm_bindgen]
    fn random() -> f64;
    #[wasm_bindgen]
    fn log(a: f64) -> f64;
}

#[wasm_bindgen_test]
fn simple() {
    random();
    assert_eq!(log(1.0), math_log(1.0));
}

#[wasm_bindgen_test]
fn import_class() {
    assert_eq!(bar(), 2);
}

#[wasm_bindgen_test]
fn construct() {
    let f = Construct::create();
    assert_eq!(f.get_internal_string(), "this");
    assert_eq!(f.clone().get_internal_string(), "this");
    f.append_to_internal_string(" foo");
    f.assert_internal_string("this foo");

    assert_eq!(f.kebab_case(), 42);
    f.set_kebab_case_val(0);
    // our setter does nothing so this is 42 anyway
    assert_eq!(f.kebab_case_val(), 42);
    assert_eq!(Construct::static_kebab_case(), 42);
    Construct::set_static_kebab_case_val(0);
    assert_eq!(Construct::static_kebab_case_val(), 42);
}

#[wasm_bindgen_test]
fn new_constructors() {
    let f = NewConstructors::new(1);
    assert_eq!(f.get(), 2);
}

#[wasm_bindgen_test]
fn rename_type() {
    let f = RenamedTypes::new(1);
    assert_eq!(f.get(), 2);
}

#[wasm_bindgen_test]
fn switch_methods() {
    assert!(!switch_methods_called());
    SwitchMethods::a();
    assert!(switch_methods_called());

    switch_methods_a();

    assert!(!switch_methods_called());
    SwitchMethods::a();
    assert!(switch_methods_called());

    assert!(!switch_methods_called());
    SwitchMethods::new().b();
    assert!(switch_methods_called());

    switch_methods_b();

    assert!(!switch_methods_called());
    SwitchMethods::new().b();
    assert!(!switch_methods_called());
}

#[wasm_bindgen_test]
fn properties() {
    let a = Properties::new();
    assert_eq!(a.a(), 1);
    a.set_a(2);
    assert_eq!(a.a(), 2);
}

#[wasm_bindgen_test]
fn rename_setter_getter() {
    let x: fn() -> RenameProperties = RenameProperties::new;
    let a = x();
    assert_eq!(a.test(), 1);
    a.another(2);
    assert_eq!(a.test(), 2);
    a.another2(3);
    assert_eq!(a.test2(), 3);
}

/// dox
#[wasm_bindgen]
pub struct AssertDenyDocsWorks {
    /// dox
    pub a: u32,
    _b: i64,
}

/// dox
#[wasm_bindgen]
pub fn assert_deny_docs_works() {}

#[wasm_bindgen_test]
fn options() {
    take_none(None);
    take_some(Some(Options::new()));
    assert!(return_null().is_none());
    assert!(return_undefined().is_none());
    assert!(return_some().is_some());
    run_rust_option_tests();
}

/// doc
#[wasm_bindgen]
pub fn rust_take_none(a: Option<Options>) {
    assert!(a.is_none());
}

/// doc
#[wasm_bindgen]
pub fn rust_take_some(a: Option<Options>) {
    assert!(a.is_some());
}

/// doc
#[wasm_bindgen]
pub fn rust_return_none() -> Option<Options> {
    None
}

/// doc
#[wasm_bindgen]
pub fn rust_return_some() -> Option<Options> {
    Some(Options::new())
}

#[wasm_bindgen_test]
fn catch_constructors() {
    assert!(CatchConstructors::new(0).is_err());
    assert!(CatchConstructors::new(1).is_ok());
}

#[wasm_bindgen_test]
fn static_structural() {
    assert_eq!(StaticStructural::static_structural(30), 33);
}

#[wasm_bindgen_test]
fn nested_namespace() {
    assert_eq!(InnerClass::inner_static_function(15), 20);

    let f = InnerClass::create_inner_instance();
    assert_eq!(f.get_internal_int(), 3);
    assert_eq!(f.clone().get_internal_int(), 3);
    f.append_to_internal_int(5);
    f.assert_internal_int(8);
}