Browse Source

c-api: Better differentiate between `wasm.h` and `wasmtime.h` APIs (#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
pull/8348/head
Nick Fitzgerald 7 months ago
committed by GitHub
parent
commit
420fc3d118
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 27
      crates/c-api/src/async.rs
  2. 6
      crates/c-api/src/extern.rs
  3. 30
      crates/c-api/src/func.rs
  4. 10
      crates/c-api/src/global.rs
  5. 16
      crates/c-api/src/instance.rs
  6. 17
      crates/c-api/src/linker.rs
  7. 19
      crates/c-api/src/memory.rs
  8. 71
      crates/c-api/src/store.rs
  9. 14
      crates/c-api/src/table.rs
  10. 8
      crates/c-api/src/val.rs

27
crates/c-api/src/async.rs

@ -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]
@ -30,7 +29,7 @@ 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);
@ -38,7 +37,7 @@ pub extern "C" fn wasmtime_context_epoch_deadline_async_yield_and_update(
#[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(
@ -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<()> {
@ -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>
@ -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>],
@ -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,
@ -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,
@ -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,
@ -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,
@ -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,

6
crates/c-api/src/extern.rs

@ -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,
}
@ -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)))

30
crates/c-api/src/func.rs

@ -1,8 +1,9 @@
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;
@ -10,8 +11,7 @@ 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 {
@ -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(
@ -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,
@ -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
@ -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,
@ -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
@ -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,
@ -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,
@ -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)))
@ -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()
}
@ -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,
) {
@ -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)

10
crates/c-api/src/global.rs

