-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nested namespaces #951
Comments
I started digging into the code to see what it would take to actually do the first fix I mentioned, and I got pretty confused by several things. These points are not all 100% on topic, but I don't know where else to mention them (I don't feel like they're worth creating separate issues for):
I'm wondering if it's not better to just deprecate both |
Thanks for the report! In the short term I think it's fine to allow strings in As for the design of these attributes, they were all added in a pretty ad-hoc fashion over time as need for them arose. I hope you're not too offended by the current state! For documentation, improvements are always welcome! Sending a PR is all that's necessary :) Long-term it seems like a good idea to consolidate these attributes indeed! For now though we'd prefer to avoid breaking changes to |
No I'm certainly not "offended" by the current state of the code. I'm just trying to understand how the current code works, and trying to get an idea of how it's supposed to work, and from there I'm trying to find a reasonable proposal for how it should be changed to make it better (in this case to support nested namespaces). But I hope you're also not offended when I say that it's been a pretty frustrating experience. Trying to understand how the current code works or is supposed to work took quite a bit of time because, as you mention yourself, the current code is pretty "ad hoc" and not super consistent. And trying to find out how to improve it has been even more frustrating because, on top of there not being a very consistent base to build on, backwards compatibility gets in the way of things as well. As an example: even after your previous post I still don't know how to change See how that's frustrating? Every step of the way I'm second guessing myself, asking myself if a bit of code is "wrong enough" to be considered a bug to be fixed or whether backwards compatibility trumps doing the right thing. If wasm-bindgen had a very clear-cut and consistent way of doing everything, then that would make this more easy to decide, but with everything being mostly "ad-hoc" solutions that are mostly consistent (consistent enough for |
I like this quick solution, or using "::" to align with rust/c++ style namespace. I really tried to use dots for nest namespace before reading wasm-bindgen source and this issue :) |
@migi sounds like the last remaining question is what to do about the attach-to-a-rust-struct vs generating a free function? If that's the case I'd recommmend just ignoring the attach-to-a-rust-struct logic whenever a string is used and then we can file an issue to clean it up when the next major version is made of wasm-bindge. Also keep in mind that this is an open source project! That means that it can get pretty old as well as gain a lot of contributors. We don't all have the time to make sure that everything is always 100% orthogonal to everything else and completely rationalizes with all historical decisions. We do our best and issues/PRs are the best way to keep making progress! |
I'm working on creating bindings for WebExtensions (see rustwasm/gloo#21) and nested namespaces would be really useful, e.g. for |
@OddCoincidence In the meantime you can use this workaround: #[wasm_bindgen]
extern "C" {
type Alarms;
// Various methods...
}
#[wasm_bindgen]
extern "C" {
type Browser;
#[wasm_bindgen(method, getter)]
fn alarms(this: &Browser) -> Alarms;
}
#[wasm_bindgen]
extern "C" {
static browser: Browser;
} And now it can be used like this: (For older versions of |
I'll do that for now then, thanks @Pauan! |
Hello. Does
still make sense? If so, can I make a PR for this? |
* Enable nested namespace (rustwasm#951) * Specify the namespace as array (rustwasm#951) * added an example to the document Co-authored-by: Alex Crichton <alex@alexcrichton.com>
Currently,
js_namespace
only accepts a Rust identifier (asyn::Ident
). Identifiers in JavaScript allow many more characters than Rust does, but that's not my point. It also means that you can't specify that a function or class (for constructors) belongs to a nested namespace (i.e.foo.bar.baz
).WebIDL namespaces have been discussed in issue #253 and
js_namespace
was implemented in #678, but the focus in these threads was on WebIDL, and "the webidl grammar does not allow nested namespaces". But do we only care about WebIDL or do we want to makewasm_bindgen
usable for generating bindings to general JavaScript code? I think the latter, right?Well in JavaScript packages, namespaces are not incredibly common and nested namespaces are definitely quite rare, but they do happen. JavaScript doesn't really have namespaces as a built-in concept, but people still make namespaces out of regular objects.
An example of a JavaScript project using nested namespaces (I admit I had to search for it, it's definitely not as common as I thought) is the game engine Phaser. When you want to crate a physics entity in Phaser, you call
new Phaser.Physics.Box2D.Body(...)
. As far as I can tell, it's impossible to write an idiomaticwasm_bindgen
binding for thatnew Phaser.Physics.Box2D.Body(...)
call.I see 2 possible ways to fix that:
We could allow strings in the
js_namespace = ...
attribute, and then parse that string to see if it contains dots. Then we could treat those dot-separated identifiers as nested namespaces, and make both constructors and static methods operate on the right JS object.We could allow some form of modules/namespaces inside
wasm_bindgen
blocks. These would represent both Rust modules (in the generated Rust bindings) and JS namespaces (in the generated wasm/JS glue code). If we do this, I think it may be slightly more ergonomic to writewasm_bindgen
glue code for JS code with (potentially nested) namespaces, but I also think this would be a lot of work.What do you think? Given how uncommon nested namespaces are, and given that namespaces are not a JavaScript language construct but rather a way in which some people use JS objects, should we even fix this? If so, which of these 2 fixes seems like the way to go? Or should we do something else entirely? I think I could implement the first fix myself, I don't think I can do the 2nd.
The text was updated successfully, but these errors were encountered: