1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
//! The `FnPtr` type.
use crate::eval::GlobalRuntimeState;
use crate::func::EncapsulatedEnviron;
use crate::tokenizer::{is_reserved_keyword_or_symbol, is_valid_function_name, Token};
use crate::types::dynamic::Variant;
use crate::{
Dynamic, Engine, FnArgsVec, FuncArgs, ImmutableString, NativeCallContext, ParseErrorType,
Position, RhaiError, RhaiResult, RhaiResultOf, Shared, StaticVec, AST, ERR,
};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{
any::type_name,
convert::{TryFrom, TryInto},
fmt,
hash::{Hash, Hasher},
mem,
ops::{Index, IndexMut},
};
/// A general function pointer, which may carry additional (i.e. curried) argument values
/// to be passed onto a function during a call.
#[derive(Clone)]
pub struct FnPtr {
name: ImmutableString,
curry: Vec<Dynamic>,
environ: Option<Shared<EncapsulatedEnviron>>,
#[cfg(not(feature = "no_function"))]
fn_def: Option<Shared<crate::ast::ScriptFnDef>>,
}
impl Hash for FnPtr {
#[inline(always)]
fn hash<H: Hasher>(&self, state: &mut H) {
self.name.hash(state);
self.curry.hash(state);
// Hash the shared [`EncapsulatedEnviron`] by hashing its shared pointer.
self.environ.as_ref().map(Shared::as_ptr).hash(state);
// Hash the linked [`ScriptFnDef`][crate::ast::ScriptFnDef] by hashing its shared pointer.
#[cfg(not(feature = "no_function"))]
self.fn_def.as_ref().map(Shared::as_ptr).hash(state);
}
}
impl fmt::Debug for FnPtr {
#[cold]
#[inline(never)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let ff = &mut f.debug_tuple("Fn");
ff.field(&self.name);
self.curry.iter().for_each(|curry| {
ff.field(curry);
});
ff.finish()?;
#[cfg(not(feature = "no_function"))]
if let Some(ref fn_def) = self.fn_def {
write!(f, ": {fn_def}")?;
}
Ok(())
}
}
impl FnPtr {
/// Create a new function pointer.
#[inline(always)]
pub fn new(name: impl Into<ImmutableString>) -> RhaiResultOf<Self> {
name.into().try_into()
}
/// Create a new function pointer without checking its parameters.
#[inline(always)]
#[must_use]
#[allow(dead_code)]
pub(crate) fn new_unchecked(name: impl Into<ImmutableString>, curry: Vec<Dynamic>) -> Self {
Self {
name: name.into(),
curry,
environ: None,
#[cfg(not(feature = "no_function"))]
fn_def: None,
}
}
/// Get the name of the function.
#[inline(always)]
#[must_use]
pub fn fn_name(&self) -> &str {
self.fn_name_raw().as_str()
}
/// Get the name of the function.
#[inline(always)]
#[must_use]
pub(crate) const fn fn_name_raw(&self) -> &ImmutableString {
&self.name
}
/// Get the underlying data of the function pointer.
#[cfg(not(feature = "no_function"))]
#[inline(always)]
#[must_use]
pub(crate) fn take_data(
self,
) -> (
ImmutableString,
Vec<Dynamic>,
Option<Shared<EncapsulatedEnviron>>,
Option<Shared<crate::ast::ScriptFnDef>>,
) {
(self.name, self.curry, self.environ, self.fn_def)
}
/// Get the underlying data of the function pointer.
#[cfg(feature = "no_function")]
#[inline(always)]
#[must_use]
pub(crate) fn take_data(
self,
) -> (
ImmutableString,
Vec<Dynamic>,
Option<Shared<EncapsulatedEnviron>>,
) {
(self.name, self.curry, self.environ)
}
/// Get the curried arguments.
#[inline(always)]
pub fn curry(&self) -> &[Dynamic] {
self.curry.as_ref()
}
/// Iterate the curried arguments.
#[inline(always)]
pub fn iter_curry(&self) -> impl Iterator<Item = &Dynamic> {
self.curry.iter()
}
/// Mutably-iterate the curried arguments.
#[inline(always)]
pub fn iter_curry_mut(&mut self) -> impl Iterator<Item = &mut Dynamic> {
self.curry.iter_mut()
}
/// Add a new curried argument.
#[inline(always)]
pub fn add_curry(&mut self, value: Dynamic) -> &mut Self {
self.curry.push(value);
self
}
/// Set curried arguments to the function pointer.
#[inline]
pub fn set_curry(&mut self, values: impl IntoIterator<Item = Dynamic>) -> &mut Self {
self.curry = values.into_iter().collect();
self
}
/// Is the function pointer curried?
#[inline(always)]
#[must_use]
pub fn is_curried(&self) -> bool {
!self.curry.is_empty()
}
/// Does the function pointer refer to an anonymous function?
///
/// Not available under `no_function`.
#[cfg(not(feature = "no_function"))]
#[inline(always)]
#[must_use]
pub fn is_anonymous(&self) -> bool {
crate::func::is_anonymous_fn(&self.name)
}
/// Call the function pointer with curried arguments (if any).
/// The function may be script-defined (not available under `no_function`) or native Rust.
///
/// This method is intended for calling a function pointer directly, possibly on another [`Engine`].
/// Therefore, the [`AST`] is _NOT_ evaluated before calling the function.
///
/// # Example
///
/// ```
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// # #[cfg(not(feature = "no_function"))]
/// # {
/// use rhai::{Engine, FnPtr};
///
/// let engine = Engine::new();
///
/// let ast = engine.compile("fn foo(x, y) { len(x) + y }")?;
///
/// let mut fn_ptr = FnPtr::new("foo")?;
///
/// // Curry values into the function pointer
/// fn_ptr.set_curry(vec!["abc".into()]);
///
/// // Values are only needed for non-curried parameters
/// let result: i64 = fn_ptr.call(&engine, &ast, ( 39_i64, ) )?;
///
/// assert_eq!(result, 42);
/// # }
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn call<T: Variant + Clone>(
&self,
engine: &Engine,
ast: &AST,
args: impl FuncArgs,
) -> RhaiResultOf<T> {
let _ast = ast;
let mut arg_values = StaticVec::new_const();
args.parse(&mut arg_values);
let global = &mut GlobalRuntimeState::new(engine);
#[cfg(not(feature = "no_function"))]
global.lib.push(_ast.shared_lib().clone());
let ctx = (engine, self.fn_name(), None, &*global, Position::NONE).into();
self.call_raw(&ctx, None, arg_values).and_then(|result| {
result.try_cast_raw().map_err(|r| {
let result_type = engine.map_type_name(r.type_name());
let cast_type = match type_name::<T>() {
typ if typ.contains("::") => engine.map_type_name(typ),
typ => typ,
};
ERR::ErrorMismatchOutputType(cast_type.into(), result_type.into(), Position::NONE)
.into()
})
})
}
/// Call the function pointer with curried arguments (if any).
/// The function may be script-defined (not available under `no_function`) or native Rust.
///
/// This method is intended for calling a function pointer that is passed into a native Rust
/// function as an argument. Therefore, the [`AST`] is _NOT_ evaluated before calling the
/// function.
#[inline]
pub fn call_within_context<T: Variant + Clone>(
&self,
context: &NativeCallContext,
args: impl FuncArgs,
) -> RhaiResultOf<T> {
let mut arg_values = StaticVec::new_const();
args.parse(&mut arg_values);
self.call_raw(context, None, arg_values).and_then(|result| {
result.try_cast_raw().map_err(|r| {
let result_type = context.engine().map_type_name(r.type_name());
let cast_type = match type_name::<T>() {
typ if typ.contains("::") => context.engine().map_type_name(typ),
typ => typ,
};
ERR::ErrorMismatchOutputType(cast_type.into(), result_type.into(), Position::NONE)
.into()
})
})
}
/// Call the function pointer with curried arguments (if any).
/// The function may be script-defined (not available under `no_function`) or native Rust.
///
/// This method is intended for calling a function pointer that is passed into a native Rust
/// function as an argument. Therefore, the [`AST`] is _NOT_ evaluated before calling the
/// function.
///
/// # WARNING - Low Level API
///
/// This function is very low level.
///
/// # Arguments
///
/// All the arguments are _consumed_, meaning that they're replaced by `()`.
/// This is to avoid unnecessarily cloning the arguments.
///
/// Do not use the arguments after this call. If they are needed afterwards,
/// clone them _before_ calling this function.
#[inline]
pub fn call_raw(
&self,
context: &NativeCallContext,
this_ptr: Option<&mut Dynamic>,
arg_values: impl AsMut<[Dynamic]>,
) -> RhaiResult {
let mut arg_values = arg_values;
let mut arg_values = arg_values.as_mut();
let mut args_data;
if self.is_curried() {
args_data = FnArgsVec::with_capacity(self.curry().len() + arg_values.len());
args_data.extend(self.curry().iter().cloned());
args_data.extend(arg_values.iter_mut().map(mem::take));
arg_values = &mut *args_data;
};
let args = &mut StaticVec::with_capacity(arg_values.len() + 1);
args.extend(arg_values.iter_mut());
// Linked to scripted function?
#[cfg(not(feature = "no_function"))]
match self.fn_def {
Some(ref fn_def) if fn_def.params.len() == args.len() => {
let global = &mut context.global_runtime_state().clone();
global.level += 1;
let caches = &mut crate::eval::Caches::new();
return context.engine().call_script_fn(
global,
caches,
&mut crate::Scope::new(),
this_ptr,
self.encapsulated_environ().map(AsRef::as_ref),
fn_def,
args,
true,
context.position(),
);
}
_ => (),
}
let is_method = this_ptr.is_some();
if let Some(obj) = this_ptr {
args.insert(0, obj);
}
context.call_fn_raw(self.fn_name(), is_method, is_method, args)
}
/// Get a reference to the [encapsulated environment][EncapsulatedEnviron].
#[inline(always)]
#[must_use]
#[allow(dead_code)]
pub(crate) const fn encapsulated_environ(&self) -> Option<&Shared<EncapsulatedEnviron>> {
self.environ.as_ref()
}
/// Set a reference to the [encapsulated environment][EncapsulatedEnviron].
#[inline(always)]
#[allow(dead_code)]
pub(crate) fn set_encapsulated_environ(
&mut self,
value: Option<impl Into<Shared<EncapsulatedEnviron>>>,
) {
self.environ = value.map(Into::into);
}
/// Get a reference to the linked [`ScriptFnDef`][crate::ast::ScriptFnDef].
#[cfg(not(feature = "no_function"))]
#[inline(always)]
#[must_use]
pub(crate) const fn fn_def(&self) -> Option<&Shared<crate::ast::ScriptFnDef>> {
self.fn_def.as_ref()
}
/// Set a reference to the linked [`ScriptFnDef`][crate::ast::ScriptFnDef].
#[cfg(not(feature = "no_function"))]
#[inline(always)]
pub(crate) fn set_fn_def(&mut self, value: Option<impl Into<Shared<crate::ast::ScriptFnDef>>>) {
self.fn_def = value.map(Into::into);
}
/// Make a call to a function pointer with either a specified number of arguments, or with extra
/// arguments attached.
///
/// If `this_ptr` is provided, it is first provided to script-defined functions bound to `this`.
///
/// When an appropriate function is not found and `move_this_ptr_to_args` is `Some`, `this_ptr`
/// is removed and inserted as the appropriate parameter number.
///
/// This is useful for calling predicate closures within an iteration loop where the extra argument
/// is the current element's index.
///
/// If the function pointer is linked to a scripted function definition, use the appropriate number
/// of arguments to call it directly (one version attaches extra arguments).
#[cfg(not(feature = "internals"))]
#[inline(always)]
#[allow(dead_code)]
pub(crate) fn call_raw_with_extra_args<const N: usize, const E: usize>(
&self,
fn_name: &str,
ctx: &NativeCallContext,
this_ptr: Option<&mut Dynamic>,
args: [Dynamic; N],
extras: [Dynamic; E],
move_this_ptr_to_args: Option<usize>,
) -> RhaiResult {
match move_this_ptr_to_args {
Some(m) => {
self._call_with_extra_args::<true, N, E>(fn_name, ctx, this_ptr, args, extras, m)
}
None => {
self._call_with_extra_args::<false, N, E>(fn_name, ctx, this_ptr, args, extras, 0)
}
}
}
/// _(internals)_ Make a call to a function pointer with either a specified number of arguments,
/// or with extra arguments attached. Exported under the `internals` feature only.
///
/// If `this_ptr` is provided, it is first provided to script-defined functions bound to `this`.
///
/// When an appropriate function is not found and `move_this_ptr_to_args` is `Some`, `this_ptr`
/// is removed and inserted as the appropriate parameter number.
///
/// This is useful for calling predicate closures within an iteration loop where the extra
/// argument is the current element's index.
///
/// If the function pointer is linked to a scripted function definition, use the appropriate
/// number of arguments to call it directly (one version attaches extra arguments).
#[cfg(feature = "internals")]
#[inline(always)]
pub fn call_raw_with_extra_args<const N: usize, const E: usize>(
&self,
fn_name: &str,
ctx: &NativeCallContext,
this_ptr: Option<&mut Dynamic>,
args: [Dynamic; N],
extras: [Dynamic; E],
move_this_ptr_to_args: Option<usize>,
) -> RhaiResult {
match move_this_ptr_to_args {
Some(m) => {
self._call_with_extra_args::<true, N, E>(fn_name, ctx, this_ptr, args, extras, m)
}
None => {
self._call_with_extra_args::<false, N, E>(fn_name, ctx, this_ptr, args, extras, 0)
}
}
}
/// Make a call to a function pointer with either a specified number of arguments, or with extra
/// arguments attached.
fn _call_with_extra_args<const MOVE_PTR: bool, const N: usize, const E: usize>(
&self,
fn_name: &str,
ctx: &NativeCallContext,
mut this_ptr: Option<&mut Dynamic>,
args: [Dynamic; N],
extras: [Dynamic; E],
move_this_ptr_to_args: usize,
) -> RhaiResult {
#[cfg(not(feature = "no_function"))]
if let Some(arity) = self.fn_def().map(|f| f.params.len()) {
if arity == N + self.curry().len() {
return self.call_raw(ctx, this_ptr, args);
}
if MOVE_PTR && this_ptr.is_some() {
if arity == N + 1 + self.curry().len() {
let mut args2 = FnArgsVec::with_capacity(args.len() + 1);
if move_this_ptr_to_args == 0 {
args2.push(this_ptr.as_mut().unwrap().clone());
args2.extend(args);
} else {
args2.extend(args);
args2.insert(move_this_ptr_to_args, this_ptr.as_mut().unwrap().clone());
}
return self.call_raw(ctx, None, args2);
}
if arity == N + E + 1 + self.curry().len() {
let mut args2 = FnArgsVec::with_capacity(args.len() + extras.len() + 1);
if move_this_ptr_to_args == 0 {
args2.push(this_ptr.as_mut().unwrap().clone());
args2.extend(args);
} else {
args2.extend(args);
args2.insert(move_this_ptr_to_args, this_ptr.as_mut().unwrap().clone());
}
args2.extend(extras);
return self.call_raw(ctx, None, args2);
}
}
if arity == N + E + self.curry().len() {
let mut args2 = FnArgsVec::with_capacity(args.len() + extras.len());
args2.extend(args);
args2.extend(extras);
return self.call_raw(ctx, this_ptr, args2);
}
}
self.call_raw(ctx, this_ptr.as_deref_mut(), args.clone())
.or_else(|err| match *err {
ERR::ErrorFunctionNotFound(sig, ..)
if MOVE_PTR && this_ptr.is_some() && sig.starts_with(self.fn_name()) =>
{
let mut args2 = FnArgsVec::with_capacity(args.len() + 1);
if move_this_ptr_to_args == 0 {
args2.push(this_ptr.as_mut().unwrap().clone());
args2.extend(args.clone());
} else {
args2.extend(args.clone());
args2.insert(move_this_ptr_to_args, this_ptr.as_mut().unwrap().clone());
}
self.call_raw(ctx, None, args2)
}
_ => Err(err),
})
.or_else(|err| match *err {
ERR::ErrorFunctionNotFound(sig, ..) if sig.starts_with(self.fn_name()) => {
if MOVE_PTR {
if let Some(ref mut this_ptr) = this_ptr {
let mut args2 = FnArgsVec::with_capacity(args.len() + extras.len() + 1);
if move_this_ptr_to_args == 0 {
args2.push(this_ptr.clone());
args2.extend(args);
args2.extend(extras);
} else {
args2.extend(args);
args2.extend(extras);
args2.insert(move_this_ptr_to_args, this_ptr.clone());
}
return self.call_raw(ctx, None, args2);
}
}
let mut args2 = FnArgsVec::with_capacity(args.len() + extras.len());
args2.extend(args);
args2.extend(extras);
self.call_raw(ctx, this_ptr, args2)
}
_ => Err(err),
})
.map_err(|err| {
Box::new(ERR::ErrorInFunctionCall(
fn_name.to_string(),
ctx.source().unwrap_or("").to_string(),
err,
Position::NONE,
))
})
}
}
impl fmt::Display for FnPtr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Fn({})", self.fn_name())
}
}
impl TryFrom<ImmutableString> for FnPtr {
type Error = RhaiError;
#[inline(always)]
fn try_from(value: ImmutableString) -> RhaiResultOf<Self> {
if is_valid_function_name(&value) {
Ok(Self {
name: value,
curry: Vec::new(),
environ: None,
#[cfg(not(feature = "no_function"))]
fn_def: None,
})
} else if is_reserved_keyword_or_symbol(&value).0
|| Token::lookup_symbol_from_syntax(&value).is_some()
{
Err(
ERR::ErrorParsing(ParseErrorType::Reserved(value.to_string()), Position::NONE)
.into(),
)
} else {
Err(ERR::ErrorFunctionNotFound(value.to_string(), Position::NONE).into())
}
}
}
#[cfg(not(feature = "no_function"))]
impl<T: Into<Shared<crate::ast::ScriptFnDef>>> From<T> for FnPtr {
#[inline(always)]
fn from(value: T) -> Self {
let fn_def = value.into();
Self {
name: fn_def.name.clone(),
curry: Vec::new(),
environ: None,
fn_def: Some(fn_def),
}
}
}
impl Index<usize> for FnPtr {
type Output = Dynamic;
#[inline(always)]
fn index(&self, index: usize) -> &Self::Output {
self.curry.index(index)
}
}
impl IndexMut<usize> for FnPtr {
#[inline(always)]
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
self.curry.index_mut(index)
}
}
impl Extend<Dynamic> for FnPtr {
#[inline(always)]
fn extend<T: IntoIterator<Item = Dynamic>>(&mut self, iter: T) {
self.curry.extend(iter);
}
}