Make Function Env immutable

Work in progress, we still have to update Emscripten
This commit is contained in:
Mark McCaskey
2020-09-30 17:30:17 -07:00
parent d9246398b9
commit a97f339687
11 changed files with 82 additions and 95 deletions

View File

@ -6,7 +6,6 @@ use crate::FunctionType;
use crate::NativeFunc;
use crate::RuntimeError;
pub use inner::{FromToNativeWasmType, HostFunction, WasmTypeList, WithEnv, WithoutEnv};
use std::cell::RefCell;
use std::cmp::max;
use std::fmt;
use wasmer_vm::{
@ -122,11 +121,11 @@ impl Function {
#[allow(clippy::cast_ptr_alignment)]
pub fn new_with_env<F, Env>(store: &Store, ty: &FunctionType, env: Env, func: F) -> Self
where
F: Fn(&mut Env, &[Val]) -> Result<Vec<Val>, RuntimeError> + 'static,
F: Fn(&Env, &[Val]) -> Result<Vec<Val>, RuntimeError> + 'static,
Env: Sized + 'static,
{
let dynamic_ctx = VMDynamicFunctionContext::from_context(VMDynamicFunctionWithEnv {
env: RefCell::new(env),
env: Box::new(env),
func: Box::new(func),
function_type: ty.clone(),
});
@ -205,7 +204,7 @@ impl Function {
/// };
/// let env = Env { multiplier: 2 };
///
/// fn sum_and_multiply(env: &mut Env, a: i32, b: i32) -> i32 {
/// fn sum_and_multiply(env: &Env, a: i32, b: i32) -> i32 {
/// (a + b) * env.multiplier
/// }
///
@ -227,7 +226,7 @@ impl Function {
// In the case of Host-defined functions `VMContext` is whatever environment
// the user want to attach to the function.
let box_env = Box::new(env);
let vmctx = Box::into_raw(box_env) as *mut _ as *mut VMContext;
let vmctx = Box::into_raw(box_env) as *mut _ as *const VMContext;
let signature = function.ty();
Self {
@ -469,8 +468,8 @@ where
{
function_type: FunctionType,
#[allow(clippy::type_complexity)]
func: Box<dyn Fn(&mut Env, &[Val]) -> Result<Vec<Val>, RuntimeError> + 'static>,
env: RefCell<Env>,
func: Box<dyn Fn(&Env, &[Val]) -> Result<Vec<Val>, RuntimeError> + 'static>,
env: Box<Env>,
}
impl<Env> VMDynamicFunction for VMDynamicFunctionWithEnv<Env>
@ -478,9 +477,7 @@ where
Env: Sized + 'static,
{
fn call(&self, args: &[Val]) -> Result<Vec<Val>, RuntimeError> {
// TODO: the `&mut *self.env.as_ptr()` is likely invoking some "mild"
// undefined behavior due to how it's used in the static fn call
unsafe { (*self.func)(&mut *self.env.as_ptr(), &args) }
(*self.func)(&*self.env, &args)
}
fn function_type(&self) -> &FunctionType {
&self.function_type
@ -1015,20 +1012,20 @@ mod inner {
Rets: WasmTypeList,
RetsAsResult: IntoResult<Rets>,
Env: Sized,
Func: Fn(&mut Env, $( $x , )*) -> RetsAsResult + Send + 'static,
Func: Fn(&Env, $( $x , )*) -> RetsAsResult + Send + 'static,
{
#[allow(non_snake_case)]
fn function_body_ptr(self) -> *const VMFunctionBody {
/// This is a function that wraps the real host
/// function. Its address will be used inside the
/// runtime.
extern fn func_wrapper<$( $x, )* Rets, RetsAsResult, Env, Func>( env: &mut Env, $( $x: $x::Native, )* ) -> Rets::CStruct
extern fn func_wrapper<$( $x, )* Rets, RetsAsResult, Env, Func>( env: &Env, $( $x: $x::Native, )* ) -> Rets::CStruct
where
$( $x: FromToNativeWasmType, )*
Rets: WasmTypeList,
RetsAsResult: IntoResult<Rets>,
Env: Sized,
Func: Fn(&mut Env, $( $x ),* ) -> RetsAsResult + 'static
Func: Fn(&Env, $( $x ),* ) -> RetsAsResult + 'static
{
let func: &Func = unsafe { &*(&() as *const () as *const Func) };

View File

@ -26,7 +26,7 @@ pub struct NativeFunc<'a, Args = (), Rets = ()> {
definition: FunctionDefinition,
store: Store,
address: *const VMFunctionBody,
vmctx: *mut VMContext,
vmctx: *const VMContext,
arg_kind: VMFunctionKind,
// exported: ExportFunction,
_phantom: PhantomData<(&'a (), Args, Rets)>,
@ -42,7 +42,7 @@ where
pub(crate) fn new(
store: Store,
address: *const VMFunctionBody,
vmctx: *mut VMContext,
vmctx: *const VMContext,
arg_kind: VMFunctionKind,
definition: FunctionDefinition,
) -> Self {
@ -163,7 +163,7 @@ macro_rules! impl_native_traits {
match self.arg_kind {
VMFunctionKind::Static => {
let results = catch_unwind(AssertUnwindSafe(|| unsafe {
let f = std::mem::transmute::<_, unsafe extern "C" fn( *mut VMContext, $( $x, )*) -> Rets::CStruct>(self.address);
let f = std::mem::transmute::<_, unsafe extern "C" fn( *const VMContext, $( $x, )*) -> Rets::CStruct>(self.address);
// We always pass the vmctx
f( self.vmctx, $( $x, )* )
})).map_err(|e| RuntimeError::new(format!("{:?}", e)))?;

View File

@ -30,7 +30,7 @@ pub struct ExportFunction {
/// The address of the native-code function.
pub address: *const VMFunctionBody,
/// Pointer to the containing `VMContext`.
pub vmctx: *mut VMContext,
pub vmctx: *const VMContext,
/// The function type, used for compatibility checking.
pub signature: FunctionType,
/// The function kind (it defines how it's the signature that provided `address` have)

View File

@ -272,8 +272,8 @@ impl Instance {
}
/// Return a raw pointer to the vmctx used by compiled wasm code.
pub fn vmctx_ptr(&self) -> *mut VMContext {
self.vmctx() as *const VMContext as *mut VMContext
pub fn vmctx_ptr(&self) -> *const VMContext {
self.vmctx() as *const VMContext
}
/// Lookup an export with the given name.
@ -384,7 +384,7 @@ impl Instance {
// Make the call.
unsafe {
catch_traps(callee_vmctx, || {
mem::transmute::<*const VMFunctionBody, unsafe extern "C" fn(*mut VMContext)>(
mem::transmute::<*const VMFunctionBody, unsafe extern "C" fn(*const VMContext)>(
callee_address,
)(callee_vmctx)
})
@ -940,7 +940,7 @@ impl InstanceHandle {
/// # Safety
/// This is unsafe because it doesn't work on just any `VMContext`, it must
/// be a `VMContext` allocated as part of an `Instance`.
pub unsafe fn from_vmctx(vmctx: *mut VMContext) -> Self {
pub unsafe fn from_vmctx(vmctx: *const VMContext) -> Self {
let instance = (&*vmctx).instance();
Self {
@ -954,7 +954,7 @@ impl InstanceHandle {
}
/// Return a raw pointer to the vmctx used by compiled wasm code.
pub fn vmctx_ptr(&self) -> *mut VMContext {
pub fn vmctx_ptr(&self) -> *const VMContext {
self.instance().vmctx_ptr()
}

View File

@ -429,13 +429,13 @@ impl Trap {
/// Wildly unsafe because it calls raw function pointers and reads/writes raw
/// function pointers.
pub unsafe fn wasmer_call_trampoline(
vmctx: *mut VMContext,
vmctx: *const VMContext,
trampoline: VMTrampoline,
callee: *const VMFunctionBody,
values_vec: *mut u8,
) -> Result<(), Trap> {
catch_traps(vmctx, || {
mem::transmute::<_, extern "C" fn(*mut VMContext, *const VMFunctionBody, *mut u8)>(
mem::transmute::<_, extern "C" fn(*const VMContext, *const VMFunctionBody, *mut u8)>(
trampoline,
)(vmctx, callee, values_vec)
})
@ -447,7 +447,7 @@ pub unsafe fn wasmer_call_trampoline(
/// # Safety
///
/// Highly unsafe since `closure` won't have any destructors run.
pub unsafe fn catch_traps<F>(vmctx: *mut VMContext, mut closure: F) -> Result<(), Trap>
pub unsafe fn catch_traps<F>(vmctx: *const VMContext, mut closure: F) -> Result<(), Trap>
where
F: FnMut(),
{
@ -501,7 +501,7 @@ pub struct CallThreadState {
jmp_buf: Cell<*const u8>,
reset_guard_page: Cell<bool>,
prev: Option<*const CallThreadState>,
vmctx: *mut VMContext,
vmctx: *const VMContext,
handling_trap: Cell<bool>,
}
@ -518,7 +518,7 @@ enum UnwindReason {
}
impl CallThreadState {
fn new(vmctx: *mut VMContext) -> Self {
fn new(vmctx: *const VMContext) -> Self {
Self {
unwind: Cell::new(UnwindReason::None),
vmctx,

View File

@ -23,7 +23,7 @@ pub struct VMFunctionImport {
pub body: *const VMFunctionBody,
/// A pointer to the `VMContext` that owns the function.
pub vmctx: *mut VMContext,
pub vmctx: *const VMContext,
}
#[cfg(test)]
@ -729,7 +729,7 @@ pub struct VMCallerCheckedAnyfunc {
/// Function signature id.
pub type_index: VMSharedSignatureIndex,
/// Function `VMContext`.
pub vmctx: *mut VMContext,
pub vmctx: *const VMContext,
// If more elements are added here, remember to add offset_of tests below!
}

View File

@ -174,7 +174,7 @@ impl WasiEnv {
}
pub(crate) fn get_memory_and_wasi_state(
&mut self,
&self,
_mem_index: u32,
) -> (&Memory, MutexGuard<WasiState>) {
let memory = self.memory();

View File

@ -10,7 +10,7 @@ use crate::WasiEnv;
/// Wasm memory. If the memory clobbered by the current syscall is also used by
/// that syscall, then it may break.
pub fn fd_filestat_get(
env: &mut WasiEnv,
env: &WasiEnv,
fd: types::__wasi_fd_t,
buf: WasmPtr<snapshot0::__wasi_filestat_t>,
) -> types::__wasi_errno_t {
@ -61,7 +61,7 @@ pub fn fd_filestat_get(
/// Wrapper around `syscalls::path_filestat_get` with extra logic to handle the size
/// difference of `wasi_filestat_t`
pub fn path_filestat_get(
env: &mut WasiEnv,
env: &WasiEnv,
fd: types::__wasi_fd_t,
flags: types::__wasi_lookupflags_t,
path: WasmPtr<u8, Array>,
@ -100,7 +100,7 @@ pub fn path_filestat_get(
/// Wrapper around `syscalls::fd_seek` with extra logic to remap the values
/// of `__wasi_whence_t`
pub fn fd_seek(
env: &mut WasiEnv,
env: &WasiEnv,
fd: types::__wasi_fd_t,
offset: types::__wasi_filedelta_t,
whence: snapshot0::__wasi_whence_t,
@ -119,7 +119,7 @@ pub fn fd_seek(
/// Wrapper around `syscalls::poll_oneoff` with extra logic to add the removed
/// userdata field back
pub fn poll_oneoff(
env: &mut WasiEnv,
env: &WasiEnv,
in_: WasmPtr<snapshot0::__wasi_subscription_t, Array>,
out_: WasmPtr<types::__wasi_event_t, Array>,
nsubscriptions: u32,

View File

@ -135,7 +135,7 @@ fn get_current_time_in_nanos() -> Result<__wasi_timestamp_t, __wasi_errno_t> {
/// A pointer to a buffer to write the argument string data.
///
pub fn args_get(
env: &mut WasiEnv,
env: &WasiEnv,
argv: WasmPtr<WasmPtr<u8, Array>, Array>,
argv_buf: WasmPtr<u8, Array>,
) -> __wasi_errno_t {
@ -170,7 +170,7 @@ pub fn args_get(
/// - `size_t *argv_buf_size`
/// The size of the argument string data.
pub fn args_sizes_get(
env: &mut WasiEnv,
env: &WasiEnv,
argc: WasmPtr<u32>,
argv_buf_size: WasmPtr<u32>,
) -> __wasi_errno_t {
@ -199,7 +199,7 @@ pub fn args_sizes_get(
/// - `__wasi_timestamp_t *resolution`
/// The resolution of the clock in nanoseconds
pub fn clock_res_get(
env: &mut WasiEnv,
env: &WasiEnv,
clock_id: __wasi_clockid_t,
resolution: WasmPtr<__wasi_timestamp_t>,
) -> __wasi_errno_t {
@ -221,7 +221,7 @@ pub fn clock_res_get(
/// - `__wasi_timestamp_t *time`
/// The value of the clock in nanoseconds
pub fn clock_time_get(
env: &mut WasiEnv,
env: &WasiEnv,
clock_id: __wasi_clockid_t,
precision: __wasi_timestamp_t,
time: WasmPtr<__wasi_timestamp_t>,
@ -251,7 +251,7 @@ pub fn clock_time_get(
/// - `char *environ_buf`
/// A pointer to a buffer to write the environment variable string data.
pub fn environ_get(
env: &mut WasiEnv,
env: &WasiEnv,
environ: WasmPtr<WasmPtr<u8, Array>, Array>,
environ_buf: WasmPtr<u8, Array>,
) -> __wasi_errno_t {
@ -269,7 +269,7 @@ pub fn environ_get(
/// - `size_t *environ_buf_size`
/// The size of the environment variable string data.
pub fn environ_sizes_get(
env: &mut WasiEnv,
env: &WasiEnv,
environ_count: WasmPtr<u32>,
environ_buf_size: WasmPtr<u32>,
) -> __wasi_errno_t {
@ -304,7 +304,7 @@ pub fn environ_sizes_get(
/// - `__wasi_advice_t advice`
/// The advice to give
pub fn fd_advise(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
offset: __wasi_filesize_t,
len: __wasi_filesize_t,
@ -327,7 +327,7 @@ pub fn fd_advise(
/// - `__wasi_filesize_t len`
/// The length from the offset marking the end of the allocation
pub fn fd_allocate(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
offset: __wasi_filesize_t,
len: __wasi_filesize_t,
@ -372,7 +372,7 @@ pub fn fd_allocate(
/// If `fd` is a directory
/// - `__WASI_EBADF`
/// If `fd` is invalid or not open
pub fn fd_close(env: &mut WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t {
pub fn fd_close(env: &WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t {
debug!("wasi::fd_close: fd={}", fd);
let (memory, mut state) = env.get_memory_and_wasi_state(0);
@ -388,7 +388,7 @@ pub fn fd_close(env: &mut WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t {
/// Inputs:
/// - `__wasi_fd_t fd`
/// The file descriptor to sync
pub fn fd_datasync(env: &mut WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t {
pub fn fd_datasync(env: &WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t {
debug!("wasi::fd_datasync");
let (memory, mut state) = env.get_memory_and_wasi_state(0);
let fd_entry = wasi_try!(state.fs.get_fd(fd));
@ -412,7 +412,7 @@ pub fn fd_datasync(env: &mut WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t {
/// - `__wasi_fdstat_t *buf`
/// The location where the metadata will be written
pub fn fd_fdstat_get(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
buf_ptr: WasmPtr<__wasi_fdstat_t>,
) -> __wasi_errno_t {
@ -440,7 +440,7 @@ pub fn fd_fdstat_get(
/// - `__wasi_fdflags_t flags`
/// The flags to apply to `fd`
pub fn fd_fdstat_set_flags(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
flags: __wasi_fdflags_t,
) -> __wasi_errno_t {
@ -466,7 +466,7 @@ pub fn fd_fdstat_set_flags(
/// - `__wasi_rights_t fs_rights_inheriting`
/// The inheriting rights to apply to `fd`
pub fn fd_fdstat_set_rights(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
fs_rights_base: __wasi_rights_t,
fs_rights_inheriting: __wasi_rights_t,
@ -497,7 +497,7 @@ pub fn fd_fdstat_set_rights(
/// - `__wasi_filestat_t *buf`
/// Where the metadata from `fd` will be written
pub fn fd_filestat_get(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
buf: WasmPtr<__wasi_filestat_t>,
) -> __wasi_errno_t {
@ -524,7 +524,7 @@ pub fn fd_filestat_get(
/// - `__wasi_filesize_t st_size`
/// New size that `fd` will be set to
pub fn fd_filestat_set_size(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
st_size: __wasi_filesize_t,
) -> __wasi_errno_t {
@ -566,7 +566,7 @@ pub fn fd_filestat_set_size(
/// - `__wasi_fstflags_t fst_flags`
/// Bit-vector for controlling which times get set
pub fn fd_filestat_set_times(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
st_atim: __wasi_timestamp_t,
st_mtim: __wasi_timestamp_t,
@ -639,7 +639,7 @@ pub fn fd_filestat_set_times(
/// - `size_t nread`
/// The number of bytes read
pub fn fd_pread(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
iovs: WasmPtr<__wasi_iovec_t, Array>,
iovs_len: u32,
@ -712,7 +712,7 @@ pub fn fd_pread(
/// - `__wasi_prestat *buf`
/// Where the metadata will be written
pub fn fd_prestat_get(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
buf: WasmPtr<__wasi_prestat_t>,
) -> __wasi_errno_t {
@ -727,7 +727,7 @@ pub fn fd_prestat_get(
}
pub fn fd_prestat_dir_name(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
path: WasmPtr<u8, Array>,
path_len: u32,
@ -787,7 +787,7 @@ pub fn fd_prestat_dir_name(
/// - `u32 *nwritten`
/// Number of bytes written
pub fn fd_pwrite(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
iovs: WasmPtr<__wasi_ciovec_t, Array>,
iovs_len: u32,
@ -873,7 +873,7 @@ pub fn fd_pwrite(
/// - `u32 *nread`
/// Number of bytes read
pub fn fd_read(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
iovs: WasmPtr<__wasi_iovec_t, Array>,
iovs_len: u32,
@ -956,7 +956,7 @@ pub fn fd_read(
/// The Number of bytes stored in `buf`; if less than `buf_len` then entire
/// directory has been read
pub fn fd_readdir(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
buf: WasmPtr<u8, Array>,
buf_len: u32,
@ -1072,7 +1072,7 @@ pub fn fd_readdir(
/// File descriptor to copy
/// - `__wasi_fd_t to`
/// Location to copy file descriptor to
pub fn fd_renumber(env: &mut WasiEnv, from: __wasi_fd_t, to: __wasi_fd_t) -> __wasi_errno_t {
pub fn fd_renumber(env: &WasiEnv, from: __wasi_fd_t, to: __wasi_fd_t) -> __wasi_errno_t {
debug!("wasi::fd_renumber: from={}, to={}", from, to);
let (memory, mut state) = env.get_memory_and_wasi_state(0);
let fd_entry = wasi_try!(state.fs.fd_map.get(&from).ok_or(__WASI_EBADF));
@ -1100,7 +1100,7 @@ pub fn fd_renumber(env: &mut WasiEnv, from: __wasi_fd_t, to: __wasi_fd_t) -> __w
/// - `__wasi_filesize_t *fd`
/// The new offset relative to the start of the file
pub fn fd_seek(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
offset: __wasi_filedelta_t,
whence: __wasi_whence_t,
@ -1168,7 +1168,7 @@ pub fn fd_seek(
/// TODO: figure out which errors this should return
/// - `__WASI_EPERM`
/// - `__WASI_ENOTCAPABLE`
pub fn fd_sync(env: &mut WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t {
pub fn fd_sync(env: &WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t {
debug!("wasi::fd_sync");
debug!("=> fd={}", fd);
let (memory, mut state) = env.get_memory_and_wasi_state(0);
@ -1203,7 +1203,7 @@ pub fn fd_sync(env: &mut WasiEnv, fd: __wasi_fd_t) -> __wasi_errno_t {
/// - `__wasi_filesize_t *offset`
/// The offset of `fd` relative to the start of the file
pub fn fd_tell(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
offset: WasmPtr<__wasi_filesize_t>,
) -> __wasi_errno_t {
@ -1237,7 +1237,7 @@ pub fn fd_tell(
/// Errors:
///
pub fn fd_write(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
iovs: WasmPtr<__wasi_ciovec_t, Array>,
iovs_len: u32,
@ -1333,7 +1333,7 @@ pub fn fd_write(
/// - __WASI_RIGHT_PATH_CREATE_DIRECTORY
/// This right must be set on the directory that the file is created in (TODO: verify that this is true)
pub fn path_create_directory(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
path: WasmPtr<u8, Array>,
path_len: u32,
@ -1436,7 +1436,7 @@ pub fn path_create_directory(
/// - `__wasi_file_stat_t *buf`
/// The location where the metadata will be stored
pub fn path_filestat_get(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
flags: __wasi_lookupflags_t,
path: WasmPtr<u8, Array>,
@ -1493,7 +1493,7 @@ pub fn path_filestat_get(
/// - `__wasi_fstflags_t fst_flags`
/// A bitmask controlling which attributes are set
pub fn path_filestat_set_times(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
flags: __wasi_lookupflags_t,
path: WasmPtr<u8, Array>,
@ -1581,7 +1581,7 @@ pub fn path_filestat_set_times(
/// - `u32 old_path_len`
/// Length of the `new_path` string
pub fn path_link(
env: &mut WasiEnv,
env: &WasiEnv,
old_fd: __wasi_fd_t,
old_flags: __wasi_lookupflags_t,
old_path: WasmPtr<u8, Array>,
@ -1664,7 +1664,7 @@ pub fn path_link(
/// Possible Errors:
/// - `__WASI_EACCES`, `__WASI_EBADF`, `__WASI_EFAULT`, `__WASI_EFBIG?`, `__WASI_EINVAL`, `__WASI_EIO`, `__WASI_ELOOP`, `__WASI_EMFILE`, `__WASI_ENAMETOOLONG?`, `__WASI_ENFILE`, `__WASI_ENOENT`, `__WASI_ENOTDIR`, `__WASI_EROFS`, and `__WASI_ENOTCAPABLE`
pub fn path_open(
env: &mut WasiEnv,
env: &WasiEnv,
dirfd: __wasi_fd_t,
dirflags: __wasi_lookupflags_t,
path: WasmPtr<u8, Array>,
@ -1901,7 +1901,7 @@ pub fn path_open(
/// - `u32 buf_used`
/// The number of bytes written to `buf`
pub fn path_readlink(
env: &mut WasiEnv,
env: &WasiEnv,
dir_fd: __wasi_fd_t,
path: WasmPtr<u8, Array>,
path_len: u32,
@ -1946,7 +1946,7 @@ pub fn path_readlink(
/// Returns __WASI_ENOTEMTPY if directory is not empty
pub fn path_remove_directory(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
path: WasmPtr<u8, Array>,
path_len: u32,
@ -2022,7 +2022,7 @@ pub fn path_remove_directory(
/// - `u32 new_path_len`
/// The number of bytes to read from `new_path`
pub fn path_rename(
env: &mut WasiEnv,
env: &WasiEnv,
old_fd: __wasi_fd_t,
old_path: WasmPtr<u8, Array>,
old_path_len: u32,
@ -2131,7 +2131,7 @@ pub fn path_rename(
/// - `u32 new_path_len`
/// The number of bytes to read from `new_path`
pub fn path_symlink(
env: &mut WasiEnv,
env: &WasiEnv,
old_path: WasmPtr<u8, Array>,
old_path_len: u32,
fd: __wasi_fd_t,
@ -2210,7 +2210,7 @@ pub fn path_symlink(
/// - `u32 path_len`
/// The number of bytes in the `path` array
pub fn path_unlink_file(
env: &mut WasiEnv,
env: &WasiEnv,
fd: __wasi_fd_t,
path: WasmPtr<u8, Array>,
path_len: u32,
@ -2304,7 +2304,7 @@ pub fn path_unlink_file(
/// - `u32 nevents`
/// The number of events seen
pub fn poll_oneoff(
env: &mut WasiEnv,
env: &WasiEnv,
in_: WasmPtr<__wasi_subscription_t, Array>,
out_: WasmPtr<__wasi_event_t, Array>,
nsubscriptions: u32,
@ -2490,13 +2490,13 @@ pub fn poll_oneoff(
__WASI_ESUCCESS
}
pub fn proc_exit(env: &mut WasiEnv, code: __wasi_exitcode_t) {
pub fn proc_exit(env: &WasiEnv, code: __wasi_exitcode_t) {
debug!("wasi::proc_exit, {}", code);
RuntimeError::raise(Box::new(WasiError::Exit(code)));
unreachable!();
}
pub fn proc_raise(env: &mut WasiEnv, sig: __wasi_signal_t) -> __wasi_errno_t {
pub fn proc_raise(env: &WasiEnv, sig: __wasi_signal_t) -> __wasi_errno_t {
debug!("wasi::proc_raise");
unimplemented!("wasi::proc_raise")
}
@ -2508,7 +2508,7 @@ pub fn proc_raise(env: &mut WasiEnv, sig: __wasi_signal_t) -> __wasi_errno_t {
/// A pointer to a buffer where the random bytes will be written
/// - `size_t buf_len`
/// The number of bytes that will be written
pub fn random_get(env: &mut WasiEnv, buf: WasmPtr<u8, Array>, buf_len: u32) -> __wasi_errno_t {
pub fn random_get(env: &WasiEnv, buf: WasmPtr<u8, Array>, buf_len: u32) -> __wasi_errno_t {
debug!("wasi::random_get buf_len: {}", buf_len);
let memory = env.memory();
@ -2526,14 +2526,14 @@ pub fn random_get(env: &mut WasiEnv, buf: WasmPtr<u8, Array>, buf_len: u32) -> _
/// ### `sched_yield()`
/// Yields execution of the thread
pub fn sched_yield(env: &mut WasiEnv) -> __wasi_errno_t {
pub fn sched_yield(env: &WasiEnv) -> __wasi_errno_t {
debug!("wasi::sched_yield");
::std::thread::yield_now();
__WASI_ESUCCESS
}
pub fn sock_recv(
env: &mut WasiEnv,
env: &WasiEnv,
sock: __wasi_fd_t,
ri_data: WasmPtr<__wasi_iovec_t, Array>,
ri_data_len: u32,
@ -2545,7 +2545,7 @@ pub fn sock_recv(
unimplemented!("wasi::sock_recv")
}
pub fn sock_send(
env: &mut WasiEnv,
env: &WasiEnv,
sock: __wasi_fd_t,
si_data: WasmPtr<__wasi_ciovec_t, Array>,
si_data_len: u32,
@ -2555,11 +2555,7 @@ pub fn sock_send(
debug!("wasi::sock_send");
unimplemented!("wasi::sock_send")
}
pub fn sock_shutdown(
env: &mut WasiEnv,
sock: __wasi_fd_t,
how: __wasi_sdflags_t,
) -> __wasi_errno_t {
pub fn sock_shutdown(env: &WasiEnv, sock: __wasi_fd_t, how: __wasi_sdflags_t) -> __wasi_errno_t {
debug!("wasi::sock_shutdown");
unimplemented!("wasi::sock_shutdown")
}

View File

@ -208,20 +208,20 @@ fn static_function_with_env() -> Result<()> {
&module,
&imports! {
"host" => {
"0" => Function::new_native_with_env(&store, env.clone(), |env: &mut Arc<AtomicUsize>| {
"0" => Function::new_native_with_env(&store, env.clone(), |env: &Arc<AtomicUsize>| {
assert_eq!(env.fetch_add(1, SeqCst), 0);
}),
"1" => Function::new_native_with_env(&store, env.clone(), |env: &mut Arc<AtomicUsize>, x: i32| -> i32 {
"1" => Function::new_native_with_env(&store, env.clone(), |env: &Arc<AtomicUsize>, x: i32| -> i32 {
assert_eq!(x, 0);
assert_eq!(env.fetch_add(1, SeqCst), 1);
1
}),
"2" => Function::new_native_with_env(&store, env.clone(), |env: &mut Arc<AtomicUsize>, x: i32, y: i64| {
"2" => Function::new_native_with_env(&store, env.clone(), |env: &Arc<AtomicUsize>, x: i32, y: i64| {
assert_eq!(x, 2);
assert_eq!(y, 3);
assert_eq!(env.fetch_add(1, SeqCst), 2);
}),
"3" => Function::new_native_with_env(&store, env.clone(), |env: &mut Arc<AtomicUsize>, a: i32, b: i64, c: i32, d: f32, e: f64| {
"3" => Function::new_native_with_env(&store, env.clone(), |env: &Arc<AtomicUsize>, a: i32, b: i64, c: i32, d: f32, e: f64| {
assert_eq!(a, 100);
assert_eq!(b, 200);
assert_eq!(c, 300);

View File

@ -85,20 +85,14 @@ fn static_host_function_without_env() -> anyhow::Result<()> {
fn static_host_function_with_env() -> anyhow::Result<()> {
let store = get_store();
fn f(env: &mut Env, a: i32, b: i64, c: f32, d: f64) -> (f64, f32, i64, i32) {
fn f(env: &Env, a: i32, b: i64, c: f32, d: f64) -> (f64, f32, i64, i32) {
assert_eq!(*env.0.borrow(), 100);
env.0.replace(101);
(d * 4.0, c * 3.0, b * 2, a * 1)
}
fn f_ok(
env: &mut Env,
a: i32,
b: i64,
c: f32,
d: f64,
) -> Result<(f64, f32, i64, i32), Infallible> {
fn f_ok(env: &Env, a: i32, b: i64, c: f32, d: f64) -> Result<(f64, f32, i64, i32), Infallible> {
assert_eq!(*env.0.borrow(), 100);
env.0.replace(101);