Skip to content

Commit 83cc988

Browse files
authored
Document the backend (#2365)
* Document the backend. All of it * Fix formatting error * Shim docs improvement * Further improve shim
1 parent e0ffa8f commit 83cc988

File tree

5 files changed

+164
-3
lines changed

5 files changed

+164
-3
lines changed

crates/backend/src/ast.rs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
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+
15
use crate::Diagnostic;
26
use proc_macro2::{Ident, Span};
37
use std::hash::{Hash, Hasher};
@@ -71,20 +75,29 @@ pub enum MethodSelf {
7175
RefShared,
7276
}
7377

78+
/// Things imported from a JS module (in an `extern` block)
7479
#[cfg_attr(feature = "extra-traits", derive(Debug))]
7580
#[derive(Clone)]
7681
pub struct Import {
82+
/// The type of module being imported from
7783
pub module: ImportModule,
84+
/// The namespace to access the item through, if any
7885
pub js_namespace: Option<Vec<String>>,
86+
/// The type of item being imported
7987
pub kind: ImportKind,
8088
}
8189

90+
/// The possible types of module to import from
8291
#[cfg_attr(feature = "extra-traits", derive(Debug))]
8392
#[derive(Clone)]
8493
pub enum ImportModule {
94+
/// No module / import from global scope
8595
None,
96+
/// Import from the named module, with relative paths interpreted
8697
Named(String, Span),
98+
/// Import from the named module, without interpreting paths
8799
RawNamed(String, Span),
100+
/// Import from an inline JS snippet
88101
Inline(usize, Span),
89102
}
90103

@@ -110,91 +123,147 @@ impl Hash for ImportModule {
110123
}
111124
}
112125

126+
/// The type of item being imported
113127
#[cfg_attr(feature = "extra-traits", derive(Debug))]
114128
#[derive(Clone)]
115129
pub enum ImportKind {
130+
/// Importing a function
116131
Function(ImportFunction),
132+
/// Importing a static value
117133
Static(ImportStatic),
134+
/// Importing a type/class
118135
Type(ImportType),
136+
/// Importing a JS enum
119137
Enum(ImportEnum),
120138
}
121139

140+
/// A function being imported from JS
122141
#[cfg_attr(feature = "extra-traits", derive(Debug))]
123142
#[derive(Clone)]
124143
pub struct ImportFunction {
144+
/// The full signature of the function
125145
pub function: Function,
146+
/// The name rust code will use
126147
pub rust_name: Ident,
148+
/// The type being returned
127149
pub js_ret: Option<syn::Type>,
150+
/// Whether to catch JS exceptions
128151
pub catch: bool,
152+
/// Whether the function is variadic on the JS side
129153
pub variadic: bool,
154+
/// Whether the function should use structural type checking
130155
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
131158
pub assert_no_shim: bool,
159+
/// The kind of function being imported
132160
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)
133164
pub shim: Ident,
165+
/// The doc comment on this import, if one is provided
134166
pub doc_comment: Option<String>,
135167
}
136168

169+
/// The type of a function being imported
137170
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
138171
#[derive(Clone)]
139172
pub enum ImportFunctionKind {
173+
/// A class method
140174
Method {
175+
/// The name of the class for this method, in JS
141176
class: String,
177+
/// The type of the class for this method, in Rust
142178
ty: syn::Type,
179+
/// The kind of method this is
143180
kind: MethodKind,
144181
},
182+
/// A standard function
145183
Normal,
146184
}
147185

186+
/// The type of a method
148187
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
149188
#[derive(Clone)]
150189
pub enum MethodKind {
190+
/// A class constructor
151191
Constructor,
192+
/// Any other kind of method
152193
Operation(Operation),
153194
}
154195

196+
/// The operation performed by a class method
155197
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
156198
#[derive(Clone)]
157199
pub struct Operation {
200+
/// Whether this method is static
158201
pub is_static: bool,
202+
/// The internal kind of this Operation
159203
pub kind: OperationKind,
160204
}
161205

206+
/// The kind of operation performed by a method
162207
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
163208
#[derive(Clone)]
164209
pub enum OperationKind {
210+
/// A standard method, nothing special
165211
Regular,
212+
/// A method for getting the value of the provided Ident
166213
Getter(Option<Ident>),
214+
/// A method for setting the value of the provided Ident
167215
Setter(Option<Ident>),
216+
/// A dynamically intercepted getter
168217
IndexingGetter,
218+
/// A dynamically intercepted setter
169219
IndexingSetter,
220+
/// A dynamically intercepted deleter
170221
IndexingDeleter,
171222
}
172223

