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

@ -21,6 +21,10 @@ pub struct Program {
pub consts: Vec<Const>,
/// rust submodules
pub modules: Vec<Module>,
/// "dictionaries", generated for WebIDL, which are basically just "typed
/// objects" in the sense that they represent a JS object with a particular
/// shape in JIT parlance.
pub dictionaries: Vec<Dictionary>,
}
/// A rust to js interface. Allows interaction with rust objects/functions
@ -253,6 +257,21 @@ pub struct Module {
pub imports: Vec<Import>,
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[derive(Clone)]
pub struct Dictionary {
pub name: Ident,
pub fields: Vec<DictionaryField>,
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
#[derive(Clone)]
pub struct DictionaryField {
pub name: Ident,
pub required: bool,
pub ty: syn::Type,
}
impl Program {
pub(crate) fn shared(&self) -> Result<shared::Program, Diagnostic> {
Ok(shared::Program {

View File

@ -74,6 +74,9 @@ impl TryToTokens for ast::Program {
errors.push(e);
}
}
for d in self.dictionaries.iter() {
d.to_tokens(tokens);
}
Diagnostic::from_vec(errors)?;
@ -1135,6 +1138,153 @@ impl<'a> TryToTokens for ast::Module {
}
}
impl ToTokens for ast::Dictionary {
fn to_tokens(&self, tokens: &mut TokenStream) {
let name = &self.name;
let mut methods = TokenStream::new();
for field in self.fields.iter() {
field.to_tokens(&mut methods);
}
let required_names = &self.fields.iter()
.filter(|f| f.required)
.map(|f| &f.name)
.collect::<Vec<_>>();
let required_types = &self.fields.iter()
.filter(|f| f.required)
.map(|f| &f.ty)
.collect::<Vec<_>>();
let required_names2 = required_names;
let required_names3 = required_names;
let const_name = Ident::new(&format!("_CONST_{}", name), Span::call_site());
(quote! {
#[derive(Clone, Debug)]
#[repr(transparent)]
pub struct #name {
obj: ::js_sys::Object,
}
impl #name {
pub fn new(#(#required_names: #required_types),*) -> #name {
let mut _ret = #name { obj: ::js_sys::Object::new() };
#(_ret.#required_names2(#required_names3);)*
return _ret
}
#methods
}
#[allow(bad_style)]
const #const_name: () = {
use js_sys::Object;
use wasm_bindgen::describe::WasmDescribe;
use wasm_bindgen::convert::*;
use wasm_bindgen::{JsValue, JsCast};
use wasm_bindgen::__rt::core::mem::ManuallyDrop;
// interop w/ JsValue
impl From<#name> for JsValue {
fn from(val: #name) -> JsValue {
val.obj.into()
}
}
impl AsRef<JsValue> for #name {
fn as_ref(&self) -> &JsValue { self.obj.as_ref() }
}
impl AsMut<JsValue> for #name {
fn as_mut(&mut self) -> &mut JsValue { self.obj.as_mut() }
}
// Boundary conversion impls
impl WasmDescribe for #name {
fn describe() {
Object::describe();
}
}
impl IntoWasmAbi for #name {
type Abi = <Object as IntoWasmAbi>::Abi;
fn into_abi(self, extra: &mut Stack) -> Self::Abi {
self.obj.into_abi(extra)
}
}
impl<'a> IntoWasmAbi for &'a #name {
type Abi = <&'a Object as IntoWasmAbi>::Abi;
fn into_abi(self, extra: &mut Stack) -> Self::Abi {
(&self.obj).into_abi(extra)
}
}
impl FromWasmAbi for #name {
type Abi = <Object as FromWasmAbi>::Abi;
unsafe fn from_abi(abi: Self::Abi, extra: &mut Stack) -> Self {
#name { obj: Object::from_abi(abi, extra) }
}
}
impl OptionIntoWasmAbi for #name {
fn none() -> Self::Abi { Object::none() }
}
impl<'a> OptionIntoWasmAbi for &'a #name {
fn none() -> Self::Abi { <&'a Object>::none() }
}
impl OptionFromWasmAbi for #name {
fn is_none(abi: &Self::Abi) -> bool { Object::is_none(abi) }
}
impl RefFromWasmAbi for #name {
type Abi = <Object as RefFromWasmAbi>::Abi;
type Anchor = ManuallyDrop<#name>;
unsafe fn ref_from_abi(js: Self::Abi, extra: &mut Stack) -> Self::Anchor {
let tmp = <Object as RefFromWasmAbi>::ref_from_abi(js, extra);
ManuallyDrop::new(#name {
obj: ManuallyDrop::into_inner(tmp),
})
}
}
impl JsCast for #name {
fn instanceof(val: &JsValue) -> bool {
Object::instanceof(val)
}
fn unchecked_from_js(val: JsValue) -> Self {
#name { obj: Object::unchecked_from_js(val) }
}
fn unchecked_from_js_ref(val: &JsValue) -> &Self {
unsafe { &*(val as *const JsValue as *const #name) }
}
fn unchecked_from_js_mut(val: &mut JsValue) -> &mut Self {
unsafe { &mut *(val as *mut JsValue as *mut #name) }
}
}
};
}).to_tokens(tokens);
}
}
impl ToTokens for ast::DictionaryField {
fn to_tokens(&self, tokens: &mut TokenStream) {
let name = &self.name;
let ty = &self.ty;
(quote! {
pub fn #name(&mut self, val: #ty) -> &mut Self {
use wasm_bindgen::JsValue;
::js_sys::Reflect::set(
self.obj.as_ref(),
&JsValue::from(stringify!(#name)),
&JsValue::from(val),
);
self
}
}).to_tokens(tokens);
}
}
/// Emits the necessary glue tokens for "descriptor", generating an appropriate
/// symbol name as well as attributes around the descriptor function itself.
struct Descriptor<'a, T>(&'a Ident, T);

