address my comments for #470

This commit is contained in:
R. Andrew Ohana 2018-07-13 22:36:51 -07:00
parent 0c908bb951
commit 80384d8da9
6 changed files with 33 additions and 47 deletions

View File

@ -10,6 +10,7 @@ pub struct Program {
pub enums: Vec<Enum>, pub enums: Vec<Enum>,
pub structs: Vec<Struct>, pub structs: Vec<Struct>,
pub type_aliases: Vec<TypeAlias>, pub type_aliases: Vec<TypeAlias>,
pub consts: Vec<Const>,
} }
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))] #[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
@ -42,7 +43,6 @@ pub enum ImportKind {
Static(ImportStatic), Static(ImportStatic),
Type(ImportType), Type(ImportType),
Enum(ImportEnum), Enum(ImportEnum),
Const(Const),
} }
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))] #[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
@ -179,7 +179,7 @@ pub struct TypeAlias {
pub struct Const { pub struct Const {
pub vis: syn::Visibility, pub vis: syn::Visibility,
pub name: Ident, pub name: Ident,
pub interface_name: Ident, pub class: Option<Ident>,
pub ty: syn::Type, pub ty: syn::Type,
pub value: ConstValue, pub value: ConstValue,
} }
@ -312,7 +312,6 @@ impl ImportKind {
ImportKind::Static(_) => false, ImportKind::Static(_) => false,
ImportKind::Type(_) => false, ImportKind::Type(_) => false,
ImportKind::Enum(_) => false, ImportKind::Enum(_) => false,
ImportKind::Const(_) => false,
} }
} }
@ -322,7 +321,6 @@ impl ImportKind {
ImportKind::Static(ref f) => shared::ImportKind::Static(f.shared()), ImportKind::Static(ref f) => shared::ImportKind::Static(f.shared()),
ImportKind::Type(ref f) => shared::ImportKind::Type(f.shared()), ImportKind::Type(ref f) => shared::ImportKind::Type(f.shared()),
ImportKind::Enum(ref f) => shared::ImportKind::Enum(f.shared()), ImportKind::Enum(ref f) => shared::ImportKind::Enum(f.shared()),
ImportKind::Const(ref f) => shared::ImportKind::Const(f.shared()),
} }
} }
} }
@ -425,9 +423,3 @@ impl StructField {
} }
} }
} }
impl Const {
fn shared(&self) -> shared::Const {
shared::Const {}
}
}

View File

@ -62,6 +62,9 @@ impl ToTokens for ast::Program {
for a in self.type_aliases.iter() { for a in self.type_aliases.iter() {
a.to_tokens(tokens); a.to_tokens(tokens);
} }
for c in self.consts.iter() {
c.to_tokens(tokens);
}
// Generate a static which will eventually be what lives in a custom section // Generate a static which will eventually be what lives in a custom section
// of the wasm executable. For now it's just a plain old static, but we'll // of the wasm executable. For now it's just a plain old static, but we'll
@ -501,7 +504,6 @@ impl ToTokens for ast::ImportKind {
ast::ImportKind::Static(ref s) => s.to_tokens(tokens), ast::ImportKind::Static(ref s) => s.to_tokens(tokens),
ast::ImportKind::Type(ref t) => t.to_tokens(tokens), ast::ImportKind::Type(ref t) => t.to_tokens(tokens),
ast::ImportKind::Enum(ref e) => e.to_tokens(tokens), ast::ImportKind::Enum(ref e) => e.to_tokens(tokens),
ast::ImportKind::Const(ref c) => c.to_tokens(tokens),
} }
} }
} }
@ -843,7 +845,6 @@ impl<'a> ToTokens for DescribeImport<'a> {
ast::ImportKind::Static(_) => return, ast::ImportKind::Static(_) => return,
ast::ImportKind::Type(_) => return, ast::ImportKind::Type(_) => return,
ast::ImportKind::Enum(_) => return, ast::ImportKind::Enum(_) => return,
ast::ImportKind::Const(_) => return,
}; };
let describe_name = format!("__wbindgen_describe_{}", f.shim); let describe_name = format!("__wbindgen_describe_{}", f.shim);
let describe_name = Ident::new(&describe_name, Span::call_site()); let describe_name = Ident::new(&describe_name, Span::call_site());
@ -967,7 +968,6 @@ impl ToTokens for ast::Const {
let vis = &self.vis; let vis = &self.vis;
let name = &self.name; let name = &self.name;
let interface_name = &self.interface_name;
let ty = &self.ty; let ty = &self.ty;
let value: TokenStream = match self.value { let value: TokenStream = match self.value {
@ -984,17 +984,24 @@ impl ToTokens for ast::Const {
FloatLiteral(f) => { FloatLiteral(f) => {
let f = Literal::f64_unsuffixed(f); let f = Literal::f64_unsuffixed(f);
quote!(#f) quote!(#f)
}, }
IntegerLiteral(i) => { IntegerLiteral(i) => {
let i = Literal::i64_unsuffixed(i); let i = Literal::i64_unsuffixed(i);
quote!(#i) quote!(#i)
}, }
Null => unimplemented!(), Null => unimplemented!(),
}; };
(quote! {
impl #interface_name { let declaration = quote!(#vis const #name: #ty = #value;);
#vis const #name: #ty = #value;
} if let Some(class) = &self.class {
}).to_tokens(tokens); (quote! {
impl #class {
#declaration
}
}).to_tokens(tokens);
} else {
declaration.to_tokens(tokens);
}
} }
} }

