2020-08-14 12:58:34 +02:00
|
|
|
use crate::plugin::*;
|
2021-03-29 07:07:10 +02:00
|
|
|
use crate::{def_package, FnPtr, ImmutableString, NativeCallContext};
|
2021-04-17 09:15:54 +02:00
|
|
|
#[cfg(feature = "no_std")]
|
|
|
|
use std::prelude::v1::*;
|
2020-11-23 12:11:32 +01:00
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
def_package! {
|
2021-12-22 05:41:55 +01:00
|
|
|
/// Package of basic function pointer utilities.
|
2021-12-20 04:42:39 +01:00
|
|
|
crate::BasicFnPackage => |lib| {
|
|
|
|
lib.standard = true;
|
2021-11-05 16:22:05 +01:00
|
|
|
|
2021-12-20 04:42:39 +01:00
|
|
|
combine_with_exported_module!(lib, "FnPtr", fn_ptr_functions);
|
|
|
|
}
|
|
|
|
}
|
2020-08-16 17:41:59 +02:00
|
|
|
|
2020-08-20 16:11:41 +02:00
|
|
|
#[export_module]
|
|
|
|
mod fn_ptr_functions {
|
2022-01-15 16:34:38 +01:00
|
|
|
/// Return the name of the function.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rhai
|
|
|
|
/// fn double(x) { x * 2 }
|
|
|
|
///
|
|
|
|
/// let f = Fn("double");
|
|
|
|
///
|
|
|
|
/// print(f.name); // prints "double"
|
|
|
|
/// ```
|
2021-02-19 16:13:53 +01:00
|
|
|
#[rhai_fn(name = "name", get = "name", pure)]
|
2021-08-13 07:42:39 +02:00
|
|
|
pub fn name(fn_ptr: &mut FnPtr) -> ImmutableString {
|
|
|
|
fn_ptr.fn_name_raw().into()
|
2020-08-20 16:11:41 +02:00
|
|
|
}
|
2020-10-18 16:10:08 +02:00
|
|
|
|
2022-01-15 16:34:38 +01:00
|
|
|
/// Return `true` if the function is an anonymous function.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rhai
|
|
|
|
/// let f = |x| x * 2;
|
|
|
|
///
|
|
|
|
/// print(f.is_anonymous); // prints true
|
|
|
|
/// ```
|
2020-10-18 16:10:08 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2021-10-20 10:22:12 +02:00
|
|
|
#[rhai_fn(name = "is_anonymous", get = "is_anonymous", pure)]
|
|
|
|
pub fn is_anonymous(fn_ptr: &mut FnPtr) -> bool {
|
|
|
|
fn_ptr.is_anonymous()
|
2020-10-18 11:02:17 +02:00
|
|
|
}
|
2020-08-16 17:41:59 +02:00
|
|
|
}
|