Skip to content

Commit

Permalink
Wrap the return type of indexing getters as Option<T> if necessary.
Browse files Browse the repository at this point in the history
  • Loading branch information
j-devel committed Sep 25, 2019
1 parent e809a45 commit 547f3dd
Showing 1 changed file with 24 additions and 1 deletion.
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<")
} 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

0 comments on commit 547f3dd

Please sign in to comment.