Skip to content

Commit

Permalink
c-api: Better differentiate between wasm.h and wasmtime.h APIs (#…
Browse files Browse the repository at this point in the history
…8344)

This renames some types and adds some type aliases to help us better distinguish
between `wasm.h` APIs and `wasmtime.h` APIs, primarily for `Store`-related
types. In general, `WasmFoo` is related to `wasm.h` and `WasmtimeFoo` is related
to `wasmtime.h`.

* `StoreRef` -> `WasmStoreRef`
* Introduce the `WasmStore[Data]` and `WasmStoreContext[Mut]` aliases
* `StoreData` -> `WasmtimeStoreData`
* `CStoreContext[Mut]` -> `WasmtimeStoreContext[Mut]`
* Introduce the `Wasmtime{Store,Caller}` aliases
  • Loading branch information
fitzgen authored Apr 12, 2024
1 parent d7d7f78 commit 420fc3d
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 99 deletions.
27 changes: 13 additions & 14 deletions crates/c-api/src/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ use std::sync::Arc;
use std::task::{Context, Poll};
use std::{ptr, str};

use wasmtime::{
AsContextMut, Caller, Func, Instance, Result, StackCreator, StackMemory, Trap, Val,
};
use wasmtime::{AsContextMut, Func, Instance, Result, StackCreator, StackMemory, Trap, Val};

use crate::{
bad_utf8, handle_result, to_str, translate_args, wasm_config_t, wasm_functype_t, wasm_trap_t,
wasmtime_caller_t, wasmtime_error_t, wasmtime_instance_pre_t, wasmtime_linker_t,
wasmtime_module_t, wasmtime_val_t, wasmtime_val_union, CStoreContextMut, WASMTIME_I32,
wasmtime_module_t, wasmtime_val_t, wasmtime_val_union, WasmtimeCaller, WasmtimeStoreContextMut,
WASMTIME_I32,
};

