Skip to content

Commit

Permalink
Implement {get,set}_points_used.
Browse files Browse the repository at this point in the history
  • Loading branch information
losfair committed Jun 4, 2019
1 parent 69944c1 commit 0867208
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
13 changes: 6 additions & 7 deletions lib/middleware-common/src/metering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ impl FunctionMiddleware for Metering {
}

/// Returns the number of points used by a function call for metering
pub fn get_points_used(_instance: &Instance) -> u64 {
unimplemented!()
pub fn get_points_used(instance: &Instance) -> u64 {
instance.get_internal(&INTERNAL_FIELD)
}

/// Sets the value of points used
pub fn set_points_used(_instance: &mut Instance, _value: u64) {
unimplemented!()
pub fn set_points_used(instance: &mut Instance, value: u64) {
instance.set_internal(&INTERNAL_FIELD, value);
}

#[cfg(all(test, feature = "singlepass"))]
Expand Down Expand Up @@ -246,7 +246,7 @@ mod tests {
assert_eq!(value, 7);

// verify is uses the correct number of points
assert_eq!(get_points_used(&instance), 42); // TODO need to update assertion to actual points used.
assert_eq!(get_points_used(&instance), 74);
}

#[test]
Expand All @@ -269,8 +269,7 @@ mod tests {
assert_eq!(result.is_err(), true); // TODO assert that the trap is caused by PointsExausted

// verify is uses the correct number of points
assert_eq!(get_points_used(&instance), 99); // TODO need to update assertion to actual points used.
// TODO should points used be close to limit or at limit when trapping
assert_eq!(get_points_used(&instance), 109); // Used points will be slightly more than `limit` because of the way we do gas checking.
}

}
10 changes: 9 additions & 1 deletion lib/runtime-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
table::Table,
typed_func::{Func, Wasm, WasmTrapInfo, WasmTypeList},
types::{FuncIndex, FuncSig, GlobalIndex, LocalOrImport, MemoryIndex, TableIndex, Type, Value},
vm,
vm::{self, InternalField},
};
use smallvec::{smallvec, SmallVec};
use std::{mem, ptr::NonNull, sync::Arc};
Expand Down Expand Up @@ -372,6 +372,14 @@ impl Instance {
pub fn module(&self) -> Module {
Module::new(Arc::clone(&self.module))
}

pub fn get_internal(&self, field: &InternalField) -> u64 {
self.inner.backing.internals.0[field.index()]
}

pub fn set_internal(&mut self, field: &InternalField, value: u64) {
self.inner.backing.internals.0[field.index()] = value;
}
}

impl InstanceInner {
Expand Down

0 comments on commit 0867208

Please sign in to comment.