Skip to content

Commit 0c63604

Browse files
committed
make dispatching host functions use Inst instead of &Instance
1 parent bbf75aa commit 0c63604

File tree

11 files changed

+56
-37
lines changed

11 files changed

+56
-37
lines changed

crates/wasmi/src/engine/executor/handler/dispatch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ fn handle_reason(reason: Option<DoneReason>) -> Result<Sp, Error> {
155155
};
156156
match reason {
157157
DoneReason::Trap(trap_code) => Err(Error::from(trap_code)),
158-
DoneReason::OutOfFuel { required_fuel: _ } => todo!(),
159-
DoneReason::Host(_host_error) => todo!(),
158+
DoneReason::OutOfFuel(_error) => todo!(),
159+
DoneReason::Host(_error) => todo!(),
160160
DoneReason::CompileError(_error) => todo!(),
161161
DoneReason::Return(sp) => Ok(sp),
162162
}

crates/wasmi/src/engine/executor/handler/exec.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ use crate::{
2727
},
2828
},
2929
EngineFunc,
30+
ResumableHostTrapError,
31+
ResumableOutOfFuelError,
3032
},
3133
errors::{FuelError, MemoryError},
3234
func::FuncEntity,
@@ -75,7 +77,10 @@ pub fn consume_fuel(
7577
.fuel_mut()
7678
.consume_fuel_unchecked(u64::from(fuel));
7779
if let Err(FuelError::OutOfFuel { required_fuel }) = consumption_result {
78-
done!(state, DoneReason::OutOfFuel { required_fuel });
80+
done!(
81+
state,
82+
DoneReason::OutOfFuel(ResumableOutOfFuelError::new(required_fuel))
83+
);
7984
}
8085
dispatch!(state, ip, sp, mem0, mem0_len, instance)
8186
}
@@ -509,7 +514,10 @@ pub fn memory_grow(
509514
}
510515
}
511516
Err(StoreError::External(MemoryError::OutOfFuel { required_fuel })) => {
512-
done!(state, DoneReason::OutOfFuel { required_fuel });
517+
done!(
518+
state,
519+
DoneReason::OutOfFuel(ResumableOutOfFuelError::new(required_fuel))
520+
);
513521
}
514522
Err(StoreError::External(MemoryError::ResourceLimiterDeniedAllocation)) => {
515523
done!(state, TrapCode::GrowthOperationLimited);

crates/wasmi/src/engine/executor/handler/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ mod state;
99
use self::{dispatch::Done, state::DoneReason};
1010
pub use self::{
1111
dispatch::{init_wasm_func_call, op_code_to_handler},
12-
state::Stack,
12+
state::{Inst, Stack},
1313
};

crates/wasmi/src/engine/executor/handler/state.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use crate::{
33
engine::{
44
executor::{handler::utils::extract_mem0, CodeMap},
55
utils::unreachable_unchecked,
6+
ResumableHostTrapError,
7+
ResumableOutOfFuelError,
68
StackConfig,
79
},
810
errors::HostError,
@@ -12,7 +14,7 @@ use crate::{
1214
Error,
1315
TrapCode,
1416
};
15-
use alloc::{boxed::Box, vec::Vec};
17+
use alloc::vec::Vec;
1618
use core::{
1719
ptr::{self, NonNull},
1820
slice,
@@ -53,8 +55,8 @@ impl<'vm> VmState<'vm> {
5355
pub enum DoneReason {
5456
Return(Sp),
5557
Trap(TrapCode),
56-
OutOfFuel { required_fuel: u64 },
57-
Host(Box<dyn HostError>),
58+
Host(ResumableHostTrapError),
59+
OutOfFuel(ResumableOutOfFuelError),
5860
CompileError(Error),
5961
}
6062

crates/wasmi/src/engine/executor/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![expect(dead_code)]
22

3-
pub use self::handler::{op_code_to_handler, Stack};
3+
pub use self::handler::{op_code_to_handler, Inst, Stack};
44
use super::{code_map::CodeMap, ResumableError};
55
use crate::{
66
engine::{

crates/wasmi/src/engine/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod utils;
1313

1414
pub(crate) use self::{
1515
block_type::BlockType,
16-
executor::Stack,
16+
executor::{Inst, Stack},
1717
func_types::DedupFuncType,
1818
translator::{
1919
FuncTranslationDriver,

crates/wasmi/src/engine/resumable.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ impl fmt::Display for ResumableHostTrapError {
7171
impl ResumableHostTrapError {
7272
/// Creates a new [`ResumableHostTrapError`].
7373
#[cold]
74-
#[expect(unused)]
7574
pub(crate) fn new(host_error: Error, host_func: Func, caller_results: SlotSpan) -> Self {
7675
Self {
7776
host_error,

crates/wasmi/src/func/caller.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::super::{AsContext, AsContextMut, StoreContext, StoreContextMut};
2-
use crate::{Engine, Error, Extern, Instance};
2+
use crate::{engine::Inst, Engine, Error, Extern};
33

44
/// Represents the caller’s context when creating a host function via [`Func::wrap`].
55
///
@@ -10,18 +10,18 @@ pub struct Caller<'a, T> {
1010
/// This is `Some` if the host function was called from a Wasm function
1111
/// since all Wasm function are associated to a module instance.
1212
/// This usually is `None` if the host function was called from the host side.
13-
instance: Option<Instance>,
13+
instance: Option<Inst>,
1414
}
1515

1616
impl<'a, T> Caller<'a, T> {
1717
/// Creates a new [`Caller`] from the given store context and [`Instance`] handle.
18-
pub(crate) fn new<C>(ctx: &'a mut C, instance: Option<&Instance>) -> Self
18+
pub(crate) fn new<C>(ctx: &'a mut C, instance: Option<Inst>) -> Self
1919
where
2020
C: AsContextMut<Data = T>,
2121
{
2222
Self {
2323
ctx: ctx.as_context_mut(),
24-
instance: instance.copied(),
24+
instance,
2525
}
2626
}
2727

@@ -30,8 +30,11 @@ impl<'a, T> Caller<'a, T> {
3030
/// Returns `None` if there is no associated [`Instance`] of the caller
3131
/// or if the caller does not provide an export under the name `name`.
3232
pub fn get_export(&self, name: &str) -> Option<Extern> {
33-
self.instance
34-
.and_then(|instance| instance.get_export(self, name))
33+
let Some(instance) = &self.instance else {
34+
return None;
35+
};
36+
let instance = unsafe { instance.as_ref() };
37+
instance.get_export(name)
3538
}
3639

3740
/// Returns a shared reference to the user provided host data.

crates/wasmi/src/func/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ use super::{
2323
StoreContext,
2424
Stored,
2525
};
26-
use crate::{collections::arena::ArenaIndex, engine::ResumableCall, Engine, Error, Val};
26+
use crate::{
27+
collections::arena::ArenaIndex,
28+
engine::{Inst, ResumableCall},
29+
Engine,
30+
Error,
31+
Val,
32+
};
2733
use alloc::{boxed::Box, sync::Arc};
2834
use core::{fmt, fmt::Debug, num::NonZeroU32};
2935

@@ -294,7 +300,7 @@ impl<T> TrampolineEntity<T> {
294300
pub fn call(
295301
&self,
296302
mut ctx: impl AsContextMut<Data = T>,
297-
instance: Option<&Instance>,
303+
instance: Option<Inst>,
298304
params: FuncInOut,
299305
) -> Result<FuncFinished, Error> {
300306
let caller = <Caller<T>>::new(&mut ctx, instance);

crates/wasmi/src/store/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ pub use self::{
1414
use crate::{
1515
collections::arena::Arena,
1616
core::{CoreMemory, ResourceLimiterRef},
17-
func::{FuncInOut, HostFuncEntity, Trampoline, TrampolineEntity, TrampolineIdx},
17+
engine::Inst,
18+
func::{FuncInOut, Trampoline, TrampolineEntity, TrampolineIdx},
1819
Engine,
1920
Error,
20-
Instance,
2121
Memory,
2222
ResourceLimiter,
2323
};
@@ -108,11 +108,11 @@ impl<T> Store<T> {
108108
/// If the called host function returned an error.
109109
fn call_host_func(
110110
&mut self,
111-
func: &HostFuncEntity,
112-
instance: Option<&Instance>,
111+
trampoline: Trampoline,
112+
instance: Option<Inst>,
113113
params_results: FuncInOut,
114114
) -> Result<(), StoreError<Error>> {
115-
let trampoline = self.resolve_trampoline(func.trampoline())?.clone();
115+
let trampoline = self.resolve_trampoline(&trampoline)?.clone();
116116
trampoline
117117
.call(self, instance, params_results)
118118
.map_err(StoreError::external)?;

0 commit comments

Comments
 (0)