1#![doc(html_root_url = "https://docs.rs/wasm-bindgen-shared/0.2")]
2
3#[cfg(test)]
4mod schema_hash_approval;
5
6pub const SCHEMA_VERSION: &str = "0.2.98";
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 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<'a>),
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<'a> {
108 name: &'a str,
109 variant_values: Vec<&'a str>,
110 comments: Vec<&'a str>,
111 generate_typescript: bool,
112 }
113
114 struct Export<'a> {
115 class: Option<&'a str>,
116 comments: Vec<&'a str>,
117 consumed: bool,
118 function: Function<'a>,
119 method_kind: MethodKind<'a>,
120 start: bool,
121 }
122
123 struct Enum<'a> {
124 name: &'a str,
125 signed: bool,
126 variants: Vec<EnumVariant<'a>>,
127 comments: Vec<&'a str>,
128 generate_typescript: bool,
129 }
130
131 struct EnumVariant<'a> {
132 name: &'a str,
133 value: u32,
134 comments: Vec<&'a str>,
135 }
136
137 struct Function<'a> {
138 arg_names: Vec<String>,
139 asyncness: bool,
140 name: &'a str,
141 generate_typescript: bool,
142 generate_jsdoc: bool,
143 variadic: bool,
144 }
145
146 struct Struct<'a> {
147 name: &'a str,
148 fields: Vec<StructField<'a>>,
149 comments: Vec<&'a str>,
150 is_inspectable: bool,
151 generate_typescript: bool,
152 }
153
154 struct StructField<'a> {
155 name: &'a str,
156 readonly: bool,
157 comments: Vec<&'a str>,
158 generate_typescript: bool,
159 generate_jsdoc: bool,
160 }
161
162 struct LocalModule<'a> {
163 identifier: &'a str,
164 contents: &'a str,
165 linked_module: bool,
166 }
167 }
168 }; } pub fn new_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("_new");
175 name
176}
177
178pub fn free_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("_free");
182 name
183}
184
185pub fn unwrap_function(struct_name: &str) -> String {
186 let mut name = "__wbg_".to_string();
187 name.extend(struct_name.chars().flat_map(|s| s.to_lowercase()));
188 name.push_str("_unwrap");
189 name
190}
191
192pub fn free_function_export_name(function_name: &str) -> String {
193 function_name.to_string()
194}
195
196pub fn struct_function_export_name(struct_: &str, f: &str) -> String {
197 let mut name = struct_
198 .chars()
199 .flat_map(|s| s.to_lowercase())
200 .collect::<String>();
201 name.push('_');
202 name.push_str(f);
203 name
204}
205
206pub fn struct_field_get(struct_: &str, f: &str) -> String {
207 let mut name = String::from("__wbg_get_");
208 name.extend(struct_.chars().flat_map(|s| s.to_lowercase()));
209 name.push('_');
210 name.push_str(f);
211 name
212}
213
214pub fn struct_field_set(struct_: &str, f: &str) -> String {
215 let mut name = String::from("__wbg_set_");
216 name.extend(struct_.chars().flat_map(|s| s.to_lowercase()));
217 name.push('_');
218 name.push_str(f);
219 name
220}
221
222pub fn version() -> String {
223 let mut v = env!("CARGO_PKG_VERSION").to_string();
224 if let Some(s) = option_env!("WBG_VERSION") {
225 v.push_str(" (");
226 v.push_str(s);
227 v.push(')');
228 }
229 v
230}