1use crate::runtime::StoreData;
2use wasmtime::{Instance, Module, Store};
3
4pub struct LoadedProgram {
6 pub name: String,
7 pub module: Module,
8 pub priority: i32,
9 pub consecutive_traps: u32,
10 pub enabled: bool,
11 pub max_consecutive_traps: u32,
12 pub export_name: String,
13 pub cached: Option<(Store<StoreData>, Instance)>,
15}
16
17impl LoadedProgram {
18 pub fn new(name: String, module: Module, priority: i32) -> Self {
19 LoadedProgram {
20 name,
21 module,
22 priority,
23 consecutive_traps: 0,
24 enabled: true,
25 max_consecutive_traps: 10,
26 export_name: "on_hook".to_string(),
27 cached: None,
28 }
29 }
30
31 pub fn record_success(&mut self) {
33 self.consecutive_traps = 0;
34 }
35
36 pub fn drop_cache(&mut self) {
38 self.cached = None;
39 }
40
41 pub fn record_trap(&mut self) -> bool {
43 self.consecutive_traps += 1;
44 if self.consecutive_traps >= self.max_consecutive_traps {
45 self.enabled = false;
46 true
47 } else {
48 false
49 }
50 }
51}