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

Make Function Env immutable #1663

Merged
merged 11 commits into from
Nov 20, 2020
24 changes: 12 additions & 12 deletions lib/api/src/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::NativeFunc;
use crate::RuntimeError;
pub use inner::{FromToNativeWasmType, HostFunction, WasmTypeList, WithEnv, WithoutEnv};
#[cfg(feature = "deprecated")]
pub use inner::{LegacyEnv, WithLegacyEnv};
pub use inner::{UnsafeMutableEnv, WithUnsafeMutableEnv};

use std::cmp::max;
use std::fmt;
Expand Down Expand Up @@ -258,22 +258,22 @@ impl Function {

/// Function used by the deprecated API to call a function with a `&mut` Env.
///
/// This is not a stable API and may be broken at any time.
/// This is not a stable API and may be broken at any time.
///
/// # Safety
/// - This function is only safe to use from the deprecated API.
#[doc(hidden)]
#[cfg(feature = "deprecated")]
pub unsafe fn new_native_with_env_legacy<F, Args, Rets, Env>(
pub unsafe fn new_native_with_unsafe_mutable_env<F, Args, Rets, Env>(
store: &Store,
env: Env,
func: F,
) -> Self
where
F: HostFunction<Args, Rets, WithLegacyEnv, Env>,
F: HostFunction<Args, Rets, WithUnsafeMutableEnv, Env>,
Args: WasmTypeList,
Rets: WasmTypeList,
Env: LegacyEnv + 'static,
Env: UnsafeMutableEnv + 'static,
{
let function = inner::Function::<Args, Rets>::new(func);
let address = function.address();
Expand Down Expand Up @@ -1026,7 +1026,7 @@ mod inner {
/// Marks an environment as being passed by `&mut`.
#[cfg(feature = "deprecated")]
#[doc(hidden)]
pub unsafe trait LegacyEnv: Sized {}
pub unsafe trait UnsafeMutableEnv: Sized {}

/// Empty trait to specify the kind of `HostFunction`: With or
/// without an environment.
Expand All @@ -1044,16 +1044,16 @@ mod inner {
impl HostFunctionKind for WithEnv {}

/// An empty struct to help Rust typing to determine
/// when a `HostFunction` does have an environment.
/// when a `HostFunction` has an environment.
///
/// This environment is passed by `&mut` and exists soley for the deprecated
/// This environment is passed by `&mut` and exists solely for the deprecated
/// API.
#[cfg(feature = "deprecated")]
#[doc(hidden)]
pub struct WithLegacyEnv;
pub struct WithUnsafeMutableEnv;

#[cfg(feature = "deprecated")]
impl HostFunctionKind for WithLegacyEnv {}
impl HostFunctionKind for WithUnsafeMutableEnv {}

/// An empty struct to help Rust typing to determine
/// when a `HostFunction` does not have an environment.
Expand Down Expand Up @@ -1286,14 +1286,14 @@ mod inner {
#[cfg(feature = "deprecated")]
#[allow(unused_parens)]
impl< $( $x, )* Rets, RetsAsResult, Env, Func >
HostFunction<( $( $x ),* ), Rets, WithLegacyEnv, Env>
HostFunction<( $( $x ),* ), Rets, WithUnsafeMutableEnv, Env>
for
Func
where
$( $x: FromToNativeWasmType, )*
Rets: WasmTypeList,
RetsAsResult: IntoResult<Rets>,
Env: LegacyEnv,
Env: UnsafeMutableEnv,
Func: Fn(&mut Env, $( $x , )*) -> RetsAsResult + Send + 'static,
{
#[allow(non_snake_case)]
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/externals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use self::function::{
};

#[cfg(feature = "deprecated")]
pub use self::function::{LegacyEnv, WithLegacyEnv};
pub use self::function::{UnsafeMutableEnv, WithUnsafeMutableEnv};
pub use self::global::Global;
pub use self::memory::Memory;
pub use self::table::Table;
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub mod internals {
//! they might change frequently or be removed in the future.

#[cfg(feature = "deprecated")]
pub use crate::externals::{LegacyEnv, WithLegacyEnv};
pub use crate::externals::{UnsafeMutableEnv, WithUnsafeMutableEnv};
pub use crate::externals::{WithEnv, WithoutEnv};
}

Expand Down
18 changes: 9 additions & 9 deletions lib/c-api/src/deprecated/import/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ pub(crate) struct CAPIImportObject {
/// but the new/current API does not. So we store them here to pass them to the Instance
/// to allow functions to access this data for backwards compatibilty.
pub(crate) imported_memories: Vec<*mut Memory>,
/// List of pointers to `LegacyEnv`s used to patch imported functions to be able to
/// List of pointers to `UnsafeMutableEnv`s used to patch imported functions to be able to
/// pass a the "vmctx" as the first argument.
/// Needed here because of extending import objects.
pub(crate) instance_pointers_to_update: Vec<NonNull<LegacyEnv>>,
pub(crate) instance_pointers_to_update: Vec<NonNull<UnsafeMutableEnv>>,
}

#[repr(C)]
Expand Down Expand Up @@ -399,7 +399,7 @@ pub unsafe extern "C" fn wasmer_import_object_imports_destroy(
let function_wrapper: Box<FunctionWrapper> =
Box::from_raw(import.value.func as *mut _);
let _: Box<Function> = Box::from_raw(function_wrapper.func.as_ptr());
let _: Box<LegacyEnv> = Box::from_raw(function_wrapper.legacy_env.as_ptr());
let _: Box<UnsafeMutableEnv> = Box::from_raw(function_wrapper.legacy_env.as_ptr());
}
wasmer_import_export_kind::WASM_GLOBAL => {
let _: Box<Global> = Box::from_raw(import.value.global as *mut _);
Expand Down Expand Up @@ -635,25 +635,25 @@ pub unsafe extern "C" fn wasmer_import_func_params_arity(

/// struct used to pass in context to functions (which must be back-patched)
#[derive(Debug, Default)]
pub(crate) struct LegacyEnv {
pub(crate) struct UnsafeMutableEnv {
pub(crate) instance_ptr: Option<NonNull<CAPIInstance>>,
}

impl LegacyEnv {
impl UnsafeMutableEnv {
pub(crate) fn ctx_ptr(&self) -> *mut CAPIInstance {
self.instance_ptr
.map(|p| p.as_ptr())
.unwrap_or(ptr::null_mut())
}
}

/// struct used to hold on to `LegacyEnv` pointer as well as the function.
/// we need to do this to initialize the context ptr inside of `LegacyEnv` when
/// struct used to hold on to `UnsafeMutableEnv` pointer as well as the function.
/// we need to do this to initialize the context ptr inside of `UnsafeMutableEnv` when
/// instantiating the module.
#[derive(Debug)]
pub(crate) struct FunctionWrapper {
pub(crate) func: NonNull<Function>,
pub(crate) legacy_env: NonNull<LegacyEnv>,
pub(crate) legacy_env: NonNull<UnsafeMutableEnv>,
}

/// Creates new host function, aka imported function. `func` is a
Expand Down Expand Up @@ -690,7 +690,7 @@ pub unsafe extern "C" fn wasmer_import_func_new(

let store = get_global_store();

let env_ptr = Box::into_raw(Box::new(LegacyEnv::default()));
let env_ptr = Box::into_raw(Box::new(UnsafeMutableEnv::default()));

let func = Function::new_with_env(store, &func_type, &mut *env_ptr, move |env, args| {
use libffi::high::call::{call, Arg};
Expand Down
3 changes: 0 additions & 3 deletions lib/c-api/wasmer_wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
# define DEPRECATED(message) __declspec(deprecated(message))
#endif

// The `jit` feature has been enabled for this build.
#define WASMER_JIT_ENABLED

MarkMcCaskey marked this conversation as resolved.
Show resolved Hide resolved
// The `compiler` feature has been enabled for this build.
#define WASMER_COMPILER_ENABLED

Expand Down
6 changes: 3 additions & 3 deletions lib/deprecated/runtime-core/src/typed_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
};
use std::marker::PhantomData;

pub use new::wasmer::internals::LegacyEnv;
pub use new::wasmer::internals::UnsafeMutableEnv;
pub use new::wasmer::{HostFunction, WasmTypeList};

/// Represents a function that can be used by WebAssembly.
Expand All @@ -28,14 +28,14 @@ where
/// Creates a new `Func`.
pub fn new<F>(func: F) -> Self
where
F: HostFunction<Args, Rets, new::wasmer::internals::WithLegacyEnv, vm::Ctx> + Send,
F: HostFunction<Args, Rets, new::wasmer::internals::WithUnsafeMutableEnv, vm::Ctx> + Send,
{
// Create an empty `vm::Ctx`, that is going to be overwritten by `Instance::new`.
let ctx = unsafe { vm::Ctx::new_uninit() };

Self {
new_function: unsafe {
new::wasmer::Function::new_native_with_env_legacy::<F, Args, Rets, vm::Ctx>(
new::wasmer::Function::new_native_with_unsafe_mutable_env::<F, Args, Rets, vm::Ctx>(
&get_global_store(),
ctx,
func,
Expand Down
4 changes: 2 additions & 2 deletions lib/deprecated/runtime-core/src/vm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{module::ModuleInfo, new};
use std::{ffi::c_void, ptr};

use new::wasmer::internals::LegacyEnv;
use new::wasmer::internals::UnsafeMutableEnv;

/// The context of the currently running WebAssembly instance.
///
Expand Down Expand Up @@ -41,7 +41,7 @@ pub struct Ctx {
}

/// We mark `Ctx` as a legacy env that can be passed by `&mut`.
unsafe impl LegacyEnv for Ctx {}
unsafe impl UnsafeMutableEnv for Ctx {}

impl Ctx {
pub(crate) unsafe fn new_uninit() -> Self {
Expand Down