wasm-bindgen icon indicating copy to clipboard operation
wasm-bindgen copied to clipboard

js_namespace and js_name don't propagate to methods

Open RReverser opened this issue 6 years ago • 8 comments

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.

RReverser avatar Sep 10 '19 12:09 RReverser

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!

alexcrichton avatar Sep 10 '19 15:09 alexcrichton

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)

RReverser avatar Sep 10 '19 17:09 RReverser

Er, sure? It won't really affect whether this gets fixed any faster...

alexcrichton avatar Sep 11 '19 13:09 alexcrichton

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)

RReverser avatar Sep 11 '19 14:09 RReverser

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);
    }
}

RReverser avatar Jan 21 '21 15:01 RReverser

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?

RReverser avatar Jan 21 '21 15:01 RReverser

Hm sorry, no, I'd have to relearn how these are all applied. This is probably just a bug that should be fixed though.

alexcrichton avatar Jan 22 '21 15:01 alexcrichton

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.

RReverser avatar Jan 22 '21 15:01 RReverser