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.101";
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 kind: ImportKind<'a>,
43 }
44
45 struct LinkedModule<'a> {
46 module: ImportModule<'a>,
47 link_function_name: &'a str,
48 }
49
50 enum ImportModule<'a> {
51 Named(&'a str),
52 RawNamed(&'a str),
53 Inline(u32),
54 }
55
56 enum ImportKind<'a> {
57 Function(ImportFunction<'a>),
58 Static(ImportStatic<'a>),
59 String(ImportString<'a>),
60 Type(ImportType<'a>),
61 Enum(StringEnum<'a>),
62 }
63
64 struct ImportFunction<'a> {
65 shim: &'a str,
66 catch: bool,
67 variadic: bool,
68 assert_no_shim: bool,
69 method: Option<MethodData<'a>>,
70 structural: bool,
71 function: Function<'a>,
72 }
73
74 struct MethodData<'a> {
75 class: &'a str,
76 kind: MethodKind<'a>,
77 }
78
79 enum MethodKind<'a> {
80 Constructor,
81 Operation(Operation<'a>),
82 }
83
84 struct Operation<'a> {
85 is_static: bool,
86 kind: OperationKind<'a>,
87 }
88
89 enum OperationKind<'a> {
90 Regular,
91 Getter(&'a str),
92 Setter(&'a str),
93 IndexingGetter,
94 IndexingSetter,
95 IndexingDeleter,
96 }
97
98 struct ImportStatic<'a> {
99 name: &'a str,
100 shim: &'a str,
101 }
102
103 struct ImportString<'a> {
104 shim: &'a str,
105 string: &'a str,
106 }
107
108 struct ImportType<'a> {
109 name: &'a str,
110 instanceof_shim: &'a str,
111 vendor_prefixes: Vec<&'a str>,
112 }
113
114 struct StringEnum<'a> {
115 name: &'a str,
116 variant_values: Vec<&'a str>,
117 comments: Vec<&'a str>,
118 generate_typescript: bool,
119 }
120
121 struct Export<'a> {
122 class: Option<&'a str>,
123 comments: Vec<&'a str>,
124 consumed: bool,
125 function: Function<'a>,
126 method_kind: MethodKind<'a>,
127 start: bool,
128 }
129
130 struct Enum<'a> {
131 name: &'a str,
132 signed: bool,
133 variants: Vec<EnumVariant<'a>>,
134 comments: Vec<&'a str>,
135 generate_typescript: bool,
136 }
137
138 struct EnumVariant<'a> {
139 name: &'a str,
140 value: u32,
141 comments: Vec<&'a str>,
142 }
143
144 struct Function<'a> {
145 args: Vec<FunctionArgumentData<'a>>,
146 asyncness: bool,
147 name: &'a str,
148 generate_typescript: bool,
149 generate_jsdoc: bool,
150 variadic: bool,
151 ret_ty_override: Option<&'a str>,
152 ret_desc: Option<&'a str>,
153 }
154
155 struct FunctionArgumentData<'a> {
156 name: String,
157 ty_override: Option<&'a str>,
158 desc: Option<&'a str>,
159 }
160
161 struct Struct<'a> {
162 name: &'a str,
163 fields: Vec<StructField<'a>>,
164 comments: Vec<&'a str>,
165 is_inspectable: bool,
166 generate_typescript: bool,
167 }
168
169 struct StructField<'a> {
170 name: &'a str,
171 readonly: bool,
172 comments: Vec<&'a str>,
173 generate_typescript: bool,
174 generate_jsdoc: bool,
175 }
176
177 struct LocalModule<'a> {
178 identifier: &'a str,
179 contents: &'a str,
180 linked_module: bool,
181 }
182 }
183 }; } pub fn new_function(struct_name: &str) -> String {
187 let mut name = "__wbg_".to_string();
188 name.extend(struct_name.chars().flat_map(|s| s.to_lowercase()));
189 name.push_str("_new");
190 name
191}
192
193pub fn free_function(struct_name: &str) -> String {
194 let mut name = "__wbg_".to_string();
195 name.extend(struct_name.chars().flat_map(|s| s.to_lowercase()));
196 name.push_str("_free");
197 name
198}
199
200pub fn unwrap_function(struct_name: &str) -> String {
201 let mut name = "__wbg_".to_string();
202 name.extend(struct_name.chars().flat_map(|s| s.to_lowercase()));
203 name.push_str("_unwrap");
204 name
205}
206
207pub fn free_function_export_name(function_name: &str) -> String {
208 function_name.to_string()
209}
210
211pub fn struct_function_export_name(struct_: &str, f: &str) -> String {
212 let mut name = struct_
213 .chars()
214 .flat_map(|s| s.to_lowercase())
215 .collect::<String>();
216 name.push('_');
217 name.push_str(f);
218 name
219}
220
221pub fn struct_field_get(struct_: &str, f: &str) -> String {
222 let mut name = String::from("__wbg_get_");
223 name.extend(struct_.chars().flat_map(|s| s.to_lowercase()));
224 name.push('_');
225 name.push_str(f);
226 name
227}
228
229pub fn struct_field_set(struct_: &str, f: &str) -> String {
230 let mut name = String::from("__wbg_set_");
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 version() -> String {
238 let mut v = env!("CARGO_PKG_VERSION").to_string();
239 if let Some(s) = option_env!("WBG_VERSION") {
240 v.push_str(" (");
241 v.push_str(s);
242 v.push(')');
243 }
244 v
245}