wasm_bindgen_shared/
lib.rs

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