Skip to content

webidl: add support for partial interfaces and mixins #460

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

Merged
merged 4 commits into from
Jul 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions crates/backend/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct Program {
pub enums: Vec<Enum>,
pub structs: Vec<Struct>,
pub type_aliases: Vec<TypeAlias>,
pub consts: Vec<Const>,
}

#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
Expand Down Expand Up @@ -42,7 +43,6 @@ pub enum ImportKind {
Static(ImportStatic),
Type(ImportType),
Enum(ImportEnum),
Const(Const),
}

#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
Expand Down Expand Up @@ -179,7 +179,7 @@ pub struct TypeAlias {
pub struct Const {
pub vis: syn::Visibility,
pub name: Ident,
pub interface_name: Ident,
pub class: Option<Ident>,
pub ty: syn::Type,
pub value: ConstValue,
}
Expand Down Expand Up @@ -312,7 +312,6 @@ impl ImportKind {
ImportKind::Static(_) => false,
ImportKind::Type(_) => false,
ImportKind::Enum(_) => false,
ImportKind::Const(_) => false,
}
}

Expand All @@ -322,7 +321,6 @@ 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()),
}
}
}
Expand Down Expand Up @@ -425,9 +423,3 @@ impl StructField {
}
}
}

impl Const {
fn shared(&self) -> shared::Const {
shared::Const {}
}
}
27 changes: 17 additions & 10 deletions crates/backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ impl ToTokens for ast::Program {
for a in self.type_aliases.iter() {
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
// of the wasm executable. For now it's just a plain old static, but we'll
Expand Down Expand Up @@ -501,7 +504,6 @@ impl ToTokens for ast::ImportKind {
ast::ImportKind::Static(ref s) => s.to_tokens(tokens),
ast::ImportKind::Type(ref t) => t.to_tokens(tokens),
ast::ImportKind::Enum(ref e) => e.to_tokens(tokens),
ast::ImportKind::Const(ref c) => c.to_tokens(tokens),
}
}
}
Expand Down Expand Up @@ -843,7 +845,6 @@ impl<'a> ToTokens for DescribeImport<'a> {
ast::ImportKind::Static(_) => return,
ast::ImportKind::Type(_) => return,
ast::ImportKind::Enum(_) => return,
ast::ImportKind::Const(_) => return,
};
let describe_name = format!("__wbindgen_describe_{}", f.shim);
let describe_name = Ident::new(&describe_name, Span::call_site());
Expand Down Expand Up @@ -967,7 +968,6 @@ impl ToTokens for ast::Const {

let vis = &self.vis;
let name = &self.name;
let interface_name = &self.interface_name;
let ty = &self.ty;

let value: TokenStream = match self.value {
Expand All @@ -984,17 +984,24 @@ impl ToTokens for ast::Const {
FloatLiteral(f) => {
let f = Literal::f64_unsuffixed(f);
quote!(#f)
},
}
IntegerLiteral(i) => {
let i = Literal::i64_unsuffixed(i);
quote!(#i)
},
}
Null => unimplemented!(),
};
(quote! {
impl #interface_name {
#vis const #name: #ty = #value;
}
}).to_tokens(tokens);

let declaration = quote!(#vis const #name: #ty = #value;);

if let Some(class) = &self.class {
(quote! {
impl #class {
#declaration
}
}).to_tokens(tokens);
} else {
declaration.to_tokens(tokens);
}
}
}
3 changes: 2 additions & 1 deletion crates/backend/src/defined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl ImportedTypes for ast::Program {
{
self.imports.imported_types(f);
self.type_aliases.imported_types(f);
self.consts.imported_types(f);
}
}

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

Expand Down
9 changes: 2 additions & 7 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1758,7 +1758,6 @@ impl<'a, 'b> SubContext<'a, 'b> {
}
shared::ImportKind::Type(_) => {}
shared::ImportKind::Enum(_) => {}
shared::ImportKind::Const(_) => {}
}
Ok(())
}
Expand Down Expand Up @@ -1918,9 +1917,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
"
const {}_target = {} {} ;
",
import.shim,
target,
fallback
import.shim, target, fallback
));
format!(
"{}_target{}",
Expand Down Expand Up @@ -2020,9 +2017,7 @@ fn format_doc_comments(comments: &Vec<String>, js_doc_comments: Option<String>)
.map(|c| format!("*{}\n", c.trim_matches('"')))
.collect();
let doc = if let Some(docs) = js_doc_comments {
docs.lines()
.map(|l| format!("* {} \n", l))
.collect()
docs.lines().map(|l| format!("* {} \n", l)).collect()
} else {
String::new()
};
Expand Down
4 changes: 0 additions & 4 deletions crates/shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ pub enum ImportKind {
Static(ImportStatic),
Type(ImportType),
Enum(ImportEnum),
Const(Const)
}

#[derive(Deserialize, Serialize)]
Expand Down Expand Up @@ -125,9 +124,6 @@ pub struct StructField {
pub comments: Vec<String>,
}

#[derive(Deserialize, Serialize)]
pub struct Const {}

pub fn new_function(struct_name: &str) -> String {
let mut name = format!("__wbg_");
name.extend(struct_name.chars().flat_map(|s| s.to_lowercase()));
Expand Down
150 changes: 150 additions & 0 deletions crates/webidl/src/first_pass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
use std::{
collections::{BTreeMap, BTreeSet}, mem,
};

use webidl;

use super::Result;

#[derive(Default)]
pub(crate) struct FirstPassRecord<'a> {
pub(crate) interfaces: BTreeSet<String>,
pub(crate) dictionaries: BTreeSet<String>,
pub(crate) enums: BTreeSet<String>,
pub(crate) mixins: BTreeMap<String, MixinData<'a>>,
}

#[derive(Default)]
pub(crate) struct MixinData<'a> {
pub(crate) non_partial: Option<&'a webidl::ast::NonPartialMixin>,
pub(crate) partials: Vec<&'a webidl::ast::PartialMixin>,
}

