Skip to content

Commit 80384d8

Browse files
committed
address my comments for #470
1 parent 0c908bb commit 80384d8

File tree

6 files changed

+33
-47
lines changed

6 files changed

+33
-47
lines changed

crates/backend/src/ast.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub struct Program {
1010
pub enums: Vec<Enum>,
1111
pub structs: Vec<Struct>,
1212
pub type_aliases: Vec<TypeAlias>,
13+
pub consts: Vec<Const>,
1314
}
1415

1516
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
@@ -42,7 +43,6 @@ pub enum ImportKind {
4243
Static(ImportStatic),
4344
Type(ImportType),
4445
Enum(ImportEnum),
45-
Const(Const),
4646
}
4747

4848
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
@@ -179,7 +179,7 @@ pub struct TypeAlias {
179179
pub struct Const {
180180
pub vis: syn::Visibility,
181181
pub name: Ident,
182-
pub interface_name: Ident,
182+
pub class: Option<Ident>,
183183
pub ty: syn::Type,
184184
pub value: ConstValue,
185185
}
@@ -312,7 +312,6 @@ impl ImportKind {
312312
ImportKind::Static(_) => false,
313313
ImportKind::Type(_) => false,
314314
ImportKind::Enum(_) => false,
315-
ImportKind::Const(_) => false,
316315
}
317316
}
318317

@@ -322,7 +321,6 @@ impl ImportKind {
322321
ImportKind::Static(ref f) => shared::ImportKind::Static(f.shared()),
323322
ImportKind::Type(ref f) => shared::ImportKind::Type(f.shared()),
324323
ImportKind::Enum(ref f) => shared::ImportKind::Enum(f.shared()),
325-
ImportKind::Const(ref f) => shared::ImportKind::Const(f.shared()),
326324
}
327325
}
328326
}
@@ -425,9 +423,3 @@ impl StructField {
425423
}
426424
}
427425
}
428-
429-
impl Const {
430-
fn shared(&self) -> shared::Const {
431-
shared::Const {}
432-
}
433-
}

crates/backend/src/codegen.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ impl ToTokens for ast::Program {
6262
for a in self.type_aliases.iter() {
6363
a.to_tokens(tokens);
6464
}
65+
for c in self.consts.iter() {
66+
c.to_tokens(tokens);
67+
}
6568

6669
// Generate a static which will eventually be what lives in a custom section
6770
// 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 {
501504
ast::ImportKind::Static(ref s) => s.to_tokens(tokens),
502505
ast::ImportKind::Type(ref t) => t.to_tokens(tokens),
503506
ast::ImportKind::Enum(ref e) => e.to_tokens(tokens),
504-
ast::ImportKind::Const(ref c) => c.to_tokens(tokens),
505507
}
506508
}
507509
}
@@ -843,7 +845,6 @@ impl<'a> ToTokens for DescribeImport<'a> {
843845
ast::ImportKind::Static(_) => return,
844846
ast::ImportKind::Type(_) => return,
845847
ast::ImportKind::Enum(_) => return,
846-
ast::ImportKind::Const(_) => return,
847848
};
848849
let describe_name = format!("__wbindgen_describe_{}", f.shim);
849850
let describe_name = Ident::new(&describe_name, Span::call_site());
@@ -967,7 +968,6 @@ impl ToTokens for ast::Const {
967968

968969
let vis = &self.vis;
969970
let name = &self.name;
970-
let interface_name = &self.interface_name;
971971
let ty = &self.ty;
972972

973973
let value: TokenStream = match self.value {
@@ -984,17 +984,24 @@ impl ToTokens for ast::Const {
984984
FloatLiteral(f) => {
985985
let f = Literal::f64_unsuffixed(f);
986986
quote!(#f)
987-
},
987+
}
988988
IntegerLiteral(i) => {
989989
let i = Literal::i64_unsuffixed(i);
990990
quote!(#i)
991-
},
991+
}
992992
Null => unimplemented!(),
993993
};
994-
(quote! {
995-
impl #interface_name {
996-
#vis const #name: #ty = #value;
997-
}
998-
}).to_tokens(tokens);
994+
995+
let declaration = quote!(#vis const #name: #ty = #value;);
996+
997+
if let Some(class) = &self.class {
998+
(quote! {
999+
impl #class {
1000+
#declaration
1001+
}
1002+
}).to_tokens(tokens);
1003+
} else {
1004+
declaration.to_tokens(tokens);
1005+
}
9991006
}
10001007
}

