Skip to content

Commit 51bac3c

Browse files
chore: fix debug feature
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent 065dc60 commit 51bac3c

18 files changed

Lines changed: 117 additions & 99 deletions

File tree

crates/parser/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
no_crate_inject,
44
attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_assignments, unused_variables))
55
))]
6-
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms, unreachable_pub)]
6+
#![warn(missing_docs, rust_2018_idioms, unreachable_pub)]
77
#![forbid(unsafe_code)]
88
//! See [`tinywasm`](https://docs.rs/tinywasm) for documentation.
99

crates/tinywasm/src/engine.rs

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
1-
use core::fmt::Debug;
2-
31
use alloc::sync::Arc;
42

53
/// Global configuration for the WebAssembly interpreter
64
///
75
/// Can be cheaply cloned and shared across multiple executions and threads.
8-
#[derive(Clone)]
6+
#[derive(Clone, Default)]
7+
#[cfg_attr(feature = "debug", derive(Debug))]
98
pub struct Engine {
109
pub(crate) inner: Arc<EngineInner>,
1110
}
1211

13-
impl Debug for Engine {
14-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
15-
f.debug_struct("Engine").finish()
16-
}
17-
}
18-
1912
impl Engine {
2013
/// Create a new engine with the given configuration
2114
pub fn new(config: Config) -> Self {
@@ -28,15 +21,10 @@ impl Engine {
2821
}
2922
}
3023

31-
impl Default for Engine {
32-
fn default() -> Engine {
33-
Engine::new(Config::default())
34-
}
35-
}
36-
24+
#[derive(Default)]
25+
#[cfg_attr(feature = "debug", derive(Debug))]
3726
pub(crate) struct EngineInner {
3827
pub(crate) config: Config,
39-
// pub(crate) allocator: Box<dyn Allocator + Send + Sync>,
4028
}
4129

4230
/// Fuel accounting policy for budgeted execution.
@@ -51,36 +39,36 @@ pub enum FuelPolicy {
5139
Weighted,
5240
}
5341

54-
/// Default initial size for the 32-bit value stack (i32, f32 values).
55-
pub const DEFAULT_VALUE_STACK_32_SIZE: usize = 64 * 1024; // 64k slots
42+
/// Default size for the 32-bit value stack (i32, f32 values).
43+
pub const DEFAULT_VALUE_STACK_32_SIZE: usize = 32 * 1024; // 32k slots
5644

57-
/// Default initial size for the 64-bit value stack (i64, f64 values).
45+
/// Default size for the 64-bit value stack (i64, f64 values).
5846
pub const DEFAULT_VALUE_STACK_64_SIZE: usize = 32 * 1024; // 32k slots
5947

60-
/// Default initial size for the 128-bit value stack (v128 values).
48+
/// Default size for the 128-bit value stack (v128 values).
6149
pub const DEFAULT_VALUE_STACK_128_SIZE: usize = 4 * 1024; // 4k slots
6250

63-
/// Default initial size for the reference value stack (funcref, externref values).
51+
/// Default size for the reference value stack (funcref, externref values).
6452
pub const DEFAULT_VALUE_STACK_REF_SIZE: usize = 4 * 1024; // 4k slots
6553

66-
/// Default initial size for the call stack (function frames).
67-
pub const DEFAULT_CALL_STACK_SIZE: usize = 2048; // 1024 frames
54+
/// Default maximum size for the call stack (function frames).
55+
pub const DEFAULT_MAX_CALL_STACK_SIZE: usize = 1024; // 1024 frames
6856

6957
/// Configuration for the WebAssembly interpreter
7058
#[derive(Clone)]
7159
#[cfg_attr(feature = "debug", derive(Debug))]
7260
#[non_exhaustive]
7361
pub struct Config {
74-
/// Initial size of the 32-bit value stack (i32, f32 values).
62+
/// Size of the 32-bit value stack (i32, f32 values).
7563
pub stack_32_size: usize,
76-
/// Initial size of the 64-bit value stack (i64, f64 values).
64+
/// Size of the 64-bit value stack (i64, f64 values).
7765
pub stack_64_size: usize,
78-
/// Initial size of the 128-bit value stack (v128 values).
66+
/// Size of the 128-bit value stack (v128 values).
7967
pub stack_128_size: usize,
80-
/// Initial size of the reference value stack (funcref, externref values).
68+
/// Size of the reference value stack (funcref, externref values).
8169
pub stack_ref_size: usize,
82-
/// Initial size of the call stack.
83-
pub call_stack_size: usize,
70+
/// Maximum size of the call stack
71+
pub max_call_stack_size: usize,
8472
/// Fuel accounting policy used by budgeted execution.
8573
pub fuel_policy: FuelPolicy,
8674
}
@@ -105,7 +93,7 @@ impl Default for Config {
10593
stack_64_size: DEFAULT_VALUE_STACK_64_SIZE,
10694
stack_128_size: DEFAULT_VALUE_STACK_128_SIZE,
10795
stack_ref_size: DEFAULT_VALUE_STACK_REF_SIZE,
108-
call_stack_size: DEFAULT_CALL_STACK_SIZE,
96+
max_call_stack_size: DEFAULT_MAX_CALL_STACK_SIZE,
10997
fuel_policy: FuelPolicy::default(),
11098
}
11199
}

