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
13pub const SCHEMA_VERSION: &str = "0.2.107";
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 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 private: bool,
142 }
143
144 struct EnumVariant<'a> {
145 name: &'a str,
146 value: u32,
147 comments: Vec<&'a str>,
148 }
149
150 struct Function<'a> {
151 args: Vec<FunctionArgumentData<'a>>,
152 asyncness: bool,
153 name: &'a str,
154 generate_typescript: bool,
155 generate_jsdoc: bool,
156 variadic: bool,
157 ret_ty_override: Option<&'a str>,
158 ret_desc: Option<&'a str>,
159 }
160
161 struct FunctionArgumentData<'a> {
162 name: String,
163 ty_override: Option<&'a str>,
164 desc: Option<&'a str>,
165 }
166
167 struct Struct<'a> {
168 name: &'a str,
169 fields: Vec<StructField<'a>>,
170 comments: Vec<&'a str>,
171 is_inspectable: bool,
172 generate_typescript: bool,
173 js_namespace: Option<Vec<&'a str>>,
174 private: bool,
175 }
176
177 struct StructField<'a> {
178 name: &'a str,
179 readonly: bool,
180 comments: Vec<&'a str>,
181 generate_typescript: bool,
182 generate_jsdoc: bool,
183 }
184
185 struct LocalModule<'a> {
186 identifier: &'a str,
187 contents: &'a str,
188 linked_module: bool,
189 }
190 }
191 }; } pub fn new_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("_new");
198 name
199}
200
201pub fn free_function(struct_name: &str) -> String {
202 let mut name = "__wbg_".to_string();
203 name.extend(struct_name.chars().flat_map(|s| s.to_lowercase()));
204 name.push_str("_free");
205 name
206}
207
208pub fn unwrap_function(struct_name: &str) -> String {
209 let mut name = "__wbg_".to_string();
210 name.extend(struct_name.chars().flat_map(|s| s.to_lowercase()));
211 name.push_str("_unwrap");
212 name
213}
214
215pub fn free_function_export_name(function_name: &str) -> String {
216 function_name.to_string()
217}
218
219pub fn struct_function_export_name(struct_: &str, f: &str) -> String {
220 let mut name = struct_
221 .chars()
222 .flat_map(|s| s.to_lowercase())
223 .collect::<String>();
224 name.push('_');
225 name.push_str(f);
226 name
227}
228
229pub fn struct_field_get(struct_: &str, f: &str) -> String {
230 let mut name = String::from("__wbg_get_");
231 name.extend(struct_.chars().flat_map(|s| s.to_lowercase()));
232 name.push('_');
233 name.push_str(f);
234 name
235}
236
237pub fn struct_field_set(struct_: &str, f: &str) -> String {
238 let mut name = String::from("__wbg_set_");
239 name.extend(struct_.chars().flat_map(|s| s.to_lowercase()));
240 name.push('_');
241 name.push_str(f);
242 name
243}
244
245pub fn version() -> String {
246 let mut v = env!("CARGO_PKG_VERSION").to_string();
247 if let Some(s) = option_env!("WBG_VERSION") {
248 v.push_str(" (");
249 v.push_str(s);
250 v.push(')');
251 }
252 v
253}