1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use wasmtime_environ::VM_HOST_FUNC_MAGIC;
use super::{VMCallerCheckedAnyfunc, VMFunctionBody, VMOpaqueContext, VMSharedSignatureIndex};
use std::{
any::Any,
ptr::{self, NonNull},
};
#[repr(C)]
pub struct VMHostFuncContext {
magic: u32,
pub(crate) host_func: NonNull<VMFunctionBody>,
wasm_to_host_trampoline: VMCallerCheckedAnyfunc,
host_state: Box<dyn Any + Send + Sync>,
}
unsafe impl Send for VMHostFuncContext {}
unsafe impl Sync for VMHostFuncContext {}
impl VMHostFuncContext {
pub unsafe fn new(
host_func: NonNull<VMFunctionBody>,
signature: VMSharedSignatureIndex,
host_state: Box<dyn Any + Send + Sync>,
) -> Box<VMHostFuncContext> {
let wasm_to_host_trampoline = VMCallerCheckedAnyfunc {
func_ptr: NonNull::new(crate::trampolines::wasm_to_host_trampoline as _).unwrap(),
type_index: signature,
vmctx: ptr::null_mut(),
};
let mut ctx = Box::new(VMHostFuncContext {
magic: wasmtime_environ::VM_HOST_FUNC_MAGIC,
host_func,
wasm_to_host_trampoline,
host_state,
});
ctx.wasm_to_host_trampoline.vmctx =
VMOpaqueContext::from_vm_host_func_context(&*ctx as *const _ as *mut _);
ctx
}
pub fn wasm_to_host_trampoline(&self) -> NonNull<VMCallerCheckedAnyfunc> {
NonNull::from(&self.wasm_to_host_trampoline)
}
pub fn host_state(&self) -> &(dyn Any + Send + Sync) {
&*self.host_state
}
}
impl VMHostFuncContext {
#[inline]
pub unsafe fn from_opaque(opaque: *mut VMOpaqueContext) -> *mut VMHostFuncContext {
debug_assert_eq!((*opaque).magic, VM_HOST_FUNC_MAGIC);
opaque.cast()
}
}