Export CallableFunction.
This commit is contained in:
parent
9e5e18af61
commit
bf845fbd7a
@ -1,6 +1,15 @@
|
|||||||
Rhai Release Notes
|
Rhai Release Notes
|
||||||
==================
|
==================
|
||||||
|
|
||||||
|
Version 1.12.0
|
||||||
|
==============
|
||||||
|
|
||||||
|
Enhancements
|
||||||
|
------------
|
||||||
|
|
||||||
|
* `CallableFunction` is exported under `internals`.
|
||||||
|
|
||||||
|
|
||||||
Version 1.11.0
|
Version 1.11.0
|
||||||
==============
|
==============
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ use crate::tokenizer::{is_valid_function_name, Token};
|
|||||||
use crate::types::RestoreOnDrop;
|
use crate::types::RestoreOnDrop;
|
||||||
use crate::{
|
use crate::{
|
||||||
calc_fn_hash, calc_fn_hash_full, Dynamic, Engine, FnArgsVec, FnPtr, ImmutableString,
|
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")]
|
#[cfg(feature = "no_std")]
|
||||||
use hashbrown::hash_map::Entry;
|
use hashbrown::hash_map::Entry;
|
||||||
@ -223,17 +223,17 @@ impl Engine {
|
|||||||
|
|
||||||
if let Some((f, s)) = func {
|
if let Some((f, s)) = func {
|
||||||
// Specific version found
|
// Specific version found
|
||||||
let new_entry = Some(FnResolutionCacheEntry {
|
let new_entry = FnResolutionCacheEntry {
|
||||||
func: f.clone(),
|
func: f.clone(),
|
||||||
source: s.cloned(),
|
source: s.cloned(),
|
||||||
});
|
};
|
||||||
return if cache.filter.is_absent_and_set(hash) {
|
return if cache.filter.is_absent_and_set(hash) {
|
||||||
// Do not cache "one-hit wonders"
|
// Do not cache "one-hit wonders"
|
||||||
*local_entry = new_entry;
|
*local_entry = Some(new_entry);
|
||||||
local_entry.as_ref()
|
local_entry.as_ref()
|
||||||
} else {
|
} else {
|
||||||
// Cache entry
|
// 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])
|
get_builtin_op_assignment_fn(token, *first_arg, rest_args[0])
|
||||||
.map(|f| FnResolutionCacheEntry {
|
.map(|f| FnResolutionCacheEntry {
|
||||||
func: CallableFunction::from_fn_builtin(f),
|
func: CallableFunction::Method(Shared::new(f)),
|
||||||
source: None,
|
source: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Some(token) => get_builtin_binary_op_fn(token, args[0], args[1])
|
Some(token) => get_builtin_binary_op_fn(token, args[0], args[1])
|
||||||
.map(|f| FnResolutionCacheEntry {
|
.map(|f| FnResolutionCacheEntry {
|
||||||
func: CallableFunction::from_fn_builtin(f),
|
func: CallableFunction::Method(Shared::new(f)),
|
||||||
source: None,
|
source: None,
|
||||||
}),
|
}),
|
||||||
|
|
||||||
@ -363,16 +363,13 @@ impl Engine {
|
|||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
|
|
||||||
if func.is_some() {
|
if let Some(FnResolutionCacheEntry { func, source }) = func {
|
||||||
let is_method = func.map_or(false, |f| f.func.is_method());
|
assert!(func.is_native());
|
||||||
|
|
||||||
// Push a new call stack frame
|
// Push a new call stack frame
|
||||||
#[cfg(feature = "debugging")]
|
#[cfg(feature = "debugging")]
|
||||||
let orig_call_stack_len = global.debugger.call_stack().len();
|
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();
|
let backup = &mut ArgBackup::new();
|
||||||
|
|
||||||
// Calling pure function but the first argument is a reference?
|
// Calling pure function but the first argument is a reference?
|
||||||
@ -411,11 +408,15 @@ impl Engine {
|
|||||||
func.get_native_fn().unwrap()(context, args)
|
func.get_native_fn().unwrap()(context, args)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let is_method = func.is_method();
|
||||||
|
|
||||||
#[cfg(feature = "debugging")]
|
#[cfg(feature = "debugging")]
|
||||||
{
|
{
|
||||||
|
use crate::eval::{DebuggerEvent, DebuggerStatus};
|
||||||
|
|
||||||
let trigger = match global.debugger.status {
|
let trigger = match global.debugger.status {
|
||||||
crate::eval::DebuggerStatus::FunctionExit(n) => n >= global.level,
|
DebuggerStatus::FunctionExit(n) => n >= global.level,
|
||||||
crate::eval::DebuggerStatus::Next(.., true) => true,
|
DebuggerStatus::Next(.., true) => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
};
|
};
|
||||||
if trigger {
|
if trigger {
|
||||||
@ -424,8 +425,8 @@ impl Engine {
|
|||||||
let node = crate::ast::Stmt::Noop(pos);
|
let node = crate::ast::Stmt::Noop(pos);
|
||||||
let node = (&node).into();
|
let node = (&node).into();
|
||||||
let event = match _result {
|
let event = match _result {
|
||||||
Ok(ref r) => crate::eval::DebuggerEvent::FunctionExitWithValue(r),
|
Ok(ref r) => DebuggerEvent::FunctionExitWithValue(r),
|
||||||
Err(ref err) => crate::eval::DebuggerEvent::FunctionExitWithError(err),
|
Err(ref err) => DebuggerEvent::FunctionExitWithError(err),
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Err(err) =
|
if let Err(err) =
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
//! Module defining the standard Rhai function type.
|
//! 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::ast::FnAccess;
|
||||||
use crate::plugin::PluginFunction;
|
use crate::plugin::PluginFunction;
|
||||||
use crate::Shared;
|
use crate::Shared;
|
||||||
@ -8,7 +8,8 @@ use std::fmt;
|
|||||||
#[cfg(feature = "no_std")]
|
#[cfg(feature = "no_std")]
|
||||||
use std::prelude::v1::*;
|
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)]
|
#[derive(Clone)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub enum CallableFunction {
|
pub enum CallableFunction {
|
||||||
@ -199,18 +200,6 @@ impl CallableFunction {
|
|||||||
Self::Script(..) => None,
|
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"))]
|
#[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 {
|
impl<T: PluginFunction + 'static + SendSync> From<T> for CallableFunction {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn from(func: T) -> Self {
|
fn from(func: T) -> Self {
|
||||||
Self::from_plugin(func)
|
Self::Plugin(Shared::new(func))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -340,7 +340,7 @@ pub use eval::{Caches, FnResolutionCache, FnResolutionCacheEntry, GlobalRuntimeS
|
|||||||
|
|
||||||
#[cfg(feature = "internals")]
|
#[cfg(feature = "internals")]
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
pub use func::{locked_read, locked_write, NativeCallContextStore};
|
pub use func::{locked_read, locked_write, CallableFunction, NativeCallContextStore};
|
||||||
|
|
||||||
#[cfg(feature = "internals")]
|
#[cfg(feature = "internals")]
|
||||||
#[cfg(feature = "metadata")]
|
#[cfg(feature = "metadata")]
|
||||||
|
Loading…
Reference in New Issue
Block a user