From 05c20cf08d1a24ce116c26666cf17454edf1d740 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Mon, 7 Oct 2024 20:09:32 +0200 Subject: [PATCH] Clean-up: move test related methods into `tests` module (#1232) * move test related methods into tests module * apply rustfmt --- crates/wasmi/src/engine/mod.rs | 102 ---------------- crates/wasmi/src/engine/tests/mod.rs | 114 ++++++++++++++++++ .../translator/tests/op/call/internal.rs | 2 +- 3 files changed, 115 insertions(+), 103 deletions(-) diff --git a/crates/wasmi/src/engine/mod.rs b/crates/wasmi/src/engine/mod.rs index 3256201f68..d7c7faf68f 100644 --- a/crates/wasmi/src/engine/mod.rs +++ b/crates/wasmi/src/engine/mod.rs @@ -15,11 +15,6 @@ mod utils; #[cfg(test)] mod tests; -#[cfg(test)] -use self::code_map::CompiledFuncRef; -#[cfg(test)] -use crate::{core::UntypedVal, ir::Instruction, ir::RegSpan}; - pub(crate) use self::{ block_type::BlockType, config::FuelCosts, @@ -274,54 +269,6 @@ impl Engine { .init_lazy_func(func_idx, func, bytes, module, func_to_validate) } - /// Resolves the [`EngineFunc`] to the underlying Wasmi bytecode instructions. - /// - /// # Note - /// - /// - This is a variant of [`Engine::resolve_instr`] that returns register - /// machine based bytecode instructions. - /// - This API is mainly intended for unit testing purposes and shall not be used - /// outside of this context. The function bodies are intended to be data private - /// to the Wasmi interpreter. - /// - /// # Errors - /// - /// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized. - /// - /// # Panics - /// - /// - If the [`EngineFunc`] is invalid for the [`Engine`]. - /// - If register machine bytecode translation is disabled. - #[cfg(test)] - pub(crate) fn resolve_instr( - &self, - func: EngineFunc, - index: usize, - ) -> Result, Error> { - self.inner.resolve_instr(func, index) - } - - /// Resolves the function local constant of [`EngineFunc`] at `index` if any. - /// - /// # Note - /// - /// This API is intended for unit testing purposes and shall not be used - /// outside of this context. The function bodies are intended to be data - /// private to the Wasmi interpreter. - /// - /// # Errors - /// - /// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized. - /// - /// # Panics - /// - /// - If the [`EngineFunc`] is invalid for the [`Engine`]. - /// - If register machine bytecode translation is disabled. - #[cfg(test)] - fn get_func_const(&self, func: EngineFunc, index: usize) -> Result, Error> { - self.inner.get_func_const(func, index) - } - /// Executes the given [`Func`] with parameters `params`. /// /// Stores the execution result into `results` upon a successful execution. @@ -739,55 +686,6 @@ impl EngineInner { .init_func_as_uncompiled(func, func_idx, bytes, module, func_to_validate) } - /// Resolves the [`InternalFuncEntity`] for [`EngineFunc`] and applies `f` to it. - /// - /// # Panics - /// - /// If [`EngineFunc`] is invalid for [`Engine`]. - #[cfg(test)] - fn resolve_func<'a, F, R>(&'a self, func: EngineFunc, f: F) -> Result - where - F: FnOnce(CompiledFuncRef<'a>) -> R, - { - // Note: We use `None` so this test-only function will never charge for compilation fuel. - Ok(f(self.code_map.get(None, func)?)) - } - - /// Returns the [`Instruction`] of `func` at `index`. - /// - /// Returns `None` if the function has no instruction at `index`. - /// - /// # Errors - /// - /// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized. - /// - /// # Pancis - /// - /// If `func` cannot be resolved to a function for the [`EngineInner`]. - #[cfg(test)] - fn resolve_instr(&self, func: EngineFunc, index: usize) -> Result, Error> { - self.resolve_func(func, |func| func.instrs().get(index).copied()) - } - - /// Returns the function local constant value of `func` at `index`. - /// - /// Returns `None` if the function has no function local constant at `index`. - /// - /// # Errors - /// - /// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized. - /// - /// # Pancis - /// - /// If `func` cannot be resolved to a function for the [`EngineInner`]. - #[cfg(test)] - fn get_func_const(&self, func: EngineFunc, index: usize) -> Result, Error> { - // Function local constants are stored in reverse order of their indices since - // they are allocated in reverse order to their absolute indices during function - // translation. That is why we need to access them in reverse order. - self.resolve_func(func, |func| func.consts().iter().rev().nth(index).copied()) - } - /// Recycles the given [`Stack`]. fn recycle_stack(&self, stack: Stack) { self.stacks.lock().recycle(stack) diff --git a/crates/wasmi/src/engine/tests/mod.rs b/crates/wasmi/src/engine/tests/mod.rs index 16b0012b33..930691f21c 100644 --- a/crates/wasmi/src/engine/tests/mod.rs +++ b/crates/wasmi/src/engine/tests/mod.rs @@ -1,2 +1,116 @@ mod host_calls; mod many_inout; + +use super::{ + code_map::{CompiledFuncRef, EngineFunc}, + EngineInner, +}; +use crate::{core::UntypedVal, ir::Instruction, Engine, Error}; + +impl Engine { + /// Resolves the [`EngineFunc`] to the underlying Wasmi bytecode instructions. + /// + /// # Note + /// + /// - This is a variant of [`Engine::resolve_instr`] that returns register + /// machine based bytecode instructions. + /// - This API is mainly intended for unit testing purposes and shall not be used + /// outside of this context. The function bodies are intended to be data private + /// to the Wasmi interpreter. + /// + /// # Errors + /// + /// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized. + /// + /// # Panics + /// + /// - If the [`EngineFunc`] is invalid for the [`Engine`]. + /// - If register machine bytecode translation is disabled. + pub(crate) fn resolve_instr( + &self, + func: EngineFunc, + index: usize, + ) -> Result, Error> { + self.inner.resolve_instr(func, index) + } + + /// Resolves the function local constant of [`EngineFunc`] at `index` if any. + /// + /// # Note + /// + /// This API is intended for unit testing purposes and shall not be used + /// outside of this context. The function bodies are intended to be data + /// private to the Wasmi interpreter. + /// + /// # Errors + /// + /// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized. + /// + /// # Panics + /// + /// - If the [`EngineFunc`] is invalid for the [`Engine`]. + /// - If register machine bytecode translation is disabled. + pub(crate) fn get_func_const( + &self, + func: EngineFunc, + index: usize, + ) -> Result, Error> { + self.inner.get_func_const(func, index) + } +} + +impl EngineInner { + /// Resolves the [`InternalFuncEntity`] for [`EngineFunc`] and applies `f` to it. + /// + /// # Panics + /// + /// If [`EngineFunc`] is invalid for [`Engine`]. + pub(crate) fn resolve_func<'a, F, R>(&'a self, func: EngineFunc, f: F) -> Result + where + F: FnOnce(CompiledFuncRef<'a>) -> R, + { + // Note: We use `None` so this test-only function will never charge for compilation fuel. + Ok(f(self.code_map.get(None, func)?)) + } + + /// Returns the [`Instruction`] of `func` at `index`. + /// + /// Returns `None` if the function has no instruction at `index`. + /// + /// # Errors + /// + /// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized. + /// + /// # Pancis + /// + /// If `func` cannot be resolved to a function for the [`EngineInner`]. + pub(crate) fn resolve_instr( + &self, + func: EngineFunc, + index: usize, + ) -> Result, Error> { + self.resolve_func(func, |func| func.instrs().get(index).copied()) + } + + /// Returns the function local constant value of `func` at `index`. + /// + /// Returns `None` if the function has no function local constant at `index`. + /// + /// # Errors + /// + /// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized. + /// + /// # Pancis + /// + /// If `func` cannot be resolved to a function for the [`EngineInner`]. + pub(crate) fn get_func_const( + &self, + func: EngineFunc, + index: usize, + ) -> Result, Error> { + // Function local constants are stored in reverse order of their indices since + // they are allocated in reverse order to their absolute indices during function + // translation. That is why we need to access them in reverse order. + self.resolve_func(func, |func| func.consts().iter().rev().nth(index).copied()) + } +} diff --git a/crates/wasmi/src/engine/translator/tests/op/call/internal.rs b/crates/wasmi/src/engine/translator/tests/op/call/internal.rs index b429b70fba..513a98feef 100644 --- a/crates/wasmi/src/engine/translator/tests/op/call/internal.rs +++ b/crates/wasmi/src/engine/translator/tests/op/call/internal.rs @@ -1,5 +1,5 @@ use super::*; -use crate::engine::{EngineFunc, RegSpan}; +use crate::{engine::EngineFunc, ir::RegSpan}; #[test] #[cfg_attr(miri, ignore)]