From 441dbfa3fff3f21aaaa1d73ec5c5a7a0e6805988 Mon Sep 17 00:00:00 2001 From: metrics Date: Fri, 27 Mar 2026 08:57:13 +0000 Subject: [PATCH] feat: mark several functions as const for improved compile-time evaluation --- crates/herkos-runtime/src/memory.rs | 8 ++++---- crates/herkos-runtime/src/module.rs | 2 +- crates/herkos-runtime/src/ops.rs | 12 ++++++------ crates/herkos-runtime/src/table.rs | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/crates/herkos-runtime/src/memory.rs b/crates/herkos-runtime/src/memory.rs index 096bae1..9780341 100644 --- a/crates/herkos-runtime/src/memory.rs +++ b/crates/herkos-runtime/src/memory.rs @@ -34,7 +34,7 @@ impl IsolatedMemory { /// # Errors /// Returns `ConstructionError::MemoryInitialPagesExceedsMax` if `initial_pages > MAX_PAGES`. #[inline(never)] - pub fn try_new(initial_pages: usize) -> Result { + pub const fn try_new(initial_pages: usize) -> Result { if initial_pages > MAX_PAGES { return Err(crate::ConstructionError::MemoryInitialPagesExceedsMax { initial: initial_pages, @@ -81,13 +81,13 @@ impl IsolatedMemory { /// Current number of active pages. #[inline(always)] - pub fn page_count(&self) -> usize { + pub const fn page_count(&self) -> usize { self.active_pages } /// Current active size in bytes. #[inline(always)] - pub fn active_size(&self) -> usize { + pub const fn active_size(&self) -> usize { self.active_pages * PAGE_SIZE } @@ -109,7 +109,7 @@ impl IsolatedMemory { /// Wasm `memory.size` — returns current page count. #[inline(always)] - pub fn size(&self) -> i32 { + pub const fn size(&self) -> i32 { self.active_pages as i32 } diff --git a/crates/herkos-runtime/src/module.rs b/crates/herkos-runtime/src/module.rs index 0a42888..0cccfcc 100644 --- a/crates/herkos-runtime/src/module.rs +++ b/crates/herkos-runtime/src/module.rs @@ -102,7 +102,7 @@ pub struct LibraryModule { impl LibraryModule { /// Create a new library module with the given globals and table. #[inline] - pub fn new(globals: G, table: Table) -> Self { + pub const fn new(globals: G, table: Table) -> Self { Self { globals, table } } } diff --git a/crates/herkos-runtime/src/ops.rs b/crates/herkos-runtime/src/ops.rs index 250cd11..83a5dab 100644 --- a/crates/herkos-runtime/src/ops.rs +++ b/crates/herkos-runtime/src/ops.rs @@ -131,7 +131,7 @@ pub fn i32_div_u(lhs: i32, rhs: i32) -> WasmResult { /// for this case (because the underlying division overflows), so we handle it /// with an explicit branch. #[inline(never)] -pub fn i32_rem_s(lhs: i32, rhs: i32) -> WasmResult { +pub const fn i32_rem_s(lhs: i32, rhs: i32) -> WasmResult { if rhs == 0 { return Err(WasmTrap::DivisionByZero); } @@ -174,7 +174,7 @@ pub fn i64_div_u(lhs: i64, rhs: i64) -> WasmResult { /// /// Same special case as `i32_rem_s`: `i64::MIN rem_s -1 = 0` per Wasm spec. #[inline(never)] -pub fn i64_rem_s(lhs: i64, rhs: i64) -> WasmResult { +pub const fn i64_rem_s(lhs: i64, rhs: i64) -> WasmResult { if rhs == 0 { return Err(WasmTrap::DivisionByZero); } @@ -198,7 +198,7 @@ pub fn i64_rem_u(lhs: i64, rhs: i64) -> WasmResult { /// Wasm `f32.min`: propagates NaN (unlike Rust's `f32::min` which ignores it). /// Also preserves the Wasm rule `min(-0.0, +0.0) = -0.0`. -pub fn wasm_min_f32(a: f32, b: f32) -> f32 { +pub const fn wasm_min_f32(a: f32, b: f32) -> f32 { if a.is_nan() || b.is_nan() { return f32::NAN; } @@ -213,7 +213,7 @@ pub fn wasm_min_f32(a: f32, b: f32) -> f32 { } /// Wasm `f32.max`: propagates NaN. `max(-0.0, +0.0) = +0.0`. -pub fn wasm_max_f32(a: f32, b: f32) -> f32 { +pub const fn wasm_max_f32(a: f32, b: f32) -> f32 { if a.is_nan() || b.is_nan() { return f32::NAN; } @@ -228,7 +228,7 @@ pub fn wasm_max_f32(a: f32, b: f32) -> f32 { } /// Wasm `f64.min`: propagates NaN. `min(-0.0, +0.0) = -0.0`. -pub fn wasm_min_f64(a: f64, b: f64) -> f64 { +pub const fn wasm_min_f64(a: f64, b: f64) -> f64 { if a.is_nan() || b.is_nan() { return f64::NAN; } @@ -243,7 +243,7 @@ pub fn wasm_min_f64(a: f64, b: f64) -> f64 { } /// Wasm `f64.max`: propagates NaN. `max(-0.0, +0.0) = +0.0`. -pub fn wasm_max_f64(a: f64, b: f64) -> f64 { +pub const fn wasm_max_f64(a: f64, b: f64) -> f64 { if a.is_nan() || b.is_nan() { return f64::NAN; } diff --git a/crates/herkos-runtime/src/table.rs b/crates/herkos-runtime/src/table.rs index 4ef2a45..4fa4914 100644 --- a/crates/herkos-runtime/src/table.rs +++ b/crates/herkos-runtime/src/table.rs @@ -45,7 +45,7 @@ impl Table { /// /// # Errors /// Returns `ConstructionError::TableInitialSizeExceedsMax` if `initial_size > MAX_SIZE`. - pub fn try_new(initial_size: usize) -> Result { + pub const fn try_new(initial_size: usize) -> Result { if initial_size > MAX_SIZE { return Err(crate::ConstructionError::TableInitialSizeExceedsMax { initial: initial_size, @@ -60,7 +60,7 @@ impl Table { /// Current number of active table slots. #[inline(always)] - pub fn size(&self) -> usize { + pub const fn size(&self) -> usize { self.active_size }