-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Tracking issue for #[wasm_import_module] #52090
Comments
The WebAssembly target doesn't actually have any concept of a single-level import, so in that sense this doesn't necessarily make sense in wasm: extern {
fn foo();
} We're currently relying on LLVM's defacto behavior to set the import module as |
I'd like to propose that this feature be put up for stabilization but I'm not currently a member of the compiler team to do so, would someone from @rust-lang/compiler like to help me champion this issue and FCP it for stabilization? Also cc @rust-lang/wg-wasm |
Is it well-defined for the attribute to have a space as in the tracking issue |
The WebAssembly specification only has the requirement of "required to be valid UTF-8", and beyond that there is no restriction on what the import module can be. We wouldn't want to restrict it to only Rust identifiers because it's frequently an ES module path like |
rustc: Update tracking issue for wasm_import_module It's now rust-lang#52090
@rfcbot merge Seems fairly uncontroversial change to me. @alexcrichton summarized this better than I could above, so read that. AFAIK |
Team member @nagisa has proposed to merge this. The next step is review by the rest of the tagged teams:
No concerns currently listed. Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
This feature will documentation in the book/reference/etc. before being finally stabilised. Please add a TODO to that effect to the text of the issue. |
Also a naming bikeshed: couldn’t we reuse |
@alexcrichton, I assume the mentions of @Mark-Simulacrum, @nagisa, as @alexcrichton mentioned above, this will be used to import from ES module paths. Once the ES module integration is standardized, this will in fact be used a lot: whenever a Wasm module is loaded as an ES module instead of via the JS API ( @alexcrichton my only concern is that this is somewhat verbose. E.g. compare this JS import import {
some_other_name as foo,
bar
} from "somewhere else"; to this Rust one: #[wasm_import_section = "somewhere else"]
extern {
#[link_name = "some_other_name"]
fn foo();
fn bar();
} I suppose this is something where sugar could be added later if it turns out to really be too verbose in practice, though. |
I am not a member of the Rust Compiler team (I am a member of the Rust WebAssembly team), however one concern I have is with using Consider this Rust code: // path/to/some/crate/foo.rs
#[wasm_import_module = "./bar.js"]
extern {
...
} In this case we have a file My expectation is that this will work. However, it doesn't work, because after the crate is compiled into I'm not sure how to fix this issue. Perhaps it can be fixed entirely by third-party tools like wasm-bindgen and wasm-pack (can it?). But I suspect it might require some changes to the |
@nagisa it's true yeah, going off #[link(wasm_import_module = "...")]
extern {
// ...
} @tschneidereit ah thanks for pointing out my mistake! I definitely meant @Pauan my hope with this attribute (and |
Sure, I agree. My concern is this:
I think that situation will be difficult for wasm-bindgen / wasm-pack to handle, because the Given that, it may be necessary for So as long as we have some way of fixing the above problem without requiring breaking changes to But I don't see any obvious ways to do that, do you have any ideas? |
@Pauan yes I think it'll be difficult to handle but I don't think it'll be a problem in practice. It sounds similar to "what if a crate uses I see |
@alexcrichton So, what you're saying is that users generally won't use |
Indeed yes, that may likely be the case |
@alexcrichton Alright, in that case I don't have any concerns. |
Don't we have |
@eddyb unfortunately no, this is configuring a two-level namespace which we don't have a configuration for right now for other platforms. Something like |
@alexcrichton But isn't the first of the two level-namespace equivalent to the library half of the pair of "library and symbol" from, e.g. Windows? |
@eddyb maybe? In that sense wasm has three namespaces because it'll still have archives of compiled C objects which need to be identified with |
@alexcrichton Ah, this makes more sense, sorry about the confusion! |
Currently no in the sense that wasm doesn't dictate any meaning with the first level of the namespaced import, it's up to bundlers and module instantiators to interpret that |
This commit stabilizes the `#[wasm_import_module]` attribute. Tracked by rust-lang#52090 this issue can be attached to foreign modules (`extern { ... }` blocks) and is used to configured the module name that the imports are listed with. The WebAssembly specification indicates two utf-8 names are associated with all imported items, one for the module the item comes from and one for the item itself. The item itself is configurable in Rust via its identifier or `#[link_name = "..."]`, but the module name was previously not configurable and defaulted to `"env"`. This commit ensures that this is also configurable. Closes rust-lang#52090
I've submitted #52445 which folds this into the |
This commit stabilizes the `#[wasm_import_module]` attribute as `#[link(wasm_import_module = "...")]`. Tracked by rust-lang#52090 this new directive in the `#[link]` attribute is used to configured the module name that the imports are listed with. The WebAssembly specification indicates two utf-8 names are associated with all imported items, one for the module the item comes from and one for the item itself. The item itself is configurable in Rust via its identifier or `#[link_name = "..."]`, but the module name was previously not configurable and defaulted to `"env"`. This commit ensures that this is also configurable. Closes rust-lang#52090
rustc: Stabilize #[wasm_import_module] as #[link(...)] This commit stabilizes the `#[wasm_import_module]` attribute as `#[link(wasm_import_module = "...")]`. Tracked by #52090 this new directive in the `#[link]` attribute is used to configured the module name that the imports are listed with. The WebAssembly specification indicates two utf-8 names are associated with all imported items, one for the module the item comes from and one for the item itself. The item itself is configurable in Rust via its identifier or `#[link_name = "..."]`, but the module name was previously not configurable and defaulted to `"env"`. This commit ensures that this is also configurable. Closes #52090
This is a tracking issue for the
#[wasm_import_module]
attribute and thewasm_import_module
feature. This attribute is applied toextern { ... }
blocks like so:The WebAssembly specification requires that all imported values from the host environment have a two-level namespace:
Additionally only globals (like static variables), functions, memories, and tables can be imported. The
extern { ... }
language block is used to import globals and functions, memories and tables cannot currently be manually imported.Each field of the wasm import needs to be configurable by Rust as both fields have semantic meaning. Typically the first field of the import is the "module" interpreted as an ES module, and the second field is what to import from that module.
Currently LLVM and LLD will default imports to the "env" module. This means that if you compile this code:
it will generate a wasm file that imports the function "foo" from the module "env". By using
#[wasm_import_module]
we can customize what happens here:This attribute must be of the form
#[wasm_import_module = "string"]
, no other forms are accepted. It can only be attached to anextern
block. All items in the block are considered to come from the same module. Through the usage of#[link_name]
we can then configure both fields of WebAssembly imports arbitrarily:The
#[wasm_import_module]
is accepted on non-wasm platforms but has no effect, it is simply an ignored attribute.Helpful links:
Open questions and TODO
#[link(wasm_import_module = "...")]
?cc rustwasm/team#82
The text was updated successfully, but these errors were encountered: