difftreelog
feat pass caller location to native function
in: master
5 files changed
bindings/jsonnet/src/native.rsdiffbeforeafterboth353536 vm.add_native(36 vm.add_native(37 name,37 name,38 Rc::new(NativeCallback::new(params, move |args| {38 Rc::new(NativeCallback::new(params, move |_caller, args| {39 let mut n_args = Vec::new();39 let mut n_args = Vec::new();40 for a in args {40 for a in args {41 n_args.push(Some(Box::new(a.clone())));41 n_args.push(Some(Box::new(a.clone())));crates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth540 &evaluate(context.clone(), s)?,540 &evaluate(context.clone(), s)?,541 &Val::Obj(evaluate_object(context, t)?),541 &Val::Obj(evaluate_object(context, t)?),542 )?,542 )?,543 Apply(value, args, tailstrict) => evaluate_apply(context, value, args, loc.as_ref(), *tailstrict)?,543 Apply(value, args, tailstrict) => {544 evaluate_apply(context, value, args, loc.as_ref(), *tailstrict)?545 }544 Function(params, body) => {546 Function(params, body) => {545 evaluate_method(context, "anonymous".into(), params.clone(), body.clone())547 evaluate_method(context, "anonymous".into(), params.clone(), body.clone())546 }548 }crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth886 Param("a".into(), None),886 Param("a".into(), None),887 Param("b".into(), None),887 Param("b".into(), None),888 ])),888 ])),889 |args| match (&args[0], &args[1]) {889 |caller, args| {890 assert_eq!(891 caller.unwrap(),892 Rc::new(PathBuf::from("native_caller.jsonnet"))893 );894 match (&args[0], &args[1]) {890 (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),895 (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),891 (_, _) => todo!(),896 (_, _) => unreachable!(),892 },897 }898 },893 )),899 )),894 );900 );895 evaluator.evaluate_snippet_raw(901 evaluator.evaluate_snippet_raw(896 Rc::new(PathBuf::from("test.jsonnet")),902 Rc::new(PathBuf::from("native_caller.jsonnet")),897 "std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),903 "std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),898 )?;904 )?;899 Ok(())905 Ok(())crates/jrsonnet-evaluator/src/native.rsdiffbeforeafterboth1use crate::{error::Result, Val};1use crate::{error::Result, Val};2use jrsonnet_parser::ParamsDesc;2use jrsonnet_parser::ParamsDesc;3use std::fmt::Debug;3use std::fmt::Debug;4use std::path::PathBuf;5use std::rc::Rc;465pub struct NativeCallback {7pub struct NativeCallback {6 pub params: ParamsDesc,8 pub params: ParamsDesc,7 handler: Box<dyn Fn(&[Val]) -> Result<Val>>,9 handler: Box<dyn Fn(Option<Rc<PathBuf>>, &[Val]) -> Result<Val>>,8}10}9impl NativeCallback {11impl NativeCallback {10 pub fn new(params: ParamsDesc, handler: impl Fn(&[Val]) -> Result<Val> + 'static) -> Self {12 pub fn new(13 params: ParamsDesc,14 handler: impl Fn(Option<Rc<PathBuf>>, &[Val]) -> Result<Val> + 'static,15 ) -> Self {11 Self {16 Self {12 params,17 params,13 handler: Box::new(handler),18 handler: Box::new(handler),14 }19 }15 }20 }16 pub fn call(&self, args: &[Val]) -> Result<Val> {21 pub fn call(&self, caller: Option<Rc<PathBuf>>, args: &[Val]) -> Result<Val> {17 (self.handler)(args)22 (self.handler)(caller, args)18 }23 }19}24}20impl Debug for NativeCallback {25impl Debug for NativeCallback {crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth124 for p in handler.params.0.iter() {124 for p in handler.params.0.iter() {125 out_args.push(args.binding(p.0.clone())?.evaluate()?);125 out_args.push(args.binding(p.0.clone())?.evaluate()?);126 }126 }127 Ok(handler.call(&out_args)?)127 Ok(handler.call(loc.clone().map(|l| l.0.clone()), &out_args)?)128 }128 }129 }129 }130 }130 }