@ -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 Wasm StoreRef {
store : Arc < UnsafeCell < Wasm Store> > ,
}
impl StoreRef {
pub unsafe fn context ( & self ) -> StoreContext < '_ , ( ) > {
impl Wasm StoreRef {
pub unsafe fn context ( & self ) -> Wasm StoreContext< '_ > {
( * self . store . get ( ) ) . as_context ( )
}
pub unsafe fn context_mut ( & mut self ) -> StoreContextMut < '_ , ( ) > {
pub unsafe fn context_mut ( & mut self ) -> Wasm StoreContextMut< '_ > {
( * 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 : Wasm StoreRef,
}
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 : Wasm StoreRef {
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 `Wasmtime StoreData` 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 {
Wasmtime StoreData {
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 (
C StoreContextMut< '_ > ,
Wasmtime StoreContextMut< '_ > ,
* 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 : C StoreContext< '_ > ) -> * mut c_void {
pub extern "C" fn wasmtime_context_get_data ( store : Wasmtime StoreContext< '_ > ) -> * 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 : C StoreContextMut< '_ > ,
mut context : Wasmtime StoreContextMut< '_ > ,
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 : C StoreContextMut< '_ > ) {
pub extern "C" fn wasmtime_context_gc ( mut context : Wasmtime StoreContextMut< '_ > ) {
context . gc ( ) ;
}
#[ no_mangle ]
pub extern "C" fn wasmtime_context_set_fuel (
mut store : C StoreContextMut< '_ > ,
mut store : Wasmtime StoreContextMut< '_ > ,
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 : C StoreContext< '_ > ,
store : Wasmtime StoreContext< '_ > ,
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 : C StoreContextMut< '_ > ,
mut store : Wasmtime StoreContextMut< '_ > ,
ticks_beyond_current : u64 ,
) {
store . set_epoch_deadline ( ticks_beyond_current ) ;