Refactor is_anonymous.

This commit is contained in:
Stephen Chung 2022-11-03 12:00:56 +08:00
parent 1282beabbb
commit 2a5ef0bee1
5 changed files with 26 additions and 11 deletions

View File

@ -259,17 +259,18 @@ impl Engine {
ast.resolver().cloned(), ast.resolver().cloned(),
); );
let mut result = Ok(Dynamic::UNIT); let result = if eval_ast && !statements.is_empty() {
let r = self.eval_global_statements(scope, global, caches, statements, lib, 0);
if eval_ast && !statements.is_empty() {
result = self.eval_global_statements(scope, global, caches, statements, lib, 0);
if rewind_scope { if rewind_scope {
scope.rewind(orig_scope_len); scope.rewind(orig_scope_len);
} }
}
result = result.and_then(|_| { r
} else {
Ok(Dynamic::UNIT)
}
.and_then(|_| {
let mut args: StaticVec<_> = arg_values.iter_mut().collect(); let mut args: StaticVec<_> = arg_values.iter_mut().collect();
// Check for data race. // Check for data race.

View File

@ -4,8 +4,8 @@ use super::{get_builtin_binary_op_fn, get_builtin_op_assignment_fn, CallableFunc
use crate::api::default_limits::MAX_DYNAMIC_PARAMETERS; use crate::api::default_limits::MAX_DYNAMIC_PARAMETERS;
use crate::ast::{Expr, FnCallExpr, FnCallHashes}; use crate::ast::{Expr, FnCallExpr, FnCallHashes};
use crate::engine::{ use crate::engine::{
KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_FN_PTR_CALL, KEYWORD_FN_PTR_CURRY, FN_ANONYMOUS, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_FN_PTR_CALL,
KEYWORD_IS_DEF_VAR, KEYWORD_PRINT, KEYWORD_TYPE_OF, KEYWORD_FN_PTR_CURRY, KEYWORD_IS_DEF_VAR, KEYWORD_PRINT, KEYWORD_TYPE_OF,
}; };
use crate::eval::{Caches, FnResolutionCacheEntry, GlobalRuntimeState}; use crate::eval::{Caches, FnResolutionCacheEntry, GlobalRuntimeState};
use crate::tokenizer::{is_valid_function_name, Token}; use crate::tokenizer::{is_valid_function_name, Token};
@ -127,6 +127,14 @@ pub fn ensure_no_data_race(fn_name: &str, args: &FnCallArgs, is_ref_mut: bool) -
Ok(()) Ok(())
} }
/// Is a function name an anonymous function?
#[cfg(not(feature = "no_function"))]
#[inline]
#[must_use]
pub fn is_anonymous_fn(name: &str) -> bool {
name.starts_with(FN_ANONYMOUS)
}
impl Engine { impl Engine {
/// Generate the signature for a function call. /// Generate the signature for a function call.
#[inline] #[inline]

View File

@ -15,6 +15,8 @@ pub use args::FuncArgs;
pub use builtin::{get_builtin_binary_op_fn, get_builtin_op_assignment_fn}; pub use builtin::{get_builtin_binary_op_fn, get_builtin_op_assignment_fn};
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
pub use call::ensure_no_data_race; pub use call::ensure_no_data_race;
#[cfg(not(feature = "no_function"))]
pub use call::is_anonymous_fn;
pub use call::FnCallArgs; pub use call::FnCallArgs;
pub use callable_function::CallableFunction; pub use callable_function::CallableFunction;
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]

View File

@ -3,8 +3,9 @@
use super::call::FnCallArgs; use super::call::FnCallArgs;
use crate::ast::FnCallHashes; use crate::ast::FnCallHashes;
use crate::eval::{Caches, GlobalRuntimeState}; use crate::eval::{Caches, GlobalRuntimeState};
use crate::parser::is_anonymous_fn;
use crate::plugin::PluginFunction; use crate::plugin::PluginFunction;
use crate::tokenizer::{Token, TokenizeState}; use crate::tokenizer::{is_valid_function_name, Token, TokenizeState};
use crate::types::dynamic::Variant; use crate::types::dynamic::Variant;
use crate::{ use crate::{
calc_fn_hash, Dynamic, Engine, EvalContext, FuncArgs, Module, Position, RhaiResult, calc_fn_hash, Dynamic, Engine, EvalContext, FuncArgs, Module, Position, RhaiResult,
@ -329,7 +330,10 @@ impl<'a> NativeCallContext<'a> {
is_method_call: bool, is_method_call: bool,
args: &mut [&mut Dynamic], args: &mut [&mut Dynamic],
) -> RhaiResult { ) -> RhaiResult {
self._call_fn_raw(fn_name, false, is_ref_mut, is_method_call, args) let name = fn_name.as_ref();
let native_only = !is_valid_function_name(name) && !is_anonymous_fn(name);
self._call_fn_raw(fn_name, native_only, is_ref_mut, is_method_call, args)
} }
/// Call a registered native Rust function inside the call context. /// Call a registered native Rust function inside the call context.
/// ///

View File

@ -106,7 +106,7 @@ impl FnPtr {
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub fn is_anonymous(&self) -> bool { pub fn is_anonymous(&self) -> bool {
self.name.starts_with(crate::engine::FN_ANONYMOUS) crate::func::is_anonymous_fn(&self.name)
} }
/// Call the function pointer with curried arguments (if any). /// Call the function pointer with curried arguments (if any).
/// The function may be script-defined (not available under `no_function`) or native Rust. /// The function may be script-defined (not available under `no_function`) or native Rust.