Minor refactor.

This commit is contained in:
Stephen Chung 2020-08-08 22:59:05 +08:00
parent e66873bb83
commit da3cce58d3
2 changed files with 11 additions and 16 deletions

View File

@ -1634,12 +1634,13 @@ fn parse_primary(
Token::FloatConstant(x) => Expr::FloatConstant(Box::new(FloatWrapper(x, settings.pos))),
Token::CharConstant(c) => Expr::CharConstant(Box::new((c, settings.pos))),
Token::StringConstant(s) => Expr::StringConstant(Box::new((s.into(), settings.pos))),
// Function call
Token::Identifier(s) if *next_token == Token::LeftParen || *next_token == Token::Bang => {
Expr::Variable(Box::new(((s, settings.pos), None, 0, None)))
}
// Normal variable access
Token::Identifier(s) => {
// prevents capturing of the function call
#[cfg(not(feature = "no_closure"))]
if *next_token == Token::LeftParen || *next_token == Token::Bang {
state.allow_capture = false;
}
let index = state.access_var(&s, settings.pos);
Expr::Variable(Box::new(((s, settings.pos), None, 0, index)))
}

View File

@ -1,12 +1,13 @@
#![cfg(not(feature = "no_function"))]
use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, Module, INT};
use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, Module, RegisterFn, INT};
use std::any::TypeId;
#[test]
fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> {
let mut module = Module::new();
let mut engine = Engine::new();
module.set_raw_fn(
#[allow(deprecated)]
engine.register_raw_fn(
"call_with_arg",
&[TypeId::of::<FnPtr>(), TypeId::of::<INT>()],
|engine: &Engine, lib: &Module, args: &mut [&mut Dynamic]| {
@ -15,9 +16,6 @@ fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> {
},
);
let mut engine = Engine::new();
engine.load_package(module);
#[cfg(not(feature = "no_object"))]
assert_eq!(
engine.eval::<INT>(
@ -77,11 +75,7 @@ fn test_closures() -> Result<(), Box<EvalAltResult>> {
"#
)?);
let mut module = Module::new();
module.set_fn_1("plus_one", |x: INT| Ok(x + 1));
engine.load_package(module);
engine.register_fn("plus_one", |x: INT| x + 1);
assert_eq!(
engine.eval::<INT>(