Skip to content

Commit

Permalink
feat: Use procedural macro to add the prefixed symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
xdoardo committed Nov 27, 2024
1 parent fe7117e commit c7b4bc7
Show file tree
Hide file tree
Showing 25 changed files with 7,221 additions and 234 deletions.
60 changes: 44 additions & 16 deletions crates/c_api/macro/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ pub fn declare_own(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let ty = extract_ident(input);
let name = ty.to_string();
let delete = quote::format_ident!("{}_delete", &name[..name.len() - 2]);
let prefixed_delete = format!("wasmi_{delete}");
let docs = format!("Deletes the [`{name}`].");

(quote! {
#[doc = #docs]
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", export_name = #prefixed_delete)]
#[wasmi_c_api_macros::prefix_symbol]
pub extern "C" fn #delete(_: ::alloc::boxed::Box<#ty>) {}
})
.into()
Expand All @@ -40,7 +39,6 @@ pub fn declare_ty(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let name = ty.to_string();
let prefix = &name[..name.len() - 2];
let copy = quote::format_ident!("{}_copy", &prefix);
let prefixed_copy = format!("wasmi_{copy}");
let docs = format!(
"Creates a new [`{name}`] which matches the provided one.\n\n\
The caller is responsible for deleting the returned value via [`{prefix}_delete`].\n\n\
Expand All @@ -52,7 +50,7 @@ pub fn declare_ty(input: proc_macro::TokenStream) -> proc_macro::TokenStream {

#[doc = #docs]
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", export_name = #prefixed_copy)]
#[wasmi_c_api_macros::prefix_symbol]
pub extern "C" fn #copy(src: &#ty) -> ::alloc::boxed::Box<#ty> {
::alloc::boxed::Box::new(src.clone())
}
Expand All @@ -66,37 +64,31 @@ pub fn declare_ref(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let name = ty.to_string();
let prefix = &name[..name.len() - 2];
let same = quote::format_ident!("{}_same", prefix);
let same_prefixed = format!("wasmi_{same}");
let same_docs = format!(
"Returns `true` if the given references are pointing to the same [`{name}`].\n\n\
This is not yet supported and aborts the process upon use."
);
let get_host_info = quote::format_ident!("{}_get_host_info", prefix);
let get_host_info_prefixed = format!("wasmi_{get_host_info}");
let get_host_info_docs = format!(
"Returns the host information of the [`{name}`].\n\n\
This is not yet supported and always returns `NULL`."
);
let set_host_info = quote::format_ident!("{}_set_host_info", prefix);
let set_host_info_prefixed = format!("wasmi_{set_host_info}");
let set_host_info_docs = format!(
"Sets the host information of the [`{name}`].\n\n\
This is not yet supported and aborts the process upon use."
);
let set_host_info_final = quote::format_ident!("{}_set_host_info_with_finalizer", prefix);
let set_host_info_final_prefixed = format!("wasmi_{set_host_info_final}");
let set_host_info_final_docs = format!(
"Sets the host information finalizer of the [`{name}`].\n\n\
This is not yet supported and aborts the process upon use."
);
let as_ref = quote::format_ident!("{}_as_ref", prefix);
let as_ref_prefixed = format!("wasmi_{as_ref}");
let as_ref_docs = format!(
"Returns the [`{name}`] as mutable reference.\n\n\
This is not yet supported and aborts the process upon use."
);
let as_ref_const = quote::format_ident!("{}_as_ref_const", prefix);
let as_ref_const_prefixed = format!("wasmi_{as_ref_const}");
let as_ref_const_docs = format!(
"Returns the [`{name}`] as immutable reference.\n\n\
This is not yet supported and aborts the process upon use."
Expand All @@ -107,7 +99,7 @@ pub fn declare_ref(input: proc_macro::TokenStream) -> proc_macro::TokenStream {

#[doc = #same_docs]
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", export_name = #same_prefixed)]
#[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 @@ -116,14 +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", export_name = #get_host_info_prefixed)]
#[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", export_name = #set_host_info_prefixed)]
#[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 @@ -132,7 +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", export_name = #set_host_info_final_prefixed)]
#[wasmi_c_api_macros::prefix_symbol]
pub extern "C" fn #set_host_info_final(
a: &#ty,
info: *mut ::core::ffi::c_void,
Expand All @@ -145,7 +137,7 @@ pub fn declare_ref(input: proc_macro::TokenStream) -> proc_macro::TokenStream {

#[doc = #as_ref_docs]
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", export_name = #as_ref_prefixed)]
#[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 @@ -154,7 +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", export_name = #as_ref_const_prefixed)]
#[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 @@ -166,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.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
}
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]
#[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
7 changes: 2 additions & 5 deletions crates/c_api/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ wasmi_c_api_macros::declare_own!(wasm_engine_t);
///
/// Wraps [`wasmi::Engine::default`].
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", export_name = "wasmi_wasm_engine_new")]
#[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 @@ -32,10 +32,7 @@ pub extern "C" fn wasm_engine_new() -> Box<wasm_engine_t> {
///
/// Wraps [`wasmi::Engine::new`].
#[no_mangle]
#[cfg_attr(
feature = "prefix-symbols",
export_name = "wasmi_wasm_engine_new_with_config"
)]
#[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
28 changes: 8 additions & 20 deletions crates/c_api/src/extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +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", export_name = "wasmi_wasm_extern_kind")]
#[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 @@ -40,7 +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", export_name = "wasmi_wasm_extern_type")]
#[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 @@ -51,7 +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", export_name = "wasmi_wasm_extern_as_func")]
#[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 @@ -72,10 +72,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",
export_name = "wasmi_wasm_extern_as_global"
)]
#[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 @@ -84,10 +81,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",
export_name = "wasmi_wasm_extern_as_global_const"
)]
#[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 @@ -96,7 +90,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", export_name = "wasmi_wasm_extern_as_table")]
#[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 @@ -117,10 +111,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",
export_name = "wasmi_wasm_extern_as_memory"
)]
#[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 @@ -129,10 +120,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",
export_name = "wasmi_wasm_extern_as_memory_const"
)]
#[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)
}
2 changes: 1 addition & 1 deletion crates/c_api/src/foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +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", export_name = "wasmi_wasm_foreign_new")]
#[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")
}
14 changes: 4 additions & 10 deletions crates/c_api/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +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",
export_name = "wasmi_wasm_frame_func_index"
)]
#[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 @@ -45,7 +42,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", export_name = "wasmi_wasm_frame_instance")]
#[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 @@ -56,10 +53,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",
export_name = "wasmi_wasm_frame_module_offset"
)]
#[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 @@ -70,7 +64,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", export_name = "wasmi_wasm_frame_copy")]
#[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")
}
23 changes: 7 additions & 16 deletions crates/c_api/src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +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", export_name = "wasmi_wasm_func_new")]
#[wasmi_c_api_macros::prefix_symbol]
pub unsafe extern "C" fn wasm_func_new(
store: &mut wasm_store_t,
ty: &wasm_functype_t,
Expand Down Expand Up @@ -189,7 +189,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", export_name = "wasmi_wasm_func_call")]
#[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 @@ -254,7 +254,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", export_name = "wasmi_wasm_func_type")]
#[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 @@ -270,10 +270,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",
export_name = "wasmi_wasm_func_param_arity"
)]
#[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 @@ -289,27 +286,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",
export_name = "wasmi_wasm_func_result_arity"
)]
#[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", export_name = "wasmi_wasm_func_as_extern")]
#[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",
export_name = "wasmi_wasm_func_as_extern_const"
)]
#[wasmi_c_api_macros::prefix_symbol]
pub extern "C" fn wasm_func_as_extern_const(f: &wasm_func_t) -> &wasm_extern_t {
&f.inner
}
Loading

0 comments on commit c7b4bc7

Please sign in to comment.