wasm_bindgen_shared/
lib.rs

1#![doc(html_root_url = "https://docs.rs/wasm-bindgen-shared/0.2")]
2
3#[cfg(test)]
4mod schema_hash_approval;
5
6// This gets changed whenever our schema changes.
7// At this time versions of wasm-bindgen and wasm-bindgen-cli are required to have the exact same
8// SCHEMA_VERSION in order to work together.
9pub const SCHEMA_VERSION: &str = "0.2.93";
10
11#[macro_export]
12macro_rules! shared_api {
13    ($mac:ident) => {
14        $mac! {
15        struct Program<'a> {
16            exports: Vec<Export<'a>>,
17            enums: Vec<Enum<'a>>,
18            imports: Vec<Import<'a>>,
19            structs: Vec<Struct<'a>>,
20            // NOTE: Originally typescript_custom_sections are just some strings
21            // But the expression type can only be parsed into a string during compilation
22            // So when encoding, LitOrExpr contains two types, one is that expressions are parsed into strings during compilation, and the other is can be parsed directly.
23            // When decoding, LitOrExpr can be decoded as a string.
24            typescript_custom_sections: Vec<LitOrExpr<'a>>,
25            local_modules: Vec<LocalModule<'a>>,
26            inline_js: Vec<&'a str>,
27            unique_crate_identifier: &'a str,
28            package_json: Option<&'a str>,
29            linked_modules: Vec<LinkedModule<'a>>,
30        }
31
32        struct Import<'a> {
33            module: Option<ImportModule<'a>>,
34            js_namespace: Option<Vec<String>>,
35            kind: ImportKind<'a>,
36        }
37
38        struct LinkedModule<'a> {
39            module: ImportModule<'a>,
40            link_function_name: &'a str,
41        }
42
43        enum ImportModule<'a> {
44            Named(&'a str),
45            RawNamed(&'a str),
46            Inline(u32),
47        }
48
49        enum ImportKind<'a> {
50            Function(ImportFunction<'a>),
51            Static(ImportStatic<'a>),
52            String(ImportString<'a>),
53            Type(ImportType<'a>),
54            Enum(StringEnum),
55        }
56
57        struct ImportFunction<'a> {
58            shim: &'a str,
59            catch: bool,
60            variadic: bool,
61            assert_no_shim: bool,
62            method: Option<MethodData<'a>>,
63            structural: bool,
64            function: Function<'a>,
65        }
66
67        struct MethodData<'a> {
68            class: &'a str,
69            kind: MethodKind<'a>,
70        }
71
72        enum MethodKind<'a> {
73            Constructor,
74            Operation(Operation<'a>),
75        }
76
77        struct Operation<'a> {
78            is_static: bool,
79            kind: OperationKind<'a>,
80        }
81
82        enum OperationKind<'a> {
83            Regular,
84            Getter(&'a str),
85            Setter(&'a str),
86            IndexingGetter,
87            IndexingSetter,
88            IndexingDeleter,
89        }
90
91        struct ImportStatic<'a> {
92            name: &'a str,
93            shim: &'a str,
94        }
95
96        struct ImportString<'a> {
97            shim: &'a str,
98            string: &'a str,
99        }
100
101        struct ImportType<'a> {
102            name: &'a str,
103            instanceof_shim: &'a str,
104            vendor_prefixes: Vec<&'a str>,
105        }
106
107        struct StringEnum {}
108
109        struct Export<'a> {
110            class: Option<&'a str>,
111            comments: Vec<&'a str>,
112            consumed: bool,
113            function: Function<'a>,
114            method_kind: MethodKind<'a>,
115            start: bool,
116        }
117
118        struct Enum<'a> {
119            name: &'a str,
120            variants: Vec<EnumVariant<'a>>,
121            comments: Vec<&'a str>,
122            generate_typescript: bool,
123        }
124
125        struct EnumVariant<'a> {
126            name: &'a str,
127            value: u32,
128            comments: Vec<&'a str>,
129        }
130
131        struct Function<'a> {
132            arg_names: Vec<String>,
133            asyncness: bool,
134            name: &'a str,
135            generate_typescript: bool,
136            generate_jsdoc: bool,
137            variadic: bool,
138        }
139
140        struct Struct<'a> {
141            name: &'a str,
142            fields: Vec<StructField<'a>>,
143            comments: Vec<&'a str>,
144            is_inspectable: bool,
145            generate_typescript: bool,
146        }
147
148        struct StructField<'a> {
149            name: &'a str,
150            readonly: bool,
151            comments: Vec<&'a str>,
152            generate_typescript: bool,
153            generate_jsdoc: bool,
154        }
155
156        struct LocalModule<'a> {
157            identifier: &'a str,
158            contents: &'a str,
159        }
160        }
161    }; // end of mac case
162} // end of mac definition
163
164pub fn new_function(struct_name: &str) -> String {
165    let mut name = "__wbg_".to_string();
166    name.extend(struct_name.chars().flat_map(|s| s.to_lowercase()));
167    name.push_str("_new");
168    name
169}
170
171pub fn free_function(struct_name: &str) -> String {
172    let mut name = "__wbg_".to_string();
173    name.extend(struct_name.chars().flat_map(|s| s.to_lowercase()));
174    name.push_str("_free");
175    name
176}
177
178pub fn unwrap_function(struct_name: &str) -> String {
179    let mut name = "__wbg_".to_string();
180    name.extend(struct_name.chars().flat_map(|s| s.to_lowercase()));
181    name.push_str("_unwrap");
182    name
183}
184
185pub fn free_function_export_name(function_name: &str) -> String {
186    function_name.to_string()
187}
188
189pub fn struct_function_export_name(struct_: &str, f: &str) -> String {
190    let mut name = struct_
191        .chars()
192        .flat_map(|s| s.to_lowercase())
193        .collect::<String>();
194    name.push('_');
195    name.push_str(f);
196    name
197}
198
199pub fn struct_field_get(struct_: &str, f: &str) -> String {
200    let mut name = String::from("__wbg_get_");
201    name.extend(struct_.chars().flat_map(|s| s.to_lowercase()));
202    name.push('_');
203    name.push_str(f);
204    name
205}
206
207pub fn struct_field_set(struct_: &str, f: &str) -> String {
208    let mut name = String::from("__wbg_set_");
209    name.extend(struct_.chars().flat_map(|s| s.to_lowercase()));
210    name.push('_');
211    name.push_str(f);
212    name
213}
214
215pub fn version() -> String {
216    let mut v = env!("CARGO_PKG_VERSION").to_string();
217    if let Some(s) = option_env!("WBG_VERSION") {
218        v.push_str(" (");
219        v.push_str(s);
220        v.push(')');
221    }
222    v
223}