Skip to content

Commit

Permalink
feat: add API to set module user data
Browse files Browse the repository at this point in the history
Add `Scanner::set_module_data` to specify data to be used by a module.
This is for the moment unused, but will be used by the cuckoo module.
  • Loading branch information
vthib committed Apr 30, 2024
1 parent 5f73583 commit 7e4cc9c
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 54 deletions.
16 changes: 9 additions & 7 deletions boreal/src/evaluator/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ use crate::compiler::module::{
BoundedValueIndex, ModuleExpression, ModuleExpressionKind, ModuleOperations, ValueOperation,
};
use crate::memory::Region;
use crate::module::{EvalContext, Module, ModuleDataMap, ScanContext, Value as ModuleValue};
use crate::module::{
EvalContext, Module, ModuleDataMap, ModuleUserData, ScanContext, Value as ModuleValue,
};

use super::{Evaluator, PoisonKind, Value};

#[derive(Debug)]
pub struct EvalData {
pub struct EvalData<'scanner> {
pub values: Vec<(&'static str, ModuleValue)>,
pub data_map: ModuleDataMap,
pub data_map: ModuleDataMap<'scanner>,
}

impl EvalData {
pub fn new(modules: &[Box<dyn Module>]) -> Self {
let mut data_map = ModuleDataMap::default();
impl<'scanner> EvalData<'scanner> {
pub fn new(modules: &[Box<dyn Module>], user_data: &'scanner ModuleUserData) -> Self {
let mut data_map = ModuleDataMap::new(user_data);

let values = modules
.iter()
Expand Down Expand Up @@ -216,7 +218,7 @@ mod tests {
fn test_types_traits() {
test_type_traits_non_clonable(EvalData {
values: Vec::new(),
data_map: ModuleDataMap::default(),
data_map: ModuleDataMap::new(&HashMap::new()),
});
}
}
43 changes: 28 additions & 15 deletions boreal/src/module/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,18 +627,19 @@ mod tests {
use super::*;

macro_rules! ctx {
() => {
($v:expr) => {
EvalContext {
mem: &mut Memory::Direct(b""),
module_data: &ModuleDataMap::default(),
module_data: &ModuleDataMap::new($v),
process_memory: false,
}
};
}

#[test]
fn test_in_range_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::in_range(&mut ctx, vec![]).is_none());
assert!(Math::in_range(&mut ctx, vec![0.5.into()]).is_none());
Expand All @@ -650,7 +651,8 @@ mod tests {

#[test]
fn test_deviation_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::deviation(&mut ctx, vec![]).is_none());
assert!(Math::deviation(&mut ctx, vec![0.5.into()]).is_none());
Expand All @@ -664,7 +666,8 @@ mod tests {

#[test]
fn test_mean_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::mean(&mut ctx, vec![]).is_none());
assert!(Math::mean(&mut ctx, vec![0.5.into()]).is_none());
Expand All @@ -674,7 +677,8 @@ mod tests {

#[test]
fn test_serial_correlation_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::serial_correlation(&mut ctx, vec![]).is_none());
assert!(Math::serial_correlation(&mut ctx, vec![0.5.into()]).is_none());
Expand All @@ -684,7 +688,8 @@ mod tests {

#[test]
fn test_monte_carlo_pi_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::monte_carlo_pi(&mut ctx, vec![]).is_none());
assert!(Math::monte_carlo_pi(&mut ctx, vec![0.5.into()]).is_none());
Expand All @@ -694,7 +699,8 @@ mod tests {

#[test]
fn test_entropy_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::entropy(&mut ctx, vec![]).is_none());
assert!(Math::entropy(&mut ctx, vec![0.5.into()]).is_none());
Expand All @@ -704,7 +710,8 @@ mod tests {

#[test]
fn test_min_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::min(&mut ctx, vec![]).is_none());
assert!(Math::min(&mut ctx, vec![0.5.into()]).is_none());
Expand All @@ -714,7 +721,8 @@ mod tests {

#[test]
fn test_max_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::max(&mut ctx, vec![]).is_none());
assert!(Math::max(&mut ctx, vec![0.5.into()]).is_none());
Expand All @@ -724,23 +732,26 @@ mod tests {

#[test]
fn test_to_number_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::to_number(&mut ctx, vec![]).is_none());
assert!(Math::to_number(&mut ctx, vec![0.into()]).is_none());
}

#[test]
fn test_abs_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::abs(&mut ctx, vec![]).is_none());
assert!(Math::abs(&mut ctx, vec![0.5.into()]).is_none());
}

#[test]
fn test_count_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::count(&mut ctx, vec![]).is_none());
assert!(Math::count(&mut ctx, vec![0.5.into()]).is_none());
Expand All @@ -749,7 +760,8 @@ mod tests {

#[test]
fn test_percentage_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::percentage(&mut ctx, vec![]).is_none());
assert!(Math::percentage(&mut ctx, vec![0.5.into()]).is_none());
Expand All @@ -758,7 +770,8 @@ mod tests {

#[test]
fn test_mode_invalid_args() {
let mut ctx = ctx!();
let user_data = HashMap::new();
let mut ctx = ctx!(&user_data);

assert!(Math::mode(&mut ctx, vec![0.5.into()]).is_none());
}
Expand Down
Loading

0 comments on commit 7e4cc9c

Please sign in to comment.