Support getting/setting metering points and internal fields with a Ctx.

This commit is contained in:
losfair
2019-06-06 14:10:57 +08:00
parent 27eacf0c4f
commit c6cd49a370
2 changed files with 25 additions and 3 deletions

View File

@ -1,7 +1,7 @@
use wasmer_runtime_core::{ use wasmer_runtime_core::{
codegen::{Event, EventSink, FunctionMiddleware, InternalEvent}, codegen::{Event, EventSink, FunctionMiddleware, InternalEvent},
module::ModuleInfo, module::ModuleInfo,
vm::InternalField, vm::{Ctx, InternalField},
wasmparser::{Operator, Type as WpType}, wasmparser::{Operator, Type as WpType},
Instance, Instance,
}; };
@ -111,16 +111,26 @@ impl FunctionMiddleware for Metering {
} }
} }
/// Returns the number of points used by a function call for metering /// Returns the number of points used by an Instance.
pub fn get_points_used(instance: &Instance) -> u64 { pub fn get_points_used(instance: &Instance) -> u64 {
instance.get_internal(&INTERNAL_FIELD) instance.get_internal(&INTERNAL_FIELD)
} }
/// Sets the value of points used /// Sets the number of points used by an Instance.
pub fn set_points_used(instance: &mut Instance, value: u64) { pub fn set_points_used(instance: &mut Instance, value: u64) {
instance.set_internal(&INTERNAL_FIELD, value); instance.set_internal(&INTERNAL_FIELD, value);
} }
/// Returns the number of points used in a Ctx.
pub fn get_points_used_ctx(ctx: &Ctx) -> u64 {
ctx.get_internal(&INTERNAL_FIELD)
}
/// Sets the number of points used in a Ctx.
pub fn set_points_used_ctx(ctx: &mut Ctx, value: u64) {
ctx.set_internal(&INTERNAL_FIELD, value);
}
#[cfg(all(test, feature = "singlepass"))] #[cfg(all(test, feature = "singlepass"))]
mod tests { mod tests {
use super::*; use super::*;

View File

@ -350,6 +350,18 @@ impl Ctx {
pub fn dynamic_sigindice_count(&self) -> usize { pub fn dynamic_sigindice_count(&self) -> usize {
unsafe { (*self.local_backing).dynamic_sigindices.len() } unsafe { (*self.local_backing).dynamic_sigindices.len() }
} }
/// Returns the value of the specified internal field.
pub fn get_internal(&self, field: &InternalField) -> u64 {
unsafe { (*self.internal.internals)[field.index()] }
}
/// Writes the value to the specified internal field.
pub fn set_internal(&mut self, field: &InternalField, value: u64) {
unsafe {
(*self.internal.internals)[field.index()] = value;
}
}
} }
#[doc(hidden)] #[doc(hidden)]