Merge pull request #17 from Eliah-Lakhin/master

FnPtr::call_dynamic that fixes issues with curry-ed lambdas when they are called dynamically
This commit is contained in:
Stephen Chung 2020-07-23 10:01:16 +08:00 committed by GitHub
commit 7b22276ea8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 1 deletions

View File

@ -1,11 +1,12 @@
//! Module containing interfaces with native-Rust functions. //! Module containing interfaces with native-Rust functions.
use crate::any::Dynamic; use crate::any::Dynamic;
use crate::engine::Engine; use crate::engine::Engine;
use crate::module::Module; use crate::module::{Module, FuncReturn};
use crate::parser::ScriptFnDef; use crate::parser::ScriptFnDef;
use crate::result::EvalAltResult; use crate::result::EvalAltResult;
use crate::token::{is_valid_identifier, Position}; use crate::token::{is_valid_identifier, Position};
use crate::utils::ImmutableString; use crate::utils::ImmutableString;
use crate::Scope;
use crate::stdlib::{boxed::Box, convert::TryFrom, fmt, rc::Rc, string::String, sync::Arc}; use crate::stdlib::{boxed::Box, convert::TryFrom, fmt, rc::Rc, string::String, sync::Arc};
@ -74,6 +75,31 @@ impl FnPtr {
pub(crate) fn curry(&self) -> &[Dynamic] { pub(crate) fn curry(&self) -> &[Dynamic] {
&self.1 &self.1
} }
/// A shortcut of `Engine::call_fn_dynamic` function that takes into
/// consideration curry-ed and passed arguments both.
pub fn call_dynamic(
&self,
engine: &Engine,
lib: impl AsRef<Module>,
mut this_ptr: Option<&mut Dynamic>,
mut arg_values: impl AsMut<[Dynamic]>
) -> FuncReturn<Dynamic> {
let mut args: Vec<Dynamic> = self
.1
.iter()
.chain(arg_values.as_mut().iter())
.cloned()
.collect();
engine.call_fn_dynamic(
&mut Scope::new(),
lib,
&self.0.as_ref(),
this_ptr,
args
)
}
} }
impl fmt::Display for FnPtr { impl fmt::Display for FnPtr {

View File

@ -2,6 +2,8 @@
use rhai::{ use rhai::{
Dynamic, Engine, EvalAltResult, FnPtr, Func, Module, ParseError, ParseErrorType, Scope, INT, Dynamic, Engine, EvalAltResult, FnPtr, Func, Module, ParseError, ParseErrorType, Scope, INT,
}; };
use std::any::TypeId;
use std::rc::Rc;
#[test] #[test]
fn test_fn() -> Result<(), Box<EvalAltResult>> { fn test_fn() -> Result<(), Box<EvalAltResult>> {
@ -157,3 +159,35 @@ fn test_fn_ptr_raw() -> Result<(), Box<EvalAltResult>> {
Ok(()) Ok(())
} }
#[test]
fn test_currying_with_registered_fn() -> Result<(), Box<EvalAltResult>> {
let mut module = Module::new();
module.set_raw_fn(
"call_with_arg",
&[TypeId::of::<FnPtr>(), TypeId::of::<INT>()],
|engine: &Engine, module: &Module, args: &mut [&mut Dynamic]| {
std::mem::take(args[0])
.cast::<FnPtr>()
.call_dynamic(engine, module, None, [std::mem::take(args[1])])
},
);
let mut engine = Engine::new();
engine.load_package(Rc::new(module));
assert_eq!(
engine.eval::<INT>(
r#"
let addition = |x, y| { x + y };
let curryed = addition.curry(100);
call_with_arg(curryed, 5)
"#
)?,
105
);
Ok(())
}