mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-16 02:00:51 +00:00
Use the JS name of an imported type for instanceof
checks
This commit is contained in:
parent
9104bf87e9
commit
998d37a353
@ -125,6 +125,7 @@ pub struct ImportStatic {
|
||||
pub struct ImportType {
|
||||
pub vis: syn::Visibility,
|
||||
pub rust_name: Ident,
|
||||
pub js_name: String,
|
||||
pub attrs: Vec<syn::Attribute>,
|
||||
pub doc_comment: Option<String>,
|
||||
pub instanceof_shim: String,
|
||||
@ -411,7 +412,7 @@ impl ImportStatic {
|
||||
impl ImportType {
|
||||
fn shared(&self) -> shared::ImportType {
|
||||
shared::ImportType {
|
||||
name: self.rust_name.to_string(),
|
||||
name: self.js_name.clone(),
|
||||
instanceof_shim: self.instanceof_shim.clone(),
|
||||
}
|
||||
}
|
||||
|
@ -542,6 +542,9 @@ impl ConvertToAst<BindgenAttrs> for syn::ForeignItemType {
|
||||
type Target = ast::ImportKind;
|
||||
|
||||
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));
|
||||
Ok(ast::ImportKind::Type(ast::ImportType {
|
||||
vis: self.vis,
|
||||
@ -549,6 +552,7 @@ impl ConvertToAst<BindgenAttrs> for syn::ForeignItemType {
|
||||
doc_comment: None,
|
||||
instanceof_shim: shim,
|
||||
rust_name: self.ident,
|
||||
js_name,
|
||||
extends: attrs.extends().cloned().collect(),
|
||||
}))
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
use wasm_bindgen_test::*;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::JsCast;
|
||||
use web_sys::HtmlElement;
|
||||
|
||||
#[wasm_bindgen(module = "./tests/wasm/element.js")]
|
||||
@ -10,6 +11,8 @@ extern {
|
||||
#[wasm_bindgen_test]
|
||||
fn test_html_element() {
|
||||
let element = new_html();
|
||||
assert!(element.is_instance_of::<HtmlElement>());
|
||||
|
||||
assert_eq!(element.title(), "", "Shouldn't have a title");
|
||||
element.set_title("boop");
|
||||
assert_eq!(element.title(), "boop", "Should have a title");
|
||||
|
@ -244,7 +244,8 @@ impl<'src> WebidlParse<'src, ()> for weedle::InterfaceDefinition<'src> {
|
||||
js_namespace: None,
|
||||
kind: backend::ast::ImportKind::Type(backend::ast::ImportType {
|
||||
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(),
|
||||
doc_comment,
|
||||
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_some(), 'world');
|
||||
};
|
||||
|
||||
exports.RenamedInRust = class {};
|
||||
exports.new_renamed = () => new exports.RenamedInRust;
|
||||
|
@ -1,5 +1,6 @@
|
||||
use wasm_bindgen_test::*;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::JsCast;
|
||||
|
||||
#[wasm_bindgen(module = "tests/wasm/simple.js")]
|
||||
extern {
|
||||
@ -20,6 +21,10 @@ extern {
|
||||
fn return_string_none() -> Option<String>;
|
||||
fn return_string_some() -> Option<String>;
|
||||
fn test_rust_optional();
|
||||
|
||||
#[wasm_bindgen(js_name = RenamedInRust)]
|
||||
type Renamed;
|
||||
fn new_renamed() -> Renamed;
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
@ -178,3 +183,15 @@ pub fn return_optional_str_none() -> Option<String> {
|
||||
pub fn return_optional_str_some() -> Option<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>());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user