Skip to content

Commit 05c20cf

Browse files
authored
Clean-up: move test related methods into tests module (#1232)
* move test related methods into tests module * apply rustfmt
1 parent 8f76eef commit 05c20cf

File tree

3 files changed

+115
-103
lines changed

3 files changed

+115
-103
lines changed

crates/wasmi/src/engine/mod.rs

Lines changed: 0 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ mod utils;
1515
#[cfg(test)]
1616
mod tests;
1717

18-
#[cfg(test)]
19-
use self::code_map::CompiledFuncRef;
20-
#[cfg(test)]
21-
use crate::{core::UntypedVal, ir::Instruction, ir::RegSpan};
22-
2318
pub(crate) use self::{
2419
block_type::BlockType,
2520
config::FuelCosts,
@@ -274,54 +269,6 @@ impl Engine {
274269
.init_lazy_func(func_idx, func, bytes, module, func_to_validate)
275270
}
276271

277-
/// Resolves the [`EngineFunc`] to the underlying Wasmi bytecode instructions.
278-
///
279-
/// # Note
280-
///
281-
/// - This is a variant of [`Engine::resolve_instr`] that returns register
282-
/// machine based bytecode instructions.
283-
/// - This API is mainly intended for unit testing purposes and shall not be used
284-
/// outside of this context. The function bodies are intended to be data private
285-
/// to the Wasmi interpreter.
286-
///
287-
/// # Errors
288-
///
289-
/// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized.
290-
///
291-
/// # Panics
292-
///
293-
/// - If the [`EngineFunc`] is invalid for the [`Engine`].
294-
/// - If register machine bytecode translation is disabled.
295-
#[cfg(test)]
296-
pub(crate) fn resolve_instr(
297-
&self,
298-
func: EngineFunc,
299-
index: usize,
300-
) -> Result<Option<Instruction>, Error> {
301-
self.inner.resolve_instr(func, index)
302-
}
303-
304-
/// Resolves the function local constant of [`EngineFunc`] at `index` if any.
305-
///
306-
/// # Note
307-
///
308-
/// This API is intended for unit testing purposes and shall not be used
309-
/// outside of this context. The function bodies are intended to be data
310-
/// private to the Wasmi interpreter.
311-
///
312-
/// # Errors
313-
///
314-
/// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized.
315-
///
316-
/// # Panics
317-
///
318-
/// - If the [`EngineFunc`] is invalid for the [`Engine`].
319-
/// - If register machine bytecode translation is disabled.
320-
#[cfg(test)]
321-
fn get_func_const(&self, func: EngineFunc, index: usize) -> Result<Option<UntypedVal>, Error> {
322-
self.inner.get_func_const(func, index)
323-
}
324-
325272
/// Executes the given [`Func`] with parameters `params`.
326273
///
327274
/// Stores the execution result into `results` upon a successful execution.
@@ -739,55 +686,6 @@ impl EngineInner {
739686
.init_func_as_uncompiled(func, func_idx, bytes, module, func_to_validate)
740687
}
741688

742-
/// Resolves the [`InternalFuncEntity`] for [`EngineFunc`] and applies `f` to it.
743-
///
744-
/// # Panics
745-
///
746-
/// If [`EngineFunc`] is invalid for [`Engine`].
747-
#[cfg(test)]
748-
fn resolve_func<'a, F, R>(&'a self, func: EngineFunc, f: F) -> Result<R, Error>
749-
where
750-
F: FnOnce(CompiledFuncRef<'a>) -> R,
751-
{
752-
// Note: We use `None` so this test-only function will never charge for compilation fuel.
753-
Ok(f(self.code_map.get(None, func)?))
754-
}
755-
756-
/// Returns the [`Instruction`] of `func` at `index`.
757-
///
758-
/// Returns `None` if the function has no instruction at `index`.
759-
///
760-
/// # Errors
761-
///
762-
/// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized.
763-
///
764-
/// # Pancis
765-
///
766-
/// If `func` cannot be resolved to a function for the [`EngineInner`].
767-
#[cfg(test)]
768-
fn resolve_instr(&self, func: EngineFunc, index: usize) -> Result<Option<Instruction>, Error> {
769-
self.resolve_func(func, |func| func.instrs().get(index).copied())
770-
}
771-
772-
/// Returns the function local constant value of `func` at `index`.
773-
///
774-
/// Returns `None` if the function has no function local constant at `index`.
775-
///
776-
/// # Errors
777-
///
778-
/// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized.
779-
///
780-
/// # Pancis
781-
///
782-
/// If `func` cannot be resolved to a function for the [`EngineInner`].
783-
#[cfg(test)]
784-
fn get_func_const(&self, func: EngineFunc, index: usize) -> Result<Option<UntypedVal>, Error> {
785-
// Function local constants are stored in reverse order of their indices since
786-
// they are allocated in reverse order to their absolute indices during function
787-
// translation. That is why we need to access them in reverse order.
788-
self.resolve_func(func, |func| func.consts().iter().rev().nth(index).copied())
789-
}
790-
791689
/// Recycles the given [`Stack`].
792690
fn recycle_stack(&self, stack: Stack) {
793691
self.stacks.lock().recycle(stack)
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,116 @@
11
mod host_calls;
22
mod many_inout;
3+
4+
use super::{
5+
code_map::{CompiledFuncRef, EngineFunc},
6+
EngineInner,
7+
};
8+
use crate::{core::UntypedVal, ir::Instruction, Engine, Error};
9+
10+
impl Engine {
11+
/// Resolves the [`EngineFunc`] to the underlying Wasmi bytecode instructions.
12+
///
13+
/// # Note
14+
///
15+
/// - This is a variant of [`Engine::resolve_instr`] that returns register
16+
/// machine based bytecode instructions.
17+
/// - This API is mainly intended for unit testing purposes and shall not be used
18+
/// outside of this context. The function bodies are intended to be data private
19+
/// to the Wasmi interpreter.
20+
///
21+
/// # Errors
22+
///
23+
/// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized.
24+
///
25+
/// # Panics
26+
///
27+
/// - If the [`EngineFunc`] is invalid for the [`Engine`].
28+
/// - If register machine bytecode translation is disabled.
29+
pub(crate) fn resolve_instr(
30+
&self,
31+
func: EngineFunc,
32+
index: usize,
33+
) -> Result<Option<Instruction>, Error> {
34+
self.inner.resolve_instr(func, index)
35+
}
36+
37+
/// Resolves the function local constant of [`EngineFunc`] at `index` if any.
38+
///
39+
/// # Note
40+
///
41+
/// This API is intended for unit testing purposes and shall not be used
42+
/// outside of this context. The function bodies are intended to be data
43+
/// private to the Wasmi interpreter.
44+
///
45+
/// # Errors
46+
///
47+
/// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized.
48+
///
49+
/// # Panics
50+
///
51+
/// - If the [`EngineFunc`] is invalid for the [`Engine`].
52+
/// - If register machine bytecode translation is disabled.
53+
pub(crate) fn get_func_const(
54+
&self,
55+
func: EngineFunc,
56+
index: usize,
57+
) -> Result<Option<UntypedVal>, Error> {
58+
self.inner.get_func_const(func, index)
59+
}
60+
}
61+
62+
impl EngineInner {
63+
/// Resolves the [`InternalFuncEntity`] for [`EngineFunc`] and applies `f` to it.
64+
///
65+
/// # Panics
66+
///
67+
/// If [`EngineFunc`] is invalid for [`Engine`].
68+
pub(crate) fn resolve_func<'a, F, R>(&'a self, func: EngineFunc, f: F) -> Result<R, Error>
69+
where
70+
F: FnOnce(CompiledFuncRef<'a>) -> R,
71+
{
72+
// Note: We use `None` so this test-only function will never charge for compilation fuel.
73+
Ok(f(self.code_map.get(None, func)?))
74+
}
75+
76+
/// Returns the [`Instruction`] of `func` at `index`.
77+
///
78+
/// Returns `None` if the function has no instruction at `index`.
79+
///
80+
/// # Errors
81+
///
82+
/// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized.
83+
///
84+
/// # Pancis
85+
///
86+
/// If `func` cannot be resolved to a function for the [`EngineInner`].
87+
pub(crate) fn resolve_instr(
88+
&self,
89+
func: EngineFunc,
90+
index: usize,
91+
) -> Result<Option<Instruction>, Error> {
92+
self.resolve_func(func, |func| func.instrs().get(index).copied())
93+
}
94+
95+
/// Returns the function local constant value of `func` at `index`.
96+
///
97+
/// Returns `None` if the function has no function local constant at `index`.
98+
///
99+
/// # Errors
100+
///
101+
/// If the `func` fails Wasm to Wasmi bytecode translation after it was lazily initialized.
102+
///
103+
/// # Pancis
104+
///
105+
/// If `func` cannot be resolved to a function for the [`EngineInner`].
106+
pub(crate) fn get_func_const(
107+
&self,
108+
func: EngineFunc,
109+
index: usize,
110+
) -> Result<Option<UntypedVal>, Error> {
111+
// Function local constants are stored in reverse order of their indices since
112+
// they are allocated in reverse order to their absolute indices during function
113+
// translation. That is why we need to access them in reverse order.
114+
self.resolve_func(func, |func| func.consts().iter().rev().nth(index).copied())
115+
}
116+
}

crates/wasmi/src/engine/translator/tests/op/call/internal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::*;
2-
use crate::engine::{EngineFunc, RegSpan};
2+
use crate::{engine::EngineFunc, ir::RegSpan};
33

44
#[test]
55
#[cfg_attr(miri, ignore)]

0 commit comments

Comments
 (0)