1
+ //! A representation of the Abstract Syntax Tree of a Rust program,
2
+ //! with all the added metadata necessary to generate WASM bindings
3
+ //! for it.
4
+
1
5
use crate :: Diagnostic ;
2
6
use proc_macro2:: { Ident , Span } ;
3
7
use std:: hash:: { Hash , Hasher } ;
@@ -71,20 +75,29 @@ pub enum MethodSelf {
71
75
RefShared ,
72
76
}
73
77
78
+ /// Things imported from a JS module (in an `extern` block)
74
79
#[ cfg_attr( feature = "extra-traits" , derive( Debug ) ) ]
75
80
#[ derive( Clone ) ]
76
81
pub struct Import {
82
+ /// The type of module being imported from
77
83
pub module : ImportModule ,
84
+ /// The namespace to access the item through, if any
78
85
pub js_namespace : Option < Vec < String > > ,
86
+ /// The type of item being imported
79
87
pub kind : ImportKind ,
80
88
}
81
89
90
+ /// The possible types of module to import from
82
91
#[ cfg_attr( feature = "extra-traits" , derive( Debug ) ) ]
83
92
#[ derive( Clone ) ]
84
93
pub enum ImportModule {
94
+ /// No module / import from global scope
85
95
None ,
96
+ /// Import from the named module, with relative paths interpreted
86
97
Named ( String , Span ) ,
98
+ /// Import from the named module, without interpreting paths
87
99
RawNamed ( String , Span ) ,
100
+ /// Import from an inline JS snippet
88
101
Inline ( usize , Span ) ,
89
102
}
90
103
@@ -110,91 +123,147 @@ impl Hash for ImportModule {
110
123
}
111
124
}
112
125
126
+ /// The type of item being imported
113
127
#[ cfg_attr( feature = "extra-traits" , derive( Debug ) ) ]
114
128
#[ derive( Clone ) ]
115
129
pub enum ImportKind {
130
+ /// Importing a function
116
131
Function ( ImportFunction ) ,
132
+ /// Importing a static value
117
133
Static ( ImportStatic ) ,
134
+ /// Importing a type/class
118
135
Type ( ImportType ) ,
136
+ /// Importing a JS enum
119
137
Enum ( ImportEnum ) ,
120
138
}
121
139
140
+ /// A function being imported from JS
122
141
#[ cfg_attr( feature = "extra-traits" , derive( Debug ) ) ]
123
142
#[ derive( Clone ) ]
124
143
pub struct ImportFunction {
144
+ /// The full signature of the function
125
145
pub function : Function ,
146
+ /// The name rust code will use
126
147
pub rust_name : Ident ,
148
+ /// The type being returned
127
149
pub js_ret : Option < syn:: Type > ,
150
+ /// Whether to catch JS exceptions
128
151
pub catch : bool ,
152
+ /// Whether the function is variadic on the JS side
129
153
pub variadic : bool ,
154
+ /// Whether the function should use structural type checking
130
155
pub structural : bool ,
156
+ /// Causes the Builder (See cli-support::js::binding::Builder) to error out if
157
+ /// it finds itself generating code for a function with this signature
131
158
pub assert_no_shim : bool ,
159
+ /// The kind of function being imported
132
160
pub kind : ImportFunctionKind ,
161
+ /// The shim name to use in the generated code. The 'shim' is a function that appears in
162
+ /// the generated JS as a wrapper around the actual function to import, performing any
163
+ /// necessary conversions (EG adding a try/catch to change a thrown error into a Result)
133
164
pub shim : Ident ,
165
+ /// The doc comment on this import, if one is provided
134
166
pub doc_comment : Option < String > ,
135
167
}
136
168
169
+ /// The type of a function being imported
137
170
#[ cfg_attr( feature = "extra-traits" , derive( Debug , PartialEq , Eq ) ) ]
138
171
#[ derive( Clone ) ]
139
172
pub enum ImportFunctionKind {
173
+ /// A class method
140
174
Method {
175
+ /// The name of the class for this method, in JS
141
176
class : String ,
177
+ /// The type of the class for this method, in Rust
142
178
ty : syn:: Type ,
179
+ /// The kind of method this is
143
180
kind : MethodKind ,
144
181
} ,
182
+ /// A standard function
145
183
Normal ,
146
184
}
147
185
186
+ /// The type of a method
148
187
#[ cfg_attr( feature = "extra-traits" , derive( Debug , PartialEq , Eq ) ) ]
149
188
#[ derive( Clone ) ]
150
189
pub enum MethodKind {
190
+ /// A class constructor
151
191
Constructor ,
192
+ /// Any other kind of method
152
193
Operation ( Operation ) ,
153
194
}
154
195
196
+ /// The operation performed by a class method
155
197
#[ cfg_attr( feature = "extra-traits" , derive( Debug , PartialEq , Eq ) ) ]
156
198
#[ derive( Clone ) ]
157
199
pub struct Operation {
200
+ /// Whether this method is static
158
201
pub is_static : bool ,
202
+ /// The internal kind of this Operation
159
203
pub kind : OperationKind ,
160
204
}
161
205
206
+ /// The kind of operation performed by a method
162
207
#[ cfg_attr( feature = "extra-traits" , derive( Debug , PartialEq , Eq ) ) ]
163
208
#[ derive( Clone ) ]
164
209
pub enum OperationKind {
210
+ /// A standard method, nothing special
165
211
Regular ,
212
+ /// A method for getting the value of the provided Ident
166
213
Getter ( Option < Ident > ) ,
214
+ /// A method for setting the value of the provided Ident
167
215
Setter ( Option < Ident > ) ,
216
+ /// A dynamically intercepted getter
168
217
IndexingGetter ,
218
+ /// A dynamically intercepted setter
169
219
IndexingSetter ,
220
+ /// A dynamically intercepted deleter
170
221
IndexingDeleter ,
171
222
}
172
223
224
+ /// The type of a static being imported
173
225
#[ cfg_attr( feature = "extra-traits" , derive( Debug , PartialEq , Eq ) ) ]
174
226
#[ derive( Clone ) ]
175
227
pub struct ImportStatic {
228
+ /// The visibility of this static in Rust
176
229
pub vis : syn:: Visibility ,
230
+ /// The type of static being imported
177
231
pub ty : syn:: Type ,
232
+ /// The name of the shim function used to access this static
178
233
pub shim : Ident ,
234
+ /// The name of this static on the Rust side
179
235
pub rust_name : Ident ,
236
+ /// The name of this static on the JS side
180
237
pub js_name : String ,
181
238
}
182
239
240
+ /// The metadata for a type being imported
183
241
#[ cfg_attr( feature = "extra-traits" , derive( Debug , PartialEq , Eq ) ) ]
184
242
#[ derive( Clone ) ]
185
243
pub struct ImportType {
244
+ /// The visibility of this type in Rust
186
245
pub vis : syn:: Visibility ,
246
+ /// The name of this type on the Rust side
187
247
pub rust_name : Ident ,
248
+ /// The name of this type on the JS side
188
249
pub js_name : String ,
250
+ /// The custom attributes to apply to this type
189
251
pub attrs : Vec < syn:: Attribute > ,
252
+ /// The TS definition to generate for this type
190
253
pub typescript_type : Option < String > ,
254
+ /// The doc comment applied to this type, if one exists
191
255
pub doc_comment : Option < String > ,
256
+ /// The name of the shim to check instanceof for this type
192
257
pub instanceof_shim : String ,
258
+ /// The name of the remote function to use for the generated is_type_of
193
259
pub is_type_of : Option < syn:: Expr > ,
260
+ /// The list of classes this extends, if any
194
261
pub extends : Vec < syn:: Path > ,
262
+ /// A custom prefix to add and attempt to fall back to, if the type isn't found
195
263
pub vendor_prefixes : Vec < Ident > ,
196
264
}
197
265
266
+ /// The metadata for an Enum being imported
198
267
#[ cfg_attr( feature = "extra-traits" , derive( Debug , PartialEq , Eq ) ) ]
199
268
#[ derive( Clone ) ]
200
269
pub struct ImportEnum {
@@ -210,76 +279,123 @@ pub struct ImportEnum {
210
279
pub rust_attrs : Vec < syn:: Attribute > ,
211
280
}
212
281
282
+ /// Information about a function being imported or exported
213
283
#[ cfg_attr( feature = "extra-traits" , derive( Debug ) ) ]
214
284
#[ derive( Clone ) ]
215
285
pub struct Function {
286
+ /// The name of the function
216
287
pub name : String ,
288
+ /// The span of the function's name in Rust code
217
289
pub name_span : Span ,
290
+ /// Whether the function has a js_name attribute
218
291
pub renamed_via_js_name : bool ,
292
+ /// The arguments to the function
219
293
pub arguments : Vec < syn:: PatType > ,
294
+ /// The return type of the function, if provided
220
295
pub ret : Option < syn:: Type > ,
296
+ /// Any custom attributes being applied to the function
221
297
pub rust_attrs : Vec < syn:: Attribute > ,
298
+ /// The visibility of this function in Rust
222
299
pub rust_vis : syn:: Visibility ,
300
+ /// Whether this is an `async` function
223
301
pub r#async : bool ,
302
+ /// Whether to generate a typescript definition for this function
224
303
pub generate_typescript : bool ,
225
304
}
226
305
306
+ /// Information about a Struct being exported
227
307
#[ cfg_attr( feature = "extra-traits" , derive( Debug , PartialEq , Eq ) ) ]
228
308
#[ derive( Clone ) ]
229
309
pub struct Struct {
310
+ /// The name of the struct in Rust code
230
311
pub rust_name : Ident ,
312
+ /// The name of the struct in JS code
231
313
pub js_name : String ,
314
+ /// All the fields of this struct to export
232
315
pub fields : Vec < StructField > ,
316
+ /// The doc comments on this struct, if provided
233
317
pub comments : Vec < String > ,
318
+ /// Whether this struct is inspectable (provides toJSON/toString properties to JS)
234
319
pub is_inspectable : bool ,
320
+ /// Whether to generate a typescript definition for this struct
235
321
pub generate_typescript : bool ,
236
322
}
237
323
324
+ /// The field of a struct
238
325
#[ cfg_attr( feature = "extra-traits" , derive( Debug , PartialEq , Eq ) ) ]
239
326
#[ derive( Clone ) ]
240
327
pub struct StructField {
328
+ /// The name of the field in Rust code
241
329
pub rust_name : syn:: Member ,
330
+ /// The name of the field in JS code
242
331
pub js_name : String ,
332
+ /// The name of the struct this field is part of
243
333
pub struct_name : Ident ,
334
+ /// Whether this value is read-only to JS
244
335
pub readonly : bool ,
336
+ /// The type of this field
245
337
pub ty : syn:: Type ,
338
+ /// The name of the getter shim for this field
246
339
pub getter : Ident ,
340
+ /// The name of the setter shim for this field
247
341
pub setter : Ident ,
342
+ /// The doc comments on this field, if any
248
343
pub comments : Vec < String > ,
344
+ /// Whether to generate a typescript definition for this field
249
345
pub generate_typescript : bool ,
250
346
}
251
347
348
+ /// Information about an Enum being exported
252
349
#[ cfg_attr( feature = "extra-traits" , derive( Debug , PartialEq , Eq ) ) ]
253
350
#[ derive( Clone ) ]
254
351
pub struct Enum {
352
+ /// The name of this enum in Rust code
255
353
pub rust_name : Ident ,
354
+ /// The name of this enum in JS code
256
355
pub js_name : String ,
356
+ /// The variants provided by this enum
257
357
pub variants : Vec < Variant > ,
358
+ /// The doc comments on this enum, if any
258
359
pub comments : Vec < String > ,
360
+ /// The value to use for a `none` variant of the enum
259
361
pub hole : u32 ,
362
+ /// Whether to generate a typescript definition for this enum
260
363
pub generate_typescript : bool ,
261
364
}
262
365
366
+ /// The variant of an enum
263
367
#[ cfg_attr( feature = "extra-traits" , derive( Debug , PartialEq , Eq ) ) ]
264
368
#[ derive( Clone ) ]
265
369
pub struct Variant {
370
+ /// The name of this variant
266
371
pub name : Ident ,
372
+ /// The backing value of this variant
267
373
pub value : u32 ,
374
+ /// The doc comments on this variant, if any
268
375
pub comments : Vec < String > ,
269
376
}
270
377
378
+ /// Unused, the type of an argument to / return from a function
271
379
#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
272
380
pub enum TypeKind {
381
+ /// A by-reference arg, EG `&T`
273
382
ByRef ,
383
+ /// A by-mutable-reference arg, EG `&mut T`
274
384
ByMutRef ,
385
+ /// A by-value arg, EG `T`
275
386
ByValue ,
276
387
}
277
388
389
+ /// Unused, the location of a type for a function argument (import/export, argument/ret)
278
390
#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
279
391
pub enum TypeLocation {
392
+ /// An imported argument (JS side type)
280
393
ImportArgument ,
394
+ /// An imported return
281
395
ImportRet ,
396
+ /// An exported argument (Rust side type)
282
397
ExportArgument ,
398
+ /// An exported return
283
399
ExportRet ,
284
400
}
285
401
0 commit comments