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

Add support for traits

Open ImUrX opened this issue 2 years ago • 18 comments

Tries to fix #2073 I honestly don't know what I'm doing but I really want this and it sure will be a nice addition to the ecosystem.

  • [x] Correctly implement information struct of traits on the AST
  • [x] Extract information of trait with the proc-macro
  • [ ] Generate typescript code for traits so you don't have a bad time developing
  • [ ] Generate JS glue code correctly
  • [ ] Make test unit

ImUrX avatar Apr 20 '22 22:04 ImUrX

@alexcrichton sorry to bother but is there a way to generate JSDocs and ts signatures with the adapter types. I think I need to read a little more to understand how adapter type works

or maybe im going the wrong way

ImUrX avatar Apr 24 '22 21:04 ImUrX

Kinda related btw microsoft/TypeScript#10570

ImUrX avatar Apr 25 '22 13:04 ImUrX

@alexcrichton sorry to bother but is there a way to generate JSDocs and ts signatures with the adapter types. I think I need to read a little more to understand how adapter type works

or maybe im going the wrong way

yeah this seems more complicated than I thought, is there no way to generate ts documentation without having code on the wit module? It really seems like the only way which means I need to modify the library more than I would like for generating the interface method signatures documentation... @alexcrichton sorry for still bothering you, but what are your thoughts on how I should follow on this?

ImUrX avatar Jul 31 '22 04:07 ImUrX

Sorry this is a feature which off the top of my head I don't really know how it would work or how it would be integrated. I unfortunately don't have the time to help out right now with deisgn as well.

alexcrichton avatar Aug 04 '22 15:08 alexcrichton

yeah understandable, this is currently a mess but it was quite interesting to do. I'm gonna finish the implementation and then clean some code and make all needed tests and see what needs fixing

ImUrX avatar Aug 04 '22 17:08 ImUrX

this would honestly be awesome to have!!

Theres no way to guarantee API stability between TS/JS and rust right now because you cant have build time failure for JS type incompatibility. Its also really difficult to cover with tests because you cant force rust behaviour from TS/JS where the tests will be. Its very difficult to ensure that these codepaths get hit.

I cant believe this isn't a higher priority - how are teams working with this? Do they just deploy and wait for it to break?

stan-irl avatar Dec 11 '22 00:12 stan-irl

@alexcrichton Any changes on this? I could really use the feature on our production system. Currently we have a huge, nasty workaround that I would love to get rid of.

felipellrocha avatar Jan 14 '23 23:01 felipellrocha

this PR would solve the following issues:

  • https://github.com/rustwasm/wasm-bindgen/issues/2714
  • https://github.com/rustwasm/wasm-bindgen/issues/1591
  • https://github.com/rustwasm/wasm-bindgen/issues/1197

stan-irl avatar Jan 15 '23 17:01 stan-irl

Well my attempts to merge in 0.2.81 to this branch were unsuccessful, because I also don't know what I'm doing. Getting failures in the CLI on decode.

blockiosaurus avatar Feb 02 '23 12:02 blockiosaurus

Quick question. Let's say I have a piece of code like the following:

#[wasm_bindgen]
pub struct A {}

#[wasm_bindgen]
impl A {
    pub fn a(&self) {
        // Do something
    }
}

pub trait Trait {
    fn b(&self);
}

impl Trait for A {
    fn b(&self) {
        // Do something else
    }
}

As far as I understand, this will only export a class A and its method a() in JavaScript, but I'd also like to call A::b() – is there a way to do this as of today?

davidedellagiustina avatar Mar 22 '23 15:03 davidedellagiustina

provide a wrapper method for calling it :P

ImUrX avatar Mar 22 '23 16:03 ImUrX

@ImUrX ok, so that's the only solution for now. Thank you!

davidedellagiustina avatar Mar 22 '23 18:03 davidedellagiustina

I would be happy to review the Rust and JS part, but the TS part needs a different maintainer that has the time to do the design work and eventually review it.

daxpedda avatar May 11 '23 16:05 daxpedda

tbh the typescript part is easy, i just havent had the time and will to finish the PR, aside from the rust part of generating the typescript code being a lot of pain

ImUrX avatar May 11 '23 17:05 ImUrX

Hi, I'm interested in this feature, would you be ok if I took a stab at it using your PR as a base? I hope I'll be able to fix the last couple things.

vdagonneau avatar Nov 06 '23 13:11 vdagonneau

yeah, be free

ImUrX avatar Nov 06 '23 19:11 ImUrX

Hey, I'm also interested in this feature. Do you, @vdagonneau and everyone else, wanna team up and work on this PR?

pr1metine avatar Jan 31 '24 20:01 pr1metine