Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Linker for preparation of Linker::func_{wrap,new} methods #681

Merged
merged 15 commits into from
Feb 15, 2023
2 changes: 1 addition & 1 deletion crates/cli/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Context {
panic!("error: fuel metering is enabled but encountered: {error}")
});
}
let mut linker = <wasmi::Linker<WasiCtx>>::default();
let mut linker = <wasmi::Linker<WasiCtx>>::new(&engine);
wasmi_wasi::define_wasi(&mut linker, &mut store, |ctx| ctx)
.map_err(|error| anyhow!("failed to add WASI definitions to the linker: {error}"))?;
let instance = linker
Expand Down
12 changes: 6 additions & 6 deletions crates/wasi/tests/wasi_wat.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use wasi_cap_std_sync::WasiCtxBuilder;
use wasmi::{Config, Extern, Instance, Store};
use wasmi::{Config, Engine, Extern, Instance, Linker, Module, Store};
use wasmi_wasi::{define_wasi, WasiCtx};

pub fn load_instance_from_wat(wat_bytes: &[u8]) -> (wasmi::Store<WasiCtx>, wasmi::Instance) {
pub fn load_instance_from_wat(wat_bytes: &[u8]) -> (Store<WasiCtx>, wasmi::Instance) {
let wasm = wat2wasm(wat_bytes);
let config = Config::default();
let engine = wasmi::Engine::new(&config);
let module = wasmi::Module::new(&engine, &wasm[..]).unwrap();
let mut linker = <wasmi::Linker<WasiCtx>>::default();
let engine = Engine::new(&config);
let module = Module::new(&engine, &wasm[..]).unwrap();
let mut linker = <Linker<WasiCtx>>::new(&engine);
// add wasi to linker
let wasi = WasiCtxBuilder::new()
.inherit_stdio()
.inherit_args()
.unwrap()
.build();
let mut store = wasmi::Store::new(&engine, wasi);
let mut store = Store::new(&engine, wasi);

define_wasi(&mut linker, &mut store, |ctx| ctx).unwrap();
let instance = linker
Expand Down
4 changes: 2 additions & 2 deletions crates/wasmi/benches/bench/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn load_module_from_file(file_name: &str) -> wasmi::Module {
/// If the benchmark Wasm file could not be opened, read or parsed.
pub fn load_instance_from_file(file_name: &str) -> (wasmi::Store<()>, wasmi::Instance) {
let module = load_module_from_file(file_name);
let linker = <wasmi::Linker<()>>::default();
let linker = <wasmi::Linker<()>>::new(module.engine());
let mut store = wasmi::Store::new(module.engine(), ());
let instance = linker
.instantiate(&mut store, &module)
Expand Down Expand Up @@ -86,7 +86,7 @@ pub fn load_instance_from_wat(wat_bytes: &[u8]) -> (wasmi::Store<()>, wasmi::Ins
let wasm = wat2wasm(wat_bytes);
let engine = wasmi::Engine::new(&bench_config());
let module = wasmi::Module::new(&engine, &wasm[..]).unwrap();
let linker = <wasmi::Linker<()>>::default();
let linker = <wasmi::Linker<()>>::new(&engine);
let mut store = wasmi::Store::new(&engine, ());
let instance = linker
.instantiate(&mut store, &module)
Expand Down
6 changes: 3 additions & 3 deletions crates/wasmi/benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ fn bench_translate_erc1155(c: &mut Criterion) {
fn bench_instantiate_wasm_kernel(c: &mut Criterion) {
c.bench_function("instantiate/wasm_kernel", |b| {
let module = load_module_from_file(WASM_KERNEL);
let linker = <Linker<()>>::default();
let linker = <Linker<()>>::new(module.engine());
b.iter(|| {
let mut store = Store::new(module.engine(), ());
let _instance = linker.instantiate(&mut store, &module).unwrap();
Expand All @@ -128,7 +128,7 @@ fn bench_instantiate_contract(c: &mut Criterion, name: &str, path: &str) {
let module = load_module_from_file(path);
let engine = module.engine();
let mut store = Store::new(&engine, ());
let mut linker = <Linker<()>>::default();
let mut linker = <Linker<()>>::new(&engine);
linker
.define(
"env",
Expand Down Expand Up @@ -857,7 +857,7 @@ fn bench_execute_host_calls(c: &mut Criterion) {
let wasm = wat2wasm(include_bytes!("wat/host_calls.wat"));
let engine = Engine::default();
let module = Module::new(&engine, &wasm[..]).unwrap();
let mut linker = <Linker<()>>::default();
let mut linker = <Linker<()>>::new(&engine);
let mut store = Store::new(&engine, ());
let host_call = Func::wrap(&mut store, |value: i64| value.wrapping_sub(1));
linker.define("benchmark", "host_call", host_call).unwrap();
Expand Down
15 changes: 10 additions & 5 deletions crates/wasmi/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub(crate) use self::{
func_args::{FuncFinished, FuncParams, FuncResults},
func_types::DedupFuncType,
};
use super::{func::FuncEntityInternal, AsContextMut, Func};
use super::{func::FuncEntityInner, AsContextMut, Func};
use crate::{
core::{Trap, TrapCode},
FuncType,
Expand Down Expand Up @@ -131,6 +131,11 @@ impl Engine {
self.inner.config()
}

/// Returns `true` if both [`Engine`] references `a` and `b` refer to the same [`Engine`].
pub fn same(a: &Engine, b: &Engine) -> bool {
Arc::ptr_eq(&a.inner, &b.inner)
}

/// Allocates a new function type to the engine.
pub(super) fn alloc_func_type(&self, func_type: FuncType) -> DedupFuncType {
self.inner.alloc_func_type(func_type)
Expand Down Expand Up @@ -589,12 +594,12 @@ impl<'engine> EngineExecutor<'engine> {
{
self.initialize_args(params);
match func.as_internal(ctx.as_context()) {
FuncEntityInternal::Wasm(wasm_func) => {
FuncEntityInner::Wasm(wasm_func) => {
let mut frame = self.stack.call_wasm_root(wasm_func, &self.res.code_map)?;
let mut cache = InstanceCache::from(frame.instance());
self.execute_wasm_func(ctx.as_context_mut(), &mut frame, &mut cache)?;
}
FuncEntityInternal::Host(host_func) => {
FuncEntityInner::Host(host_func) => {
let host_func = host_func.clone();
self.stack
.call_host_root(ctx.as_context_mut(), host_func, &self.res.func_types)?;
Expand Down Expand Up @@ -681,10 +686,10 @@ impl<'engine> EngineExecutor<'engine> {
},
CallOutcome::NestedCall(called_func) => {
match called_func.as_internal(ctx.as_context()) {
FuncEntityInternal::Wasm(wasm_func) => {
FuncEntityInner::Wasm(wasm_func) => {
*frame = self.stack.call_wasm(frame, wasm_func, &self.res.code_map)?;
}
FuncEntityInternal::Host(host_func) => {
FuncEntityInner::Host(host_func) => {
cache.reset_default_memory_bytes();
let host_func = host_func.clone();
self.stack
Expand Down
9 changes: 6 additions & 3 deletions crates/wasmi/src/engine/stack/frames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ pub struct FuncFrame {

impl FuncFrame {
/// Creates a new [`FuncFrame`].
pub fn new(ip: InstructionPtr, instance: Instance) -> Self {
Self { ip, instance }
pub fn new(ip: InstructionPtr, instance: &Instance) -> Self {
Self {
ip,
instance: *instance,
}
}

/// Returns the current instruction pointer.
Expand Down Expand Up @@ -66,7 +69,7 @@ impl CallStack {
}

/// Initializes the [`CallStack`] given the Wasm function.
pub(crate) fn init(&mut self, ip: InstructionPtr, instance: Instance) -> FuncFrame {
pub(crate) fn init(&mut self, ip: InstructionPtr, instance: &Instance) -> FuncFrame {
self.clear();
FuncFrame::new(ip, instance)
}
Expand Down
Loading