Wasm-bindgen: Support WebIDL namespaces

Created on 8 Jun 2018  Â·  15Comments  Â·  Source: rustwasm/wasm-bindgen

namespace whatever {
    void double(long x);
}

I think we want to emit a wrapper module around the emitted bindings for these, eg the equivalent of:

pub mod whatever {
    #[wasm_bindgen]
    extern {
        #[wasm_bindgen(js_namespace = whatever)]
        pub fn double(x: i23) -> i32;
}

We will need to add support for this at the backend::ast level.

webidl help wanted

Most helpful comment

Implemented as modules in #678 .

All 15 comments

I'm looking at this and I don't think the backend::Program supports modules yet. Is this correct, and how would you modify the data structures to incorporate modules? Would you need the ability to nest modules (I would say yes, since you can have namespaces within namespaces)?

The webidl grammar does not allow nested namespaces. Inside a namespace only readonly attributes and regular operations are allowed.

I had a look at this issue myself recently. Mostly just looking and trying to understand the existing code base. Here's what I got so far: I added support for parsing NonpartialNamespaces and added an AST node to store the namespace name and operations defined in the namespace. After that I added an ToToken impl for the namespace node. Now I'm stuck on how to get the codegen to render the function definitions inside the module. It is already possible to define the module correctly, but I don't understand enough of the TokenStream stuff yet to actually insert the functions into the module.

I also had to introduce additional WebidlParse impls for the Operation nodes so I can add references of the functions to their respective namespace. The existing implementations simply add them to the program and the only connection to their class/interface seems to be the class name as a string.

I had a look too but I didn't get as far as you. Is your code online somewhere?

Everything I have so far is here: https://github.com/migerh/wasm-bindgen/tree/wip-namespace-and-console

It'll build, but the console test will fail of course. The console module is there, but there are no functions in it.

thankyou :)

I'm glad you posted that. I'd independently duplicated a lot of your code :P. I'm not sure how to generate tokens for the module, but I'm guessing it will be like a struct/interface, in that there are an unknown collection of interior objects (in this case functions) inside an enclosing object (in this case mod). I'll have a look to see if I can finish your work if you like, or i'll leave it to you if you prefer? Or we can both work on it and see who can figure it out :P

@derekdreery Maybe we can crack this together :) I can now generate something that at least somewhat resembles the suggestion above. Two issues are open atm:

  • In the bindings.rs the js_namespace is enclosed with quotes. wasm-bindgen can't parse that. For now I'm patching this manually by removing the quotes from the generated bindings.rs.
  • The test can't find the functions inside the module although they're there.

ooh awesome. I've made some progress too, I'm not on my computer atm but
will post my updates when I am.

On Thu, Jul 19, 2018 at 9:38 PM, Michael Gerhaeuser <
[email protected]> wrote:

@derekdreery https://github.com/derekdreery Maybe we can crack this
together :) I can now generate something that at least somewhat resembles
the suggestion above. Two issues are open atm:

  • In the bindings.rs the js_namespace is enclosed with quotes.
    wasm-bindgen can't parse that. For now I'm patching this manually by
    removing the quotes from the generated bindings.rs.
  • The test can't find the functions inside the module although they're
    there.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/rustwasm/wasm-bindgen/issues/253#issuecomment-406406730,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABU-XkF_XmcMft3a5qvXd4bmTmfLXUMTks5uIO47gaJpZM4Ug30b
.

Unimportant observation: I wonder if we should use ImportModule rather than ImportNamespace, since we are generating modules from our ast (this seems to be the pattern the other variants take).

If you want to look at my WIP it's at derekdreery/wasm-bindgen#wip-namespace-and-console.

Something I find helpful is to run rustfmt on the output of the build process, at target/wasm32-unknown-unknown/debug/build/web-sys-xxx/out/bindings.rs (where xxx is the metadata hash for your build). You can then view it as rust code and see if it's correct.

I think we can use an alternative codegen tactic for namespaces, the equivalent to:

// namespace apollo {
//     bool thirteen(x: unsigned long);
// }
#[wasm_bindgen]
extern {
    type apollo;

    #[wasm_bindgen(static_method_of = apollo)]
    fn thirteen(x: u32) -> bool;
}

Details here: https://github.com/rustwasm/wasm-bindgen/pull/678#issuecomment-412145357

Implemented as modules in #678 .

We still need support for namespace attributes, although that shouldn't be much work at this point. I'm going to leave this open for now.

@ohanar perhaps we could open a separate issue for that with some updated instructions?

Filed https://github.com/rustwasm/wasm-bindgen/issues/746 for attributes within namespaces. Closing this issue!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MarcAntoine-Arnaud picture MarcAntoine-Arnaud  Â·  3Comments

bantic picture bantic  Â·  4Comments

arilotter picture arilotter  Â·  3Comments

alexcrichton picture alexcrichton  Â·  3Comments

fitzgen picture fitzgen  Â·  4Comments