-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Support WebIDL constants #470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6359872
862e4c5
e351294
b23b9cc
88f0e84
473ac6d
9ebd8bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ pub enum ImportKind { | |
Static(ImportStatic), | ||
Type(ImportType), | ||
Enum(ImportEnum), | ||
Const(Const), | ||
} | ||
|
||
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))] | ||
|
@@ -174,6 +175,24 @@ pub struct TypeAlias { | |
pub src: syn::Type, | ||
} | ||
|
||
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))] | ||
pub struct Const { | ||
pub vis: syn::Visibility, | ||
pub name: Ident, | ||
pub interface_name: Ident, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use |
||
pub ty: syn::Type, | ||
pub value: ConstValue, | ||
} | ||
|
||
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))] | ||
/// same as webidl::ast::ConstValue | ||
pub enum ConstValue { | ||
BooleanLiteral(bool), | ||
FloatLiteral(f64), | ||
IntegerLiteral(i64), | ||
Null, | ||
} | ||
|
||
impl Program { | ||
pub(crate) fn shared(&self) -> shared::Program { | ||
shared::Program { | ||
|
@@ -293,6 +312,7 @@ impl ImportKind { | |
ImportKind::Static(_) => false, | ||
ImportKind::Type(_) => false, | ||
ImportKind::Enum(_) => false, | ||
ImportKind::Const(_) => false, | ||
} | ||
} | ||
|
||
|
@@ -302,6 +322,7 @@ impl ImportKind { | |
ImportKind::Static(ref f) => shared::ImportKind::Static(f.shared()), | ||
ImportKind::Type(ref f) => shared::ImportKind::Type(f.shared()), | ||
ImportKind::Enum(ref f) => shared::ImportKind::Enum(f.shared()), | ||
ImportKind::Const(ref f) => shared::ImportKind::Const(f.shared()), | ||
} | ||
} | ||
} | ||
|
@@ -404,3 +425,9 @@ impl StructField { | |
} | ||
} | ||
} | ||
|
||
impl Const { | ||
fn shared(&self) -> shared::Const { | ||
shared::Const {} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,12 +29,12 @@ use std::path::Path; | |
use backend::defined::{ImportedTypeDefinitions, RemoveUndefinedImports}; | ||
use backend::util::{ident_ty, rust_ident, wrap_import_function}; | ||
use failure::ResultExt; | ||
use heck::CamelCase; | ||
use heck::{CamelCase, ShoutySnakeCase}; | ||
use quote::ToTokens; | ||
|
||
use util::{ | ||
create_basic_method, create_function, create_getter, create_setter, webidl_ty_to_syn_ty, | ||
TypePosition, | ||
create_basic_method, create_function, create_getter, create_setter, webidl_const_ty_to_syn_ty, | ||
webidl_const_v_to_backend_const_v, webidl_ty_to_syn_ty, TypePosition, | ||
}; | ||
|
||
/// Either `Ok(t)` or `Err(failure::Error)`. | ||
|
@@ -297,9 +297,9 @@ impl<'a> WebidlParse<&'a str> for webidl::ast::InterfaceMember { | |
attr.webidl_parse(program, self_name) | ||
} | ||
webidl::ast::InterfaceMember::Operation(ref op) => op.webidl_parse(program, self_name), | ||
webidl::ast::InterfaceMember::Const(ref c) => c.webidl_parse(program, self_name), | ||
// TODO | ||
webidl::ast::InterfaceMember::Const(_) | ||
| webidl::ast::InterfaceMember::Iterable(_) | ||
webidl::ast::InterfaceMember::Iterable(_) | ||
| webidl::ast::InterfaceMember::Maplike(_) | ||
| webidl::ast::InterfaceMember::Setlike(_) => { | ||
warn!("Unsupported WebIDL interface member: {:?}", self); | ||
|
@@ -474,3 +474,28 @@ impl<'a> WebidlParse<()> for webidl::ast::Enum { | |
Ok(()) | ||
} | ||
} | ||
|
||
impl<'a> WebidlParse<&'a str> for webidl::ast::Const { | ||
fn webidl_parse( | ||
&self, | ||
program: &mut backend::ast::Program, | ||
interface_name: &'a str, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
) -> Result<()> { | ||
let syn_ty = webidl_const_ty_to_syn_ty(&self.type_); | ||
program.imports.push(backend::ast::Import { | ||
module: None, | ||
version: None, | ||
js_namespace: None, | ||
kind: backend::ast::ImportKind::Const(backend::ast::Const { | ||
vis: syn::Visibility::Public(syn::VisPublic { | ||
pub_token: Default::default(), | ||
}), | ||
name: rust_ident(self.name.to_shouty_snake_case().as_str()), | ||
interface_name: rust_ident(interface_name), | ||
ty: syn_ty, | ||
value: webidl_const_v_to_backend_const_v(&self.value), | ||
}), | ||
}); | ||
Ok(()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't really an import, constants are not really importing anything. This constants should really be a field in the program.