Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasmtime-c-api: Add support for GC references to the wasm_* C API functions #8326

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions crates/c-api/src/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use wasmtime::{
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, WasmtimeStoreContextMut, WASMTIME_I32,
};

#[no_mangle]
Expand All @@ -30,15 +30,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 +103,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: Caller<'a, crate::WasmtimeStoreData>,
params: &'a [Val],
results: &'a mut [Val],
) -> Result<()> {
Expand Down Expand Up @@ -172,7 +172,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>,
Caller<'a, crate::WasmtimeStoreData>,
&'a [Val],
&'a mut [Val],
) -> Box<dyn Future<Output = Result<()>> + Send + 'a>
Expand Down Expand Up @@ -219,7 +219,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 +245,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 +295,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 +311,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 +330,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 +345,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
4 changes: 2 additions & 2 deletions crates/c-api/src/extern.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
wasm_externkind_t, wasm_externtype_t, wasm_func_t, wasm_global_t, wasm_memory_t, wasm_table_t,
CStoreContext, StoreRef,
StoreRef, WasmtimeStoreContext,
};
use std::mem::ManuallyDrop;
use wasmtime::{Extern, Func, Global, Memory, SharedMemory, Table};
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
36 changes: 20 additions & 16 deletions crates/c-api/src/func.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
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 anyhow::{Error, Result};
use std::any::Any;
Expand Down Expand Up @@ -59,11 +60,11 @@ unsafe fn create_function(
let func = Func::new(
store.store.context_mut(),
ty,
move |_caller, params, results| {
move |mut caller, params, results| {
let params: wasm_val_vec_t = params
.iter()
.cloned()
.map(|p| wasm_val_t::from_val(p))
.map(|p| wasm_val_t::from_val(&mut caller.as_context_mut(), p))
.collect::<Vec<_>>()
.into();
let mut out_results: wasm_val_vec_t = vec![wasm_val_t::default(); results.len()].into();
Expand All @@ -72,7 +73,7 @@ unsafe fn create_function(
return Err(trap.error);
}

let out_results = out_results.as_slice();
let out_results = out_results.as_slice_mut();
for i in 0..results.len() {
results[i] = out_results[i].val();
}
Expand Down Expand Up @@ -150,7 +151,10 @@ pub unsafe extern "C" fn wasm_func_call(
match result {
Ok(Ok(())) => {
for (slot, val) in results.iter_mut().zip(wt_results.iter().cloned()) {
crate::initialize(slot, wasm_val_t::from_val(val));
crate::initialize(
slot,
wasm_val_t::from_val(&mut func.ext.store.context_mut(), val),
);
}
ptr::null_mut()
}
Expand Down Expand Up @@ -200,7 +204,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: Caller<'a, crate::WasmtimeStoreData>,
}

pub type wasmtime_func_callback_t = extern "C" fn(
Expand All @@ -221,7 +225,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 +242,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(Caller<'_, crate::WasmtimeStoreData>, &[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 +295,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 +311,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(Caller<'_, crate::WasmtimeStoreData>, &mut [ValRaw]) -> Result<()> {
let foreign = crate::ForeignData { data, finalizer };
move |caller, values| {
let _ = &foreign; // move entire foreign into this closure
Expand All @@ -321,7 +325,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 +371,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 +394,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 +403,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 +428,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 +437,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
17 changes: 8 additions & 9 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 @@ -65,10 +65,9 @@ pub unsafe extern "C" fn wasm_global_type(g: &wasm_global_t) -> Box<wasm_globalt
#[no_mangle]
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(
out,
wasm_val_t::from_val(global.get(g.ext.store.context_mut())),
);
let mut ctx = g.ext.store.context_mut();
let val = global.get(&mut ctx);
crate::initialize(out, wasm_val_t::from_val(&mut ctx, val));
}

#[no_mangle]
Expand All @@ -79,7 +78,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 +92,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 +110,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
12 changes: 6 additions & 6 deletions crates/c-api/src/instance.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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, StoreRef, WasmtimeStoreContextMut, WasmtimeStoreData,
};
use std::mem::MaybeUninit;
use wasmtime::{Instance, InstancePre, Trap};
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