From bf845fbd7a5926f5a30e7fae7163cb2047f83a1b Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 17 Nov 2022 18:05:52 +0800 Subject: [PATCH] Export CallableFunction. --- CHANGELOG.md | 9 +++++++++ src/func/call.rs | 33 +++++++++++++++++---------------- src/func/callable_function.rs | 19 ++++--------------- src/lib.rs | 2 +- 4 files changed, 31 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1772d4f1..11b1bbd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,15 @@ Rhai Release Notes ================== +Version 1.12.0 +============== + +Enhancements +------------ + +* `CallableFunction` is exported under `internals`. + + Version 1.11.0 ============== diff --git a/src/func/call.rs b/src/func/call.rs index 4dcc7ced..0d47eb24 100644 --- a/src/func/call.rs +++ b/src/func/call.rs @@ -12,7 +12,7 @@ use crate::tokenizer::{is_valid_function_name, Token}; use crate::types::RestoreOnDrop; use crate::{ calc_fn_hash, calc_fn_hash_full, Dynamic, Engine, FnArgsVec, FnPtr, ImmutableString, - OptimizationLevel, Position, RhaiError, RhaiResult, RhaiResultOf, Scope, ERR, + OptimizationLevel, Position, RhaiError, RhaiResult, RhaiResultOf, Scope, Shared, ERR, }; #[cfg(feature = "no_std")] use hashbrown::hash_map::Entry; @@ -223,17 +223,17 @@ impl Engine { if let Some((f, s)) = func { // Specific version found - let new_entry = Some(FnResolutionCacheEntry { + let new_entry = FnResolutionCacheEntry { func: f.clone(), source: s.cloned(), - }); + }; return if cache.filter.is_absent_and_set(hash) { // Do not cache "one-hit wonders" - *local_entry = new_entry; + *local_entry = Some(new_entry); local_entry.as_ref() } else { // Cache entry - entry.insert(new_entry).as_ref() + entry.insert(Some(new_entry)).as_ref() }; } @@ -279,13 +279,13 @@ impl Engine { get_builtin_op_assignment_fn(token, *first_arg, rest_args[0]) .map(|f| FnResolutionCacheEntry { - func: CallableFunction::from_fn_builtin(f), + func: CallableFunction::Method(Shared::new(f)), source: None, }) } Some(token) => get_builtin_binary_op_fn(token, args[0], args[1]) .map(|f| FnResolutionCacheEntry { - func: CallableFunction::from_fn_builtin(f), + func: CallableFunction::Method(Shared::new(f)), source: None, }), @@ -363,16 +363,13 @@ impl Engine { true, ); - if func.is_some() { - let is_method = func.map_or(false, |f| f.func.is_method()); + if let Some(FnResolutionCacheEntry { func, source }) = func { + assert!(func.is_native()); // Push a new call stack frame #[cfg(feature = "debugging")] let orig_call_stack_len = global.debugger.call_stack().len(); - let FnResolutionCacheEntry { func, source } = func.unwrap(); - assert!(func.is_native()); - let backup = &mut ArgBackup::new(); // Calling pure function but the first argument is a reference? @@ -411,11 +408,15 @@ impl Engine { func.get_native_fn().unwrap()(context, args) }; + let is_method = func.is_method(); + #[cfg(feature = "debugging")] { + use crate::eval::{DebuggerEvent, DebuggerStatus}; + let trigger = match global.debugger.status { - crate::eval::DebuggerStatus::FunctionExit(n) => n >= global.level, - crate::eval::DebuggerStatus::Next(.., true) => true, + DebuggerStatus::FunctionExit(n) => n >= global.level, + DebuggerStatus::Next(.., true) => true, _ => false, }; if trigger { @@ -424,8 +425,8 @@ impl Engine { let node = crate::ast::Stmt::Noop(pos); let node = (&node).into(); let event = match _result { - Ok(ref r) => crate::eval::DebuggerEvent::FunctionExitWithValue(r), - Err(ref err) => crate::eval::DebuggerEvent::FunctionExitWithError(err), + Ok(ref r) => DebuggerEvent::FunctionExitWithValue(r), + Err(ref err) => DebuggerEvent::FunctionExitWithError(err), }; if let Err(err) = diff --git a/src/func/callable_function.rs b/src/func/callable_function.rs index f11d41a7..893709b6 100644 --- a/src/func/callable_function.rs +++ b/src/func/callable_function.rs @@ -1,6 +1,6 @@ //! Module defining the standard Rhai function type. -use super::native::{FnAny, FnBuiltin, FnPlugin, IteratorFn, SendSync}; +use super::native::{FnAny, FnPlugin, IteratorFn, SendSync}; use crate::ast::FnAccess; use crate::plugin::PluginFunction; use crate::Shared; @@ -8,7 +8,8 @@ use std::fmt; #[cfg(feature = "no_std")] use std::prelude::v1::*; -/// A type encapsulating a function callable by Rhai. +/// _(internals)_ A type encapsulating a function callable by Rhai. +/// Exported under the `internals` feature only. #[derive(Clone)] #[non_exhaustive] pub enum CallableFunction { @@ -199,18 +200,6 @@ impl CallableFunction { Self::Script(..) => None, } } - /// Create a new [`CallableFunction::Method`] from a built-in function. - #[inline(always)] - #[must_use] - pub fn from_fn_builtin(func: FnBuiltin) -> Self { - Self::Method(Shared::new(func)) - } - /// Create a new [`CallableFunction::Plugin`]. - #[inline(always)] - #[must_use] - pub fn from_plugin(func: impl PluginFunction + 'static + SendSync) -> Self { - Self::Plugin(Shared::new(func)) - } } #[cfg(not(feature = "no_function"))] @@ -232,7 +221,7 @@ impl From> for CallableFunction { impl From for CallableFunction { #[inline(always)] fn from(func: T) -> Self { - Self::from_plugin(func) + Self::Plugin(Shared::new(func)) } } diff --git a/src/lib.rs b/src/lib.rs index c552656b..7d5d753a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -340,7 +340,7 @@ pub use eval::{Caches, FnResolutionCache, FnResolutionCacheEntry, GlobalRuntimeS #[cfg(feature = "internals")] #[allow(deprecated)] -pub use func::{locked_read, locked_write, NativeCallContextStore}; +pub use func::{locked_read, locked_write, CallableFunction, NativeCallContextStore}; #[cfg(feature = "internals")] #[cfg(feature = "metadata")]