224+
/// The type of a static being imported
173225
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
174226
#[derive(Clone)]
175227
pub struct ImportStatic {
228+
/// The visibility of this static in Rust
176229
pub vis: syn::Visibility,
230+
/// The type of static being imported
177231
pub ty: syn::Type,
232+
/// The name of the shim function used to access this static
178233
pub shim: Ident,
234+
/// The name of this static on the Rust side
179235
pub rust_name: Ident,
236+
/// The name of this static on the JS side
180237
pub js_name: String,
181238
}
182239

240+
/// The metadata for a type being imported
183241
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
184242
#[derive(Clone)]
185243
pub struct ImportType {
244+
/// The visibility of this type in Rust
186245
pub vis: syn::Visibility,
246+
/// The name of this type on the Rust side
187247
pub rust_name: Ident,
248+
/// The name of this type on the JS side
188249
pub js_name: String,
250+
/// The custom attributes to apply to this type
189251
pub attrs: Vec<syn::Attribute>,
252+
/// The TS definition to generate for this type
190253
pub typescript_type: Option<String>,
254+
/// The doc comment applied to this type, if one exists
191255
pub doc_comment: Option<String>,
256+
/// The name of the shim to check instanceof for this type
192257
pub instanceof_shim: String,
258+
/// The name of the remote function to use for the generated is_type_of
193259
pub is_type_of: Option<syn::Expr>,
260+
/// The list of classes this extends, if any
194261
pub extends: Vec<syn::Path>,
262+
/// A custom prefix to add and attempt to fall back to, if the type isn't found
195263
pub vendor_prefixes: Vec<Ident>,
196264
}
197265

