[wasmtime-api] reduce examples complexity: hostref for store/engine (#489)
* reduce amount of store.clone() * use HostRef Engine as ref / use Engine::default()
This commit is contained in:
@@ -270,14 +270,14 @@ fn main() -> Result<()> {
|
|||||||
strategy,
|
strategy,
|
||||||
);
|
);
|
||||||
let engine = HostRef::new(Engine::new(config));
|
let engine = HostRef::new(Engine::new(config));
|
||||||
let store = HostRef::new(Store::new(engine));
|
let store = HostRef::new(Store::new(&engine));
|
||||||
|
|
||||||
let mut module_registry = HashMap::new();
|
let mut module_registry = HashMap::new();
|
||||||
|
|
||||||
// Make spectest available by default.
|
// Make spectest available by default.
|
||||||
module_registry.insert(
|
module_registry.insert(
|
||||||
"spectest".to_owned(),
|
"spectest".to_owned(),
|
||||||
Instance::from_handle(store.clone(), instantiate_spectest()?)?,
|
Instance::from_handle(&store, instantiate_spectest()?)?,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Make wasi available by default.
|
// Make wasi available by default.
|
||||||
@@ -301,36 +301,36 @@ fn main() -> Result<()> {
|
|||||||
|
|
||||||
module_registry.insert(
|
module_registry.insert(
|
||||||
"wasi_unstable".to_owned(),
|
"wasi_unstable".to_owned(),
|
||||||
Instance::from_handle(store.clone(), wasi.clone())?,
|
Instance::from_handle(&store, wasi.clone())?,
|
||||||
);
|
);
|
||||||
module_registry.insert(
|
module_registry.insert(
|
||||||
"wasi_unstable_preview0".to_owned(),
|
"wasi_unstable_preview0".to_owned(),
|
||||||
Instance::from_handle(store.clone(), wasi)?,
|
Instance::from_handle(&store, wasi)?,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Load the preload wasm modules.
|
// Load the preload wasm modules.
|
||||||
for filename in &args.flag_preload {
|
for filename in &args.flag_preload {
|
||||||
let path = Path::new(&filename);
|
let path = Path::new(&filename);
|
||||||
instantiate_module(store.clone(), &module_registry, path)
|
instantiate_module(&store, &module_registry, path)
|
||||||
.with_context(|| format!("failed to process preload at `{}`", path.display()))?;
|
.with_context(|| format!("failed to process preload at `{}`", path.display()))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the main wasm module.
|
// Load the main wasm module.
|
||||||
let path = Path::new(&args.arg_file);
|
let path = Path::new(&args.arg_file);
|
||||||
handle_module(store, &module_registry, &args, path)
|
handle_module(&store, &module_registry, &args, path)
|
||||||
.with_context(|| format!("failed to process main module `{}`", path.display()))?;
|
.with_context(|| format!("failed to process main module `{}`", path.display()))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn instantiate_module(
|
fn instantiate_module(
|
||||||
store: HostRef<Store>,
|
store: &HostRef<Store>,
|
||||||
module_registry: &HashMap<String, (Instance, HashMap<String, usize>)>,
|
module_registry: &HashMap<String, (Instance, HashMap<String, usize>)>,
|
||||||
path: &Path,
|
path: &Path,
|
||||||
) -> Result<(HostRef<Instance>, HostRef<Module>, Vec<u8>)> {
|
) -> Result<(HostRef<Instance>, HostRef<Module>, Vec<u8>)> {
|
||||||
// Read the wasm module binary either as `*.wat` or a raw binary
|
// Read the wasm module binary either as `*.wat` or a raw binary
|
||||||
let data = wat::parse_file(path.to_path_buf())?;
|
let data = wat::parse_file(path.to_path_buf())?;
|
||||||
|
|
||||||
let module = HostRef::new(Module::new(store.clone(), &data)?);
|
let module = HostRef::new(Module::new(store, &data)?);
|
||||||
|
|
||||||
// Resolve import using module_registry.
|
// Resolve import using module_registry.
|
||||||
let imports = module
|
let imports = module
|
||||||
@@ -356,18 +356,18 @@ fn instantiate_module(
|
|||||||
})
|
})
|
||||||
.collect::<Result<Vec<_>, _>>()?;
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
|
||||||
let instance = HostRef::new(Instance::new(store.clone(), module.clone(), &imports)?);
|
let instance = HostRef::new(Instance::new(store, &module, &imports)?);
|
||||||
|
|
||||||
Ok((instance, module, data))
|
Ok((instance, module, data))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_module(
|
fn handle_module(
|
||||||
store: HostRef<Store>,
|
store: &HostRef<Store>,
|
||||||
module_registry: &HashMap<String, (Instance, HashMap<String, usize>)>,
|
module_registry: &HashMap<String, (Instance, HashMap<String, usize>)>,
|
||||||
args: &Args,
|
args: &Args,
|
||||||
path: &Path,
|
path: &Path,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let (instance, _module, data) = instantiate_module(store.clone(), module_registry, path)?;
|
let (instance, _module, data) = instantiate_module(store, module_registry, path)?;
|
||||||
|
|
||||||
// If a function to invoke was given, invoke it.
|
// If a function to invoke was given, invoke it.
|
||||||
if let Some(f) = &args.flag_invoke {
|
if let Some(f) = &args.flag_invoke {
|
||||||
@@ -379,7 +379,7 @@ fn handle_module(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn invoke_export(
|
fn invoke_export(
|
||||||
store: HostRef<Store>,
|
store: &HostRef<Store>,
|
||||||
instance: HostRef<Instance>,
|
instance: HostRef<Instance>,
|
||||||
data: &ModuleData,
|
data: &ModuleData,
|
||||||
name: &str,
|
name: &str,
|
||||||
|
|||||||
@@ -10,10 +10,10 @@ fn main() -> Result<()> {
|
|||||||
|
|
||||||
// Instantiate engine and store.
|
// Instantiate engine and store.
|
||||||
let engine = HostRef::new(Engine::default());
|
let engine = HostRef::new(Engine::default());
|
||||||
let store = HostRef::new(Store::new(engine));
|
let store = HostRef::new(Store::new(&engine));
|
||||||
|
|
||||||
// Load a module.
|
// Load a module.
|
||||||
let module = HostRef::new(Module::new(store.clone(), &wasm)?);
|
let module = HostRef::new(Module::new(&store, &wasm)?);
|
||||||
|
|
||||||
// Find index of the `gcd` export.
|
// Find index of the `gcd` export.
|
||||||
let gcd_index = module
|
let gcd_index = module
|
||||||
@@ -26,7 +26,7 @@ fn main() -> Result<()> {
|
|||||||
.0;
|
.0;
|
||||||
|
|
||||||
// Instantiate the module.
|
// Instantiate the module.
|
||||||
let instance = HostRef::new(Instance::new(store.clone(), module, &[])?);
|
let instance = HostRef::new(Instance::new(&store, &module, &[])?);
|
||||||
|
|
||||||
// Invoke `gcd` export
|
// Invoke `gcd` export
|
||||||
let gcd = instance.borrow().exports()[gcd_index]
|
let gcd = instance.borrow().exports()[gcd_index]
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ impl Callable for HelloCallback {
|
|||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
// Initialize.
|
// Initialize.
|
||||||
println!("Initializing...");
|
println!("Initializing...");
|
||||||
let engine = HostRef::new(Engine::new(Config::default()));
|
let engine = HostRef::new(Engine::default());
|
||||||
let store = HostRef::new(Store::new(engine));
|
let store = HostRef::new(Store::new(&engine));
|
||||||
|
|
||||||
// Load binary.
|
// Load binary.
|
||||||
println!("Loading binary...");
|
println!("Loading binary...");
|
||||||
@@ -30,19 +30,18 @@ fn main() -> Result<()> {
|
|||||||
|
|
||||||
// Compile.
|
// Compile.
|
||||||
println!("Compiling module...");
|
println!("Compiling module...");
|
||||||
let module =
|
let module = HostRef::new(Module::new(&store, &binary).context("> Error compiling module!")?);
|
||||||
HostRef::new(Module::new(store.clone(), &binary).context("> Error compiling module!")?);
|
|
||||||
|
|
||||||
// Create external print functions.
|
// Create external print functions.
|
||||||
println!("Creating callback...");
|
println!("Creating callback...");
|
||||||
let hello_type = FuncType::new(Box::new([]), Box::new([]));
|
let hello_type = FuncType::new(Box::new([]), Box::new([]));
|
||||||
let hello_func = HostRef::new(Func::new(store.clone(), hello_type, Rc::new(HelloCallback)));
|
let hello_func = HostRef::new(Func::new(&store, hello_type, Rc::new(HelloCallback)));
|
||||||
|
|
||||||
// Instantiate.
|
// Instantiate.
|
||||||
println!("Instantiating module...");
|
println!("Instantiating module...");
|
||||||
let imports = vec![hello_func.into()];
|
let imports = vec![hello_func.into()];
|
||||||
let instance = HostRef::new(
|
let instance = HostRef::new(
|
||||||
Instance::new(store.clone(), module, imports.as_slice())
|
Instance::new(&store, &module, imports.as_slice())
|
||||||
.context("> Error instantiating module!")?,
|
.context("> Error instantiating module!")?,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -64,8 +64,8 @@ macro_rules! call {
|
|||||||
fn main() -> Result<(), Error> {
|
fn main() -> Result<(), Error> {
|
||||||
// Initialize.
|
// Initialize.
|
||||||
println!("Initializing...");
|
println!("Initializing...");
|
||||||
let engine = HostRef::new(Engine::new(Config::default()));
|
let engine = HostRef::new(Engine::default());
|
||||||
let store = HostRef::new(Store::new(engine));
|
let store = HostRef::new(Store::new(&engine));
|
||||||
|
|
||||||
// Load binary.
|
// Load binary.
|
||||||
println!("Loading binary...");
|
println!("Loading binary...");
|
||||||
@@ -73,14 +73,12 @@ fn main() -> Result<(), Error> {
|
|||||||
|
|
||||||
// Compile.
|
// Compile.
|
||||||
println!("Compiling module...");
|
println!("Compiling module...");
|
||||||
let module =
|
let module = HostRef::new(Module::new(&store, &binary).context("> Error compiling module!")?);
|
||||||
HostRef::new(Module::new(store.clone(), &binary).context("> Error compiling module!")?);
|
|
||||||
|
|
||||||
// Instantiate.
|
// Instantiate.
|
||||||
println!("Instantiating module...");
|
println!("Instantiating module...");
|
||||||
let instance = HostRef::new(
|
let instance =
|
||||||
Instance::new(store.clone(), module, &[]).context("> Error instantiating module!")?,
|
HostRef::new(Instance::new(&store, &module, &[]).context("> Error instantiating module!")?);
|
||||||
);
|
|
||||||
|
|
||||||
// Extract export.
|
// Extract export.
|
||||||
println!("Extracting export...");
|
println!("Extracting export...");
|
||||||
@@ -141,7 +139,7 @@ fn main() -> Result<(), Error> {
|
|||||||
// TODO(wasm+): Once Wasm allows multiple memories, turn this into import.
|
// TODO(wasm+): Once Wasm allows multiple memories, turn this into import.
|
||||||
println!("Creating stand-alone memory...");
|
println!("Creating stand-alone memory...");
|
||||||
let memorytype = MemoryType::new(Limits::new(5, 5));
|
let memorytype = MemoryType::new(Limits::new(5, 5));
|
||||||
let mut memory2 = Memory::new(store.clone(), memorytype);
|
let mut memory2 = Memory::new(&store, memorytype);
|
||||||
check!(memory2.size(), 5u32);
|
check!(memory2.size(), 5u32);
|
||||||
check!(memory2.grow(1), false);
|
check!(memory2.grow(1), false);
|
||||||
check!(memory2.grow(0), true);
|
check!(memory2.grow(0), true);
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ impl Callable for Callback {
|
|||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
// Initialize.
|
// Initialize.
|
||||||
println!("Initializing...");
|
println!("Initializing...");
|
||||||
let engine = HostRef::new(Engine::new(Config::default()));
|
let engine = HostRef::new(Engine::default());
|
||||||
let store = HostRef::new(Store::new(engine));
|
let store = HostRef::new(Store::new(&engine));
|
||||||
|
|
||||||
// Load binary.
|
// Load binary.
|
||||||
println!("Loading binary...");
|
println!("Loading binary...");
|
||||||
@@ -33,8 +33,7 @@ fn main() -> Result<()> {
|
|||||||
|
|
||||||
// Compile.
|
// Compile.
|
||||||
println!("Compiling module...");
|
println!("Compiling module...");
|
||||||
let module =
|
let module = HostRef::new(Module::new(&store, &binary).context("Error compiling module!")?);
|
||||||
HostRef::new(Module::new(store.clone(), &binary).context("Error compiling module!")?);
|
|
||||||
|
|
||||||
// Create external print functions.
|
// Create external print functions.
|
||||||
println!("Creating callback...");
|
println!("Creating callback...");
|
||||||
@@ -42,13 +41,13 @@ fn main() -> Result<()> {
|
|||||||
Box::new([ValType::I32, ValType::I64]),
|
Box::new([ValType::I32, ValType::I64]),
|
||||||
Box::new([ValType::I64, ValType::I32]),
|
Box::new([ValType::I64, ValType::I32]),
|
||||||
);
|
);
|
||||||
let callback_func = HostRef::new(Func::new(store.clone(), callback_type, Rc::new(Callback)));
|
let callback_func = HostRef::new(Func::new(&store, callback_type, Rc::new(Callback)));
|
||||||
|
|
||||||
// Instantiate.
|
// Instantiate.
|
||||||
println!("Instantiating module...");
|
println!("Instantiating module...");
|
||||||
let imports = vec![callback_func.into()];
|
let imports = vec![callback_func.into()];
|
||||||
let instance = HostRef::new(
|
let instance = HostRef::new(
|
||||||
Instance::new(store.clone(), module, imports.as_slice())
|
Instance::new(&store, &module, imports.as_slice())
|
||||||
.context("Error instantiating module!")?,
|
.context("Error instantiating module!")?,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -34,9 +34,9 @@ pub(crate) struct WasmtimeFn {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl WasmtimeFn {
|
impl WasmtimeFn {
|
||||||
pub fn new(store: HostRef<Store>, instance: InstanceHandle, export: Export) -> WasmtimeFn {
|
pub fn new(store: &HostRef<Store>, instance: InstanceHandle, export: Export) -> WasmtimeFn {
|
||||||
WasmtimeFn {
|
WasmtimeFn {
|
||||||
store,
|
store: store.clone(),
|
||||||
instance,
|
instance,
|
||||||
export,
|
export,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ impl Extern {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn from_wasmtime_export(
|
pub(crate) fn from_wasmtime_export(
|
||||||
store: HostRef<Store>,
|
store: &HostRef<Store>,
|
||||||
instance_handle: InstanceHandle,
|
instance_handle: InstanceHandle,
|
||||||
export: wasmtime_runtime::Export,
|
export: wasmtime_runtime::Export,
|
||||||
) -> Extern {
|
) -> Extern {
|
||||||
@@ -118,18 +118,18 @@ pub struct Func {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Func {
|
impl Func {
|
||||||
pub fn new(store: HostRef<Store>, ty: FuncType, callable: Rc<dyn Callable + 'static>) -> Self {
|
pub fn new(store: &HostRef<Store>, ty: FuncType, callable: Rc<dyn Callable + 'static>) -> Self {
|
||||||
let callable = Rc::new(NativeCallable::new(callable, &ty, &store));
|
let callable = Rc::new(NativeCallable::new(callable, &ty, &store));
|
||||||
Func::from_wrapped(store, ty, callable)
|
Func::from_wrapped(store, ty, callable)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_wrapped(
|
fn from_wrapped(
|
||||||
store: HostRef<Store>,
|
store: &HostRef<Store>,
|
||||||
r#type: FuncType,
|
r#type: FuncType,
|
||||||
callable: Rc<dyn WrappedCallable + 'static>,
|
callable: Rc<dyn WrappedCallable + 'static>,
|
||||||
) -> Func {
|
) -> Func {
|
||||||
Func {
|
Func {
|
||||||
_store: store,
|
_store: store.clone(),
|
||||||
callable,
|
callable,
|
||||||
r#type,
|
r#type,
|
||||||
}
|
}
|
||||||
@@ -159,7 +159,7 @@ impl Func {
|
|||||||
|
|
||||||
pub(crate) fn from_wasmtime_function(
|
pub(crate) fn from_wasmtime_function(
|
||||||
export: wasmtime_runtime::Export,
|
export: wasmtime_runtime::Export,
|
||||||
store: HostRef<Store>,
|
store: &HostRef<Store>,
|
||||||
instance_handle: InstanceHandle,
|
instance_handle: InstanceHandle,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let ty = if let wasmtime_runtime::Export::Function { signature, .. } = &export {
|
let ty = if let wasmtime_runtime::Export::Function { signature, .. } = &export {
|
||||||
@@ -167,7 +167,7 @@ impl Func {
|
|||||||
} else {
|
} else {
|
||||||
panic!("expected function export")
|
panic!("expected function export")
|
||||||
};
|
};
|
||||||
let callable = WasmtimeFn::new(store.clone(), instance_handle, export.clone());
|
let callable = WasmtimeFn::new(store, instance_handle, export.clone());
|
||||||
Func::from_wrapped(store, ty, Rc::new(callable))
|
Func::from_wrapped(store, ty, Rc::new(callable))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -187,11 +187,11 @@ pub struct Global {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Global {
|
impl Global {
|
||||||
pub fn new(store: HostRef<Store>, r#type: GlobalType, val: Val) -> Global {
|
pub fn new(store: &HostRef<Store>, r#type: GlobalType, val: Val) -> Global {
|
||||||
let (wasmtime_export, wasmtime_state) =
|
let (wasmtime_export, wasmtime_state) =
|
||||||
generate_global_export(&r#type, val).expect("generated global");
|
generate_global_export(&r#type, val).expect("generated global");
|
||||||
Global {
|
Global {
|
||||||
_store: store,
|
_store: store.clone(),
|
||||||
r#type,
|
r#type,
|
||||||
wasmtime_export,
|
wasmtime_export,
|
||||||
wasmtime_state: Some(wasmtime_state),
|
wasmtime_state: Some(wasmtime_state),
|
||||||
@@ -248,7 +248,7 @@ impl Global {
|
|||||||
|
|
||||||
pub(crate) fn from_wasmtime_global(
|
pub(crate) fn from_wasmtime_global(
|
||||||
export: wasmtime_runtime::Export,
|
export: wasmtime_runtime::Export,
|
||||||
store: HostRef<Store>,
|
store: &HostRef<Store>,
|
||||||
) -> Global {
|
) -> Global {
|
||||||
let global = if let wasmtime_runtime::Export::Global { ref global, .. } = export {
|
let global = if let wasmtime_runtime::Export::Global { ref global, .. } = export {
|
||||||
global
|
global
|
||||||
@@ -257,7 +257,7 @@ impl Global {
|
|||||||
};
|
};
|
||||||
let ty = GlobalType::from_cranelift_global(global.clone());
|
let ty = GlobalType::from_cranelift_global(global.clone());
|
||||||
Global {
|
Global {
|
||||||
_store: store,
|
_store: store.clone(),
|
||||||
r#type: ty,
|
r#type: ty,
|
||||||
wasmtime_export: export,
|
wasmtime_export: export,
|
||||||
wasmtime_state: None,
|
wasmtime_state: None,
|
||||||
@@ -302,7 +302,7 @@ fn set_table_item(
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Table {
|
impl Table {
|
||||||
pub fn new(store: HostRef<Store>, r#type: TableType, init: Val) -> Table {
|
pub fn new(store: &HostRef<Store>, r#type: TableType, init: Val) -> Table {
|
||||||
match r#type.element() {
|
match r#type.element() {
|
||||||
ValType::FuncRef => (),
|
ValType::FuncRef => (),
|
||||||
_ => panic!("table is not for funcref"),
|
_ => panic!("table is not for funcref"),
|
||||||
@@ -317,7 +317,7 @@ impl Table {
|
|||||||
let len = unsafe { (*definition).current_elements };
|
let len = unsafe { (*definition).current_elements };
|
||||||
for i in 0..len {
|
for i in 0..len {
|
||||||
let _success =
|
let _success =
|
||||||
set_table_item(&mut wasmtime_handle, &store, index, i, init.clone());
|
set_table_item(&mut wasmtime_handle, store, index, i, init.clone());
|
||||||
assert!(_success);
|
assert!(_success);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -325,7 +325,7 @@ impl Table {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Table {
|
Table {
|
||||||
store,
|
store: store.clone(),
|
||||||
r#type,
|
r#type,
|
||||||
wasmtime_handle,
|
wasmtime_handle,
|
||||||
wasmtime_export,
|
wasmtime_export,
|
||||||
@@ -387,7 +387,7 @@ impl Table {
|
|||||||
|
|
||||||
pub(crate) fn from_wasmtime_table(
|
pub(crate) fn from_wasmtime_table(
|
||||||
export: wasmtime_runtime::Export,
|
export: wasmtime_runtime::Export,
|
||||||
store: HostRef<Store>,
|
store: &HostRef<Store>,
|
||||||
instance_handle: wasmtime_runtime::InstanceHandle,
|
instance_handle: wasmtime_runtime::InstanceHandle,
|
||||||
) -> Table {
|
) -> Table {
|
||||||
let table = if let wasmtime_runtime::Export::Table { ref table, .. } = export {
|
let table = if let wasmtime_runtime::Export::Table { ref table, .. } = export {
|
||||||
@@ -397,7 +397,7 @@ impl Table {
|
|||||||
};
|
};
|
||||||
let ty = TableType::from_cranelift_table(table.table.clone());
|
let ty = TableType::from_cranelift_table(table.table.clone());
|
||||||
Table {
|
Table {
|
||||||
store,
|
store: store.clone(),
|
||||||
r#type: ty,
|
r#type: ty,
|
||||||
wasmtime_handle: instance_handle,
|
wasmtime_handle: instance_handle,
|
||||||
wasmtime_export: export,
|
wasmtime_export: export,
|
||||||
@@ -413,11 +413,11 @@ pub struct Memory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Memory {
|
impl Memory {
|
||||||
pub fn new(store: HostRef<Store>, r#type: MemoryType) -> Memory {
|
pub fn new(store: &HostRef<Store>, r#type: MemoryType) -> Memory {
|
||||||
let (wasmtime_handle, wasmtime_export) =
|
let (wasmtime_handle, wasmtime_export) =
|
||||||
generate_memory_export(&r#type).expect("generated memory");
|
generate_memory_export(&r#type).expect("generated memory");
|
||||||
Memory {
|
Memory {
|
||||||
_store: store,
|
_store: store.clone(),
|
||||||
r#type,
|
r#type,
|
||||||
wasmtime_handle,
|
wasmtime_handle,
|
||||||
wasmtime_export,
|
wasmtime_export,
|
||||||
@@ -471,7 +471,7 @@ impl Memory {
|
|||||||
|
|
||||||
pub(crate) fn from_wasmtime_memory(
|
pub(crate) fn from_wasmtime_memory(
|
||||||
export: wasmtime_runtime::Export,
|
export: wasmtime_runtime::Export,
|
||||||
store: HostRef<Store>,
|
store: &HostRef<Store>,
|
||||||
instance_handle: wasmtime_runtime::InstanceHandle,
|
instance_handle: wasmtime_runtime::InstanceHandle,
|
||||||
) -> Memory {
|
) -> Memory {
|
||||||
let memory = if let wasmtime_runtime::Export::Memory { ref memory, .. } = export {
|
let memory = if let wasmtime_runtime::Export::Memory { ref memory, .. } = export {
|
||||||
@@ -481,7 +481,7 @@ impl Memory {
|
|||||||
};
|
};
|
||||||
let ty = MemoryType::from_cranelift_memory(memory.memory.clone());
|
let ty = MemoryType::from_cranelift_memory(memory.memory.clone());
|
||||||
Memory {
|
Memory {
|
||||||
_store: store,
|
_store: store.clone(),
|
||||||
r#type: ty,
|
r#type: ty,
|
||||||
wasmtime_handle: instance_handle,
|
wasmtime_handle: instance_handle,
|
||||||
wasmtime_export: export,
|
wasmtime_export: export,
|
||||||
|
|||||||
@@ -61,8 +61,8 @@ pub struct Instance {
|
|||||||
|
|
||||||
impl Instance {
|
impl Instance {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
store: HostRef<Store>,
|
store: &HostRef<Store>,
|
||||||
module: HostRef<Module>,
|
module: &HostRef<Module>,
|
||||||
externs: &[Extern],
|
externs: &[Extern],
|
||||||
) -> Result<Instance> {
|
) -> Result<Instance> {
|
||||||
let context = store.borrow_mut().context().clone();
|
let context = store.borrow_mut().context().clone();
|
||||||
@@ -84,7 +84,7 @@ impl Instance {
|
|||||||
let name = export.name().to_string();
|
let name = export.name().to_string();
|
||||||
let export = instance_handle.lookup(&name).expect("export");
|
let export = instance_handle.lookup(&name).expect("export");
|
||||||
exports.push(Extern::from_wasmtime_export(
|
exports.push(Extern::from_wasmtime_export(
|
||||||
store.clone(),
|
store,
|
||||||
instance_handle.clone(),
|
instance_handle.clone(),
|
||||||
export,
|
export,
|
||||||
));
|
));
|
||||||
@@ -103,7 +103,7 @@ impl Instance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_handle(
|
pub fn from_handle(
|
||||||
store: HostRef<Store>,
|
store: &HostRef<Store>,
|
||||||
instance_handle: InstanceHandle,
|
instance_handle: InstanceHandle,
|
||||||
) -> Result<(Instance, HashMap<String, usize>)> {
|
) -> Result<(Instance, HashMap<String, usize>)> {
|
||||||
let contexts = HashSet::new();
|
let contexts = HashSet::new();
|
||||||
@@ -121,7 +121,7 @@ impl Instance {
|
|||||||
}
|
}
|
||||||
export_names_map.insert(name.to_owned(), exports.len());
|
export_names_map.insert(name.to_owned(), exports.len());
|
||||||
exports.push(Extern::from_wasmtime_export(
|
exports.push(Extern::from_wasmtime_export(
|
||||||
store.clone(),
|
store,
|
||||||
instance_handle.clone(),
|
instance_handle.clone(),
|
||||||
export.clone(),
|
export.clone(),
|
||||||
));
|
));
|
||||||
|
|||||||
@@ -182,10 +182,10 @@ pub struct Module {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Module {
|
impl Module {
|
||||||
pub fn new(store: HostRef<Store>, binary: &[u8]) -> Result<Module> {
|
pub fn new(store: &HostRef<Store>, binary: &[u8]) -> Result<Module> {
|
||||||
let (imports, exports) = read_imports_and_exports(binary)?;
|
let (imports, exports) = read_imports_and_exports(binary)?;
|
||||||
Ok(Module {
|
Ok(Module {
|
||||||
store,
|
store: store.clone(),
|
||||||
binary: binary.into(),
|
binary: binary.into(),
|
||||||
imports,
|
imports,
|
||||||
exports,
|
exports,
|
||||||
|
|||||||
@@ -102,13 +102,13 @@ pub struct Store {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Store {
|
impl Store {
|
||||||
pub fn new(engine: HostRef<Engine>) -> Store {
|
pub fn new(engine: &HostRef<Engine>) -> Store {
|
||||||
let flags = engine.borrow().config().flags().clone();
|
let flags = engine.borrow().config().flags().clone();
|
||||||
let features = engine.borrow().config().features().clone();
|
let features = engine.borrow().config().features().clone();
|
||||||
let debug_info = engine.borrow().config().debug_info();
|
let debug_info = engine.borrow().config().debug_info();
|
||||||
let strategy = engine.borrow().config().strategy();
|
let strategy = engine.borrow().config().strategy();
|
||||||
Store {
|
Store {
|
||||||
engine,
|
engine: engine.clone(),
|
||||||
context: Context::create(flags, features, debug_info, strategy),
|
context: Context::create(flags, features, debug_info, strategy),
|
||||||
global_exports: Rc::new(RefCell::new(HashMap::new())),
|
global_exports: Rc::new(RefCell::new(HashMap::new())),
|
||||||
signature_cache: HashMap::new(),
|
signature_cache: HashMap::new(),
|
||||||
|
|||||||
@@ -230,6 +230,6 @@ pub(crate) fn from_checked_anyfunc(
|
|||||||
signature,
|
signature,
|
||||||
vmctx: item.vmctx,
|
vmctx: item.vmctx,
|
||||||
};
|
};
|
||||||
let f = Func::from_wasmtime_function(export, store.clone(), instance_handle);
|
let f = Func::from_wasmtime_function(export, store, instance_handle);
|
||||||
Val::FuncRef(HostRef::new(f))
|
Val::FuncRef(HostRef::new(f))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -609,7 +609,7 @@ pub unsafe extern "C" fn wasm_func_new(
|
|||||||
ty: *const wasm_functype_t,
|
ty: *const wasm_functype_t,
|
||||||
callback: wasm_func_callback_t,
|
callback: wasm_func_callback_t,
|
||||||
) -> *mut wasm_func_t {
|
) -> *mut wasm_func_t {
|
||||||
let store = (*store).store.clone();
|
let store = &(*store).store;
|
||||||
let ty = (*ty).functype.clone();
|
let ty = (*ty).functype.clone();
|
||||||
let callback = Rc::new(callback);
|
let callback = Rc::new(callback);
|
||||||
let func = Box::new(wasm_func_t {
|
let func = Box::new(wasm_func_t {
|
||||||
@@ -663,13 +663,13 @@ pub unsafe extern "C" fn wasm_instance_new(
|
|||||||
imports: *const *const wasm_extern_t,
|
imports: *const *const wasm_extern_t,
|
||||||
result: *mut *mut wasm_trap_t,
|
result: *mut *mut wasm_trap_t,
|
||||||
) -> *mut wasm_instance_t {
|
) -> *mut wasm_instance_t {
|
||||||
let store = (*store).store.clone();
|
let store = &(*store).store;
|
||||||
let mut externs: Vec<Extern> = Vec::with_capacity((*module).imports.len());
|
let mut externs: Vec<Extern> = Vec::with_capacity((*module).imports.len());
|
||||||
for i in 0..(*module).imports.len() {
|
for i in 0..(*module).imports.len() {
|
||||||
let import = *imports.offset(i as isize);
|
let import = *imports.offset(i as isize);
|
||||||
externs.push((*import).ext.clone());
|
externs.push((*import).ext.clone());
|
||||||
}
|
}
|
||||||
let module = (*module).module.clone();
|
let module = &(*module).module;
|
||||||
match Instance::new(store, module, &externs) {
|
match Instance::new(store, module, &externs) {
|
||||||
Ok(instance) => {
|
Ok(instance) => {
|
||||||
let instance = Box::new(wasm_instance_t {
|
let instance = Box::new(wasm_instance_t {
|
||||||
@@ -731,7 +731,7 @@ pub unsafe extern "C" fn wasm_module_new(
|
|||||||
binary: *const wasm_byte_vec_t,
|
binary: *const wasm_byte_vec_t,
|
||||||
) -> *mut wasm_module_t {
|
) -> *mut wasm_module_t {
|
||||||
let binary = (*binary).as_slice();
|
let binary = (*binary).as_slice();
|
||||||
let store = (*store).store.clone();
|
let store = &(*store).store;
|
||||||
let module = Module::new(store, binary).expect("module");
|
let module = Module::new(store, binary).expect("module");
|
||||||
let imports = module
|
let imports = module
|
||||||
.imports()
|
.imports()
|
||||||
@@ -766,9 +766,9 @@ pub unsafe extern "C" fn wasm_store_delete(store: *mut wasm_store_t) {
|
|||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn wasm_store_new(engine: *mut wasm_engine_t) -> *mut wasm_store_t {
|
pub unsafe extern "C" fn wasm_store_new(engine: *mut wasm_engine_t) -> *mut wasm_store_t {
|
||||||
let engine = (*engine).engine.clone();
|
let engine = &(*engine).engine;
|
||||||
let store = Box::new(wasm_store_t {
|
let store = Box::new(wasm_store_t {
|
||||||
store: HostRef::new(Store::new(engine)),
|
store: HostRef::new(Store::new(&engine)),
|
||||||
});
|
});
|
||||||
Box::into_raw(store)
|
Box::into_raw(store)
|
||||||
}
|
}
|
||||||
@@ -804,7 +804,7 @@ pub unsafe extern "C" fn wasm_func_new_with_env(
|
|||||||
env: *mut ::core::ffi::c_void,
|
env: *mut ::core::ffi::c_void,
|
||||||
finalizer: ::core::option::Option<unsafe extern "C" fn(arg1: *mut ::core::ffi::c_void)>,
|
finalizer: ::core::option::Option<unsafe extern "C" fn(arg1: *mut ::core::ffi::c_void)>,
|
||||||
) -> *mut wasm_func_t {
|
) -> *mut wasm_func_t {
|
||||||
let store = (*store).store.clone();
|
let store = &(*store).store;
|
||||||
let ty = (*ty).functype.clone();
|
let ty = (*ty).functype.clone();
|
||||||
let callback = Rc::new(CallbackWithEnv {
|
let callback = Rc::new(CallbackWithEnv {
|
||||||
callback,
|
callback,
|
||||||
@@ -1327,7 +1327,7 @@ pub unsafe extern "C" fn wasm_global_new(
|
|||||||
val: *const wasm_val_t,
|
val: *const wasm_val_t,
|
||||||
) -> *mut wasm_global_t {
|
) -> *mut wasm_global_t {
|
||||||
let global = HostRef::new(Global::new(
|
let global = HostRef::new(Global::new(
|
||||||
(*store).store.clone(),
|
&(*store).store,
|
||||||
(*gt).globaltype.clone(),
|
(*gt).globaltype.clone(),
|
||||||
(*val).val(),
|
(*val).val(),
|
||||||
));
|
));
|
||||||
@@ -1446,10 +1446,7 @@ pub unsafe extern "C" fn wasm_memory_new(
|
|||||||
store: *mut wasm_store_t,
|
store: *mut wasm_store_t,
|
||||||
mt: *const wasm_memorytype_t,
|
mt: *const wasm_memorytype_t,
|
||||||
) -> *mut wasm_memory_t {
|
) -> *mut wasm_memory_t {
|
||||||
let memory = HostRef::new(Memory::new(
|
let memory = HostRef::new(Memory::new(&(*store).store, (*mt).memorytype.clone()));
|
||||||
(*store).store.clone(),
|
|
||||||
(*mt).memorytype.clone(),
|
|
||||||
));
|
|
||||||
let m = Box::new(wasm_memory_t { memory, ext: None });
|
let m = Box::new(wasm_memory_t { memory, ext: None });
|
||||||
Box::into_raw(m)
|
Box::into_raw(m)
|
||||||
}
|
}
|
||||||
@@ -1537,11 +1534,7 @@ pub unsafe extern "C" fn wasm_table_new(
|
|||||||
Val::AnyRef(AnyRef::Null)
|
Val::AnyRef(AnyRef::Null)
|
||||||
};
|
};
|
||||||
let t = Box::new(wasm_table_t {
|
let t = Box::new(wasm_table_t {
|
||||||
table: HostRef::new(Table::new(
|
table: HostRef::new(Table::new(&(*store).store, (*tt).tabletype.clone(), init)),
|
||||||
(*store).store.clone(),
|
|
||||||
(*tt).tabletype.clone(),
|
|
||||||
init,
|
|
||||||
)),
|
|
||||||
ext: None,
|
ext: None,
|
||||||
});
|
});
|
||||||
Box::into_raw(t)
|
Box::into_raw(t)
|
||||||
|
|||||||
@@ -25,10 +25,10 @@ fn test_import_calling_export() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let engine = HostRef::new(Engine::new(Config::default()));
|
let engine = HostRef::new(Engine::new(Config::default()));
|
||||||
let store = HostRef::new(Store::new(engine));
|
let store = HostRef::new(Store::new(&engine));
|
||||||
let module = HostRef::new(
|
let module = HostRef::new(
|
||||||
Module::new(
|
Module::new(
|
||||||
store.clone(),
|
&store,
|
||||||
&read("tests/import_calling_export.wasm").expect("failed to read wasm file"),
|
&read("tests/import_calling_export.wasm").expect("failed to read wasm file"),
|
||||||
)
|
)
|
||||||
.expect("failed to create module"),
|
.expect("failed to create module"),
|
||||||
@@ -39,15 +39,14 @@ fn test_import_calling_export() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let callback_func = HostRef::new(Func::new(
|
let callback_func = HostRef::new(Func::new(
|
||||||
store.clone(),
|
&store,
|
||||||
FuncType::new(Box::new([]), Box::new([])),
|
FuncType::new(Box::new([]), Box::new([])),
|
||||||
callback.clone(),
|
callback.clone(),
|
||||||
));
|
));
|
||||||
|
|
||||||
let imports = vec![callback_func.into()];
|
let imports = vec![callback_func.into()];
|
||||||
let instance = HostRef::new(
|
let instance = HostRef::new(
|
||||||
Instance::new(store.clone(), module, imports.as_slice())
|
Instance::new(&store, &module, imports.as_slice()).expect("failed to instantiate module"),
|
||||||
.expect("failed to instantiate module"),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
let exports = Ref::map(instance.borrow(), |instance| instance.exports());
|
let exports = Ref::map(instance.borrow(), |instance| instance.exports());
|
||||||
|
|||||||
Reference in New Issue
Block a user