mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-13 05:48:45 +00:00
feat(deprecated) Fix host function, and drop the empty vm::Ctx correctly.
This commit is contained in:
@@ -3,6 +3,7 @@ use std::error::Error;
|
||||
|
||||
pub use new::wasmer::Exports;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct PreInstance {
|
||||
pub(crate) vmctx: vm::Ctx,
|
||||
}
|
||||
|
||||
@@ -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 :-].
|
||||
{
|
||||
// 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 _;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<F, Args, Rets>(func: F) -> Self
|
||||
where
|
||||
F: new::wasm_common::HostFunction<Args, Rets, new::wasm_common::WithEnv, vm::Ctx>,
|
||||
Args: new::wasm_common::WasmTypeList,
|
||||
Rets: new::wasm_common::WasmTypeList,
|
||||
F: new::wasmer::HostFunction<Args, Rets, new::wasmer::WithEnv, vm::Ctx>,
|
||||
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::<F, Args, Rets, vm::Ctx>(
|
||||
&store, ctx, func,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user