1
0
mirror of https://github.com/fluencelabs/wasm-bindgen synced 2025-03-31 17:31:06 +00:00

Use the JS name of an imported type for instanceof checks

This commit is contained in:
Nick Fitzgerald 2018-08-07 16:18:13 -07:00
parent 9104bf87e9
commit 998d37a353
6 changed files with 31 additions and 2 deletions
crates
backend/src
macro-support/src
web-sys/tests/wasm
webidl/src
tests/wasm

@ -125,6 +125,7 @@ pub struct ImportStatic {
pub struct ImportType { pub struct ImportType {
pub vis: syn::Visibility, pub vis: syn::Visibility,
pub rust_name: Ident, pub rust_name: Ident,
pub js_name: String,
pub attrs: Vec<syn::Attribute>, pub attrs: Vec<syn::Attribute>,
pub doc_comment: Option<String>, pub doc_comment: Option<String>,
pub instanceof_shim: String, pub instanceof_shim: String,
@ -411,7 +412,7 @@ impl ImportStatic {
impl ImportType { impl ImportType {
fn shared(&self) -> shared::ImportType { fn shared(&self) -> shared::ImportType {
shared::ImportType { shared::ImportType {
name: self.rust_name.to_string(), name: self.js_name.clone(),
instanceof_shim: self.instanceof_shim.clone(), instanceof_shim: self.instanceof_shim.clone(),
} }
} }

@ -542,6 +542,9 @@ impl ConvertToAst<BindgenAttrs> for syn::ForeignItemType {
type Target = ast::ImportKind; type Target = ast::ImportKind;
fn convert(self, attrs: BindgenAttrs) -> Result<Self::Target, Diagnostic> { fn convert(self, attrs: BindgenAttrs) -> Result<Self::Target, Diagnostic> {
let js_name = attrs
.js_name()
.map_or_else(|| self.ident.to_string(), |s| s.to_string());
let shim = format!("__wbg_instanceof_{}_{}", self.ident, ShortHash(&self.ident)); let shim = format!("__wbg_instanceof_{}_{}", self.ident, ShortHash(&self.ident));
Ok(ast::ImportKind::Type(ast::ImportType { Ok(ast::ImportKind::Type(ast::ImportType {
vis: self.vis, vis: self.vis,
@ -549,6 +552,7 @@ impl ConvertToAst<BindgenAttrs> for syn::ForeignItemType {
doc_comment: None, doc_comment: None,
instanceof_shim: shim, instanceof_shim: shim,
rust_name: self.ident, rust_name: self.ident,
js_name,
extends: attrs.extends().cloned().collect(), extends: attrs.extends().cloned().collect(),
})) }))
} }

@ -1,5 +1,6 @@
use wasm_bindgen_test::*; use wasm_bindgen_test::*;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::HtmlElement; use web_sys::HtmlElement;
#[wasm_bindgen(module = "./tests/wasm/element.js")] #[wasm_bindgen(module = "./tests/wasm/element.js")]
@ -10,6 +11,8 @@ extern {
#[wasm_bindgen_test] #[wasm_bindgen_test]
fn test_html_element() { fn test_html_element() {
let element = new_html(); let element = new_html();
assert!(element.is_instance_of::<HtmlElement>());
assert_eq!(element.title(), "", "Shouldn't have a title"); assert_eq!(element.title(), "", "Shouldn't have a title");
element.set_title("boop"); element.set_title("boop");
assert_eq!(element.title(), "boop", "Should have a title"); assert_eq!(element.title(), "boop", "Should have a title");

@ -244,7 +244,8 @@ impl<'src> WebidlParse<'src, ()> for weedle::InterfaceDefinition<'src> {
js_namespace: None, js_namespace: None,
kind: backend::ast::ImportKind::Type(backend::ast::ImportType { kind: backend::ast::ImportKind::Type(backend::ast::ImportType {
vis: public(), vis: public(),
name: rust_ident(camel_case_ident(self.identifier.0).as_str()), rust_name: rust_ident(camel_case_ident(self.identifier.0).as_str()),
js_name: self.identifier.0.to_string(),
attrs: Vec::new(), attrs: Vec::new(),
doc_comment, doc_comment,
instanceof_shim: format!("__widl_instanceof_{}", self.identifier.0), instanceof_shim: format!("__widl_instanceof_{}", self.identifier.0),

@ -87,3 +87,6 @@ exports.test_rust_optional = function() {
assert.strictEqual(wasm.return_optional_str_none(), undefined); assert.strictEqual(wasm.return_optional_str_none(), undefined);
assert.strictEqual(wasm.return_optional_str_some(), 'world'); assert.strictEqual(wasm.return_optional_str_some(), 'world');
}; };
exports.RenamedInRust = class {};
exports.new_renamed = () => new exports.RenamedInRust;

@ -1,5 +1,6 @@
use wasm_bindgen_test::*; use wasm_bindgen_test::*;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
#[wasm_bindgen(module = "tests/wasm/simple.js")] #[wasm_bindgen(module = "tests/wasm/simple.js")]
extern { extern {
@ -20,6 +21,10 @@ extern {
fn return_string_none() -> Option<String>; fn return_string_none() -> Option<String>;
fn return_string_some() -> Option<String>; fn return_string_some() -> Option<String>;
fn test_rust_optional(); fn test_rust_optional();
#[wasm_bindgen(js_name = RenamedInRust)]
type Renamed;
fn new_renamed() -> Renamed;
} }
#[wasm_bindgen_test] #[wasm_bindgen_test]
@ -178,3 +183,15 @@ pub fn return_optional_str_none() -> Option<String> {
pub fn return_optional_str_some() -> Option<String> { pub fn return_optional_str_some() -> Option<String> {
Some("world".to_string()) Some("world".to_string())
} }
#[wasm_bindgen_test]
fn renaming_imports_and_instanceof() {
let null = JsValue::NULL;
assert!(!null.is_instance_of::<Renamed>());
let arr: JsValue = Array::new().into();
assert!(!arr.is_instance_of::<Renamed>());
let renamed: JsValue = new_renamed().into();
assert!(renamed.is_instance_of::<Renamed>());
}