Skip to main content

rns_hooks/
program.rs

1use crate::runtime::StoreData;
2use wasmtime::{Instance, Module, Store};
3
4/// A compiled WASM hook program ready for execution.
5pub 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    /// Cached store and instance for cross-call state persistence.
14    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    /// Reset the consecutive trap counter after a successful execution.
32    pub fn record_success(&mut self) {
33        self.consecutive_traps = 0;
34    }
35
36    /// Drop the cached store/instance (e.g. on reload).
37    pub fn drop_cache(&mut self) {
38        self.cached = None;
39    }
40
41    /// Increment the trap counter. Returns `true` if the program was auto-disabled.
42    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}