#[no_mangle]
Expand All @@ -30,15 +29,15 @@ pub extern "C" fn wasmtime_config_async_stack_size_set(c: &mut wasm_config_t, si

#[no_mangle]
pub extern "C" fn wasmtime_context_epoch_deadline_async_yield_and_update(
mut store: CStoreContextMut<'_>,
mut store: WasmtimeStoreContextMut<'_>,
delta: u64,
) {
store.epoch_deadline_async_yield_and_update(delta);
}

#[no_mangle]
pub extern "C" fn wasmtime_context_fuel_async_yield_interval(
mut store: CStoreContextMut<'_>,
mut store: WasmtimeStoreContextMut<'_>,
interval: Option<NonZeroU64>,
) -> Option<Box<wasmtime_error_t>> {
handle_result(
Expand Down Expand Up @@ -103,7 +102,7 @@ pub type wasmtime_func_async_continuation_callback_t = extern "C" fn(*mut c_void
async fn invoke_c_async_callback<'a>(
cb: wasmtime_func_async_callback_t,
data: CallbackDataPtr,
mut caller: Caller<'a, crate::StoreData>,
mut caller: WasmtimeCaller<'a>,
params: &'a [Val],
results: &'a mut [Val],
) -> Result<()> {
Expand Down Expand Up @@ -172,7 +171,7 @@ unsafe fn c_async_callback_to_rust_fn(
data: *mut c_void,
finalizer: Option<extern "C" fn(*mut std::ffi::c_void)>,
) -> impl for<'a> Fn(
Caller<'a, crate::StoreData>,
WasmtimeCaller<'a>,
&'a [Val],
&'a mut [Val],
) -> Box<dyn Future<Output = Result<()>> + Send + 'a>
Expand Down Expand Up @@ -219,7 +218,7 @@ fn handle_call_error(
}

async fn do_func_call_async(
mut store: CStoreContextMut<'_>,
mut store: WasmtimeStoreContextMut<'_>,
func: &Func,
args: impl ExactSizeIterator<Item = Val>,
results: &mut [MaybeUninit<wasmtime_val_t>],
Expand All @@ -245,7 +244,7 @@ async fn do_func_call_async(

#[no_mangle]
pub unsafe extern "C" fn wasmtime_func_call_async<'a>(
mut store: CStoreContextMut<'a>,
mut store: WasmtimeStoreContextMut<'a>,
func: &'a Func,
args: *const wasmtime_val_t,
nargs: usize,
Expand Down Expand Up @@ -295,7 +294,7 @@ pub unsafe extern "C" fn wasmtime_linker_define_async_func(

async fn do_linker_instantiate_async(
linker: &wasmtime_linker_t,
store: CStoreContextMut<'_>,
store: WasmtimeStoreContextMut<'_>,
module: &wasmtime_module_t,
instance_ptr: &mut Instance,
trap_ret: &mut *mut wasm_trap_t,
Expand All @@ -311,7 +310,7 @@ async fn do_linker_instantiate_async(
#[no_mangle]
pub extern "C" fn wasmtime_linker_instantiate_async<'a>(
linker: &'a wasmtime_linker_t,
store: CStoreContextMut<'a>,
store: WasmtimeStoreContextMut<'a>,
module: &'a wasmtime_module_t,
instance_ptr: &'a mut Instance,
trap_ret: &'a mut *mut wasm_trap_t,
Expand All @@ -330,7 +329,7 @@ pub extern "C" fn wasmtime_linker_instantiate_async<'a>(

async fn do_instance_pre_instantiate_async(
instance_pre: &wasmtime_instance_pre_t,
store: CStoreContextMut<'_>,
store: WasmtimeStoreContextMut<'_>,
instance_ptr: &mut Instance,
trap_ret: &mut *mut wasm_trap_t,
err_ret: &mut *mut wasmtime_error_t,
Expand All @@ -345,7 +344,7 @@ async fn do_instance_pre_instantiate_async(
#[no_mangle]
pub extern "C" fn wasmtime_instance_pre_instantiate_async<'a>(
instance_pre: &'a wasmtime_instance_pre_t,
store: CStoreContextMut<'a>,
store: WasmtimeStoreContextMut<'a>,
instance_ptr: &'a mut Instance,
trap_ret: &'a mut *mut wasm_trap_t,
err_ret: &'a mut *mut wasmtime_error_t,
Expand Down
6 changes: 3 additions & 3 deletions crates/c-api/src/extern.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::{
wasm_externkind_t, wasm_externtype_t, wasm_func_t, wasm_global_t, wasm_memory_t, wasm_table_t,
CStoreContext, StoreRef,
WasmStoreRef, WasmtimeStoreContext,
};
use std::mem::ManuallyDrop;
use wasmtime::{Extern, Func, Global, Memory, SharedMemory, Table};

#[derive(Clone)]
pub struct wasm_extern_t {
pub(crate) store: StoreRef,
pub(crate) store: WasmStoreRef,
pub(crate) which: Extern,
}

Expand Down Expand Up @@ -152,7 +152,7 @@ pub unsafe extern "C" fn wasmtime_extern_delete(e: &mut ManuallyDrop<wasmtime_ex

#[no_mangle]
pub unsafe extern "C" fn wasmtime_extern_type(
store: CStoreContext<'_>,
store: WasmtimeStoreContext<'_>,
e: &wasmtime_extern_t,
) -> Box<wasm_externtype_t> {
Box::new(wasm_externtype_t::from_extern_type(e.to_extern().ty(store)))
Expand Down
30 changes: 15 additions & 15 deletions crates/c-api/src/func.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use crate::wasm_trap_t;
use crate::{
wasm_extern_t, wasm_functype_t, wasm_store_t, wasm_val_t, wasm_val_vec_t, wasmtime_error_t,
wasmtime_extern_t, wasmtime_val_t, wasmtime_val_union, CStoreContext, CStoreContextMut,
wasmtime_extern_t, wasmtime_val_t, wasmtime_val_union, WasmtimeStoreContext,
WasmtimeStoreContextMut,
};
use crate::{wasm_trap_t, WasmtimeCaller};
use anyhow::{Error, Result};
use std::any::Any;
use std::ffi::c_void;
use std::mem::{self, MaybeUninit};
use std::panic::{self, AssertUnwindSafe};
use std::ptr;
use std::str;
use wasmtime::{AsContextMut, Caller, Extern, Func, Trap, Val, ValRaw};

use wasmtime::{AsContextMut, Extern, Func, Trap, Val, ValRaw};
#[derive(Clone)]
#[repr(transparent)]
pub struct wasm_func_t {
Expand Down Expand Up @@ -200,7 +200,7 @@ pub extern "C" fn wasm_func_as_extern_const(f: &wasm_func_t) -> &wasm_extern_t {

#[repr(C)]
pub struct wasmtime_caller_t<'a> {
pub(crate) caller: Caller<'a, crate::StoreData>,
pub(crate) caller: WasmtimeCaller<'a>,
}

pub type wasmtime_func_callback_t = extern "C" fn(
Expand All @@ -221,7 +221,7 @@ pub type wasmtime_func_unchecked_callback_t = extern "C" fn(

#[no_mangle]
pub unsafe extern "C" fn wasmtime_func_new(
store: CStoreContextMut<'_>,
store: WasmtimeStoreContextMut<'_>,
ty: &wasm_functype_t,
callback: wasmtime_func_callback_t,
data: *mut c_void,
Expand All @@ -238,7 +238,7 @@ pub(crate) unsafe fn c_callback_to_rust_fn(
callback: wasmtime_func_callback_t,
data: *mut c_void,
finalizer: Option<extern "C" fn(*mut std::ffi::c_void)>,
) -> impl Fn(Caller<'_, crate::StoreData>, &[Val], &mut [Val]) -> Result<()> {
) -> impl Fn(WasmtimeCaller<'_>, &[Val], &mut [Val]) -> Result<()> {
let foreign = crate::ForeignData { data, finalizer };
move |mut caller, params, results| {
let _ = &foreign; // move entire foreign into this closure
Expand Down Expand Up @@ -291,7 +291,7 @@ pub(crate) unsafe fn c_callback_to_rust_fn(

#[no_mangle]
pub unsafe extern "C" fn wasmtime_func_new_unchecked(
store: CStoreContextMut<'_>,
store: WasmtimeStoreContextMut<'_>,
ty: &wasm_functype_t,
callback: wasmtime_func_unchecked_callback_t,
data: *mut c_void,
Expand All @@ -307,7 +307,7 @@ pub(crate) unsafe fn c_unchecked_callback_to_rust_fn(
callback: wasmtime_func_unchecked_callback_t,
data: *mut c_void,
finalizer: Option<extern "C" fn(*mut std::ffi::c_void)>,
) -> impl Fn(Caller<'_, crate::StoreData>, &mut [ValRaw]) -> Result<()> {
) -> impl Fn(WasmtimeCaller<'_>, &mut [ValRaw]) -> Result<()> {
let foreign = crate::ForeignData { data, finalizer };
move |caller, values| {
let _ = &foreign; // move entire foreign into this closure
Expand All @@ -321,7 +321,7 @@ pub(crate) unsafe fn c_unchecked_callback_to_rust_fn(

#[no_mangle]
pub unsafe extern "C" fn wasmtime_func_call(
mut store: CStoreContextMut<'_>,
mut store: WasmtimeStoreContextMut<'_>,
func: &Func,
args: *const wasmtime_val_t,
nargs: usize,
Expand Down Expand Up @@ -367,7 +367,7 @@ pub unsafe extern "C" fn wasmtime_func_call(

#[no_mangle]
pub unsafe extern "C" fn wasmtime_func_call_unchecked(
store: CStoreContextMut<'_>,
store: WasmtimeStoreContextMut<'_>,
func: &Func,
args_and_results: *mut ValRaw,
args_and_results_len: usize,
Expand All @@ -390,7 +390,7 @@ fn store_err(err: Error, trap_ret: &mut *mut wasm_trap_t) -> Option<Box<wasmtime

#[no_mangle]
pub extern "C" fn wasmtime_func_type(
store: CStoreContext<'_>,
store: WasmtimeStoreContext<'_>,
func: &Func,
) -> Box<wasm_functype_t> {
Box::new(wasm_functype_t::new(func.ty(store)))
Expand All @@ -399,7 +399,7 @@ pub extern "C" fn wasmtime_func_type(
#[no_mangle]
pub extern "C" fn wasmtime_caller_context<'a>(
caller: &'a mut wasmtime_caller_t,
) -> CStoreContextMut<'a> {
) -> WasmtimeStoreContextMut<'a> {
caller.caller.as_context_mut()
}

Expand All @@ -424,7 +424,7 @@ pub unsafe extern "C" fn wasmtime_caller_export_get(

#[no_mangle]
pub unsafe extern "C" fn wasmtime_func_from_raw(
store: CStoreContextMut<'_>,
store: WasmtimeStoreContextMut<'_>,
raw: *mut c_void,
func: &mut Func,
) {
Expand All @@ -433,7 +433,7 @@ pub unsafe extern "C" fn wasmtime_func_from_raw(

#[no_mangle]
pub unsafe extern "C" fn wasmtime_func_to_raw(
store: CStoreContextMut<'_>,
store: WasmtimeStoreContextMut<'_>,
func: &Func,
) -> *mut c_void {
func.to_raw(store)
Expand Down
10 changes: 5 additions & 5 deletions crates/c-api/src/global.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
handle_result, wasm_extern_t, wasm_globaltype_t, wasm_store_t, wasm_val_t, wasmtime_error_t,
wasmtime_val_t, CStoreContext, CStoreContextMut,
wasmtime_val_t, WasmtimeStoreContext, WasmtimeStoreContextMut,
};
use std::mem::MaybeUninit;
use wasmtime::{Extern, Global};
Expand Down Expand Up @@ -79,7 +79,7 @@ pub unsafe extern "C" fn wasm_global_set(g: &mut wasm_global_t, val: &wasm_val_t

#[no_mangle]
pub unsafe extern "C" fn wasmtime_global_new(
mut store: CStoreContextMut<'_>,
mut store: WasmtimeStoreContextMut<'_>,
gt: &wasm_globaltype_t,
val: &wasmtime_val_t,
ret: &mut Global,
Expand All @@ -93,15 +93,15 @@ pub unsafe extern "C" fn wasmtime_global_new(

#[no_mangle]
pub extern "C" fn wasmtime_global_type(
store: CStoreContext<'_>,
store: WasmtimeStoreContext<'_>,
global: &Global,
) -> Box<wasm_globaltype_t> {
Box::new(wasm_globaltype_t::new(global.ty(store)))
}

#[no_mangle]
pub extern "C" fn wasmtime_global_get(
mut store: CStoreContextMut<'_>,
mut store: WasmtimeStoreContextMut<'_>,
global: &Global,
val: &mut MaybeUninit<wasmtime_val_t>,
) {
Expand All @@ -111,7 +111,7 @@ pub extern "C" fn wasmtime_global_get(

#[no_mangle]
pub unsafe extern "C" fn wasmtime_global_set(
mut store: CStoreContextMut<'_>,
mut store: WasmtimeStoreContextMut<'_>,
global: &Global,
val: &wasmtime_val_t,
) -> Option<Box<wasmtime_error_t>> {
Expand Down
16 changes: 8 additions & 8 deletions crates/c-api/src/instance.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use crate::{
wasm_extern_t, wasm_extern_vec_t, wasm_module_t, wasm_store_t, wasm_trap_t, wasmtime_error_t,
wasmtime_extern_t, wasmtime_module_t, CStoreContextMut, StoreData, StoreRef,
wasmtime_extern_t, wasmtime_module_t, WasmStoreRef, WasmtimeStoreContextMut, WasmtimeStoreData,
};
use std::mem::MaybeUninit;
use wasmtime::{Instance, InstancePre, Trap};

#[derive(Clone)]
pub struct wasm_instance_t {
store: StoreRef,
store: WasmStoreRef,
instance: Instance,
}

wasmtime_c_api_macros::declare_ref!(wasm_instance_t);

impl wasm_instance_t {
pub(crate) fn new(store: StoreRef, instance: Instance) -> wasm_instance_t {
pub(crate) fn new(store: WasmStoreRef, instance: Instance) -> wasm_instance_t {
wasm_instance_t { store, instance }
}
}
Expand Down Expand Up @@ -70,7 +70,7 @@ pub unsafe extern "C" fn wasm_instance_exports(

#[no_mangle]
pub unsafe extern "C" fn wasmtime_instance_new(
store: CStoreContextMut<'_>,
store: WasmtimeStoreContextMut<'_>,
module: &wasmtime_module_t,
imports: *const wasmtime_extern_t,
nimports: usize,
Expand Down Expand Up @@ -111,7 +111,7 @@ pub(crate) fn handle_instantiate(

#[no_mangle]
pub unsafe extern "C" fn wasmtime_instance_export_get(
store: CStoreContextMut<'_>,
store: WasmtimeStoreContextMut<'_>,
instance: &Instance,
name: *const u8,
name_len: usize,
Expand All @@ -133,7 +133,7 @@ pub unsafe extern "C" fn wasmtime_instance_export_get(

#[no_mangle]
pub unsafe extern "C" fn wasmtime_instance_export_nth(
store: CStoreContextMut<'_>,
store: WasmtimeStoreContextMut<'_>,
instance: &Instance,
index: usize,
name_ptr: &mut *const u8,
Expand All @@ -153,7 +153,7 @@ pub unsafe extern "C" fn wasmtime_instance_export_nth(

#[repr(transparent)]
pub struct wasmtime_instance_pre_t {
pub(crate) underlying: InstancePre<StoreData>,
pub(crate) underlying: InstancePre<WasmtimeStoreData>,
}

#[no_mangle]
Expand All @@ -163,7 +163,7 @@ pub unsafe extern "C" fn wasmtime_instance_pre_delete(_instance_pre: Box<wasmtim
#[no_mangle]
pub unsafe extern "C" fn wasmtime_instance_pre_instantiate(
instance_pre: &wasmtime_instance_pre_t,
store: CStoreContextMut<'_>,
store: WasmtimeStoreContextMut<'_>,
instance_ptr: &mut Instance,
trap_ptr: &mut *mut wasm_trap_t,
) -> Option<Box<wasmtime_error_t>> {
Expand Down
Loading

0 comments on commit 420fc3d

Please sign in to comment.