Skip to content

Commit

Permalink
Remove type stuff from iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-uk1 committed Jan 24, 2020
1 parent a7449ec commit 3f50918
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 43 deletions.
4 changes: 2 additions & 2 deletions crates/backend/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,9 @@ pub struct DictionaryField {
#[derive(Clone)]
pub enum Iterable {
/// The iterator returns pairs of the form `[key, value]` (in webidl `iterable<Type, Type>`)
MapLike { key: syn::Type, value: syn::Type },
MapLike,
/// The iterator returns the given type (in webidl `iterable<Type>`)
ArrayLike { value: syn::Type },
ArrayLike,
}

impl Export {
Expand Down
10 changes: 5 additions & 5 deletions crates/backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1526,27 +1526,27 @@ fn iterable(rust_name: &Ident, iterable: &ast::Iterable) -> TokenStream {
let iter_name = Ident::new(&format!("{}Iter", rust_name), rust_name.span());

let item = match iterable {
ast::Iterable::MapLike { .. } => quote!((::wasm_bindgen::JsValue, ::wasm_bindgen::JsValue)),
ast::Iterable::ArrayLike { .. } => quote!(::wasm_bindgen::JsValue),
ast::Iterable::MapLike => quote!((::wasm_bindgen::JsValue, ::wasm_bindgen::JsValue)),
ast::Iterable::ArrayLike => quote!(::wasm_bindgen::JsValue),
};
let get_values = match iterable {
ast::Iterable::MapLike { .. } => quote!{
ast::Iterable::MapLike => quote!{
let entry = next.value();
debug_assert!(entry.has_type::<js_sys::Array>());
let entry: js_sys::Array = entry.unchecked_into();
let key = entry.get(0);
let value = entry.get(1);
Some((key, value))
},
ast::Iterable::ArrayLike { .. } => quote!(Some(next.value())),
ast::Iterable::ArrayLike => quote!(Some(next.value())),
};

quote! {
#[allow(non_upper_case_globals)]
#[allow(clippy::all)]
const #const_name: () = {
use wasm_bindgen::{UnwrapThrowExt, JsCast};
struct #iter_name(js_sys::Iterator);
struct #iter_name(::js_sys::Iterator);

impl Iterator for #iter_name {
type Item = #item;
Expand Down
44 changes: 8 additions & 36 deletions crates/webidl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,8 +809,6 @@ impl<'src> FirstPassRecord<'src> {
) {
let mut fields = Vec::new();
let mut iterable = None;
let iter_type =
|iter: &weedle::types::Type| iter.to_idl_type(self).to_syn_type(TypePosition::Return);
for member in item.definition.members.body.iter() {
match member {
InterfaceMember::Operation(op) => {
Expand All @@ -827,34 +825,11 @@ impl<'src> FirstPassRecord<'src> {
doc_comment: None,
});
}
InterfaceMember::Iterable(IterableInterfaceMember::Single(single)) => {
match iter_type(&single.generics.body.type_) {
Some(value) => {
iterable = Some(ast::Iterable::ArrayLike { value });
}
None => {
log::warn!(
"Skipping iterable because types could not be converted on {}",
item.definition.identifier.0
);
}
};
InterfaceMember::Iterable(IterableInterfaceMember::Single(_)) => {
iterable = Some(ast::Iterable::ArrayLike);
}
InterfaceMember::Iterable(IterableInterfaceMember::Double(double)) => {
match (
iter_type(&double.generics.body.0.type_),
iter_type(&double.generics.body.2.type_),
) {
(Some(key), Some(value)) => {
iterable = Some(ast::Iterable::MapLike { key, value })
}
_ => {
log::warn!(
"Skipping iterable because types could not be converted on {}",
item.definition.identifier.0
);
}
}
InterfaceMember::Iterable(IterableInterfaceMember::Double(_)) => {
iterable = Some(ast::Iterable::MapLike);
}
_ => {
log::warn!(
Expand All @@ -878,13 +853,10 @@ impl<'src> FirstPassRecord<'src> {
/// Convert a `webidl::Iterable` into an `ast::Iterable`.
fn iterable(&self, iterable: &Iterable) -> Option<ast::Iterable> {
Some(match iterable {
Iterable::MapLike { key, value } => ast::Iterable::MapLike {
key: key.to_idl_type(self).to_syn_type(TypePosition::Return)?,
value: value.to_idl_type(self).to_syn_type(TypePosition::Return)?,
},
Iterable::ArrayLike { value } => ast::Iterable::ArrayLike {
value: value.to_idl_type(self).to_syn_type(TypePosition::Return)?,
},
// In the future this data could be used to generate types and glue code in the ast.
// For now we discard the type info and use `JsValue`s
Iterable::MapLike { .. } => ast::Iterable::MapLike,
Iterable::ArrayLike { .. } => ast::Iterable::ArrayLike,
})
}
}

0 comments on commit 3f50918

Please sign in to comment.