Add a host_state field to Instance.
This allows hosts to embed arbitrary state along with instances.
This commit is contained in:
@@ -174,6 +174,7 @@ impl CompiledModule {
|
|||||||
self.imports.clone(),
|
self.imports.clone(),
|
||||||
&data_initializers,
|
&data_initializers,
|
||||||
self.signatures.clone(),
|
self.signatures.clone(),
|
||||||
|
Box::new(()),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -214,6 +215,7 @@ pub fn instantiate(
|
|||||||
raw.imports,
|
raw.imports,
|
||||||
&*raw.data_initializers,
|
&*raw.data_initializers,
|
||||||
raw.signatures,
|
raw.signatures,
|
||||||
|
Box::new(()),
|
||||||
)
|
)
|
||||||
.map_err(SetupError::Instantiate)
|
.map_err(SetupError::Instantiate)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ use crate::vmcontext::{
|
|||||||
VMGlobalImport, VMMemoryDefinition, VMMemoryImport, VMSharedSignatureIndex, VMTableDefinition,
|
VMGlobalImport, VMMemoryDefinition, VMMemoryImport, VMSharedSignatureIndex, VMTableDefinition,
|
||||||
VMTableImport,
|
VMTableImport,
|
||||||
};
|
};
|
||||||
|
use core::any::Any;
|
||||||
use core::slice;
|
use core::slice;
|
||||||
use core::{mem, ptr};
|
use core::{mem, ptr};
|
||||||
use cranelift_entity::EntityRef;
|
use cranelift_entity::EntityRef;
|
||||||
@@ -22,6 +23,7 @@ use cranelift_wasm::{
|
|||||||
GlobalIndex, GlobalInit, MemoryIndex, SignatureIndex, TableIndex,
|
GlobalIndex, GlobalInit, MemoryIndex, SignatureIndex, TableIndex,
|
||||||
};
|
};
|
||||||
use std::borrow::ToOwned;
|
use std::borrow::ToOwned;
|
||||||
|
use std::boxed::Box;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::string::String;
|
use std::string::String;
|
||||||
use wasmtime_environ::{DataInitializer, Module, TableElements, VMOffsets};
|
use wasmtime_environ::{DataInitializer, Module, TableElements, VMOffsets};
|
||||||
@@ -85,7 +87,12 @@ pub(crate) struct InstanceContents {
|
|||||||
/// Pointers to functions in executable memory.
|
/// Pointers to functions in executable memory.
|
||||||
finished_functions: BoxedSlice<DefinedFuncIndex, *const VMFunctionBody>,
|
finished_functions: BoxedSlice<DefinedFuncIndex, *const VMFunctionBody>,
|
||||||
|
|
||||||
/// Context pointer used by compiled wasm code.
|
/// Hosts can store arbitrary per-instance information here.
|
||||||
|
host_state: Box<dyn Any>,
|
||||||
|
|
||||||
|
/// Context pointer used by compiled wasm code. This field is last, and
|
||||||
|
/// represents a dynamically-sized array that extends beyond the nominal
|
||||||
|
/// end of the struct (similar to a flexible array member).
|
||||||
vmctx: VMContext,
|
vmctx: VMContext,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -453,6 +460,7 @@ impl Instance {
|
|||||||
imports: Imports,
|
imports: Imports,
|
||||||
data_initializers: &[DataInitializer<'_>],
|
data_initializers: &[DataInitializer<'_>],
|
||||||
vmshared_signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>,
|
vmshared_signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>,
|
||||||
|
host_state: Box<dyn Any>,
|
||||||
) -> Result<Self, InstantiationError> {
|
) -> Result<Self, InstantiationError> {
|
||||||
let mut tables = create_tables(&module);
|
let mut tables = create_tables(&module);
|
||||||
let mut memories = create_memories(&module)?;
|
let mut memories = create_memories(&module)?;
|
||||||
@@ -498,6 +506,7 @@ impl Instance {
|
|||||||
memories,
|
memories,
|
||||||
tables,
|
tables,
|
||||||
finished_functions,
|
finished_functions,
|
||||||
|
host_state,
|
||||||
vmctx: VMContext {},
|
vmctx: VMContext {},
|
||||||
};
|
};
|
||||||
unsafe {
|
unsafe {
|
||||||
@@ -674,6 +683,11 @@ impl Instance {
|
|||||||
let temporary_mut = &mut *(self as *const Self as *mut Self);
|
let temporary_mut = &mut *(self as *const Self as *mut Self);
|
||||||
temporary_mut.lookup(field)
|
temporary_mut.lookup(field)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return a reference to the custom state attached to this instance.
|
||||||
|
pub fn host_state(&mut self) -> &mut Any {
|
||||||
|
return &mut *self.mmap_field.contents_mut().host_state;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_table_init_bounds(
|
fn check_table_init_bounds(
|
||||||
|
|||||||
@@ -2,9 +2,10 @@ use cranelift_codegen::ir::types;
|
|||||||
use cranelift_codegen::{ir, isa};
|
use cranelift_codegen::{ir, isa};
|
||||||
use cranelift_entity::PrimaryMap;
|
use cranelift_entity::PrimaryMap;
|
||||||
use cranelift_wasm::{DefinedFuncIndex, Global, GlobalInit, Memory, Table, TableElementType};
|
use cranelift_wasm::{DefinedFuncIndex, Global, GlobalInit, Memory, Table, TableElementType};
|
||||||
|
use std::rc::Rc;
|
||||||
use target_lexicon::HOST;
|
use target_lexicon::HOST;
|
||||||
use wasmtime_environ::{translate_signature, Export, MemoryPlan, Module, TablePlan};
|
use wasmtime_environ::{translate_signature, Export, MemoryPlan, Module, TablePlan};
|
||||||
use wasmtime_jit::{target_tunables, CompiledModule};
|
use wasmtime_jit::target_tunables;
|
||||||
use wasmtime_runtime::{Imports, Instance, InstantiationError, VMFunctionBody};
|
use wasmtime_runtime::{Imports, Instance, InstantiationError, VMFunctionBody};
|
||||||
|
|
||||||
extern "C" fn spectest_print() {}
|
extern "C" fn spectest_print() {}
|
||||||
@@ -213,12 +214,12 @@ pub fn instantiate_spectest() -> Result<Instance, InstantiationError> {
|
|||||||
let data_initializers = Vec::new();
|
let data_initializers = Vec::new();
|
||||||
let signatures = PrimaryMap::new();
|
let signatures = PrimaryMap::new();
|
||||||
|
|
||||||
CompiledModule::from_parts(
|
Instance::new(
|
||||||
module,
|
Rc::new(module),
|
||||||
finished_functions.into_boxed_slice(),
|
finished_functions.into_boxed_slice(),
|
||||||
imports,
|
imports,
|
||||||
data_initializers.into_boxed_slice(),
|
&data_initializers,
|
||||||
signatures.into_boxed_slice(),
|
signatures.into_boxed_slice(),
|
||||||
|
Box::new(()),
|
||||||
)
|
)
|
||||||
.instantiate()
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user