Description
All the inline usages of Rust in js that I have seen have this #[no_mangle]
attribute added to each function.
E.g. see this example by @killercup , or this one by browserify.
I haven't coded any hybrid js+wasm yet, but to me it seems that having to specify the #[no_mangle]
attribute in order to access wasm module exports via the exports
function at their original name is a bit of a boilerplate.
So I wonder: can't we add a mode to rustc that allows you to omit the attribute?
Specifically, I would propose that there should be a -Z root-no-mangle
(eventually -C) flag for rustc which automatically adds a #[no_mangle]
attribute to any function marked pub
that is in the root of the crate.
This flag could be added by cargo to the leaf crate, at least in cases where it compiles actual inline Rust. I'm not so sure about larger codebases that are not inlined.
A few points:
- If Rust wants to become the "systems language of the web", the
#[no_mangle]
attribute stands in the way. - The Rust mangling system was designed with "Rust being used by Rust" in mind. For this, the system is perfect. It keeps the crates outside of each other's way. The only ones to spoil the fun are those native dependencies. Now "Rust being used by Javascript" is a novel use case and I think it deserves the adjustment in mangling patterns.
- Yes, this might cause weird linker errors, e.g. if you include a native C library and it defines a symbol of the same name, but I think this error will be experienced later on, and also, everything should be pure Rust xD.
- One can also think of scenarios where there is no leaf crate but instead js is using various functions from crates on crates.io directly. I think in these cases, there is no leaf crate and thus the flag won't be passed to any crate.
- Possibly one can extend this feature to work on modules as well, aka levelling up the entire public API of the leaf crate by one level. I don't know much about wasm yet to tell whether this makes any sense.