View File

@ -70,6 +70,7 @@ impl ImportedTypes for ast::Program {
{ {
self.imports.imported_types(f); self.imports.imported_types(f);
self.type_aliases.imported_types(f); self.type_aliases.imported_types(f);
self.consts.imported_types(f);
} }
} }
@ -106,7 +107,6 @@ impl ImportedTypes for ast::ImportKind {
ast::ImportKind::Function(fun) => fun.imported_types(f), ast::ImportKind::Function(fun) => fun.imported_types(f),
ast::ImportKind::Type(ty) => ty.imported_types(f), ast::ImportKind::Type(ty) => ty.imported_types(f),
ast::ImportKind::Enum(enm) => enm.imported_types(f), ast::ImportKind::Enum(enm) => enm.imported_types(f),
ast::ImportKind::Const(c) => c.imported_types(f),
} }
} }
} }
@ -254,6 +254,7 @@ impl RemoveUndefinedImports for ast::Program {
{ {
self.imports.remove_undefined_imports(is_defined); self.imports.remove_undefined_imports(is_defined);
self.type_aliases.remove_undefined_imports(is_defined); self.type_aliases.remove_undefined_imports(is_defined);
self.consts.remove_undefined_imports(is_defined);
} }
} }

View File

@ -1758,7 +1758,6 @@ impl<'a, 'b> SubContext<'a, 'b> {
} }
shared::ImportKind::Type(_) => {} shared::ImportKind::Type(_) => {}
shared::ImportKind::Enum(_) => {} shared::ImportKind::Enum(_) => {}
shared::ImportKind::Const(_) => {}
} }
Ok(()) Ok(())
} }
@ -1918,9 +1917,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
" "
const {}_target = {} {} ; const {}_target = {} {} ;
", ",
import.shim, import.shim, target, fallback
target,
fallback
)); ));
format!( format!(
"{}_target{}", "{}_target{}",
@ -2020,9 +2017,7 @@ fn format_doc_comments(comments: &Vec<String>, js_doc_comments: Option<String>)
.map(|c| format!("*{}\n", c.trim_matches('"'))) .map(|c| format!("*{}\n", c.trim_matches('"')))
.collect(); .collect();
let doc = if let Some(docs) = js_doc_comments { let doc = if let Some(docs) = js_doc_comments {
docs.lines() docs.lines().map(|l| format!("* {} \n", l)).collect()
.map(|l| format!("* {} \n", l))
.collect()
} else { } else {
String::new() String::new()
}; };

View File

@ -34,7 +34,6 @@ pub enum ImportKind {
Static(ImportStatic), Static(ImportStatic),
Type(ImportType), Type(ImportType),
Enum(ImportEnum), Enum(ImportEnum),
Const(Const)
} }
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
@ -125,9 +124,6 @@ pub struct StructField {
pub comments: Vec<String>, pub comments: Vec<String>,
} }
#[derive(Deserialize, Serialize)]
pub struct Const {}
pub fn new_function(struct_name: &str) -> String { pub fn new_function(struct_name: &str) -> String {
let mut name = format!("__wbg_"); let mut name = format!("__wbg_");
name.extend(struct_name.chars().flat_map(|s| s.to_lowercase())); name.extend(struct_name.chars().flat_map(|s| s.to_lowercase()));

View File

@ -647,23 +647,18 @@ impl<'a> WebidlParse<&'a str> for webidl::ast::Const {
&self, &self,
program: &mut backend::ast::Program, program: &mut backend::ast::Program,
_: &FirstPassRecord<'_>, _: &FirstPassRecord<'_>,
interface_name: &'a str, self_name: &'a str,
) -> Result<()> { ) -> Result<()> {
let syn_ty = webidl_const_ty_to_syn_ty(&self.type_); let ty = webidl_const_ty_to_syn_ty(&self.type_);
program.imports.push(backend::ast::Import {
module: None, program.consts.push(backend::ast::Const {
version: None, vis: public(),
js_namespace: None, name: rust_ident(self.name.to_shouty_snake_case().as_str()),
kind: backend::ast::ImportKind::Const(backend::ast::Const { class: Some(rust_ident(self_name.to_camel_case().as_str())),
vis: syn::Visibility::Public(syn::VisPublic { ty,
pub_token: Default::default(), value: webidl_const_v_to_backend_const_v(&self.value),
}),
name: rust_ident(self.name.to_shouty_snake_case().as_str()),
interface_name: rust_ident(interface_name.to_camel_case().as_str()),
ty: syn_ty,
value: webidl_const_v_to_backend_const_v(&self.value),
}),
}); });
Ok(()) Ok(())
} }
} }