Skip to content

Commit

Permalink
Add prefix-symbols feature (#1315)
Browse files Browse the repository at this point in the history
* feat: Add the `prefix-symbols` feature

* fix: Remove leftover lifetime specifiers in `export_name` attributes

* fix: Cleaner `_prefixed` function names in macros

* feat: Use procedural macro to generate prefixed symbols

* fix: Add missing uses of `prefix_symbol` proc macro
  • Loading branch information
xdoardo authored Nov 27, 2024
1 parent 8cc0258 commit 4abde9c
Show file tree
Hide file tree
Showing 27 changed files with 236 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/c_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ doctest = false
[features]
default = ["std"]
std = []
prefix-symbols = []
44 changes: 44 additions & 0 deletions crates/c_api/macro/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub fn declare_own(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
(quote! {
#[doc = #docs]
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn #delete(_: ::alloc::boxed::Box<#ty>) {}
})
.into()
Expand All @@ -49,6 +50,7 @@ pub fn declare_ty(input: proc_macro::TokenStream) -> proc_macro::TokenStream {

#[doc = #docs]
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn #copy(src: &#ty) -> ::alloc::boxed::Box<#ty> {
::alloc::boxed::Box::new(src.clone())
}
Expand Down Expand Up @@ -97,6 +99,7 @@ pub fn declare_ref(input: proc_macro::TokenStream) -> proc_macro::TokenStream {

#[doc = #same_docs]
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn #same(_a: &#ty, _b: &#ty) -> ::core::primitive::bool {
#[cfg(feature = "std")]
::std::eprintln!("`{}` is not implemented", ::core::stringify!(#same));
Expand All @@ -105,12 +108,14 @@ pub fn declare_ref(input: proc_macro::TokenStream) -> proc_macro::TokenStream {

#[doc = #get_host_info_docs]
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn #get_host_info(a: &#ty) -> *mut ::core::ffi::c_void {
::core::ptr::null_mut()
}

#[doc = #set_host_info_docs]
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn #set_host_info(a: &#ty, info: *mut ::core::ffi::c_void) {
#[cfg(feature = "std")]
::std::eprintln!("`{}` is not implemented", ::core::stringify!(#set_host_info));
Expand All @@ -119,6 +124,7 @@ pub fn declare_ref(input: proc_macro::TokenStream) -> proc_macro::TokenStream {

#[doc = #set_host_info_final_docs]
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn #set_host_info_final(
a: &#ty,
info: *mut ::core::ffi::c_void,
Expand All @@ -131,6 +137,7 @@ pub fn declare_ref(input: proc_macro::TokenStream) -> proc_macro::TokenStream {

#[doc = #as_ref_docs]
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn #as_ref(a: &#ty) -> ::alloc::boxed::Box<crate::wasm_ref_t> {
#[cfg(feature = "std")]
::std::eprintln!("`{}` is not implemented", ::core::stringify!(#as_ref));
Expand All @@ -139,6 +146,7 @@ pub fn declare_ref(input: proc_macro::TokenStream) -> proc_macro::TokenStream {

#[doc = #as_ref_const_docs]
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn #as_ref_const(a: &#ty) -> ::alloc::boxed::Box<crate::wasm_ref_t> {
#[cfg(feature = "std")]
::std::eprintln!("`{}` is not implemented", ::core::stringify!(#as_ref_const));
Expand All @@ -150,3 +158,39 @@ pub fn declare_ref(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
})
.into()
}

#[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 == "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! {
#[export_name = #prefixed_fn_name]
}
.into();
attr.extend(input);

attr
}
1 change: 1 addition & 0 deletions crates/c_api/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ wasmi_c_api_macros::declare_own!(wasm_config_t);
///
/// [`wasm_engine_new_with_config`]: crate::wasm_engine_new_with_config
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_config_new() -> Box<wasm_config_t> {
Box::new(wasm_config_t {
inner: Config::default(),
Expand Down
2 changes: 2 additions & 0 deletions crates/c_api/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ wasmi_c_api_macros::declare_own!(wasm_engine_t);
///
/// Wraps [`wasmi::Engine::default`].
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_engine_new() -> Box<wasm_engine_t> {
Box::new(wasm_engine_t {
inner: Engine::default(),
Expand All @@ -31,6 +32,7 @@ pub extern "C" fn wasm_engine_new() -> Box<wasm_engine_t> {
///
/// Wraps [`wasmi::Engine::new`].
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_engine_new_with_config(config: Box<wasm_config_t>) -> Box<wasm_engine_t> {
Box::new(wasm_engine_t {
inner: Engine::new(&config.inner),
Expand Down
10 changes: 10 additions & 0 deletions crates/c_api/src/extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ wasmi_c_api_macros::declare_ref!(wasm_extern_t);

/// Returns the [`wasm_extern_kind`] of the [`wasm_extern_t`].
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_extern_kind(e: &wasm_extern_t) -> wasm_externkind_t {
match e.which {
Extern::Func(_) => wasm_externkind_t::WASM_EXTERN_FUNC,
Expand All @@ -39,6 +40,7 @@ pub extern "C" fn wasm_extern_kind(e: &wasm_extern_t) -> wasm_externkind_t {
/// It is the caller's responsibility not to alias the [`wasm_extern_t`]
/// with its underlying, internal [`WasmStoreRef`].
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub unsafe extern "C" fn wasm_extern_type(e: &wasm_extern_t) -> Box<wasm_externtype_t> {
Box::new(wasm_externtype_t::from_extern_type(
e.which.ty(e.store.context()),
Expand All @@ -49,6 +51,7 @@ pub unsafe extern "C" fn wasm_extern_type(e: &wasm_extern_t) -> Box<wasm_externt
///
/// Returns `None` if `e` is not a [`wasm_func_t`].
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_extern_as_func(e: &mut wasm_extern_t) -> Option<&mut wasm_func_t> {
wasm_func_t::try_from_mut(e)
}
Expand All @@ -57,6 +60,7 @@ pub extern "C" fn wasm_extern_as_func(e: &mut wasm_extern_t) -> Option<&mut wasm
///
/// Returns `None` if `e` is not a [`wasm_func_t`].
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_extern_as_func_const(e: &wasm_extern_t) -> Option<&wasm_func_t> {
wasm_func_t::try_from(e)
}
Expand All @@ -65,6 +69,7 @@ pub extern "C" fn wasm_extern_as_func_const(e: &wasm_extern_t) -> Option<&wasm_f
///
/// Returns `None` if `e` is not a [`wasm_global_t`].
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_extern_as_global(e: &mut wasm_extern_t) -> Option<&mut wasm_global_t> {
wasm_global_t::try_from_mut(e)
}
Expand All @@ -73,6 +78,7 @@ pub extern "C" fn wasm_extern_as_global(e: &mut wasm_extern_t) -> Option<&mut wa
///
/// Returns `None` if `e` is not a [`wasm_global_t`].
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_extern_as_global_const(e: &wasm_extern_t) -> Option<&wasm_global_t> {
wasm_global_t::try_from(e)
}
Expand All @@ -81,6 +87,7 @@ pub extern "C" fn wasm_extern_as_global_const(e: &wasm_extern_t) -> Option<&wasm
///
/// Returns `None` if `e` is not a [`wasm_table_t`].
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_extern_as_table(e: &mut wasm_extern_t) -> Option<&mut wasm_table_t> {
wasm_table_t::try_from_mut(e)
}
Expand All @@ -89,6 +96,7 @@ pub extern "C" fn wasm_extern_as_table(e: &mut wasm_extern_t) -> Option<&mut was
///
/// Returns `None` if `e` is not a [`wasm_table_t`].
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_extern_as_table_const(e: &wasm_extern_t) -> Option<&wasm_table_t> {
wasm_table_t::try_from(e)
}
Expand All @@ -97,6 +105,7 @@ pub extern "C" fn wasm_extern_as_table_const(e: &wasm_extern_t) -> Option<&wasm_
///
/// Returns `None` if `e` is not a [`wasm_memory_t`].
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_extern_as_memory(e: &mut wasm_extern_t) -> Option<&mut wasm_memory_t> {
wasm_memory_t::try_from_mut(e)
}
Expand All @@ -105,6 +114,7 @@ pub extern "C" fn wasm_extern_as_memory(e: &mut wasm_extern_t) -> Option<&mut wa
///
/// Returns `None` if `e` is not a [`wasm_memory_t`].
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_extern_as_memory_const(e: &wasm_extern_t) -> Option<&wasm_memory_t> {
wasm_memory_t::try_from(e)
}
1 change: 1 addition & 0 deletions crates/c_api/src/foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ wasmi_c_api_macros::declare_ref!(wasm_foreign_t);
///
/// This API is unsupported and will panic upon use.
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_foreign_new(_store: &crate::wasm_store_t) -> Box<wasm_foreign_t> {
unimplemented!("wasm_foreign_new")
}
5 changes: 5 additions & 0 deletions crates/c_api/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ wasmi_c_api_macros::declare_own!(wasm_frame_t);
///
/// This API is unsupported and will panic upon use.
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_frame_func_index(_frame: &wasm_frame_t<'_>) -> u32 {
unimplemented!("wasm_frame_func_index")
}
Expand All @@ -27,6 +28,7 @@ pub extern "C" fn wasm_frame_func_index(_frame: &wasm_frame_t<'_>) -> u32 {
///
/// This API is unsupported and will panic upon use.
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_frame_func_offset(_frame: &wasm_frame_t<'_>) -> usize {
unimplemented!("wasm_frame_func_offset")
}
Expand All @@ -37,6 +39,7 @@ pub extern "C" fn wasm_frame_func_offset(_frame: &wasm_frame_t<'_>) -> usize {
///
/// This API is unsupported and will panic upon use.
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_frame_instance(_arg1: *const wasm_frame_t<'_>) -> *mut wasm_instance_t {
unimplemented!("wasm_frame_instance")
}
Expand All @@ -47,6 +50,7 @@ pub extern "C" fn wasm_frame_instance(_arg1: *const wasm_frame_t<'_>) -> *mut wa
///
/// This API is unsupported and will panic upon use.
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_frame_module_offset(_frame: &wasm_frame_t<'_>) -> usize {
unimplemented!("wasm_frame_module_offset")
}
Expand All @@ -57,6 +61,7 @@ pub extern "C" fn wasm_frame_module_offset(_frame: &wasm_frame_t<'_>) -> usize {
///
/// This API is unsupported and will panic upon use.
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_frame_copy<'a>(_frame: &wasm_frame_t<'a>) -> Box<wasm_frame_t<'a>> {
unimplemented!("wasm_frame_copy")
}
8 changes: 8 additions & 0 deletions crates/c_api/src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ unsafe fn create_function(
/// It is the caller's responsibility not to alias the [`wasm_functype_t`]
/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub unsafe extern "C" fn wasm_func_new(
store: &mut wasm_store_t,
ty: &wasm_functype_t,
Expand All @@ -141,6 +142,7 @@ pub unsafe extern "C" fn wasm_func_new(
/// It is the caller's responsibility not to alias the [`wasm_functype_t`]
/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub unsafe extern "C" fn wasm_func_new_with_env(
store: &mut wasm_store_t,
ty: &wasm_functype_t,
Expand Down Expand Up @@ -184,6 +186,7 @@ fn prepare_params_and_results(
/// It is the caller's responsibility not to alias the [`wasm_func_t`]
/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub unsafe extern "C" fn wasm_func_call(
func: &mut wasm_func_t,
params: *const wasm_val_vec_t,
Expand Down Expand Up @@ -248,6 +251,7 @@ fn error_from_panic(panic: Box<dyn Any + Send>) -> Error {
/// It is the caller's responsibility not to alias the [`wasm_func_t`]
/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub unsafe extern "C" fn wasm_func_type(f: &wasm_func_t) -> Box<wasm_functype_t> {
Box::new(wasm_functype_t::new(f.func().ty(f.inner.store.context())))
}
Expand All @@ -263,6 +267,7 @@ pub unsafe extern "C" fn wasm_func_type(f: &wasm_func_t) -> Box<wasm_functype_t>
///
/// [`FuncType::params`]: wasmi::FuncType::params
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub unsafe extern "C" fn wasm_func_param_arity(f: &wasm_func_t) -> usize {
f.func().ty(f.inner.store.context()).params().len()
}
Expand All @@ -278,18 +283,21 @@ pub unsafe extern "C" fn wasm_func_param_arity(f: &wasm_func_t) -> usize {
///
/// [`FuncType::results`]: wasmi::FuncType::results
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub unsafe extern "C" fn wasm_func_result_arity(f: &wasm_func_t) -> usize {
f.func().ty(f.inner.store.context()).results().len()
}

/// Returns the [`wasm_func_t`] as mutable reference to [`wasm_extern_t`].
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_func_as_extern(f: &mut wasm_func_t) -> &mut wasm_extern_t {
&mut f.inner
}

/// Returns the [`wasm_func_t`] as shared reference to [`wasm_extern_t`].
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_func_as_extern_const(f: &wasm_func_t) -> &wasm_extern_t {
&f.inner
}
6 changes: 6 additions & 0 deletions crates/c_api/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl wasm_global_t {
/// It is the caller's responsibility not to alias the [`wasm_global_t`]
/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub unsafe extern "C" fn wasm_global_new(
store: &mut wasm_store_t,
ty: &wasm_globaltype_t,
Expand All @@ -70,12 +71,14 @@ pub unsafe extern "C" fn wasm_global_new(

/// Returns the [`wasm_global_t`] as mutable reference to [`wasm_extern_t`].
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_global_as_extern(g: &mut wasm_global_t) -> &mut wasm_extern_t {
&mut g.inner
}

/// Returns the [`wasm_global_t`] as shared reference to [`wasm_extern_t`].
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_global_as_extern_const(g: &wasm_global_t) -> &wasm_extern_t {
&g.inner
}
Expand All @@ -89,6 +92,7 @@ pub extern "C" fn wasm_global_as_extern_const(g: &wasm_global_t) -> &wasm_extern
/// It is the caller's responsibility not to alias the [`wasm_global_t`]
/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub unsafe extern "C" fn wasm_global_type(g: &wasm_global_t) -> Box<wasm_globaltype_t> {
let globaltype = g.global().ty(g.inner.store.context());
Box::new(wasm_globaltype_t::new(globaltype))
Expand All @@ -103,6 +107,7 @@ pub unsafe extern "C" fn wasm_global_type(g: &wasm_global_t) -> Box<wasm_globalt
/// It is the caller's responsibility not to alias the [`wasm_global_t`]
/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub unsafe extern "C" fn wasm_global_get(g: &mut wasm_global_t, out: &mut MaybeUninit<wasm_val_t>) {
let global = g.global();
crate::initialize(
Expand All @@ -121,6 +126,7 @@ pub unsafe extern "C" fn wasm_global_get(g: &mut wasm_global_t, out: &mut MaybeU
/// - It is the caller's responsibility not to alias the [`wasm_global_t`]
/// with its underlying, internal [`WasmStoreRef`](crate::WasmStoreRef).
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub unsafe extern "C" fn wasm_global_set(g: &mut wasm_global_t, val: &wasm_val_t) {
let global = g.global();
drop(global.set(g.inner.store.context_mut(), val.to_val()));
Expand Down
2 changes: 2 additions & 0 deletions crates/c_api/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl wasm_instance_t {
///
/// [Wasm core specification]: https://webassembly.github.io/spec/core/exec/modules.html#exec-instantiation
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub unsafe extern "C" fn wasm_instance_new(
store: &mut wasm_store_t,
wasm_module: &wasm_module_t,
Expand Down Expand Up @@ -80,6 +81,7 @@ pub unsafe extern "C" fn wasm_instance_new(
/// It is the caller's responsibility not to alias the [`wasm_instance_t`]
/// with its underlying, internal [`WasmStoreRef`].
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub unsafe extern "C" fn wasm_instance_exports(
instance: &mut wasm_instance_t,
out: &mut wasm_extern_vec_t,
Expand Down
Loading

0 comments on commit 4abde9c

Please sign in to comment.