js_namespace and js_name don't propagate to methods
Describe the Bug
js_namespace and js_name defined on an imported class don't propagate to its constructor.
Steps to Reproduce
#[wasm_bindgen]
extern {
#[wasm_bindgen(js_name = CustomName)]
type Type;
#[wasm_bindgen(constructor)]
fn new() -> Type;
}
Expected Behavior
References to new CustomName are generated on the JS side.
Actual Behavior
References to new Type are generated on the JS side.
Additional Context
It is possible to work around this by duplicating js_name / js_namespace on each constructor, but this feels surprising and inconsistent with behaviour of instanceof and other operators that take type's name instead.
Thanks for the report! This is currently done because of the local behavior of macros (they only see their one item as input) but if you copy js_name and js_namespace to each item it should work. Would be great to not have to do that though!
but if you copy js_name and js_namespace to each item it should work
Yeah, that's what I mentioned as a workaround. At the very least, it would be nice to error out on non-applicable attributes, whereas currently they just silently fail to produce the right output.
@alexcrichton I believe the bug label still applies though? (at least in terms of surprising / silent failure)
Er, sure? It won't really affect whether this gets fixed any faster...
Oh, of course, I just think it's useful for visibility if someone is looking through bugs by filtering labels :) (maybe it's just my weird habit though)
Probably part of the same issue: this seems to apply to exports as well. E.g.:
#[wasm_bindgen(js_name = MyStruct2)]
pub struct MyStruct;
#[wasm_bindgen]
impl MyStruct {
pub fn do_smth(&self) {}
}
generates:
export class MyStruct {
free() {
const ptr = this.ptr;
this.ptr = 0;
wasm.__wbg_mystruct_free(ptr);
}
/**
*/
do_smth() {
wasm.mystruct_do_smth(this.ptr);
}
}
/**
*/
export class MyStruct2 {
free() {
const ptr = this.ptr;
this.ptr = 0;
wasm.__wbg_mystruct2_free(ptr);
}
}
Oh... looks like for exports that workaround doesn't work:
#[wasm_bindgen(js_name = MyStruct2)]
pub struct MyStruct;
#[wasm_bindgen(js_name = MyStruct2)]
impl MyStruct {
pub fn do_smth(&self) {}
}
still generates the same code.
@alexcrichton Any ideas how to rename struct completely?
Hm sorry, no, I'd have to relearn how these are all applied. This is probably just a bug that should be fixed though.
For now I worked around this by renaming struct in the Rust source and using #[allow(non_camel_case_types)] but yeah seems like a bug.