From 031a5a934809bc5879955a9623dbc961cc4b8279 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 11 May 2024 06:38:46 +0800 Subject: [PATCH] Add FnPtr to get_literal_value. --- src/api/custom_syntax.rs | 3 ++- tests/custom_syntax.rs | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/api/custom_syntax.rs b/src/api/custom_syntax.rs index f1c101585..6643eeba5 100644 --- a/src/api/custom_syntax.rs +++ b/src/api/custom_syntax.rs @@ -131,9 +131,10 @@ impl Expression<'_> { /// Returns [`None`] also if the constant is not of the specified type. #[inline] #[must_use] - pub fn get_literal_value(&self) -> Option { + pub fn get_literal_value(&self) -> Option { // Coded this way in order to maximally leverage potentials for dead-code removal. match self.0 { + Expr::DynamicConstant(x, ..) => x.clone().try_cast::(), Expr::IntegerConstant(x, ..) => reify! { *x => Option }, #[cfg(not(feature = "no_float"))] diff --git a/tests/custom_syntax.rs b/tests/custom_syntax.rs index 30a50fe30..8beb869a5 100644 --- a/tests/custom_syntax.rs +++ b/tests/custom_syntax.rs @@ -1,6 +1,6 @@ #![cfg(not(feature = "no_custom_syntax"))] -use rhai::{Dynamic, Engine, EvalAltResult, ImmutableString, LexError, ParseErrorType, Position, Scope, INT}; +use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, ImmutableString, LexError, ParseErrorType, Position, Scope, INT}; #[test] fn test_custom_syntax() { @@ -278,7 +278,7 @@ fn test_custom_syntax_func() { let mut engine = Engine::new(); engine - .register_custom_syntax(["hello", "$func$"], false, |context, inputs| context.eval_expression_tree(&inputs[0])) + .register_custom_syntax(["hello", "$func$"], false, |_, inputs| Ok(inputs[0].get_literal_value::().unwrap().into())) .unwrap(); assert_eq!(engine.eval::("call(hello |x| { x + 1 }, 41)").unwrap(), 42);