feat(deprecated) Fix host function, and drop the empty vm::Ctx correctly.

This commit is contained in:
Ivan Enderlin
2020-06-25 11:14:00 +02:00
parent 7050bd9f58
commit 25eb0af97d
4 changed files with 35 additions and 12 deletions

View File

@@ -3,6 +3,7 @@ use std::error::Error;
pub use new::wasmer::Exports; pub use new::wasmer::Exports;
#[derive(Debug)]
pub(crate) struct PreInstance { pub(crate) struct PreInstance {
pub(crate) vmctx: vm::Ctx, pub(crate) vmctx: vm::Ctx,
} }

View File

@@ -3,12 +3,13 @@ use crate::{
error::InstantiationError, error::InstantiationError,
import::{ImportObject, Namespace}, import::{ImportObject, Namespace},
instance::{Instance, PreInstance}, instance::{Instance, PreInstance},
new, new, vm,
}; };
use new::wasmer_runtime::Export; use new::wasmer_runtime::Export;
use std::{ use std::{
collections::HashMap, collections::HashMap,
convert::{AsRef, Infallible}, convert::{AsRef, Infallible},
ptr,
}; };
pub use new::wasm_common::{DataInitializer, ExportIndex}; pub use new::wasm_common::{DataInitializer, ExportIndex};
@@ -49,8 +50,29 @@ impl Module {
.into_iter() .into_iter()
.map(|((namespace, name), export)| match export { .map(|((namespace, name), export)| match export {
Export::Function(mut function) => { 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::<vm::Ctx>(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 _; function.vmctx = pre_instance.vmctx_ptr() as _;
} }

View File

@@ -5,7 +5,6 @@ use crate::{
types::{Type, Value}, types::{Type, Value},
vm, vm,
}; };
use std::ptr;
pub struct Func { pub struct Func {
new_function: new::wasmer::Function, new_function: new::wasmer::Function,
@@ -14,19 +13,21 @@ pub struct Func {
impl Func { impl Func {
pub fn new<F, Args, Rets>(func: F) -> Self pub fn new<F, Args, Rets>(func: F) -> Self
where where
F: new::wasm_common::HostFunction<Args, Rets, new::wasm_common::WithEnv, vm::Ctx>, F: new::wasmer::HostFunction<Args, Rets, new::wasmer::WithEnv, vm::Ctx>,
Args: new::wasm_common::WasmTypeList, Args: new::wasmer::WasmTypeList,
Rets: new::wasm_common::WasmTypeList, Rets: new::wasmer::WasmTypeList,
{ {
// Create a fake `vm::Ctx`, that is going to be overwritten by `Instance::new`. // Create an empty `vm::Ctx`, that is going to be overwritten by `Instance::new`.
let ctx: &mut vm::Ctx = unsafe { &mut *ptr::null_mut() }; let ctx = vm::Ctx::new();
// TODO: check this, is incorrect. We should have a global store as we have in the // TODO: check this, is incorrect. We should have a global store as we have in the
// wasmer C API. // wasmer C API.
let store = Default::default(); let store = Default::default();
Self { Self {
new_function: new::wasmer::Function::new_env(&store, ctx, func), new_function: new::wasmer::Function::new_env::<F, Args, Rets, vm::Ctx>(
&store, ctx, func,
),
} }
} }

View File

@@ -26,9 +26,8 @@ pub use new::wasm_common::{
TableType as TableDescriptor, TableType as TableDescriptor,
Type, Type,
ValueType, ValueType,
WasmExternType,
}; };
pub use new::wasmer::Val as Value; pub use new::wasmer::{Val as Value, WasmExternType};
#[derive(Debug, Copy, Clone, Eq, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct GlobalDescriptor { pub struct GlobalDescriptor {