crates/backend/src/defined.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ impl ImportedTypes for ast::Program {
7070
{
7171
self.imports.imported_types(f);
7272
self.type_aliases.imported_types(f);
73+
self.consts.imported_types(f);
7374
}
7475
}
7576

@@ -106,7 +107,6 @@ impl ImportedTypes for ast::ImportKind {
106107
ast::ImportKind::Function(fun) => fun.imported_types(f),
107108
ast::ImportKind::Type(ty) => ty.imported_types(f),
108109
ast::ImportKind::Enum(enm) => enm.imported_types(f),
109-
ast::ImportKind::Const(c) => c.imported_types(f),
110110
}
111111
}
112112
}
@@ -254,6 +254,7 @@ impl RemoveUndefinedImports for ast::Program {
254254
{
255255
self.imports.remove_undefined_imports(is_defined);
256256
self.type_aliases.remove_undefined_imports(is_defined);
257+
self.consts.remove_undefined_imports(is_defined);
257258
}
258259
}
259260

crates/cli-support/src/js/mod.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,7 +1758,6 @@ impl<'a, 'b> SubContext<'a, 'b> {
17581758
}
17591759
shared::ImportKind::Type(_) => {}
17601760
shared::ImportKind::Enum(_) => {}
1761-
shared::ImportKind::Const(_) => {}
17621761
}
17631762
Ok(())
17641763
}
@@ -1918,9 +1917,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
19181917
"
19191918
const {}_target = {} {} ;
19201919
",
1921-
import.shim,
1922-
target,
1923-
fallback
1920+
import.shim, target, fallback
19241921
));
19251922
format!(
19261923
"{}_target{}",
@@ -2020,9 +2017,7 @@ fn format_doc_comments(comments: &Vec<String>, js_doc_comments: Option<String>)
20202017
.map(|c| format!("*{}\n", c.trim_matches('"')))
20212018
.collect();
20222019
let doc = if let Some(docs) = js_doc_comments {
2023-
docs.lines()
2024-
.map(|l| format!("* {} \n", l))
2025-
.collect()
2020+
docs.lines().map(|l| format!("* {} \n", l)).collect()
20262021
} else {
20272022
String::new()
20282023
};

crates/shared/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ pub enum ImportKind {
3434
Static(ImportStatic),
3535
Type(ImportType),
3636
Enum(ImportEnum),
37-
Const(Const)
3837
}
3938

4039
#[derive(Deserialize, Serialize)]
@@ -125,9 +124,6 @@ pub struct StructField {
125124
pub comments: Vec<String>,
126125
}
127126

128-
#[derive(Deserialize, Serialize)]
129-
pub struct Const {}
130-
131127
pub fn new_function(struct_name: &str) -> String {
132128
let mut name = format!("__wbg_");
133129
name.extend(struct_name.chars().flat_map(|s| s.to_lowercase()));

crates/webidl/src/lib.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -647,23 +647,18 @@ impl<'a> WebidlParse<&'a str> for webidl::ast::Const {
647647
&self,
648648
program: &mut backend::ast::Program,
649649
_: &FirstPassRecord<'_>,
650-
interface_name: &'a str,
650+
self_name: &'a str,
651651
) -> Result<()> {
652-
let syn_ty = webidl_const_ty_to_syn_ty(&self.type_);
653-
program.imports.push(backend::ast::Import {
654-
module: None,
655-
version: None,
656-
js_namespace: None,
657-
kind: backend::ast::ImportKind::Const(backend::ast::Const {
658-
vis: syn::Visibility::Public(syn::VisPublic {
659-
pub_token: Default::default(),
660-
}),
661-
name: rust_ident(self.name.to_shouty_snake_case().as_str()),
662-
interface_name: rust_ident(interface_name.to_camel_case().as_str()),
663-
ty: syn_ty,
664-
value: webidl_const_v_to_backend_const_v(&self.value),
665-
}),
652+
let ty = webidl_const_ty_to_syn_ty(&self.type_);
653+
654+
program.consts.push(backend::ast::Const {
655+
vis: public(),
656+
name: rust_ident(self.name.to_shouty_snake_case().as_str()),
657+
class: Some(rust_ident(self_name.to_camel_case().as_str())),
658+
ty,
659+
value: webidl_const_v_to_backend_const_v(&self.value),
666660
});
661+
667662
Ok(())
668663
}
669664
}

0 commit comments

Comments
 (0)