-
Notifications
You must be signed in to change notification settings - Fork 291
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
Add prefix-symbols
feature
#1315
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1315 +/- ##
=======================================
Coverage 81.58% 81.58%
=======================================
Files 306 306
Lines 25271 25271
=======================================
Hits 20617 20617
Misses 4654 4654 ☔ View full report in Codecov by Sentry. |
@xdoardo Thanks for the PR!
It is intentional that all Wasm C-API symbols do not start with Unforunately CI is not yet happy and it seems that both new features for the C-API are conflicting each other which itself conflicts with Rust API design guidelines that crate features must be additive. This change has another downside that it is not reflected in the Wasmi C headers. How could we best resolve this? 🤔 Does this mean that symbols like |
Hello again @Robbepop , thanks for the response. Let me give a bit of context so to explain better why and how I came to this PR. In this PR I add support for multiple backends (or runtimes) to Wasmer: among others, we have support for V8, WAMR and wasmi. All of them are embedded using their implementation of the Of course, using the respective pristine builds, the three libraries mentioned above all export the The other nice side of this endeavour is that with
and from the library side I can simply import To conclude: #1312 had pretty much the same meaning, at least for my use case. However, without the shims created by The next question, then, might be: why not use
If I had to choose, I think that if anyone needs to use the
Yes, only with the -- This comment has quite a bit of technical details which I tried to report briefly but in a meaningful manner. Of course, if something is not clear or, even better, if I missed some obvious and easier way to do what I'm trying to do without jumping through all these hoops, I'm happy to provide more context. |
Okay, so this means before we merge this PR we have to revert the other one that enables Rust name mangling conditionally. Both features conflict with each other and if this feature is superior then we only want this solution. In order to reflect this feature in the C-API headers we probably should also add this feature there so that users can add a prefix to the C headers conditionally. I looked up how this could be done and it is kinda a lot of work but this is roughly it: #ifndef WASMI_NAME_PREFIX
#define WASMI_NAME_PREFIX // by default: no name prefix
#endif
#define CONCAT(a, b) a##b
#define EXPORT(name) CONCAT(WASMI_NAME_PREFIX, name)
void EXPORT(wasm_store_delete)(void);
// etc.. C user code: #define WASMI_NAME_PREFIX wasmi_
#include "wasm.h"
void foo(void) {
wasmi_wasm_store_delete();
} Though, I am very uncertain if I really want to change the C-API headers which are a given by the Wasm spec standard. This probably is a really bad idea. |
I agree. I didn't remove the previous feature in this PR because I wanted to come to a consensus before doing it: my previous PR was, in hindsight, not a good solution to the problem at hand. Sorry for that.
I agree. My thought here is that the asymmetry between the symbols exported in the library and the symbols declared in the C-API header can make a lot of sense or no sense at all depending on the use case. In my case it does, as I can simply copy the code for another C-API implementer, change The choice, here, is between changing a standard API or expect users to know what they're doing when enabling this feature (the third option being removing both symbols-related features). We should also take into account that the current |
Good point. I think it would be fine if we revert your other PR and add docs that describe how to use this new |
Uhm, I'm not sure why |
You can view the logs here:
The Would you be so kind and separate out the revert of the other PR into another PR? This would make this PR simpler to review. |
59f37be
to
4728ac8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix the clippy
CI job here: https://github.com/wasmi-labs/wasmi/actions/runs/12049971351/job/33597861395?pr=1315#step:5:18
Then we should be fine with merging this.
Thanks a lot!
crates/c_api/macro/lib.rs
Outdated
#[proc_macro_attribute] | ||
pub fn prefix_symbol( | ||
_attributes: proc_macro::TokenStream, | ||
input: proc_macro::TokenStream, | ||
) -> proc_macro::TokenStream { | ||
let mut stream = TokenStream::from(input.clone()).into_iter(); | ||
|
||
let mut fn_name = None; | ||
|
||
while let Some(token) = stream.next() { | ||
match token { | ||
TokenTree::Ident(ref ident) if ident.to_string() == "fn" => { | ||
if let Some(TokenTree::Ident(ident_name)) = stream.next() { | ||
fn_name = Some(ident_name.to_string()); | ||
break; | ||
} | ||
} | ||
_ => continue, | ||
} | ||
} | ||
|
||
if fn_name.is_none() { | ||
panic!("expected a valid Rust function definition, but it does not appear in: {input:?}"); | ||
} | ||
|
||
let prefixed_fn_name = format!("wasmi_{}", fn_name.unwrap()); | ||
|
||
let mut attr: proc_macro::TokenStream = quote! { | ||
#[cfg_attr(feature = "prefix-symbols", export_name = #prefixed_fn_name)] | ||
} | ||
.into(); | ||
attr.extend(input); | ||
|
||
attr | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! I think this is fine for merging even though it does not really check for function signatures. What I had in my mind is to perform shallow parsing similar to syn
but overly specific to our use case as to not have to use syn
as dependency. Shouldn't be too much work but I am willing to implement this as a follow-up PR so we can get this PR merged as is.
@xdoardo Could you please remove this massive |
Yep, sorry, my bad! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for all the work you've put into this!
LGTM
Hello again!
This small patch adds a
prefix-symbols
feature that adds awasmi_
prefix to all the public symbols that did not have it before, keeping the symbols not mangled. This way, users (that need to usewasmi
's c_api implementation with other backends) need not worry aboutOption<Box<_>>
es but can simply usebindgen
to take care of it.I think that this method makes more sense than the other one we ended up merging.
Again, thank you in advance for taking the time to review the PR.