crates/tinywasm/src/error.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use alloc::string::{String, ToString};
22
use alloc::vec::Vec;
3+
use core::fmt::Debug;
34
use core::{fmt::Display, ops::ControlFlow};
45
use tinywasm_types::FuncType;
56
use tinywasm_types::archive::TwasmError;
@@ -8,7 +9,6 @@ use tinywasm_types::archive::TwasmError;
89
pub use tinywasm_parser::ParseError;
910

1011
/// Errors that can occur for `TinyWasm` operations
11-
#[cfg_attr(feature = "debug", derive(Debug))]
1212
#[non_exhaustive]
1313
pub enum Error {
1414
/// A WebAssembly trap occurred
@@ -49,9 +49,9 @@ pub enum Error {
4949
Twasm(TwasmError),
5050
}
5151

52-
#[derive(Debug)]
5352
/// Errors that can occur when linking a WebAssembly module
5453
#[non_exhaustive]
54+
#[cfg_attr(feature = "debug", derive(Debug))]
5555
pub enum LinkingError {
5656
/// An unknown import was encountered
5757
UnknownImport {
@@ -80,11 +80,11 @@ impl LinkingError {
8080
}
8181
}
8282

83-
#[derive(Debug)]
8483
/// A WebAssembly trap
8584
///
8685
/// See <https://webassembly.github.io/spec/core/intro/overview.html#trap>
8786
#[non_exhaustive]
87+
#[cfg_attr(feature = "debug", derive(Debug))]
8888
pub enum Trap {
8989
/// An unreachable instruction was executed
9090
Unreachable,
@@ -206,9 +206,12 @@ impl Display for Error {
206206
Self::InvalidLabelType => write!(f, "invalid label type"),
207207
Self::Other(message) => write!(f, "unknown error: {message}"),
208208
Self::UnsupportedFeature(feature) => write!(f, "unsupported feature: {feature}"),
209+
#[cfg(feature = "debug")]
209210
Self::InvalidHostFnReturn { expected, actual } => {
210211
write!(f, "invalid host function return: expected={expected:?}, actual={actual:?}")
211212
}
213+
#[cfg(not(feature = "debug"))]
214+
Self::InvalidHostFnReturn { .. } => write!(f, "invalid host function return"),
212215
Self::InvalidStore => write!(f, "invalid store"),
213216
}
214217
}
@@ -244,13 +247,22 @@ impl Display for Trap {
244247
Self::UninitializedElement { index } => {
245248
write!(f, "uninitialized element: index={index}")
246249
}
250+
#[cfg(feature = "debug")]
247251
Self::IndirectCallTypeMismatch { expected, actual } => {
248252
write!(f, "indirect call type mismatch: expected={expected:?}, actual={actual:?}")
249253
}
254+
#[cfg(not(feature = "debug"))]
255+
Self::IndirectCallTypeMismatch { .. } => write!(f, "indirect call type mismatch"),
250256
}
251257
}
252258
}
253259

