Skip to content

Commit

Permalink
wip on metering host fns
Browse files Browse the repository at this point in the history
  • Loading branch information
thedavidmeister committed Oct 24, 2023
1 parent 4cb068d commit c3c5055
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions crates/host/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ use std::num::TryFromIntError;

use crate::guest::read_bytes;
use crate::prelude::*;
use wasmer::Global;
use wasmer::Memory;
use wasmer::StoreMut;
use wasmer::TypedFunction;
use wasmer_middlewares::metering::MeteringPoints;

#[derive(Clone, Default)]
pub struct Env {
pub memory: Option<Memory>,
pub allocate: Option<TypedFunction<i32, i32>>,
pub deallocate: Option<TypedFunction<(i32, i32), ()>>,
pub wasmer_metering_points_exhausted: Option<Global>,
pub wasmer_metering_remaining_points: Option<Global>,
}

impl Env {
Expand Down Expand Up @@ -97,4 +101,31 @@ impl Env {
}
}
}

/// Mimics upstream function of the same name but accesses the global directly from env.
/// https://github.com/wasmerio/wasmer/blob/master/lib/middlewares/src/metering.rs#L285
pub fn get_remaining_points(
&self,
store_mut: &mut StoreMut,
) -> Result<MeteringPoints, wasmer::RuntimeError> {
let exhaused: i32 = self
.wasmer_metering_points_exhausted
.ok_or(wasm_error!(WasmErrorInner::Memory))?
.get(store_mut)
.try_into()
.map_err(|_| wasm_error!(WasmErrorInner::PointerMap))?;

if exhaused > 0 {
return Ok(MeteringPoints::Exhausted);
}

let points = self
.wasmer_metering_remaining_points
.ok_or(wasm_error!(WasmErrorInner::Memory))?
.get(store_mut)
.try_into()
.map_err(|_| wasm_error!(WasmErrorInner::PointerMap))?;

Ok(MeteringPoints::Remaining(points))
}
}

0 comments on commit c3c5055

Please sign in to comment.