1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use std::convert::TryFrom;
use anyhow::Result;
use wasmtime::{AsContextMut, Trap, TypedFunc};
use dataplane::smartmodule::{
SmartModuleInput, SmartModuleOutput, SmartModuleInternalError, SmartModuleExtraParams,
};
use crate::{
WasmSlice,
smartmodule::{SmartModuleWithEngine, SmartEngine, SmartModuleContext, SmartModuleInstance},
};
const FILTER_FN_NAME: &str = "filter";
type OldFilterFn = TypedFunc<(i32, i32), i32>;
type FilterFn = TypedFunc<(i32, i32, u32), i32>;
pub struct SmartModuleFilter {
base: SmartModuleContext,
filter_fn: FilterFnKind,
}
enum FilterFnKind {
Old(OldFilterFn),
New(FilterFn),
}
impl FilterFnKind {
fn call(&self, store: impl AsContextMut, slice: WasmSlice) -> Result<i32, Trap> {
match self {
Self::Old(filter_fn) => filter_fn.call(store, (slice.0, slice.1)),
Self::New(filter_fn) => filter_fn.call(store, slice),
}
}
}
impl SmartModuleFilter {
pub fn new(
engine: &SmartEngine,
module: &SmartModuleWithEngine,
params: SmartModuleExtraParams,
version: i16,
) -> Result<Self> {
let mut base = SmartModuleContext::new(engine, module, params, version)?;
let filter_fn = if let Ok(filt_fn) = base
.instance
.get_typed_func(&mut base.store, FILTER_FN_NAME)
{
FilterFnKind::New(filt_fn)
} else {
let filt_fn: OldFilterFn = base
.instance
.get_typed_func(&mut base.store, FILTER_FN_NAME)?;
FilterFnKind::Old(filt_fn)
};
Ok(Self { base, filter_fn })
}
}
impl SmartModuleInstance for SmartModuleFilter {
fn process(&mut self, input: SmartModuleInput) -> Result<SmartModuleOutput> {
let slice = self.base.write_input(&input)?;
let filter_output = self.filter_fn.call(&mut self.base.store, slice)?;
if filter_output < 0 {
let internal_error = SmartModuleInternalError::try_from(filter_output)
.unwrap_or(SmartModuleInternalError::UnknownError);
return Err(internal_error.into());
}
let output: SmartModuleOutput = self.base.read_output()?;
Ok(output)
}
fn params(&self) -> SmartModuleExtraParams {
self.base.params.clone()
}
}