use super::VMOpaqueContext;
use crate::{StoreBox, VMFuncRef};
use std::any::Any;
use wasmtime_environ::{VM_ARRAY_CALL_HOST_FUNC_MAGIC, VM_NATIVE_CALL_HOST_FUNC_MAGIC};
#[repr(C)]
pub struct VMArrayCallHostFuncContext {
magic: u32,
pub(crate) func_ref: VMFuncRef,
host_state: Box<dyn Any + Send + Sync>,
}
impl VMArrayCallHostFuncContext {
pub unsafe fn new(
func_ref: VMFuncRef,
host_state: Box<dyn Any + Send + Sync>,
) -> StoreBox<VMArrayCallHostFuncContext> {
debug_assert!(func_ref.vmctx.is_null());
let ctx = StoreBox::new(VMArrayCallHostFuncContext {
magic: wasmtime_environ::VM_ARRAY_CALL_HOST_FUNC_MAGIC,
func_ref,
host_state,
});
let vmctx = VMOpaqueContext::from_vm_array_call_host_func_context(ctx.get());
unsafe {
(*ctx.get()).func_ref.vmctx = vmctx;
}
ctx
}
#[inline]
pub fn host_state(&self) -> &(dyn Any + Send + Sync) {
&*self.host_state
}
#[inline]
pub fn func_ref(&self) -> &VMFuncRef {
&self.func_ref
}
#[inline]
pub unsafe fn from_opaque(opaque: *mut VMOpaqueContext) -> *mut VMArrayCallHostFuncContext {
debug_assert_eq!((*opaque).magic, VM_ARRAY_CALL_HOST_FUNC_MAGIC);
opaque.cast()
}
}
#[repr(C)]
pub struct VMNativeCallHostFuncContext {
magic: u32,
func_ref: VMFuncRef,
host_state: Box<dyn Any + Send + Sync>,
}
#[test]
fn vmnative_call_host_func_context_offsets() {
use memoffset::offset_of;
use wasmtime_environ::{HostPtr, PtrSize};
assert_eq!(
usize::from(HostPtr.vmnative_call_host_func_context_func_ref()),
offset_of!(VMNativeCallHostFuncContext, func_ref)
);
}
impl VMNativeCallHostFuncContext {
pub unsafe fn new(
func_ref: VMFuncRef,
host_state: Box<dyn Any + Send + Sync>,
) -> StoreBox<VMNativeCallHostFuncContext> {
let ctx = StoreBox::new(VMNativeCallHostFuncContext {
magic: wasmtime_environ::VM_NATIVE_CALL_HOST_FUNC_MAGIC,
func_ref,
host_state,
});
let vmctx = VMOpaqueContext::from_vm_native_call_host_func_context(ctx.get());
unsafe {
(*ctx.get()).func_ref.vmctx = vmctx;
}
ctx
}
#[inline]
pub fn host_state(&self) -> &(dyn Any + Send + Sync) {
&*self.host_state
}
#[inline]
pub fn func_ref(&self) -> &VMFuncRef {
&self.func_ref
}
#[inline]
pub unsafe fn from_opaque(opaque: *mut VMOpaqueContext) -> *mut VMNativeCallHostFuncContext {
debug_assert_eq!((*opaque).magic, VM_NATIVE_CALL_HOST_FUNC_MAGIC);
opaque.cast()
}
}