Skip to content

Commit

Permalink
feat: Add prefix-symbols feature
Browse files Browse the repository at this point in the history
  • Loading branch information
xdoardo committed Nov 27, 2024
1 parent f4fc8c0 commit 4728ac8
Show file tree
Hide file tree
Showing 25 changed files with 383 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 = []
16 changes: 16 additions & 0 deletions crates/c_api/macro/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ 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", &name[..name.len() - 2]);
let docs = format!("Deletes the [`{name}`].");

(quote! {
#[doc = #docs]
#[no_mangle]
#[cfg_attr(feature = "prefix-symbols", export_name = #prefixed_delete)]
pub extern "C" fn #delete(_: ::alloc::boxed::Box<#ty>) {}
})
.into()
Expand All @@ -38,6 +40,7 @@ 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", &prefix);
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 @@ -49,6 +52,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)]
pub extern "C" fn #copy(src: &#ty) -> ::alloc::boxed::Box<#ty> {
::alloc::boxed::Box::new(src.clone())
}
Expand All @@ -62,31 +66,37 @@ 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", prefix);
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", prefix);
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", prefix);
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_with_finalizer", prefix);
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", prefix);
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", prefix);
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 @@ -97,6 +107,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)]
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 +116,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)]
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)]
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 +132,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)]
pub extern "C" fn #set_host_info_final(
a: &#ty,
info: *mut ::core::ffi::c_void,
Expand All @@ -131,6 +145,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)]
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 +154,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)]
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 Down
5 changes: 5 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", export_name = "wasmi_wasm_engine_new")]
pub extern "C" fn wasm_engine_new() -> Box<wasm_engine_t> {
Box::new(wasm_engine_t {
inner: Engine::default(),
Expand All @@ -31,6 +32,10 @@ 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"
)]
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: 28 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", export_name = "wasmi_wasm_extern_kind")]
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", export_name = "wasmi_wasm_extern_type")]
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", export_name = "wasmi_wasm_extern_as_func")]
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,10 @@ 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",
export_name = "wasmi_wasm_extern_as_func_const"
)]
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 +72,10 @@ 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"
)]
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 +84,10 @@ 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"
)]
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 +96,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")]
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 +105,10 @@ 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",
export_name = "wasmi_wasm_extern_as_table_const"
)]
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 +117,10 @@ 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"
)]
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 +129,10 @@ 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"
)]
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", export_name = "wasmi_wasm_foreign_new")]
pub extern "C" fn wasm_foreign_new(_store: &crate::wasm_store_t) -> Box<wasm_foreign_t> {
unimplemented!("wasm_foreign_new")
}
14 changes: 14 additions & 0 deletions crates/c_api/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ 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"
)]
pub extern "C" fn wasm_frame_func_index(_frame: &wasm_frame_t<'_>) -> u32 {
unimplemented!("wasm_frame_func_index")
}
Expand All @@ -27,6 +31,10 @@ 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",
export_name = "wasmi_wasm_frame_func_offset"
)]
pub extern "C" fn wasm_frame_func_offset(_frame: &wasm_frame_t<'_>) -> usize {
unimplemented!("wasm_frame_func_offset")
}
Expand All @@ -37,6 +45,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")]
pub extern "C" fn wasm_frame_instance(_arg1: *const wasm_frame_t<'_>) -> *mut wasm_instance_t {
unimplemented!("wasm_frame_instance")
}
Expand All @@ -47,6 +56,10 @@ 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"
)]
pub extern "C" fn wasm_frame_module_offset(_frame: &wasm_frame_t<'_>) -> usize {
unimplemented!("wasm_frame_module_offset")
}
Expand All @@ -57,6 +70,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<'a>")]
pub extern "C" fn wasm_frame_copy<'a>(_frame: &wasm_frame_t<'a>) -> Box<wasm_frame_t<'a>> {
unimplemented!("wasm_frame_copy")
}
20 changes: 20 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", export_name = "wasmi_wasm_func_new")]
pub unsafe extern "C" fn wasm_func_new(
store: &mut wasm_store_t,
ty: &wasm_functype_t,
Expand All @@ -141,6 +142,10 @@ 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",
export_name = "wasmi_wasm_func_new_with_env"
)]
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 +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")]
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 +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")]
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 +270,10 @@ 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"
)]
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 +289,27 @@ 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"
)]
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")]
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"
)]
pub extern "C" fn wasm_func_as_extern_const(f: &wasm_func_t) -> &wasm_extern_t {
&f.inner
}
4 changes: 4 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", export_name = "wasmi_wasm_global_new")]
pub unsafe extern "C" fn wasm_global_new(
store: &mut wasm_store_t,
ty: &wasm_globaltype_t,
Expand Down Expand Up @@ -89,6 +90,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", export_name = "wasmi_wasm_global_type")]
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 +105,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", export_name = "wasmi_wasm_global_get")]
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 +124,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", export_name = "wasmi_wasm_global_set")]
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
Loading

0 comments on commit 4728ac8

Please sign in to comment.