Merge branch 'master' into plugins

This commit is contained in:
Stephen Chung
2020-06-05 15:39:44 +08:00
6 changed files with 319 additions and 255 deletions

View File

@@ -1,9 +1,9 @@
use crate::any::Dynamic;
use crate::parser::FnDef;
use crate::parser::ScriptFnDef;
use crate::plugin::PluginFunction;
use crate::result::EvalAltResult;
use crate::stdlib::{boxed::Box, rc::Rc, sync::Arc};
use crate::stdlib::{boxed::Box, fmt, rc::Rc, sync::Arc};
#[cfg(feature = "sync")]
pub trait SendSync: Send + Sync {}
@@ -78,10 +78,22 @@ pub enum CallableFunction {
Method(Shared<FnAny>),
/// An iterator function.
Iterator(IteratorFn),
/// A script-defined function.
Script(Shared<FnDef>),
/// A plugin-defined function,
Plugin(SharedPluginFunction),
/// A script-defined function.
Script(Shared<ScriptFnDef>),
}
impl fmt::Debug for CallableFunction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Pure(_) => write!(f, "NativePureFunction"),
Self::Method(_) => write!(f, "NativeMethod"),
Self::Iterator(_) => write!(f, "IteratorFunction"),
Self::Plugin(_) => write!(f, "PluginFunction"),
Self::Script(fn_def) => write!(f, "{:?}", fn_def),
}
}
}
impl CallableFunction {
@@ -136,12 +148,23 @@ impl CallableFunction {
Self::Plugin(_) => panic!(),
}
}
/// Get a shared reference to a script-defined function definition.
///
/// # Panics
///
/// Panics if the `CallableFunction` is not `Script`.
pub fn get_shared_fn_def(&self) -> Shared<ScriptFnDef> {
match self {
Self::Pure(_) | Self::Method(_) | Self::Plugin(_) | Self::Iterator(_) => panic!(),
Self::Script(f) => f.clone(),
}
}
/// Get a reference to a script-defined function definition.
///
/// # Panics
///
/// Panics if the `CallableFunction` is not `Script`.
pub fn get_fn_def(&self) -> &FnDef {
pub fn get_fn_def(&self) -> &ScriptFnDef {
match self {
Self::Pure(_) | Self::Method(_) | Self::Iterator(_) => panic!(),
Self::Script(f) => f,
@@ -192,3 +215,21 @@ impl CallableFunction {
Self::Plugin(Rc::new(plugin))
}
}
impl From<IteratorFn> for CallableFunction {
fn from(func: IteratorFn) -> Self {
Self::Iterator(func)
}
}
impl From<ScriptFnDef> for CallableFunction {
fn from(func: ScriptFnDef) -> Self {
Self::Script(func.into())
}
}
impl From<Shared<ScriptFnDef>> for CallableFunction {
fn from(func: Shared<ScriptFnDef>) -> Self {
Self::Script(func)
}
}