wasm_bindgen_shared/
lib.rs

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