pub(crate) trait FirstPass {
fn first_pass<'a>(&'a self, record: &mut FirstPassRecord<'a>) -> Result<()>;
}

impl FirstPass for [webidl::ast::Definition] {
fn first_pass<'a>(&'a self, record: &mut FirstPassRecord<'a>) -> Result<()> {
for def in self {
def.first_pass(record)?;
}

Ok(())
}
}

impl FirstPass for webidl::ast::Definition {
fn first_pass<'a>(&'a self, record: &mut FirstPassRecord<'a>) -> Result<()> {
use webidl::ast::Definition::*;

match self {
Dictionary(dictionary) => dictionary.first_pass(record),
Enum(enum_) => enum_.first_pass(record),
Interface(interface) => interface.first_pass(record),
Mixin(mixin) => mixin.first_pass(record),
_ => {
// Other definitions aren't currently used in the first pass
Ok(())
}
}
}
}

impl FirstPass for webidl::ast::Dictionary {
fn first_pass<'a>(&'a self, record: &mut FirstPassRecord<'a>) -> Result<()> {
use webidl::ast::Dictionary::*;

match self {
NonPartial(dictionary) => dictionary.first_pass(record),
_ => {
// Other dictionaries aren't currently used in the first pass
Ok(())
}
}
}
}

impl FirstPass for webidl::ast::NonPartialDictionary {
fn first_pass<'a>(&'a self, record: &mut FirstPassRecord<'a>) -> Result<()> {
if record.dictionaries.insert(self.name.clone()) {
warn!("Encountered multiple declarations of {}", self.name);
}

Ok(())
}
}

impl FirstPass for webidl::ast::Enum {
fn first_pass<'a>(&'a self, record: &mut FirstPassRecord<'a>) -> Result<()> {
if record.enums.insert(self.name.clone()) {
warn!("Encountered multiple declarations of {}", self.name);
}

Ok(())
}
}

impl FirstPass for webidl::ast::Interface {
fn first_pass<'a>(&'a self, record: &mut FirstPassRecord<'a>) -> Result<()> {
use webidl::ast::Interface::*;

match self {
NonPartial(interface) => interface.first_pass(record),
_ => {
// Other interfaces aren't currently used in the first pass
Ok(())
}
}
}
}

impl FirstPass for webidl::ast::NonPartialInterface {
fn first_pass<'a>(&'a self, record: &mut FirstPassRecord<'a>) -> Result<()> {
if record.interfaces.insert(self.name.clone()) {
warn!("Encountered multiple declarations of {}", self.name);
}

Ok(())
}
}

impl FirstPass for webidl::ast::Mixin {
fn first_pass<'a>(&'a self, record: &mut FirstPassRecord<'a>) -> Result<()> {
use webidl::ast::Mixin::*;

match self {
NonPartial(mixin) => mixin.first_pass(record),
Partial(mixin) => mixin.first_pass(record),
}
}
}

impl FirstPass for webidl::ast::NonPartialMixin {
fn first_pass<'a>(&'a self, record: &mut FirstPassRecord<'a>) -> Result<()> {
let entry = record
.mixins
.entry(self.name.clone())
.or_insert(Default::default());
if mem::replace(&mut entry.non_partial, Some(self)).is_some() {
warn!(
"Encounterd multiple declarations of {}, using last encountered",
self.name
);
}

Ok(())
}
}

impl FirstPass for webidl::ast::PartialMixin {
fn first_pass<'a>(&'a self, record: &mut FirstPassRecord<'a>) -> Result<()> {
let entry = record
.mixins
.entry(self.name.clone())
.or_insert(Default::default());
entry.partials.push(self);

Ok(())
}
}
Loading