mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-16 17:18:57 +00:00
Address feedback
This commit is contained in:
@@ -105,7 +105,6 @@ impl<'a> WasmerEnv for &'a ::std::sync::atomic::AtomicU32 {}
|
||||
impl<'a> WasmerEnv for &'a ::std::sync::atomic::AtomicI64 {}
|
||||
impl<'a> WasmerEnv for &'a ::std::sync::atomic::AtomicUsize {}
|
||||
impl<'a> WasmerEnv for &'a ::std::sync::atomic::AtomicIsize {}
|
||||
//impl WasmerEnv for dyn ::std::any::Any + Clone {}
|
||||
impl<T: WasmerEnv> WasmerEnv for Box<T> {
|
||||
fn init_with_instance(&mut self, instance: &Instance) -> Result<(), HostEnvInitError> {
|
||||
(&mut **self).init_with_instance(instance)
|
||||
@@ -119,13 +118,6 @@ impl<T: WasmerEnv> WasmerEnv for ::std::sync::Arc<::std::sync::Mutex<T>> {
|
||||
}
|
||||
}
|
||||
|
||||
/*impl<T: WasmerEnv> WasmerEnv for &'static T {
|
||||
fn init_with_instance(&mut self, instance: &Instance) -> Result<(), HostEnvInitError> {
|
||||
T::init_with_instance()
|
||||
(*self).init_with_instance(instance)
|
||||
}
|
||||
}*/
|
||||
|
||||
/// Lazily init an item
|
||||
pub struct LazyInit<T: Sized> {
|
||||
/// The data to be initialized
|
||||
|
||||
40
lib/api/src/externals/function.rs
vendored
40
lib/api/src/externals/function.rs
vendored
@@ -88,8 +88,8 @@ impl Function {
|
||||
where
|
||||
F: Fn(&[Val]) -> Result<Vec<Val>, RuntimeError> + 'static,
|
||||
{
|
||||
let dynamic_ctx: VMDynamicFunctionContext<VMDynamicFunctionWithoutEnv> =
|
||||
VMDynamicFunctionContext::from_context(VMDynamicFunctionWithoutEnv {
|
||||
let dynamic_ctx: VMDynamicFunctionContext<DynamicFunctionWithoutEnv> =
|
||||
VMDynamicFunctionContext::from_context(DynamicFunctionWithoutEnv {
|
||||
func: Arc::new(func),
|
||||
function_type: ty.clone(),
|
||||
});
|
||||
@@ -100,16 +100,16 @@ impl Function {
|
||||
let host_env = Box::into_raw(Box::new(dynamic_ctx)) as *mut _;
|
||||
let vmctx = VMFunctionEnvironment { host_env };
|
||||
let host_env_clone_fn: fn(*mut std::ffi::c_void) -> *mut std::ffi::c_void = |ptr| {
|
||||
let duped_env: VMDynamicFunctionContext<VMDynamicFunctionWithoutEnv> = unsafe {
|
||||
let ptr: *mut VMDynamicFunctionContext<VMDynamicFunctionWithoutEnv> = ptr as _;
|
||||
let item: &VMDynamicFunctionContext<VMDynamicFunctionWithoutEnv> = &*ptr;
|
||||
let duped_env: VMDynamicFunctionContext<DynamicFunctionWithoutEnv> = unsafe {
|
||||
let ptr: *mut VMDynamicFunctionContext<DynamicFunctionWithoutEnv> = ptr as _;
|
||||
let item: &VMDynamicFunctionContext<DynamicFunctionWithoutEnv> = &*ptr;
|
||||
item.clone()
|
||||
};
|
||||
Box::into_raw(Box::new(duped_env)) as _
|
||||
};
|
||||
let host_env_drop_fn: fn(*mut std::ffi::c_void) = |ptr: *mut std::ffi::c_void| {
|
||||
unsafe {
|
||||
Box::from_raw(ptr as *mut VMDynamicFunctionContext<VMDynamicFunctionWithoutEnv>)
|
||||
Box::from_raw(ptr as *mut VMDynamicFunctionContext<DynamicFunctionWithoutEnv>)
|
||||
};
|
||||
};
|
||||
|
||||
@@ -162,8 +162,8 @@ impl Function {
|
||||
F: Fn(&Env, &[Val]) -> Result<Vec<Val>, RuntimeError> + 'static,
|
||||
Env: Sized + WasmerEnv + 'static,
|
||||
{
|
||||
let dynamic_ctx: VMDynamicFunctionContext<VMDynamicFunctionWithEnv<Env>> =
|
||||
VMDynamicFunctionContext::from_context(VMDynamicFunctionWithEnv {
|
||||
let dynamic_ctx: VMDynamicFunctionContext<DynamicFunctionWithEnv<Env>> =
|
||||
VMDynamicFunctionContext::from_context(DynamicFunctionWithEnv {
|
||||
env: {
|
||||
let e = Box::new(env);
|
||||
e
|
||||
@@ -179,7 +179,7 @@ impl Function {
|
||||
let vmctx = VMFunctionEnvironment { host_env };
|
||||
let import_init_function_ptr: fn(_, _) -> Result<(), _> =
|
||||
|ptr: *mut std::ffi::c_void, instance: *const std::ffi::c_void| {
|
||||
let ptr = ptr as *mut VMDynamicFunctionContext<VMDynamicFunctionWithEnv<Env>>;
|
||||
let ptr = ptr as *mut VMDynamicFunctionContext<DynamicFunctionWithEnv<Env>>;
|
||||
unsafe {
|
||||
let env = &mut *ptr;
|
||||
let env: &mut Env = &mut *env.ctx.env;
|
||||
@@ -193,16 +193,16 @@ impl Function {
|
||||
)
|
||||
});
|
||||
let host_env_clone_fn: fn(*mut std::ffi::c_void) -> *mut std::ffi::c_void = |ptr| {
|
||||
let duped_env: VMDynamicFunctionContext<VMDynamicFunctionWithEnv<Env>> = unsafe {
|
||||
let ptr: *mut VMDynamicFunctionContext<VMDynamicFunctionWithEnv<Env>> = ptr as _;
|
||||
let item: &VMDynamicFunctionContext<VMDynamicFunctionWithEnv<Env>> = &*ptr;
|
||||
let duped_env: VMDynamicFunctionContext<DynamicFunctionWithEnv<Env>> = unsafe {
|
||||
let ptr: *mut VMDynamicFunctionContext<DynamicFunctionWithEnv<Env>> = ptr as _;
|
||||
let item: &VMDynamicFunctionContext<DynamicFunctionWithEnv<Env>> = &*ptr;
|
||||
item.clone()
|
||||
};
|
||||
Box::into_raw(Box::new(duped_env)) as _
|
||||
};
|
||||
let host_env_drop_fn: fn(*mut std::ffi::c_void) = |ptr: *mut std::ffi::c_void| {
|
||||
unsafe {
|
||||
Box::from_raw(ptr as *mut VMDynamicFunctionContext<VMDynamicFunctionWithEnv<Env>>)
|
||||
Box::from_raw(ptr as *mut VMDynamicFunctionContext<DynamicFunctionWithEnv<Env>>)
|
||||
};
|
||||
};
|
||||
|
||||
@@ -795,13 +795,13 @@ pub(crate) trait VMDynamicFunction {
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct VMDynamicFunctionWithoutEnv {
|
||||
pub(crate) struct DynamicFunctionWithoutEnv {
|
||||
#[allow(clippy::type_complexity)]
|
||||
func: Arc<dyn Fn(&[Val]) -> Result<Vec<Val>, RuntimeError> + 'static>,
|
||||
function_type: FunctionType,
|
||||
}
|
||||
|
||||
impl VMDynamicFunction for VMDynamicFunctionWithoutEnv {
|
||||
impl VMDynamicFunction for DynamicFunctionWithoutEnv {
|
||||
fn call(&self, args: &[Val]) -> Result<Vec<Val>, RuntimeError> {
|
||||
(*self.func)(&args)
|
||||
}
|
||||
@@ -810,19 +810,17 @@ impl VMDynamicFunction for VMDynamicFunctionWithoutEnv {
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub(crate) struct VMDynamicFunctionWithEnv<Env>
|
||||
pub(crate) struct DynamicFunctionWithEnv<Env>
|
||||
where
|
||||
Env: Sized + 'static,
|
||||
{
|
||||
// This field _must_ come first in this struct.
|
||||
env: Box<Env>,
|
||||
function_type: FunctionType,
|
||||
#[allow(clippy::type_complexity)]
|
||||
func: Arc<dyn Fn(&Env, &[Val]) -> Result<Vec<Val>, RuntimeError> + 'static>,
|
||||
env: Box<Env>,
|
||||
}
|
||||
|
||||
impl<Env: Sized + Clone + 'static> Clone for VMDynamicFunctionWithEnv<Env> {
|
||||
impl<Env: Sized + Clone + 'static> Clone for DynamicFunctionWithEnv<Env> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
env: self.env.clone(),
|
||||
@@ -832,7 +830,7 @@ impl<Env: Sized + Clone + 'static> Clone for VMDynamicFunctionWithEnv<Env> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<Env> VMDynamicFunction for VMDynamicFunctionWithEnv<Env>
|
||||
impl<Env> VMDynamicFunction for DynamicFunctionWithEnv<Env>
|
||||
where
|
||||
Env: Sized + 'static,
|
||||
{
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use crate::externals::function::{
|
||||
FunctionDefinition, HostFunctionDefinition, VMDynamicFunction, VMDynamicFunctionWithEnv,
|
||||
VMDynamicFunctionWithoutEnv, WasmFunctionDefinition,
|
||||
DynamicFunctionWithEnv, DynamicFunctionWithoutEnv, FunctionDefinition, HostFunctionDefinition,
|
||||
VMDynamicFunction, WasmFunctionDefinition,
|
||||
};
|
||||
use crate::{FromToNativeWasmType, Function, FunctionType, RuntimeError, Store, WasmTypeList};
|
||||
use std::panic::{catch_unwind, AssertUnwindSafe};
|
||||
@@ -205,13 +205,13 @@ macro_rules! impl_native_traits {
|
||||
VMFunctionKind::Dynamic => {
|
||||
let params_list = [ $( $x.to_native().to_value() ),* ];
|
||||
let results = if !has_env {
|
||||
type VMContextWithoutEnv = VMDynamicFunctionContext<VMDynamicFunctionWithoutEnv>;
|
||||
type VMContextWithoutEnv = VMDynamicFunctionContext<DynamicFunctionWithoutEnv>;
|
||||
unsafe {
|
||||
let ctx = self.vmctx.host_env as *mut VMContextWithoutEnv;
|
||||
(*ctx).ctx.call(¶ms_list)?
|
||||
}
|
||||
} else {
|
||||
type VMContextWithEnv = VMDynamicFunctionContext<VMDynamicFunctionWithEnv<std::ffi::c_void>>;
|
||||
type VMContextWithEnv = VMDynamicFunctionContext<DynamicFunctionWithEnv<std::ffi::c_void>>;
|
||||
unsafe {
|
||||
let ctx = self.vmctx.host_env as *mut VMContextWithEnv;
|
||||
(*ctx).ctx.call(¶ms_list)?
|
||||
|
||||
@@ -46,11 +46,25 @@ impl From<VMExport> for Export {
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO: rename, etc.
|
||||
/// Extra metadata about `ExportFunction`s.
|
||||
///
|
||||
/// The metadata acts as a kind of manual virtual dispatch. We store the
|
||||
/// user-supplied `WasmerEnv` as a void pointer and have methods on it
|
||||
/// that have been adapted to accept a void pointer.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct ExportFunctionMetadata {
|
||||
/// duplicated here so we can free it....
|
||||
/// TODO: refactor all this stuff so it's less of a nightmare.
|
||||
/// This field is stored here to be accessible by `Drop`.
|
||||
///
|
||||
/// At the time it was added, it's not accessed anywhere outside of
|
||||
/// the `Drop` implementation. This field is the "master copy" of the env,
|
||||
/// that is, the original env passed in by the user. Every time we create
|
||||
/// an `Instance` we clone this with the `host_env_clone_fn` field.
|
||||
///
|
||||
/// Thus, we only bother to store the master copy at all here so that
|
||||
/// we can free it.
|
||||
///
|
||||
/// See `wasmer_vm::export::VMExportFunction::vmctx` for the version of
|
||||
/// this pointer that is used by the VM when creating an `Instance`.
|
||||
pub host_env: *mut std::ffi::c_void,
|
||||
/// Function pointer to `WasmerEnv::init_with_instance(&mut self, instance: &Instance)`.
|
||||
///
|
||||
@@ -69,7 +83,6 @@ pub struct ExportFunctionMetadata {
|
||||
// so all the `host_env`s freed at the `Instance` level won't touch the original.
|
||||
impl Drop for ExportFunctionMetadata {
|
||||
fn drop(&mut self) {
|
||||
dbg!("DROPPING ORIGINAL HOST ENV!");
|
||||
if !self.host_env.is_null() {
|
||||
(self.host_env_drop_fn)(self.host_env);
|
||||
}
|
||||
|
||||
@@ -357,7 +357,6 @@ fn multi_use_host_fn_manages_memory_correctly() -> Result<()> {
|
||||
|
||||
impl WasmerEnv for Env {
|
||||
fn init_with_instance(&mut self, instance: &Instance) -> Result<(), HostEnvInitError> {
|
||||
dbg!("Initing the env!");
|
||||
let memory = instance.exports.get_memory("memory")?.clone();
|
||||
self.memory.initialize(memory);
|
||||
Ok(())
|
||||
@@ -368,7 +367,6 @@ fn multi_use_host_fn_manages_memory_correctly() -> Result<()> {
|
||||
memory: LazyInit::default(),
|
||||
};
|
||||
fn host_fn(env: &Env) {
|
||||
dbg!(env as *const _);
|
||||
assert!(env.memory.get_ref().is_some());
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user