Skip to content
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

Fix return type of WebIDL indexing getters #1789

Merged
merged 9 commits into from
Oct 4, 2019
25 changes: 24 additions & 1 deletion crates/webidl/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::ptr;

use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
use proc_macro2::{Ident, Span};
use quote::quote;
use syn;
use wasm_bindgen_backend::ast;
use wasm_bindgen_backend::util::{ident_ty, leading_colon_path_ty, raw_ident, rust_ident};
Expand Down Expand Up @@ -227,6 +228,7 @@ impl<'src> FirstPassRecord<'src> {
idl_arguments: impl Iterator<Item = (&'a str, &'a IdlType<'src>)>,
ret: &IdlType<'src>,
kind: ast::ImportFunctionKind,
is_indexing_getter: bool,
structural: bool,
catch: bool,
variadic: bool,
Expand Down Expand Up @@ -294,9 +296,24 @@ impl<'src> FirstPassRecord<'src> {
}
},
};
let js_ret = ret.clone();

let is_indexing_getter_to_fix = is_indexing_getter && // exclude non index getters.
!catch && // exclude indexing getters that return `Result<T>`.
if let Some(ref ty) = ret { // exclude indexing getters that return `Option<T>`.
!format!("{}", quote! { #ty })
.replace(" ", "")
.starts_with("Option<")
j-devel marked this conversation as resolved.
Show resolved Hide resolved
} else {
false
};

let mut js_ret = ret.clone();
let ret = if catch {
Some(ret.map_or_else(|| result_ty(unit_ty()), result_ty))
} else if is_indexing_getter_to_fix {
let ret = Some(ret.map_or_else(|| option_ty(unit_ty()), option_ty));
js_ret = ret.clone();
ret
} else {
ret
};
Expand Down Expand Up @@ -349,6 +366,7 @@ impl<'src> FirstPassRecord<'src> {
None.into_iter(),
&ret,
kind,
false,
is_structural(attrs.as_ref(), container_attrs),
throws(attrs),
false,
Expand Down Expand Up @@ -379,6 +397,7 @@ impl<'src> FirstPassRecord<'src> {
Some((name, &field_ty)).into_iter(),
&IdlType::Void,
kind,
false,
is_structural(attrs.as_ref(), container_attrs),
throws(attrs),
false,
Expand Down Expand Up @@ -516,6 +535,8 @@ impl<'src> FirstPassRecord<'src> {
OperationId::IndexingDeleter => ("delete", true, false),
};

let is_indexing_getter = id == &OperationId::IndexingGetter;

let mut ret = Vec::new();
for signature in actual_signatures.iter() {
// Ignore signatures with invalid return types
Expand Down Expand Up @@ -598,6 +619,7 @@ impl<'src> FirstPassRecord<'src> {
.map(|(idl_type, orig_arg)| (orig_arg.name, idl_type)),
&ret_ty,
kind.clone(),
is_indexing_getter,
structural,
catch,
variadic,
Expand All @@ -624,6 +646,7 @@ impl<'src> FirstPassRecord<'src> {
.map(|(name, idl_type)| (&name[..], idl_type.clone())),
&ret_ty,
kind.clone(),
is_indexing_getter,
structural,
catch,
false,
Expand Down