260+
impl Debug for Error {
261+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
262+
write!(f, "{}", self)
263+
}
264+
}
265+
254266
impl core::error::Error for Error {}
255267

256268
#[cfg(feature = "parser")]

crates/tinywasm/src/func.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ pub(crate) struct ExecutionState {
1919
pub(crate) callframe: CallFrame,
2020
}
2121

22-
#[derive(Debug)]
2322
/// A function handle
23+
#[cfg_attr(feature = "debug", derive(Debug))]
2424
pub struct FuncHandle {
2525
pub(crate) module_addr: ModuleInstanceAddr,
2626
pub(crate) addr: u32,
2727
pub(crate) ty: FuncType,
2828
}
2929

30-
#[derive(Debug)]
3130
/// Resumable execution for an untyped function call.
31+
#[cfg_attr(feature = "debug", derive(Debug))]
3232
pub struct FuncExecution<'store> {
3333
store: &'store mut Store,
3434
state: FuncExecutionState,
@@ -40,8 +40,8 @@ enum FuncExecutionState {
4040
Completed { result: Option<Vec<WasmValue>> },
4141
}
4242

43-
#[derive(Debug)]
4443
/// Resumable execution for a typed function call.
44+
#[cfg_attr(feature = "debug", derive(Debug))]
4545
pub struct FuncExecutionTyped<'store, R> {
4646
execution: FuncExecution<'store>,
4747
marker: core::marker::PhantomData<R>,
@@ -212,8 +212,8 @@ fn collect_call_results(store: &mut Store, func_ty: &FuncType) -> Result<Vec<Was
212212
Ok(res)
213213
}
214214

215-
#[derive(Debug)]
216215
/// A typed function handle
216+
#[cfg_attr(feature = "debug", derive(Debug))]
217217
pub struct FuncHandleTyped<P, R> {
218218
/// The underlying function handle
219219
pub func: FuncHandle,

crates/tinywasm/src/imports.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,15 @@ impl FuncContext<'_> {
100100
}
101101
}
102102

103+
#[cfg(feature = "debug")]
103104
impl Debug for HostFunction {
104105
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
105106
f.debug_struct("HostFunction").field("ty", &self.ty).field("func", &"...").finish()
106107
}
107108
}
108109

109-
#[derive(Debug, Clone)]
110+
#[derive(Clone)]
111+
#[cfg_attr(feature = "debug", derive(Debug))]
110112
#[non_exhaustive]
111113
/// An external value
112114
pub enum Extern {
@@ -219,7 +221,6 @@ impl From<&Import> for ExternName {
219221
}
220222
}
221223

222-
#[derive(Debug, Default)]
223224
/// Imports for a module instance
224225
///
225226
/// This is used to link a module instance to its imports
@@ -254,7 +255,8 @@ impl From<&Import> for ExternName {
254255
///
255256
/// Note that module instance addresses for [`Imports::link_module`] can be obtained from [`crate::ModuleInstance::id`].
256257
/// Now, the imports object can be passed to [`crate::ModuleInstance::instantiate`].
257-
#[derive(Clone)]
258+
#[derive(Default, Clone)]
259+
#[cfg_attr(feature = "debug", derive(Debug))]
258260
pub struct Imports {
259261
values: BTreeMap<ExternName, Extern>,
260262
modules: BTreeMap<String, ModuleInstanceAddr>,
@@ -322,9 +324,19 @@ impl Imports {
322324
None
323325
}
324326

325-
fn compare_types<T: Debug + PartialEq>(import: &Import, actual: &T, expected: &T) -> Result<()> {
327+
#[cfg(not(feature = "debug"))]
328+
fn compare_types<T: PartialEq>(import: &Import, actual: &T, expected: &T) -> Result<()> {
329+
if expected != actual {
330+
log::error!("failed to link import {}", import.name);
331+
return Err(LinkingError::incompatible_import_type(import).into());
332+
}
333+
Ok(())
334+
}
335+
336+
#[cfg(feature = "debug")]
337+
fn compare_types<T: PartialEq + Debug>(import: &Import, actual: &T, expected: &T) -> Result<()> {
326338
if expected != actual {
327-
log::error!("failed to link import {}, expected {:?}, got {:?}", import.name, expected, actual);
339+
log::error!("failed to link import {}: expected {:?}, got {:?}", import.name, expected, actual);
328340
return Err(LinkingError::incompatible_import_type(import).into());
329341
}
330342
Ok(())

crates/tinywasm/src/interpreter/stack/call_stack.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub(crate) struct CallStack {
1010

1111
impl CallStack {
1212
pub(crate) fn new(config: &crate::engine::Config) -> Self {
13-
Self { stack: Vec::with_capacity(config.call_stack_size) }
13+
Self { stack: Vec::with_capacity(config.max_call_stack_size) }
1414
}
1515

1616
pub(crate) fn clear(&mut self) {

crates/tinywasm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
no_crate_inject,
44
attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_assignments, unused_variables))
55
))]
6-
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms, unreachable_pub)]
6+
#![warn(missing_docs, rust_2018_idioms, unreachable_pub)]
77
#![deny(unsafe_code)]
88