266+
/// The metadata for an Enum being imported
198267
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
199268
#[derive(Clone)]
200269
pub struct ImportEnum {
@@ -210,76 +279,123 @@ pub struct ImportEnum {
210279
pub rust_attrs: Vec<syn::Attribute>,
211280
}
212281

282+
/// Information about a function being imported or exported
213283
#[cfg_attr(feature = "extra-traits", derive(Debug))]
214284
#[derive(Clone)]
215285
pub struct Function {
286+
/// The name of the function
216287
pub name: String,
288+
/// The span of the function's name in Rust code
217289
pub name_span: Span,
290+
/// Whether the function has a js_name attribute
218291
pub renamed_via_js_name: bool,
292+
/// The arguments to the function
219293
pub arguments: Vec<syn::PatType>,
294+
/// The return type of the function, if provided
220295
pub ret: Option<syn::Type>,
296+
/// Any custom attributes being applied to the function
221297
pub rust_attrs: Vec<syn::Attribute>,
298+
/// The visibility of this function in Rust
222299
pub rust_vis: syn::Visibility,
300+
/// Whether this is an `async` function
223301
pub r#async: bool,
302+
/// Whether to generate a typescript definition for this function
224303
pub generate_typescript: bool,
225304
}
226305

306+
/// Information about a Struct being exported
227307
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
228308
#[derive(Clone)]
229309
pub struct Struct {
310+
/// The name of the struct in Rust code
230311
pub rust_name: Ident,
312+
/// The name of the struct in JS code
231313
pub js_name: String,
314+
/// All the fields of this struct to export
232315
pub fields: Vec<StructField>,
316+
/// The doc comments on this struct, if provided
233317
pub comments: Vec<String>,
318+
/// Whether this struct is inspectable (provides toJSON/toString properties to JS)
234319
pub is_inspectable: bool,
320+
/// Whether to generate a typescript definition for this struct
235321
pub generate_typescript: bool,
236322
}
237323

324+
/// The field of a struct
238325
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
239326
#[derive(Clone)]
240327
pub struct StructField {
328+
/// The name of the field in Rust code
241329
pub rust_name: syn::Member,
330+
/// The name of the field in JS code
242331
pub js_name: String,
332+
/// The name of the struct this field is part of
243333
pub struct_name: Ident,
334+
/// Whether this value is read-only to JS
244335
pub readonly: bool,
336+
/// The type of this field
245337
pub ty: syn::Type,
338+
/// The name of the getter shim for this field
246339
pub getter: Ident,
340+
/// The name of the setter shim for this field
247341
pub setter: Ident,
342+
/// The doc comments on this field, if any
248343
pub comments: Vec<String>,
344+
/// Whether to generate a typescript definition for this field
249345
pub generate_typescript: bool,
250346
}
251347

348+
/// Information about an Enum being exported
252349
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
253350
#[derive(Clone)]
254351
pub struct Enum {
352+
/// The name of this enum in Rust code
255353
pub rust_name: Ident,
354+
/// The name of this enum in JS code
256355
pub js_name: String,
356+
/// The variants provided by this enum
257357
pub variants: Vec<Variant>,
358+
/// The doc comments on this enum, if any
258359
pub comments: Vec<String>,
360+
/// The value to use for a `none` variant of the enum
259361
pub hole: u32,
362+
/// Whether to generate a typescript definition for this enum
260363
pub generate_typescript: bool,
261364
}
262365

366+
/// The variant of an enum
263367
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
264368
#[derive(Clone)]
265369
pub struct Variant {
370+
/// The name of this variant
266371
pub name: Ident,
372+
/// The backing value of this variant
267373
pub value: u32,
374+
/// The doc comments on this variant, if any
268375
pub comments: Vec<String>,
269376
}
270377

378+
/// Unused, the type of an argument to / return from a function
271379
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
272380
pub enum TypeKind {
381+
/// A by-reference arg, EG `&T`
273382
ByRef,
383+
/// A by-mutable-reference arg, EG `&mut T`
274384
ByMutRef,
385+
/// A by-value arg, EG `T`
275386
ByValue,
276387
}
277388

389+
/// Unused, the location of a type for a function argument (import/export, argument/ret)
278390
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
279391
pub enum TypeLocation {
392+
/// An imported argument (JS side type)
280393
ImportArgument,
394+
/// An imported return
281395
ImportRet,
396+
/// An exported argument (Rust side type)
282397
ExportArgument,
398+
/// An exported return
283399
ExportRet,
284400
}
285401

crates/backend/src/codegen.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ use std::sync::Mutex;
1010
use syn;
1111
use wasm_bindgen_shared as shared;
1212

13+
/// A trait for converting AST structs into Tokens and adding them to a TokenStream,
14+
/// or providing a diagnostic if conversion fails.
1315
pub trait TryToTokens {
16+
/// Attempt to convert a `Self` into tokens and add it to the `TokenStream`
1417
fn try_to_tokens(&self, tokens: &mut TokenStream) -> Result<(), Diagnostic>;
1518

19+
/// Attempt to convert a `Self` into a new `TokenStream`
1620
fn try_to_token_stream(&self) -> Result<TokenStream, Diagnostic> {
1721
let mut tokens = TokenStream::new();
1822
self.try_to_tokens(&mut tokens)?;

crates/backend/src/error.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@ use proc_macro2::*;
22
use quote::{ToTokens, TokenStreamExt};
33
use syn::parse::Error;
44

5+
/// Provide a Diagnostic with the given span and message
56
#[macro_export]
67
macro_rules! err_span {
78
($span:expr, $($msg:tt)*) => (
89
$crate::Diagnostic::spanned_error(&$span, format!($($msg)*))
910
)
1011
}
1112

13+
/// Immediately fail and return an Err, with the arguments passed to err_span!
1214
#[macro_export]
1315
macro_rules! bail_span {
1416
($($t:tt)*) => (
1517
return Err(err_span!($($t)*).into())
1618
)
1719
}
1820

21+
/// A struct representing a diagnostic to emit to the end-user as an error.
1922
#[derive(Debug)]
2023
pub struct Diagnostic {
2124
inner: Repr,
@@ -34,6 +37,7 @@ enum Repr {
3437
}
3538

3639
impl Diagnostic {
40+
/// Generate a `Diagnostic` from an informational message with no Span
3741
pub fn error<T: Into<String>>(text: T) -> Diagnostic {
3842
Diagnostic {
3943
inner: Repr::Single {
@@ -43,6 +47,7 @@ impl Diagnostic {
4347
}
4448
}
4549

50+
/// Generate a `Diagnostic` from a Span and an informational message
4651
pub fn span_error<T: Into<String>>(span: Span, text: T) -> Diagnostic {
4752
Diagnostic {
4853
inner: Repr::Single {
@@ -52,6 +57,7 @@ impl Diagnostic {
5257
}
5358
}
5459

60+
/// Generate a `Diagnostic` from the span of any tokenizable object and a message
5561
pub fn spanned_error<T: Into<String>>(node: &dyn ToTokens, text: T) -> Diagnostic {
5662
Diagnostic {
5763
inner: Repr::Single {
@@ -61,6 +67,8 @@ impl Diagnostic {
6167
}
6268
}
6369

70+
/// Attempt to generate a `Diagnostic` from a vector of other `Diagnostic` instances.
71+
/// If the `Vec` is empty, returns `Ok(())`, otherwise returns the new `Diagnostic`
6472
pub fn from_vec(diagnostics: Vec<Diagnostic>) -> Result<(), Diagnostic> {
6573
if diagnostics.len() == 0 {
6674
Ok(())
@@ -71,6 +79,7 @@ impl Diagnostic {
7179
}
7280
}
7381

82+
/// Immediately trigger a panic from this `Diagnostic`
7483
#[allow(unconditional_recursion)]
7584
pub fn panic(&self) -> ! {
7685
match &self.inner {

0 commit comments

Comments
 (0)