ergotree_interpreter/eval/
method_call.rs

1use ergotree_ir::mir::method_call::MethodCall;
2use ergotree_ir::mir::value::Value;
3
4use super::smethod_eval_fn;
5use super::Context;
6use super::Env;
7use super::EvalError;
8use super::Evaluable;
9
10impl Evaluable for MethodCall {
11    fn eval<'ctx>(
12        &self,
13        env: &mut Env<'ctx>,
14        ectx: &Context<'ctx>,
15    ) -> Result<Value<'ctx>, EvalError> {
16        let ov = self.obj.eval(env, ectx)?;
17        let argsv: Result<Vec<Value>, EvalError> =
18            self.args.iter().map(|arg| arg.eval(env, ectx)).collect();
19        smethod_eval_fn(&self.method)?(env, ectx, ov, argsv?)
20    }
21}
22
23#[allow(clippy::unwrap_used)]
24#[cfg(test)]
25#[cfg(feature = "arbitrary")]
26mod tests {
27    use ergotree_ir::mir::constant::Constant;
28    use ergotree_ir::mir::expr::Expr;
29    use ergotree_ir::mir::global_vars::GlobalVars;
30    use ergotree_ir::mir::option_get::OptionGet;
31    use ergotree_ir::mir::unary_op::OneArgOpTryBuild;
32    use ergotree_ir::types::sbox;
33    use sigma_test_util::force_any_val;
34
35    use crate::eval::context::Context;
36    use crate::eval::tests::eval_out;
37
38    use super::*;
39
40    #[test]
41    fn eval_box_get_reg() {
42        let mc: Expr = MethodCall::new(
43            GlobalVars::SelfBox.into(),
44            sbox::GET_REG_METHOD.clone(),
45            vec![Constant::from(0i8).into()],
46        )
47        .unwrap()
48        .into();
49        let option_get_expr: Expr = OptionGet::try_build(mc).unwrap().into();
50        let ctx = force_any_val::<Context>();
51        assert_eq!(
52            eval_out::<i64>(&option_get_expr, &ctx),
53            ctx.self_box.value.as_i64()
54        );
55    }
56}