Add customisable is_type_of

This commit is contained in:
Ingvar Stepanyan
2019-03-26 19:29:46 +00:00
parent 4211fcd992
commit cb880bdbff
6 changed files with 43 additions and 20 deletions

View File

@ -183,6 +183,7 @@ pub struct ImportType {
pub attrs: Vec<syn::Attribute>,
pub doc_comment: Option<String>,
pub instanceof_shim: String,
pub is_type_of: Option<syn::Expr>,
pub extends: Vec<syn::Path>,
pub vendor_prefixes: Vec<Ident>,
}

View File

@ -588,6 +588,13 @@ impl ToTokens for ast::ImportType {
}
};
let is_type_of = self.is_type_of.as_ref().map(|is_type_of| quote! {
fn is_type_of(val: &JsValue) -> bool {
let is_type_of: fn(&JsValue) -> bool = #is_type_of;
is_type_of(val)
}
});
(quote! {
#[allow(bad_style)]
#(#attrs)*
@ -720,6 +727,8 @@ impl ToTokens for ast::ImportType {
panic!("cannot check instanceof on non-wasm targets");
}
#is_type_of
#[inline]
fn unchecked_from_js(val: JsValue) -> Self {
#rust_name { obj: val.into() }