Export CallableFunction.

This commit is contained in:
Stephen Chung 2022-11-17 18:05:52 +08:00
parent 9e5e18af61
commit bf845fbd7a
4 changed files with 31 additions and 32 deletions

View File

@ -1,6 +1,15 @@
Rhai Release Notes
==================
Version 1.12.0
==============
Enhancements
------------
* `CallableFunction` is exported under `internals`.
Version 1.11.0
==============

View File

@ -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) =

View File

@ -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<Shared<crate::ast::ScriptFnDef>> for CallableFunction {
impl<T: PluginFunction + 'static + SendSync> From<T> for CallableFunction {
#[inline(always)]
fn from(func: T) -> Self {
Self::from_plugin(func)
Self::Plugin(Shared::new(func))
}
}

View File

@ -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")]