Restore plugins code.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use crate::any::Dynamic;
|
||||
use crate::engine::Engine;
|
||||
use crate::parser::ScriptFnDef;
|
||||
use crate::plugin::PluginFunction;
|
||||
use crate::result::EvalAltResult;
|
||||
use crate::utils::ImmutableString;
|
||||
|
||||
@@ -96,6 +97,11 @@ pub type FnAny =
|
||||
/// A standard function that gets an iterator from a type.
|
||||
pub type IteratorFn = fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>>;
|
||||
|
||||
#[cfg(feature = "sync")]
|
||||
pub type SharedPluginFunction = Arc<dyn PluginFunction + Send + Sync>;
|
||||
#[cfg(not(feature = "sync"))]
|
||||
pub type SharedPluginFunction = Rc<dyn PluginFunction>;
|
||||
|
||||
/// A standard callback function.
|
||||
#[cfg(not(feature = "sync"))]
|
||||
pub type Callback<T, R> = Box<dyn Fn(&T) -> R + 'static>;
|
||||
@@ -113,6 +119,8 @@ pub enum CallableFunction {
|
||||
Method(Shared<FnAny>),
|
||||
/// An iterator function.
|
||||
Iterator(IteratorFn),
|
||||
/// A plugin-defined function,
|
||||
Plugin(SharedPluginFunction),
|
||||
/// A script-defined function.
|
||||
Script(Shared<ScriptFnDef>),
|
||||
}
|
||||
@@ -123,6 +131,7 @@ impl fmt::Debug for CallableFunction {
|
||||
Self::Pure(_) => write!(f, "NativePureFunction"),
|
||||
Self::Method(_) => write!(f, "NativeMethod"),
|
||||
Self::Iterator(_) => write!(f, "NativeIterator"),
|
||||
Self::Plugin(_) => write!(f, "PluginFunction"),
|
||||
Self::Script(fn_def) => fmt::Debug::fmt(fn_def, f),
|
||||
}
|
||||
}
|
||||
@@ -134,6 +143,7 @@ impl fmt::Display for CallableFunction {
|
||||
Self::Pure(_) => write!(f, "NativePureFunction"),
|
||||
Self::Method(_) => write!(f, "NativeMethod"),
|
||||
Self::Iterator(_) => write!(f, "NativeIterator"),
|
||||
Self::Plugin(_) => write!(f, "PluginFunction"),
|
||||
CallableFunction::Script(s) => fmt::Display::fmt(s, f),
|
||||
}
|
||||
}
|
||||
@@ -144,28 +154,35 @@ impl CallableFunction {
|
||||
pub fn is_pure(&self) -> bool {
|
||||
match self {
|
||||
Self::Pure(_) => true,
|
||||
Self::Method(_) | Self::Iterator(_) | Self::Script(_) => false,
|
||||
Self::Method(_) | Self::Iterator(_) | Self::Script(_) | Self::Plugin(_) => false,
|
||||
}
|
||||
}
|
||||
/// Is this a native Rust method function?
|
||||
pub fn is_method(&self) -> bool {
|
||||
match self {
|
||||
Self::Method(_) => true,
|
||||
Self::Pure(_) | Self::Iterator(_) | Self::Script(_) => false,
|
||||
Self::Pure(_) | Self::Iterator(_) | Self::Script(_) | Self::Plugin(_) => false,
|
||||
}
|
||||
}
|
||||
/// Is this an iterator function?
|
||||
pub fn is_iter(&self) -> bool {
|
||||
match self {
|
||||
Self::Iterator(_) => true,
|
||||
Self::Pure(_) | Self::Method(_) | Self::Script(_) => false,
|
||||
Self::Pure(_) | Self::Method(_) | Self::Script(_) | Self::Plugin(_) => false,
|
||||
}
|
||||
}
|
||||
/// Is this a Rhai-scripted function?
|
||||
pub fn is_script(&self) -> bool {
|
||||
match self {
|
||||
Self::Script(_) => true,
|
||||
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => false,
|
||||
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) | Self::Plugin(_) => false,
|
||||
}
|
||||
}
|
||||
/// Is this a plugin-defined function?
|
||||
pub fn is_plugin_fn(&self) -> bool {
|
||||
match self {
|
||||
Self::Plugin(_) => true,
|
||||
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) | Self::Script(_) => false,
|
||||
}
|
||||
}
|
||||
/// Get a reference to a native Rust function.
|
||||
@@ -176,7 +193,7 @@ impl CallableFunction {
|
||||
pub fn get_native_fn(&self) -> &FnAny {
|
||||
match self {
|
||||
Self::Pure(f) | Self::Method(f) => f.as_ref(),
|
||||
Self::Iterator(_) | Self::Script(_) => unreachable!(),
|
||||
Self::Iterator(_) | Self::Script(_) | Self::Plugin(_) => unreachable!(),
|
||||
}
|
||||
}
|
||||
/// Get a shared reference to a script-defined function definition.
|
||||
@@ -186,7 +203,7 @@ impl CallableFunction {
|
||||
/// Panics if the `CallableFunction` is not `Script`.
|
||||
pub fn get_shared_fn_def(&self) -> Shared<ScriptFnDef> {
|
||||
match self {
|
||||
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => unreachable!(),
|
||||
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) | Self::Plugin(_) => unreachable!(),
|
||||
Self::Script(f) => f.clone(),
|
||||
}
|
||||
}
|
||||
@@ -197,7 +214,7 @@ impl CallableFunction {
|
||||
/// Panics if the `CallableFunction` is not `Script`.
|
||||
pub fn get_fn_def(&self) -> &ScriptFnDef {
|
||||
match self {
|
||||
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => unreachable!(),
|
||||
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) | Self::Plugin(_) => unreachable!(),
|
||||
Self::Script(f) => f,
|
||||
}
|
||||
}
|
||||
@@ -209,7 +226,18 @@ impl CallableFunction {
|
||||
pub fn get_iter_fn(&self) -> IteratorFn {
|
||||
match self {
|
||||
Self::Iterator(f) => *f,
|
||||
Self::Pure(_) | Self::Method(_) | Self::Script(_) => unreachable!(),
|
||||
Self::Pure(_) | Self::Method(_) | Self::Script(_) | Self::Plugin(_) => unreachable!(),
|
||||
}
|
||||
}
|
||||
/// Get a reference to a plugin function.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the `CallableFunction` is not `Plugin`.
|
||||
pub fn get_plugin_fn<'s>(&'s self) -> SharedPluginFunction {
|
||||
match self {
|
||||
Self::Plugin(f) => f.clone(),
|
||||
Self::Pure(_) | Self::Method(_) | Self::Script(_) | Self::Iterator(_) => unreachable!(),
|
||||
}
|
||||
}
|
||||
/// Create a new `CallableFunction::Pure`.
|
||||
@@ -220,6 +248,18 @@ impl CallableFunction {
|
||||
pub fn from_method(func: Box<FnAny>) -> Self {
|
||||
Self::Method(func.into())
|
||||
}
|
||||
|
||||
#[cfg(feature = "sync")]
|
||||
/// Create a new `CallableFunction::Plugin`.
|
||||
pub fn from_plugin(plugin: impl PluginFunction + 'static + Send + Sync) -> Self {
|
||||
Self::Plugin(Arc::new(plugin))
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "sync"))]
|
||||
/// Create a new `CallableFunction::Plugin`.
|
||||
pub fn from_plugin(plugin: impl PluginFunction + 'static) -> Self {
|
||||
Self::Plugin(Rc::new(plugin))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<IteratorFn> for CallableFunction {
|
||||
|
Reference in New Issue
Block a user