View File

@ -84,6 +84,7 @@ impl ImportedTypes for ast::Program {
{
self.imports.imported_types(f);
self.consts.imported_types(f);
self.dictionaries.imported_types(f);
}
}
@ -298,21 +299,44 @@ impl ImportedTypes for ast::Const {
}
}
impl ImportedTypes for ast::Dictionary {
fn imported_types<F>(&self, f: &mut F)
where
F: FnMut(&Ident, ImportedTypeKind),
{
f(&self.name, ImportedTypeKind::Definition);
for field in self.fields.iter() {
field.imported_types(f);
}
}
}
impl ImportedTypes for ast::DictionaryField {
fn imported_types<F>(&self, f: &mut F)
where
F: FnMut(&Ident, ImportedTypeKind),
{
self.ty.imported_types(f);
}
}
/// Remove any methods, statics, &c, that reference types that are *not*
/// defined.
pub trait RemoveUndefinedImports {
fn remove_undefined_imports<F>(&mut self, is_defined: &F)
fn remove_undefined_imports<F>(&mut self, is_defined: &F) -> bool
where
F: Fn(&Ident) -> bool;
}
impl RemoveUndefinedImports for ast::Program {
fn remove_undefined_imports<F>(&mut self, is_defined: &F)
fn remove_undefined_imports<F>(&mut self, is_defined: &F) -> bool
where
F: Fn(&Ident) -> bool,
{
self.imports.remove_undefined_imports(is_defined);
self.consts.remove_undefined_imports(is_defined);
let a = self.imports.remove_undefined_imports(is_defined);
let b = self.consts.remove_undefined_imports(is_defined);
let c = self.dictionaries.remove_undefined_imports(is_defined);
a || b || c
}
}
@ -320,10 +344,11 @@ impl<T> RemoveUndefinedImports for Vec<T>
where
T: ImportedTypeReferences,
{
fn remove_undefined_imports<F>(&mut self, is_defined: &F)
fn remove_undefined_imports<F>(&mut self, is_defined: &F) -> bool
where
F: Fn(&Ident) -> bool,
{
let before = self.len();
self.retain(|x| {
let mut all_defined = true;
x.imported_type_references(&mut |id| {
@ -336,5 +361,6 @@ where
});
all_defined
});
before != self.len()
}
}