Debian Codegraph @

.cargo/vendor/wasm-bindgen-shared/src/lib.rs

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