Skip to content

Commit dd030b3

Browse files
committed
Fixing code generation
1 parent caa41b2 commit dd030b3

File tree

188 files changed

+1178
-1715
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

188 files changed

+1178
-1715
lines changed

crates/backend/src/ast.rs

-57
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ pub struct Program {
1717
pub enums: Vec<Enum>,
1818
/// rust structs
1919
pub structs: Vec<Struct>,
20-
/// rust consts
21-
pub consts: Vec<Const>,
22-
/// "dictionaries", generated for WebIDL, which are basically just "typed
23-
/// objects" in the sense that they represent a JS object with a particular
24-
/// shape in JIT parlance.
25-
pub dictionaries: Vec<Dictionary>,
2620
/// custom typescript sections to be included in the definition file
2721
pub typescript_custom_sections: Vec<String>,
2822
/// Inline JS snippets
@@ -36,8 +30,6 @@ impl Program {
3630
self.imports.is_empty() &&
3731
self.enums.is_empty() &&
3832
self.structs.is_empty() &&
39-
self.consts.is_empty() &&
40-
self.dictionaries.is_empty() &&
4133
self.typescript_custom_sections.is_empty() &&
4234
self.inline_js.is_empty()
4335
}
@@ -284,55 +276,6 @@ pub enum TypeLocation {
284276
ExportRet,
285277
}
286278

287-
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq))]
288-
#[derive(Clone)]
289-
pub struct Const {
290-
pub vis: syn::Visibility,
291-
pub name: Ident,
292-
pub class: Option<Ident>,
293-
pub ty: syn::Type,
294-
pub value: ConstValue,
295-
}
296-
297-
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq))]
298-
#[derive(Clone)]
299-
/// same as webidl::ast::ConstValue
300-
pub enum ConstValue {
301-
BooleanLiteral(bool),
302-
FloatLiteral(f64),
303-
SignedIntegerLiteral(i64),
304-
UnsignedIntegerLiteral(u64),
305-
Null,
306-
}
307-
308-
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
309-
#[derive(Clone)]
310-
pub struct DictionaryConstructor {
311-
pub doc_comment: Option<String>,
312-
pub rust_attrs: Vec<syn::Attribute>,
313-
}
314-
315-
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
316-
#[derive(Clone)]
317-
pub struct Dictionary {
318-
pub vis: syn::Visibility,
319-
pub name: Ident,
320-
pub fields: Vec<DictionaryField>,
321-
pub constructor: Option<DictionaryConstructor>,
322-
pub doc_comment: Option<String>,
323-
}
324-
325-
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
326-
#[derive(Clone)]
327-
pub struct DictionaryField {
328-
pub rust_name: Ident,
329-
pub js_name: String,
330-
pub required: bool,
331-
pub ty: syn::Type,
332-
pub doc_comment: Option<String>,
333-
pub rust_attrs: Vec<syn::Attribute>,
334-
}
335-
336279
impl Export {
337280
/// Mangles a rust -> javascript export, so that the created Ident will be unique over function
338281
/// name and class name, if the function belongs to a javascript class.

crates/backend/src/codegen.rs

-157
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,6 @@ impl TryToTokens for ast::Program {
6464
for e in self.enums.iter() {
6565
e.to_tokens(tokens);
6666
}
67-
for c in self.consts.iter() {
68-
c.to_tokens(tokens);
69-
}
70-
for d in self.dictionaries.iter() {
71-
d.to_tokens(tokens);
72-
}
7367

7468
Diagnostic::from_vec(errors)?;
7569

@@ -1211,157 +1205,6 @@ impl ToTokens for ast::ImportStatic {
12111205
}
12121206
}
12131207

1214-
impl ToTokens for ast::ConstValue {
1215-
fn to_tokens(&self, tokens: &mut TokenStream) {
1216-
use crate::ast::ConstValue::*;
1217-
1218-
match self {
1219-
BooleanLiteral(false) => quote!(false),
1220-
BooleanLiteral(true) => quote!(true),
1221-
// the actual type is unknown because of typedefs
1222-
// so we cannot use std::fxx::INFINITY
1223-
// but we can use type inference
1224-
FloatLiteral(f) if f.is_infinite() && f.is_sign_positive() => quote!(1.0 / 0.0),
1225-
FloatLiteral(f) if f.is_infinite() && f.is_sign_negative() => quote!(-1.0 / 0.0),
1226-
FloatLiteral(f) if f.is_nan() => quote!(0.0 / 0.0),
1227-
// again no suffix
1228-
// panics on +-inf, nan
1229-
FloatLiteral(f) => {
1230-
let f = Literal::f64_suffixed(*f);
1231-
quote!(#f)
1232-
}
1233-
SignedIntegerLiteral(i) => {
1234-
let i = Literal::i64_suffixed(*i);
1235-
quote!(#i)
1236-
}
1237-
UnsignedIntegerLiteral(i) => {
1238-
let i = Literal::u64_suffixed(*i);
1239-
quote!(#i)
1240-
}
1241-
Null => unimplemented!(),
1242-
}.to_tokens(tokens)
1243-
}
1244-
}
1245-
1246-
impl ToTokens for ast::Const {
1247-
fn to_tokens(&self, tokens: &mut TokenStream) {
1248-
let vis = &self.vis;
1249-
let name = &self.name;
1250-
let ty = &self.ty;
1251-
let value = &self.value;
1252-
1253-
let declaration = quote!(#vis const #name: #ty = #value as #ty;);
1254-
1255-
if let Some(class) = &self.class {
1256-
(quote! {
1257-
impl #class {
1258-
#declaration
1259-
}
1260-
})
1261-
.to_tokens(tokens);
1262-
} else {
1263-
declaration.to_tokens(tokens);
1264-
}
1265-
}
1266-
}
1267-
1268-
impl ToTokens for ast::Dictionary {
1269-
fn to_tokens(&self, tokens: &mut TokenStream) {
1270-
let name = &self.name;
1271-
let vis = &self.vis;
1272-
let mut methods = TokenStream::new();
1273-
for field in self.fields.iter() {
1274-
field.to_tokens(&mut methods);
1275-
}
1276-
let required_names = &self
1277-
.fields
1278-
.iter()
1279-
.filter(|f| f.required)
1280-
.map(|f| &f.rust_name)
1281-
.collect::<Vec<_>>();
1282-
let required_types = &self
1283-
.fields
1284-
.iter()
1285-
.filter(|f| f.required)
1286-
.map(|f| &f.ty)
1287-
.collect::<Vec<_>>();
1288-
let required_names2 = required_names;
1289-
let required_names3 = required_names;
1290-
let doc_comment = match &self.doc_comment {
1291-
None => "",
1292-
Some(doc_string) => doc_string,
1293-
};
1294-
1295-
let ctor = match &self.constructor {
1296-
Some(ctor) => {
1297-
let attrs = &ctor.rust_attrs;
1298-
1299-
let doc_comment = match &ctor.doc_comment {
1300-
None => "",
1301-
Some(doc_string) => doc_string,
1302-
};
1303-
1304-
quote! {
1305-
#(#attrs)*
1306-
#[doc = #doc_comment]
1307-
pub fn new(#(#required_names: #required_types),*) -> #name {
1308-
let mut ret: #name = ::js_sys::Object::new().into();
1309-
#(ret.#required_names2(#required_names3);)*
1310-
ret
1311-
}
1312-
}
1313-
},
1314-
None => quote! {},
1315-
};
1316-
1317-
(quote! {
1318-
#[wasm_bindgen]
1319-
extern "C" {
1320-
#[wasm_bindgen(extends = ::js_sys::Object)]
1321-
#[doc = #doc_comment]
1322-
#[derive(Clone, Debug)]
1323-
#vis type #name;
1324-
}
1325-
1326-
#[allow(clippy::all)]
1327-
impl #name {
1328-
#ctor
1329-
#methods
1330-
}
1331-
})
1332-
.to_tokens(tokens);
1333-
}
1334-
}
1335-
1336-
impl ToTokens for ast::DictionaryField {
1337-
fn to_tokens(&self, tokens: &mut TokenStream) {
1338-
let attrs = &self.rust_attrs;
1339-
let rust_name = &self.rust_name;
1340-
let js_name = &self.js_name;
1341-
let ty = &self.ty;
1342-
let doc_comment = match &self.doc_comment {
1343-
None => "",
1344-
Some(doc_string) => doc_string,
1345-
};
1346-
(quote! {
1347-
#(#attrs)*
1348-
#[allow(clippy::all)]
1349-
#[doc = #doc_comment]
1350-
pub fn #rust_name(&mut self, val: #ty) -> &mut Self {
1351-
use wasm_bindgen::JsValue;
1352-
let r = ::js_sys::Reflect::set(
1353-
self.as_ref(),
1354-
&JsValue::from(#js_name),
1355-
&JsValue::from(val),
1356-
);
1357-
debug_assert!(r.is_ok(), "setting properties should never fail on our dictionary objects");
1358-
let _ = r;
1359-
self
1360-
}
1361-
}).to_tokens(tokens);
1362-
}
1363-
}
1364-
13651208
/// Emits the necessary glue tokens for "descriptor", generating an appropriate
13661209
/// symbol name as well as attributes around the descriptor function itself.
13671210
struct Descriptor<'a, T> {

0 commit comments

Comments
 (0)