99
//! A tiny WebAssembly Runtime written in Rust

crates/tinywasm/src/store/function.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use crate::Function;
22
use alloc::rc::Rc;
33
use tinywasm_types::*;
44

5-
#[derive(Debug, Clone)]
65
/// A WebAssembly Function Instance
76
///
87
/// See <https://webassembly.github.io/spec/core/exec/runtime.html#function-instances>
8+
#[derive(Clone)]
9+
#[cfg_attr(feature = "debug", derive(Debug))]
910
pub(crate) struct FunctionInstance {
1011
pub(crate) func: Function,
1112
pub(crate) owner: ModuleInstanceAddr, // index into store.module_instances, none for host functions

crates/tinywasm/src/store/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use alloc::rc::Rc;
22
use alloc::{boxed::Box, format, string::ToString, vec::Vec};
3-
use core::fmt::Debug;
43
use core::sync::atomic::{AtomicUsize, Ordering};
54
use tinywasm_types::*;
65

@@ -40,7 +39,8 @@ pub struct Store {
4039
pub(crate) stack: Stack,
4140
}
4241

43-
impl Debug for Store {
42+
#[cfg(feature = "debug")]
43+
impl core::fmt::Debug for Store {
4444
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
4545
f.debug_struct("Store")
4646
.field("id", &self.id)
@@ -323,9 +323,14 @@ impl Store {
323323
})?;
324324
self.state.globals[addr as usize].value.get().unwrap_ref()
325325
}
326+
#[cfg(feature = "debug")]
326327
ElementItem::Expr(item) => {
327328
return Err(Error::UnsupportedFeature(format!("const expression other than ref: {item:?}")));
328329
}
330+
#[cfg(not(feature = "debug"))]
331+
ElementItem::Expr(_) => {
332+
return Err(Error::UnsupportedFeature("const expression other than ref".to_string()));
333+
}
329334
};
330335

331336
Ok(res)
@@ -467,7 +472,10 @@ impl Store {
467472
TinyWasmValue::Value64(i) => i as i64,
468473
other => return Err(Error::Other(format!("expected i32 or i64, got {other:?}"))),
469474
},
475+
#[cfg(feature = "debug")]
470476
other => return Err(Error::Other(format!("expected i32, got {other:?}"))),
477+
#[cfg(not(feature = "debug"))]
478+
_ => return Err(Error::Other("expected i32 or i64".to_string())),
471479
})
472480
}
473481

crates/tinywasm/src/store/table.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::log;
21
use crate::{Error, Result, Trap};
32
use alloc::{vec, vec::Vec};
43
use tinywasm_types::*;
@@ -142,9 +141,7 @@ impl TableInstance {
142141
if end > self.elements.len() || end < offset {
143142
return Err(crate::Trap::TableOutOfBounds { offset, len: init.len(), max: self.elements.len() }.into());
144143
}
145-
146144
self.elements[offset..end].copy_from_slice(init);
147-
log::debug!("table: {:?}", self.elements);
148145
Ok(())
149146
}
150147
}

0 commit comments

Comments
 (0)