Implement support for WebIDL dictionaries

This commit adds support for generating bindings for dictionaries defined in
WebIDL. Dictionaries are associative arrays which are simply objects in JS with
named keys and some values. In Rust given a dictionary like:

    dictionary Foo {
        long field;
    };

we'll generate a struct like:

    pub struct Foo {
        obj: js_sys::Object,
    }

    impl Foo {
        pub fn new() -> Foo { /* make a blank object */ }

        pub fn field(&mut self, val: i32) -> &mut Self {
            // set the field using `js_sys::Reflect`
        }
    }

    // plus a bunch of AsRef, From, and wasm abi impls

At the same time this adds support for partial dictionaries and dictionary
inheritance. All dictionary fields are optional by default and hence only have
builder-style setters, but dictionaries can also have required fields. Required
fields are exposed as arguments to the `new` constructor.

Closes #241
This commit is contained in:
Alex Crichton
2018-08-14 10:16:18 -07:00
parent 13fe2b4aca
commit d6e48195b3
13 changed files with 502 additions and 31 deletions

View File

@ -9,6 +9,7 @@
use std::collections::{BTreeMap, BTreeSet};
use weedle::{DictionaryDefinition, PartialDictionaryDefinition};
use weedle::argument::Argument;
use weedle::attribute::ExtendedAttribute;
use weedle::interface::{StringifierOrStatic, Special};
@ -24,13 +25,13 @@ use util::camel_case_ident;
#[derive(Default)]
pub(crate) struct FirstPassRecord<'src> {
pub(crate) interfaces: BTreeMap<&'src str, InterfaceData<'src>>,
pub(crate) dictionaries: BTreeSet<&'src str>,
pub(crate) enums: BTreeSet<&'src str>,
/// The mixins, mapping their name to the webidl ast node for the mixin.
pub(crate) mixins: BTreeMap<&'src str, MixinData<'src>>,
pub(crate) typedefs: BTreeMap<&'src str, &'src weedle::types::Type<'src>>,
pub(crate) namespaces: BTreeMap<&'src str, NamespaceData<'src>>,
pub(crate) includes: BTreeMap<&'src str, BTreeSet<&'src str>>,
pub(crate) dictionaries: BTreeMap<&'src str, DictionaryData<'src>>,
}
/// We need to collect interface data during the first pass, to be used later.
@ -61,6 +62,13 @@ pub(crate) struct NamespaceData<'src> {
pub(crate) operations: BTreeMap<OperationId<'src>, OperationData<'src>>,
}
#[derive(Default)]
pub(crate) struct DictionaryData<'src> {
/// Whether only partial namespaces were encountered
pub(crate) partials: Vec<&'src PartialDictionaryDefinition<'src>>,
pub(crate) definition: Option<&'src DictionaryDefinition<'src>>,
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
pub(crate) enum OperationId<'src> {
Constructor,
@ -99,6 +107,7 @@ impl<'src> FirstPass<'src, ()> for weedle::Definition<'src> {
match self {
Dictionary(dictionary) => dictionary.first_pass(record, ()),
PartialDictionary(dictionary) => dictionary.first_pass(record, ()),
Enum(enum_) => enum_.first_pass(record, ()),
IncludesStatement(includes) => includes.first_pass(record, ()),
Interface(interface) => interface.first_pass(record, ()),
@ -118,14 +127,19 @@ impl<'src> FirstPass<'src, ()> for weedle::Definition<'src> {
impl<'src> FirstPass<'src, ()> for weedle::DictionaryDefinition<'src> {
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, (): ()) -> Result<()> {
if util::is_chrome_only(&self.attributes) {
return Ok(());
}
if !record.dictionaries.insert(self.identifier.0) {
info!("Encountered multiple dictionary declarations: {}", self.identifier.0);
}
record.dictionaries.entry(self.identifier.0)
.or_default()
.definition = Some(self);
Ok(())
}
}
impl<'src> FirstPass<'src, ()> for weedle::PartialDictionaryDefinition<'src> {
fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, (): ()) -> Result<()> {
record.dictionaries.entry(self.identifier.0)
.or_default()
.partials
.push(self);
Ok(())
}
}
@ -153,7 +167,7 @@ impl<'src> FirstPass<'src, ()> for weedle::IncludesStatementDefinition<'src> {
record
.includes
.entry(self.lhs_identifier.0)
.or_insert_with(Default::default)
.or_default()
.insert(self.rhs_identifier.0);
Ok(())
@ -372,7 +386,7 @@ impl<'src> FirstPass<'src, ()> for weedle::InterfaceMixinDefinition<'src>{
let mixin_data = record
.mixins
.entry(self.identifier.0)
.or_insert_with(Default::default);
.or_default();
mixin_data.partial = false;
mixin_data.members.extend(&self.members.body);
}