Skip to content

Commit 441dbfa

Browse files
author
metrics
committed
feat: mark several functions as const for improved compile-time evaluation
1 parent 4427973 commit 441dbfa

4 files changed

Lines changed: 13 additions & 13 deletions

File tree

crates/herkos-runtime/src/memory.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl<const MAX_PAGES: usize> IsolatedMemory<MAX_PAGES> {
3434
/// # Errors
3535
/// Returns `ConstructionError::MemoryInitialPagesExceedsMax` if `initial_pages > MAX_PAGES`.
3636
#[inline(never)]
37-
pub fn try_new(initial_pages: usize) -> Result<Self, crate::ConstructionError> {
37+
pub const fn try_new(initial_pages: usize) -> Result<Self, crate::ConstructionError> {
3838
if initial_pages > MAX_PAGES {
3939
return Err(crate::ConstructionError::MemoryInitialPagesExceedsMax {
4040
initial: initial_pages,
@@ -81,13 +81,13 @@ impl<const MAX_PAGES: usize> IsolatedMemory<MAX_PAGES> {
8181

8282
/// Current number of active pages.
8383
#[inline(always)]
84-
pub fn page_count(&self) -> usize {
84+
pub const fn page_count(&self) -> usize {
8585
self.active_pages
8686
}
8787

8888
/// Current active size in bytes.
8989
#[inline(always)]
90-
pub fn active_size(&self) -> usize {
90+
pub const fn active_size(&self) -> usize {
9191
self.active_pages * PAGE_SIZE
9292
}
9393

@@ -109,7 +109,7 @@ impl<const MAX_PAGES: usize> IsolatedMemory<MAX_PAGES> {
109109

110110
/// Wasm `memory.size` — returns current page count.
111111
#[inline(always)]
112-
pub fn size(&self) -> i32 {
112+
pub const fn size(&self) -> i32 {
113113
self.active_pages as i32
114114
}
115115

crates/herkos-runtime/src/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub struct LibraryModule<G, const TABLE_SIZE: usize> {
102102
impl<G, const TABLE_SIZE: usize> LibraryModule<G, TABLE_SIZE> {
103103
/// Create a new library module with the given globals and table.
104104
#[inline]
105-
pub fn new(globals: G, table: Table<TABLE_SIZE>) -> Self {
105+
pub const fn new(globals: G, table: Table<TABLE_SIZE>) -> Self {
106106
Self { globals, table }
107107
}
108108
}

crates/herkos-runtime/src/ops.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub fn i32_div_u(lhs: i32, rhs: i32) -> WasmResult<i32> {
131131
/// for this case (because the underlying division overflows), so we handle it
132132
/// with an explicit branch.
133133
#[inline(never)]
134-
pub fn i32_rem_s(lhs: i32, rhs: i32) -> WasmResult<i32> {
134+
pub const fn i32_rem_s(lhs: i32, rhs: i32) -> WasmResult<i32> {
135135
if rhs == 0 {
136136
return Err(WasmTrap::DivisionByZero);
137137
}
@@ -174,7 +174,7 @@ pub fn i64_div_u(lhs: i64, rhs: i64) -> WasmResult<i64> {
174174
///
175175
/// Same special case as `i32_rem_s`: `i64::MIN rem_s -1 = 0` per Wasm spec.
176176
#[inline(never)]
177-
pub fn i64_rem_s(lhs: i64, rhs: i64) -> WasmResult<i64> {
177+
pub const fn i64_rem_s(lhs: i64, rhs: i64) -> WasmResult<i64> {
178178
if rhs == 0 {
179179
return Err(WasmTrap::DivisionByZero);
180180
}
@@ -198,7 +198,7 @@ pub fn i64_rem_u(lhs: i64, rhs: i64) -> WasmResult<i64> {
198198

199199
/// Wasm `f32.min`: propagates NaN (unlike Rust's `f32::min` which ignores it).
200200
/// Also preserves the Wasm rule `min(-0.0, +0.0) = -0.0`.
201-
pub fn wasm_min_f32(a: f32, b: f32) -> f32 {
201+
pub const fn wasm_min_f32(a: f32, b: f32) -> f32 {
202202
if a.is_nan() || b.is_nan() {
203203
return f32::NAN;
204204
}
@@ -213,7 +213,7 @@ pub fn wasm_min_f32(a: f32, b: f32) -> f32 {
213213
}
214214

215215
/// Wasm `f32.max`: propagates NaN. `max(-0.0, +0.0) = +0.0`.
216-
pub fn wasm_max_f32(a: f32, b: f32) -> f32 {
216+
pub const fn wasm_max_f32(a: f32, b: f32) -> f32 {
217217
if a.is_nan() || b.is_nan() {
218218
return f32::NAN;
219219
}
@@ -228,7 +228,7 @@ pub fn wasm_max_f32(a: f32, b: f32) -> f32 {
228228
}
229229

230230
/// Wasm `f64.min`: propagates NaN. `min(-0.0, +0.0) = -0.0`.
231-
pub fn wasm_min_f64(a: f64, b: f64) -> f64 {
231+
pub const fn wasm_min_f64(a: f64, b: f64) -> f64 {
232232
if a.is_nan() || b.is_nan() {
233233
return f64::NAN;
234234
}
@@ -243,7 +243,7 @@ pub fn wasm_min_f64(a: f64, b: f64) -> f64 {
243243
}
244244

245245
/// Wasm `f64.max`: propagates NaN. `max(-0.0, +0.0) = +0.0`.
246-
pub fn wasm_max_f64(a: f64, b: f64) -> f64 {
246+
pub const fn wasm_max_f64(a: f64, b: f64) -> f64 {
247247
if a.is_nan() || b.is_nan() {
248248
return f64::NAN;
249249
}

crates/herkos-runtime/src/table.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl<const MAX_SIZE: usize> Table<MAX_SIZE> {
4545
///
4646
/// # Errors
4747
/// Returns `ConstructionError::TableInitialSizeExceedsMax` if `initial_size > MAX_SIZE`.
48-
pub fn try_new(initial_size: usize) -> Result<Self, crate::ConstructionError> {
48+
pub const fn try_new(initial_size: usize) -> Result<Self, crate::ConstructionError> {
4949
if initial_size > MAX_SIZE {
5050
return Err(crate::ConstructionError::TableInitialSizeExceedsMax {
5151
initial: initial_size,
@@ -60,7 +60,7 @@ impl<const MAX_SIZE: usize> Table<MAX_SIZE> {
6060

6161
/// Current number of active table slots.
6262
#[inline(always)]
63-
pub fn size(&self) -> usize {
63+
pub const fn size(&self) -> usize {
6464
self.active_size
6565
}
6666

0 commit comments

Comments
 (0)