@ -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};
@ -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,
@ -93,7 +93,7 @@ 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)))
@ -101,7 +101,7 @@ pub extern "C" fn wasmtime_global_type(
#[no_mangle]
pub extern "C" fn wasmtime_global_get(
mut store: CStoreContextMut<'_>,
mut store: WasmtimeStoreContextMut<'_>,
global: &Global,
val: &mut MaybeUninit<wasmtime_val_t>,
) {
@ -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>> {

16
crates/c-api/src/instance.rs

@ -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 }
}
}
@ -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,
@ -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,
@ -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,
@ -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]
@ -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>> {

17
crates/c-api/src/linker.rs

@ -1,6 +1,7 @@
use crate::{
bad_utf8, handle_result, wasm_engine_t, wasm_functype_t, wasm_trap_t, wasmtime_error_t,
wasmtime_extern_t, wasmtime_instance_pre_t, wasmtime_module_t, CStoreContext, CStoreContextMut,
wasmtime_extern_t, wasmtime_instance_pre_t, wasmtime_module_t, WasmtimeStoreContext,
WasmtimeStoreContextMut,
};
use std::ffi::c_void;
use std::mem::MaybeUninit;
@ -9,7 +10,7 @@ use wasmtime::{Func, Instance, Linker};
#[repr(C)]
pub struct wasmtime_linker_t {
pub(crate) linker: Linker<crate::StoreData>,
pub(crate) linker: Linker<crate::WasmtimeStoreData>,
}
wasmtime_c_api_macros::declare_own!(wasmtime_linker_t);
@ -50,7 +51,7 @@ pub(crate) use to_str;
#[no_mangle]
pub unsafe extern "C" fn wasmtime_linker_define(
linker: &mut wasmtime_linker_t,
store: CStoreContext<'_>,
store: WasmtimeStoreContext<'_>,
module: *const u8,
module_len: usize,
name: *const u8,
@ -123,7 +124,7 @@ pub extern "C" fn wasmtime_linker_define_wasi(
#[no_mangle]
pub unsafe extern "C" fn wasmtime_linker_define_instance(
linker: &mut wasmtime_linker_t,
store: CStoreContextMut<'_>,
store: WasmtimeStoreContextMut<'_>,
name: *const u8,
name_len: usize,
instance: &Instance,
@ -136,7 +137,7 @@ pub unsafe extern "C" fn wasmtime_linker_define_instance(
#[no_mangle]
pub extern "C" fn wasmtime_linker_instantiate(
linker: &wasmtime_linker_t,
store: CStoreContextMut<'_>,
store: WasmtimeStoreContextMut<'_>,
module: &wasmtime_module_t,
instance_ptr: &mut Instance,
trap_ptr: &mut *mut wasm_trap_t,
@ -161,7 +162,7 @@ pub unsafe extern "C" fn wasmtime_linker_instantiate_pre(
#[no_mangle]
pub unsafe extern "C" fn wasmtime_linker_module(
linker: &mut wasmtime_linker_t,
store: CStoreContextMut<'_>,
store: WasmtimeStoreContextMut<'_>,
name: *const u8,
name_len: usize,
module: &wasmtime_module_t,
@ -174,7 +175,7 @@ pub unsafe extern "C" fn wasmtime_linker_module(
#[no_mangle]
pub unsafe extern "C" fn wasmtime_linker_get_default(
linker: &wasmtime_linker_t,
store: CStoreContextMut<'_>,
store: WasmtimeStoreContextMut<'_>,
name: *const u8,
name_len: usize,
func: &mut Func,
@ -187,7 +188,7 @@ pub unsafe extern "C" fn wasmtime_linker_get_default(
#[no_mangle]
pub unsafe extern "C" fn wasmtime_linker_get(
linker: &wasmtime_linker_t,
store: CStoreContextMut<'_>,
store: WasmtimeStoreContextMut<'_>,
module: *const u8,
module_len: usize,
name: *const u8,

19
crates/c-api/src/memory.rs

@ -1,6 +1,6 @@
use crate::{
handle_result, wasm_extern_t, wasm_memorytype_t, wasm_store_t, wasmtime_error_t, CStoreContext,
CStoreContextMut,
handle_result, wasm_extern_t, wasm_memorytype_t, wasm_store_t, wasmtime_error_t,
WasmtimeStoreContext, WasmtimeStoreContextMut,
};
use std::convert::TryFrom;
use wasmtime::{Extern, Memory};
@ -88,7 +88,7 @@ pub unsafe extern "C" fn wasm_memory_grow(
#[no_mangle]
pub extern "C" fn wasmtime_memory_new(
store: CStoreContextMut<'_>,
store: WasmtimeStoreContextMut<'_>,
ty: &wasm_memorytype_t,
ret: &mut Memory,
) -> Option<Box<wasmtime_error_t>> {
@ -97,30 +97,33 @@ pub extern "C" fn wasmtime_memory_new(
#[no_mangle]
pub extern "C" fn wasmtime_memory_type(
store: CStoreContext<'_>,
store: WasmtimeStoreContext<'_>,
mem: &Memory,
) -> Box<wasm_memorytype_t> {
Box::new(wasm_memorytype_t::new(mem.ty(store)))
}
#[no_mangle]
pub extern "C" fn wasmtime_memory_data(store: CStoreContext<'_>, mem: &Memory) -> *const u8 {
pub extern "C" fn wasmtime_memory_data(store: WasmtimeStoreContext<'_>, mem: &Memory) -> *const u8 {
mem.data(store).as_ptr()
}
#[no_mangle]
pub extern "C" fn wasmtime_memory_data_size(store: CStoreContext<'_>, mem: &Memory) -> usize {
pub extern "C" fn wasmtime_memory_data_size(
store: WasmtimeStoreContext<'_>,
mem: &Memory,
) -> usize {
mem.data(store).len()
}
#[no_mangle]
pub extern "C" fn wasmtime_memory_size(store: CStoreContext<'_>, mem: &Memory) -> u64 {
pub extern "C" fn wasmtime_memory_size(store: WasmtimeStoreContext<'_>, mem: &Memory) -> u64 {
mem.size(store)
}
#[no_mangle]
pub extern "C" fn wasmtime_memory_grow(
store: CStoreContextMut<'_>,
store: WasmtimeStoreContextMut<'_>,
mem: &Memory,
delta: u64,
prev_size: &mut u64,

71
crates/c-api/src/store.rs

@ -3,11 +3,19 @@ use std::cell::UnsafeCell;
use std::ffi::c_void;
use std::sync::Arc;
use wasmtime::{
AsContext, AsContextMut, Store, StoreContext, StoreContextMut, StoreLimits, StoreLimitsBuilder,
UpdateDeadline, Val,
AsContext, AsContextMut, Caller, Store, StoreContext, StoreContextMut, StoreLimits,
StoreLimitsBuilder, UpdateDeadline, Val,
};
/// This representation of a `Store` is used to implement the `wasm.h` API.
// Store-related type aliases for `wasm.h` APIs. Not for use with `wasmtime.h`
// APIs!
pub type WasmStoreData = ();
pub type WasmStore = Store<WasmStoreData>;
pub type WasmStoreContext<'a> = StoreContext<'a, WasmStoreData>;
pub type WasmStoreContextMut<'a> = StoreContextMut<'a, WasmStoreData>;
/// This representation of a `Store` is used to implement the `wasm.h` API (and
/// *not* the `wasmtime.h` API!)
///
/// This is stored alongside `Func` and such for `wasm.h` so each object is
/// independently owned. The usage of `Arc` here is mostly to just get it to be
@ -18,16 +26,16 @@ use wasmtime::{
/// The aliasing requirements are documented in the C API `wasm.h` itself (at
/// least Wasmtime's implementation).
#[derive(Clone)]
pub struct StoreRef {
store: Arc<UnsafeCell<Store<()>>>,
pub struct WasmStoreRef {
store: Arc<UnsafeCell<WasmStore>>,
}
impl StoreRef {
pub unsafe fn context(&self) -> StoreContext<'_, ()> {
impl WasmStoreRef {
pub unsafe fn context(&self) -> WasmStoreContext<'_> {
(*self.store.get()).as_context()
}
pub unsafe fn context_mut(&mut self) -> StoreContextMut<'_, ()> {
pub unsafe fn context_mut(&mut self) -> WasmStoreContextMut<'_> {
(*self.store.get()).as_context_mut()
}
}
@ -35,7 +43,7 @@ impl StoreRef {
#[repr(C)]
#[derive(Clone)]
pub struct wasm_store_t {
pub(crate) store: StoreRef,
pub(crate) store: WasmStoreRef,
}
wasmtime_c_api_macros::declare_own!(wasm_store_t);
@ -45,30 +53,34 @@ pub extern "C" fn wasm_store_new(engine: &wasm_engine_t) -> Box<wasm_store_t> {
let engine = &engine.engine;
let store = Store::new(engine, ());
Box::new(wasm_store_t {
store: StoreRef {
store: WasmStoreRef {
store: Arc::new(UnsafeCell::new(store)),
},
})
}
// Store-related type aliases for `wasmtime.h` APIs. Not for use with `wasm.h`
// APIs!
pub type WasmtimeStore = Store<WasmtimeStoreData>;
pub type WasmtimeStoreContext<'a> = StoreContext<'a, WasmtimeStoreData>;
pub type WasmtimeStoreContextMut<'a> = StoreContextMut<'a, WasmtimeStoreData>;
pub type WasmtimeCaller<'a> = Caller<'a, WasmtimeStoreData>;
/// Representation of a `Store` for `wasmtime.h` This notably tries to move more
/// burden of aliasing on the caller rather than internally, allowing for a more
/// raw representation of contexts and such that requires less `unsafe` in the
/// implementation.
///
/// Note that this notably carries `StoreData` as a payload which allows storing
/// foreign data and configuring WASI as well.
/// Note that this notably carries `WasmtimeStoreData` as a payload which allows
/// storing foreign data and configuring WASI as well.
#[repr(C)]
pub struct wasmtime_store_t {
pub(crate) store: Store<StoreData>,
pub(crate) store: WasmtimeStore,
}
wasmtime_c_api_macros::declare_own!(wasmtime_store_t);
pub type CStoreContext<'a> = StoreContext<'a, StoreData>;
pub type CStoreContextMut<'a> = StoreContextMut<'a, StoreData>;
pub struct StoreData {
pub struct WasmtimeStoreData {
foreign: crate::ForeignData,
#[cfg(feature = "wasi")]
pub(crate) wasi: Option<wasi_common::WasiCtx>,
@ -94,7 +106,7 @@ pub extern "C" fn wasmtime_store_new(
Box::new(wasmtime_store_t {
store: Store::new(
&engine.engine,
StoreData {
WasmtimeStoreData {
foreign: ForeignData { data, finalizer },
#[cfg(feature = "wasi")]
wasi: None,
@ -114,7 +126,7 @@ pub const WASMTIME_UPDATE_DEADLINE_YIELD: wasmtime_update_deadline_kind_t = 1;
pub extern "C" fn wasmtime_store_epoch_deadline_callback(
store: &mut wasmtime_store_t,
func: extern "C" fn(
CStoreContextMut<'_>,
WasmtimeStoreContextMut<'_>,
*mut c_void,
*mut u64,
*mut wasmtime_update_deadline_kind_t,
@ -148,7 +160,9 @@ pub extern "C" fn wasmtime_store_epoch_deadline_callback(
}
#[no_mangle]
pub extern "C" fn wasmtime_store_context(store: &mut wasmtime_store_t) -> CStoreContextMut<'_> {
pub extern "C" fn wasmtime_store_context(
store: &mut wasmtime_store_t,
) -> WasmtimeStoreContextMut<'_> {
store.store.as_context_mut()
}
@ -182,19 +196,22 @@ pub extern "C" fn wasmtime_store_limiter(
}
#[no_mangle]
pub extern "C" fn wasmtime_context_get_data(store: CStoreContext<'_>) -> *mut c_void {
pub extern "C" fn wasmtime_context_get_data(store: WasmtimeStoreContext<'_>) -> *mut c_void {
store.data().foreign.data
}
#[no_mangle]
pub extern "C" fn wasmtime_context_set_data(mut store: CStoreContextMut<'_>, data: *mut c_void) {
pub extern "C" fn wasmtime_context_set_data(
mut store: WasmtimeStoreContextMut<'_>,
data: *mut c_void,
) {
store.data_mut().foreign.data = data;
}
#[cfg(feature = "wasi")]
#[no_mangle]
pub extern "C" fn wasmtime_context_set_wasi(
mut context: CStoreContextMut<'_>,
mut context: WasmtimeStoreContextMut<'_>,
wasi: Box<crate::wasi_config_t>,
) -> Option<Box<wasmtime_error_t>> {
crate::handle_result(wasi.into_wasi_ctx(), |wasi| {
@ -203,13 +220,13 @@ pub extern "C" fn wasmtime_context_set_wasi(
}
#[no_mangle]
pub extern "C" fn wasmtime_context_gc(mut context: CStoreContextMut<'_>) {
pub extern "C" fn wasmtime_context_gc(mut context: WasmtimeStoreContextMut<'_>) {
context.gc();
}
#[no_mangle]
pub extern "C" fn wasmtime_context_set_fuel(
mut store: CStoreContextMut<'_>,
mut store: WasmtimeStoreContextMut<'_>,
fuel: u64,
) -> Option<Box<wasmtime_error_t>> {
crate::handle_result(store.set_fuel(fuel), |()| {})
@ -217,7 +234,7 @@ pub extern "C" fn wasmtime_context_set_fuel(
#[no_mangle]
pub extern "C" fn wasmtime_context_get_fuel(
store: CStoreContext<'_>,
store: WasmtimeStoreContext<'_>,
fuel: &mut u64,
) -> Option<Box<wasmtime_error_t>> {
crate::handle_result(store.get_fuel(), |amt| {
@ -227,7 +244,7 @@ pub extern "C" fn wasmtime_context_get_fuel(
#[no_mangle]
pub extern "C" fn wasmtime_context_set_epoch_deadline(
mut store: CStoreContextMut<'_>,
mut store: WasmtimeStoreContextMut<'_>,
ticks_beyond_current: u64,
) {
store.set_epoch_deadline(ticks_beyond_current);

14
crates/c-api/src/table.rs

@ -1,6 +1,6 @@
use crate::{
handle_result, wasm_extern_t, wasm_ref_t, wasm_store_t, wasm_tabletype_t, wasmtime_error_t,
wasmtime_val_t, CStoreContext, CStoreContextMut,
wasmtime_val_t, WasmtimeStoreContext, WasmtimeStoreContextMut,
};
use anyhow::anyhow;
use std::mem::MaybeUninit;
@ -116,7 +116,7 @@ pub extern "C" fn wasm_table_as_extern_const(t: &wasm_table_t) -> &wasm_extern_t
#[no_mangle]
pub unsafe extern "C" fn wasmtime_table_new(
mut store: CStoreContextMut<'_>,
mut store: WasmtimeStoreContextMut<'_>,
tt: &wasm_tabletype_t,
init: &wasmtime_val_t,
out: &mut Table,
@ -132,7 +132,7 @@ pub unsafe extern "C" fn wasmtime_table_new(
#[no_mangle]
pub unsafe extern "C" fn wasmtime_table_type(
store: CStoreContext<'_>,
store: WasmtimeStoreContext<'_>,
table: &Table,
) -> Box<wasm_tabletype_t> {
Box::new(wasm_tabletype_t::new(table.ty(store)))
@ -140,7 +140,7 @@ pub unsafe extern "C" fn wasmtime_table_type(
#[no_mangle]
pub extern "C" fn wasmtime_table_get(
mut store: CStoreContextMut<'_>,
mut store: WasmtimeStoreContextMut<'_>,
table: &Table,
index: u32,
ret: &mut MaybeUninit<wasmtime_val_t>,
@ -156,7 +156,7 @@ pub extern "C" fn wasmtime_table_get(
#[no_mangle]
pub unsafe extern "C" fn wasmtime_table_set(
mut store: CStoreContextMut<'_>,
mut store: WasmtimeStoreContextMut<'_>,
table: &Table,
index: u32,
val: &wasmtime_val_t,
@ -171,13 +171,13 @@ pub unsafe extern "C" fn wasmtime_table_set(
}
#[no_mangle]
pub extern "C" fn wasmtime_table_size(store: CStoreContext<'_>, table: &Table) -> u32 {
pub extern "C" fn wasmtime_table_size(store: WasmtimeStoreContext<'_>, table: &Table) -> u32 {
table.size(store)
}
#[no_mangle]
pub unsafe extern "C" fn wasmtime_table_grow(
mut store: CStoreContextMut<'_>,
mut store: WasmtimeStoreContextMut<'_>,
table: &Table,
delta: u32,
val: &wasmtime_val_t,

8
crates/c-api/src/val.rs

@ -1,7 +1,7 @@
use crate::r#ref::ref_to_val;
use crate::{
from_valtype, into_valtype, wasm_ref_t, wasm_valkind_t, wasmtime_valkind_t, CStoreContextMut,
WASM_I32,
from_valtype, into_valtype, wasm_ref_t, wasm_valkind_t, wasmtime_valkind_t,
WasmtimeStoreContextMut, WASM_I32,
};
use std::mem::{self, ManuallyDrop, MaybeUninit};
use std::ptr;
@ -237,7 +237,7 @@ impl Drop for wasmtime_val_t {
#[no_mangle]
pub unsafe extern "C" fn wasmtime_val_delete(
cx: CStoreContextMut<'_>,
cx: WasmtimeStoreContextMut<'_>,
val: &mut ManuallyDrop<wasmtime_val_t>,
) {
// TODO: needed for when we re-add externref support.
@ -248,7 +248,7 @@ pub unsafe extern "C" fn wasmtime_val_delete(
#[no_mangle]
pub unsafe extern "C" fn wasmtime_val_copy(
mut cx: CStoreContextMut<'_>,
mut cx: WasmtimeStoreContextMut<'_>,
dst: &mut MaybeUninit<wasmtime_val_t>,
src: &wasmtime_val_t,
) {

Loading…
Cancel
Save