Skip to content

Commit

Permalink
Make Base tunables generic in LimitingTunables
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Oct 27, 2020
1 parent 984d478 commit 1337d73
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions examples/tunables_limit_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,30 @@ use wasmer::{
imports,
vm::{self, MemoryError, MemoryStyle, TableStyle, VMMemoryDefinition, VMTableDefinition},
wat2wasm, Instance, Memory, MemoryType, Module, Pages, Store, TableType, Target,
Tunables as BaseTunables,
Tunables as ReferenceTunables,
};
use wasmer_compiler_cranelift::Cranelift;
use wasmer_engine::Tunables;
use wasmer_engine_jit::JIT;

/// A custom tunables that allows you to set a memory limit
struct LimitingTunables {
/// A custom tunables that allows you to set a memory limit.
///
/// After adjusting the memory limits, it delegates all other logic
/// to the base tunables.
struct LimitingTunables<T: Tunables> {
/// The maxium a linear memory is allowed to be (in Wasm pages, 65 KiB each).
/// Since Wasmer ensures there is only none or one memory, this is practically
/// an upper limit for the guest memory.
max_memory: Pages,
/// The base implementation we delegate all the logic to
base: BaseTunables,
base: T,
}

impl LimitingTunables {
pub fn for_target(target: &Target, limit: Pages) -> Self {
impl<T: Tunables> LimitingTunables<T> {
pub fn new(base: T, limit: Pages) -> Self {
Self {
max_memory: limit,
base: BaseTunables::for_target(target),
base,
}
}

Expand All @@ -52,7 +55,7 @@ impl LimitingTunables {
}
}

impl Tunables for LimitingTunables {
impl<T: Tunables> Tunables for LimitingTunables<T> {
/// Construct a `MemoryStyle` for the provided `MemoryType`
///
/// Delegated to base.
Expand Down Expand Up @@ -132,8 +135,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// Here is where the fun begins

let target = Target::default(); // TODO: should this use engine.target(), which is private?
let tunables = LimitingTunables::for_target(&target, Pages(24));
let base = ReferenceTunables::for_target(&Target::default());
let tunables = LimitingTunables::new(base, Pages(24));

// Create a store, that holds the engine and our custom tunables
let store = Store::new_with_tunables(&engine, tunables);
Expand Down

0 comments on commit 1337d73

Please sign in to comment.