diff --git a/lib/deprecated/runtime-core/src/instance.rs b/lib/deprecated/runtime-core/src/instance.rs index 0fc5c75de..26a6a6709 100644 --- a/lib/deprecated/runtime-core/src/instance.rs +++ b/lib/deprecated/runtime-core/src/instance.rs @@ -3,6 +3,7 @@ use std::error::Error; pub use new::wasmer::Exports; +#[derive(Debug)] pub(crate) struct PreInstance { pub(crate) vmctx: vm::Ctx, } diff --git a/lib/deprecated/runtime-core/src/module.rs b/lib/deprecated/runtime-core/src/module.rs index 03aa1176b..05b44172b 100644 --- a/lib/deprecated/runtime-core/src/module.rs +++ b/lib/deprecated/runtime-core/src/module.rs @@ -3,12 +3,13 @@ use crate::{ error::InstantiationError, import::{ImportObject, Namespace}, instance::{Instance, PreInstance}, - new, + new, vm, }; use new::wasmer_runtime::Export; use std::{ collections::HashMap, convert::{AsRef, Infallible}, + ptr, }; pub use new::wasm_common::{DataInitializer, ExportIndex}; @@ -49,8 +50,29 @@ impl Module { .into_iter() .map(|((namespace, name), export)| match export { Export::Function(mut function) => { - if function.vmctx.is_null() { - // That's an ugly hack. Go your way :-]. + // That's an ugly hack. Go your way :-]. + { + // An empty `vm::Ctx` was created in + // `Func` as a host function + // environment. The internals of + // `new::wasmer::externals::Function` + // passes the environment inside the + // `VMContext` pointer. That's another + // hack. This empty `vm::Ctx` must be + // replaced by another `vm::Ctx` value + // owned by `Instance`, because that's the + // only way to update this structure + // correctly for compatibility + // purposes. Before doing this operation, + // we must drop the empty `vm::Ctx` first. + unsafe { + ptr::drop_in_place::(function.vmctx as _); + } + + // And now we can update the pointer to + // `VMContext`, which is actually a + // `vm::Ctx` pointer, to fallback on the + // environment hack. function.vmctx = pre_instance.vmctx_ptr() as _; } diff --git a/lib/deprecated/runtime-core/src/typed_func.rs b/lib/deprecated/runtime-core/src/typed_func.rs index 97f73e43c..115d6c85d 100644 --- a/lib/deprecated/runtime-core/src/typed_func.rs +++ b/lib/deprecated/runtime-core/src/typed_func.rs @@ -5,7 +5,6 @@ use crate::{ types::{Type, Value}, vm, }; -use std::ptr; pub struct Func { new_function: new::wasmer::Function, @@ -14,19 +13,21 @@ pub struct Func { impl Func { pub fn new(func: F) -> Self where - F: new::wasm_common::HostFunction, - Args: new::wasm_common::WasmTypeList, - Rets: new::wasm_common::WasmTypeList, + F: new::wasmer::HostFunction, + Args: new::wasmer::WasmTypeList, + Rets: new::wasmer::WasmTypeList, { - // Create a fake `vm::Ctx`, that is going to be overwritten by `Instance::new`. - let ctx: &mut vm::Ctx = unsafe { &mut *ptr::null_mut() }; + // Create an empty `vm::Ctx`, that is going to be overwritten by `Instance::new`. + let ctx = vm::Ctx::new(); // TODO: check this, is incorrect. We should have a global store as we have in the // wasmer C API. let store = Default::default(); Self { - new_function: new::wasmer::Function::new_env(&store, ctx, func), + new_function: new::wasmer::Function::new_env::( + &store, ctx, func, + ), } } diff --git a/lib/deprecated/runtime-core/src/types.rs b/lib/deprecated/runtime-core/src/types.rs index c2c86511e..6058de266 100644 --- a/lib/deprecated/runtime-core/src/types.rs +++ b/lib/deprecated/runtime-core/src/types.rs @@ -26,9 +26,8 @@ pub use new::wasm_common::{ TableType as TableDescriptor, Type, ValueType, - WasmExternType, }; -pub use new::wasmer::Val as Value; +pub use new::wasmer::{Val as Value, WasmExternType}; #[derive(Debug, Copy, Clone, Eq, PartialEq)] pub struct GlobalDescriptor {