Skip to content

Commit 15bb5bd

Browse files
committed
initial EngineExecutor impls using WasmFuncCall
Lots of todos and unimplemented stuff. very WIP but works on some basic things already. there may be dragons. lots of dragons.
1 parent 5c16242 commit 15bb5bd

File tree

1 file changed

+36
-101
lines changed
  • crates/wasmi/src/engine/executor

1 file changed

+36
-101
lines changed

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

Lines changed: 36 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#![expect(dead_code)]
22

3-
pub(crate) use self::stack::Stack;
4-
use self::{instr_ptr::InstructionPtr, instrs::execute_instrs, stack::CallFrame};
3+
pub use self::handler::{op_code_to_handler, Stack};
4+
use super::{code_map::CodeMap, ResumableError};
55
use crate::{
66
engine::{
7+
executor::handler::init_wasm_func_call,
78
CallParams,
89
CallResults,
910
EngineInner,
@@ -12,22 +13,15 @@ use crate::{
1213
ResumableCallOutOfFuel,
1314
},
1415
func::HostFuncEntity,
15-
ir::{Slot, SlotSpan},
16-
CallHook,
16+
ir::SlotSpan,
1717
Error,
1818
Func,
1919
FuncEntity,
2020
Store,
2121
StoreContextMut,
2222
};
2323

24-
use super::{code_map::CodeMap, ResumableError};
25-
26-
mod cache;
2724
mod handler;
28-
mod instr_ptr;
29-
mod instrs;
30-
mod stack;
3125

3226
impl EngineInner {
3327
/// Executes the given [`Func`] with the given `params` and returns the `results`.
@@ -249,54 +243,21 @@ impl<'engine> EngineExecutor<'engine> {
249243
match store.inner.resolve_func(func) {
250244
FuncEntity::Wasm(wasm_func) => {
251245
// We reserve space on the stack to write the results of the root function execution.
252-
let len_results = results.len_results();
253-
self.stack.values.extend_by(len_results, do_nothing)?;
254246
let instance = *wasm_func.instance();
255247
let engine_func = wasm_func.func_body();
256-
let compiled_func = self
257-
.code_map
258-
.get(Some(store.inner.fuel_mut()), engine_func)?;
259-
let (mut uninit_params, offsets) = self
260-
.stack
261-
.values
262-
.alloc_call_frame(compiled_func, do_nothing)?;
263-
for value in params.call_params() {
264-
unsafe { uninit_params.init_next(value) };
265-
}
266-
uninit_params.init_zeroes();
267-
self.stack.calls.push(
268-
CallFrame::new(
269-
InstructionPtr::new(compiled_func.ops().as_ptr()),
270-
offsets,
271-
SlotSpan::new(Slot::from(0)),
272-
),
273-
Some(instance),
274-
)?;
275-
store.invoke_call_hook(CallHook::CallingWasm)?;
276-
self.execute_func(store)?;
277-
store.invoke_call_hook(CallHook::ReturningFromWasm)?;
248+
let call =
249+
init_wasm_func_call(store, self.code_map, self.stack, engine_func, instance)?;
250+
let results = call.write_params(params).execute()?.write_results(results);
251+
Ok(results)
278252
}
279-
FuncEntity::Host(host_func) => {
253+
FuncEntity::Host(_host_func) => {
280254
// The host function signature is required for properly
281255
// adjusting, inspecting and manipulating the value stack.
282256
// In case the host function returns more values than it takes
283257
// we are required to extend the value stack.
284-
let len_params = host_func.len_params();
285-
let len_results = host_func.len_results();
286-
let max_inout = len_params.max(len_results);
287-
let uninit = self
288-
.stack
289-
.values
290-
.extend_by(usize::from(max_inout), do_nothing)?;
291-
for (uninit, param) in uninit.iter_mut().zip(params.call_params()) {
292-
uninit.write(param);
293-
}
294-
let host_func = *host_func;
295-
self.dispatch_host_func(store, host_func)?;
258+
todo!()
296259
}
297-
};
298-
let results = self.write_results_back(results);
299-
Ok(results)
260+
}
300261
}
301262

302263
/// Resumes the execution of the given [`Func`] using `params` after a host function trapped.
@@ -310,28 +271,29 @@ impl<'engine> EngineExecutor<'engine> {
310271
/// - When encountering a Wasm or host trap during the execution of `func`.
311272
fn resume_func_host_trap<T, Results>(
312273
&mut self,
313-
store: &mut Store<T>,
314-
params: impl CallParams,
315-
caller_results: SlotSpan,
316-
results: Results,
274+
_store: &mut Store<T>,
275+
_params: impl CallParams,
276+
_caller_results: SlotSpan,
277+
_results: Results,
317278
) -> Result<<Results as CallResults>::Results, Error>
318279
where
319280
Results: CallResults,
320281
{
321-
let caller = self
322-
.stack
323-
.calls
324-
.peek()
325-
.expect("must have caller call frame on stack upon function resumption");
326-
let mut caller_sp = unsafe { self.stack.values.stack_ptr_at(caller.base_offset()) };
327-
let call_params = params.call_params();
328-
let len_params = call_params.len();
329-
for (result, param) in caller_results.iter_sized(len_params).zip(call_params) {
330-
unsafe { caller_sp.set(result, param) };
331-
}
332-
self.execute_func(store)?;
333-
let results = self.write_results_back(results);
334-
Ok(results)
282+
// let caller = self
283+
// .stack
284+
// .calls
285+
// .peek()
286+
// .expect("must have caller call frame on stack upon function resumption");
287+
// let mut caller_sp = unsafe { self.stack.values.stack_ptr_at(caller.base_offset()) };
288+
// let call_params = params.call_params();
289+
// let len_params = call_params.len();
290+
// for (result, param) in caller_results.iter_sized(len_params).zip(call_params) {
291+
// unsafe { caller_sp.set(result, param) };
292+
// }
293+
// self.execute_func(store)?;
294+
// let results = self.write_results_back(results);
295+
// Ok(results)
296+
todo!()
335297
}
336298

337299
/// Resumes the execution of the given [`Func`] using `params` after running out of fuel.
@@ -344,25 +306,16 @@ impl<'engine> EngineExecutor<'engine> {
344306
/// - When encountering a Wasm or host trap during the execution of `func`.
345307
fn resume_func_out_of_fuel<T, Results>(
346308
&mut self,
347-
store: &mut Store<T>,
348-
results: Results,
309+
_store: &mut Store<T>,
310+
_results: Results,
349311
) -> Result<<Results as CallResults>::Results, Error>
350312
where
351313
Results: CallResults,
352314
{
353-
self.execute_func(store)?;
354-
let results = self.write_results_back(results);
355-
Ok(results)
356-
}
357-
358-
/// Executes the top most Wasm function on the [`Stack`] until the [`Stack`] is empty.
359-
///
360-
/// # Errors
361-
///
362-
/// When encountering a Wasm or host trap during execution.
363-
#[inline(always)]
364-
fn execute_func<T>(&mut self, store: &mut Store<T>) -> Result<(), Error> {
365-
execute_instrs(store.prune(), self.stack, self.code_map)
315+
// self.execute_func(store)?;
316+
// let results = self.write_results_back(results);
317+
// Ok(results)
318+
todo!()
366319
}
367320

368321
/// Convenience forwarder to dispatch host functions.
@@ -382,22 +335,4 @@ impl<'engine> EngineExecutor<'engine> {
382335
// Ok(())
383336
todo!()
384337
}
385-
386-
/// Writes the results of the function execution back into the `results` buffer.
387-
///
388-
/// # Note
389-
///
390-
/// The value stack is empty after this operation.
391-
///
392-
/// # Panics
393-
///
394-
/// - If the `results` buffer length does not match the remaining amount of stack values.
395-
#[inline(always)]
396-
fn write_results_back<Results>(&mut self, results: Results) -> <Results as CallResults>::Results
397-
where
398-
Results: CallResults,
399-
{
400-
let len_results = results.len_results();
401-
results.call_results(&self.stack.values.as_slice()[..len_results])
402-
}
403338
}

0 commit comments

Comments
 (0)