Implement {get,set}_points_used.

This commit is contained in:
losfair
2019-06-05 02:28:19 +08:00
parent 69944c1dc2
commit 0867208e0c
2 changed files with 15 additions and 8 deletions

View File

@ -112,13 +112,13 @@ impl FunctionMiddleware for Metering {
} }
/// Returns the number of points used by a function call for metering /// Returns the number of points used by a function call for metering
pub fn get_points_used(_instance: &Instance) -> u64 { pub fn get_points_used(instance: &Instance) -> u64 {
unimplemented!() instance.get_internal(&INTERNAL_FIELD)
} }
/// Sets the value of points used /// Sets the value of points used
pub fn set_points_used(_instance: &mut Instance, _value: u64) { pub fn set_points_used(instance: &mut Instance, value: u64) {
unimplemented!() instance.set_internal(&INTERNAL_FIELD, value);
} }
#[cfg(all(test, feature = "singlepass"))] #[cfg(all(test, feature = "singlepass"))]
@ -246,7 +246,7 @@ mod tests {
assert_eq!(value, 7); assert_eq!(value, 7);
// verify is uses the correct number of points // 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] #[test]
@ -269,8 +269,7 @@ mod tests {
assert_eq!(result.is_err(), true); // TODO assert that the trap is caused by PointsExausted assert_eq!(result.is_err(), true); // TODO assert that the trap is caused by PointsExausted
// verify is uses the correct number of points // verify is uses the correct number of points
assert_eq!(get_points_used(&instance), 99); // TODO need to update assertion to actual points used. assert_eq!(get_points_used(&instance), 109); // Used points will be slightly more than `limit` because of the way we do gas checking.
// TODO should points used be close to limit or at limit when trapping
} }
} }

View File

@ -13,7 +13,7 @@ use crate::{
table::Table, table::Table,
typed_func::{Func, Wasm, WasmTrapInfo, WasmTypeList}, typed_func::{Func, Wasm, WasmTrapInfo, WasmTypeList},
types::{FuncIndex, FuncSig, GlobalIndex, LocalOrImport, MemoryIndex, TableIndex, Type, Value}, types::{FuncIndex, FuncSig, GlobalIndex, LocalOrImport, MemoryIndex, TableIndex, Type, Value},
vm, vm::{self, InternalField},
}; };
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use std::{mem, ptr::NonNull, sync::Arc}; use std::{mem, ptr::NonNull, sync::Arc};
@ -372,6 +372,14 @@ impl Instance {
pub fn module(&self) -> Module { pub fn module(&self) -> Module {
Module::new(Arc::clone(&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 { impl InstanceInner {