Restore plugins code.

This commit is contained in:
Stephen Chung 2020-07-02 12:47:24 +08:00
parent e614790897
commit e14cfbd9d7
6 changed files with 184 additions and 15 deletions

View File

@ -2019,6 +2019,7 @@ impl Engine {
) )
.map_err(|err| err.new_position(*pos)) .map_err(|err| err.new_position(*pos))
} }
Ok(f) if f.is_plugin_fn() => f.get_plugin_fn().call(args.as_mut(), *pos),
Ok(f) => { Ok(f) => {
f.get_native_fn()(self, args.as_mut()).map_err(|err| err.new_position(*pos)) f.get_native_fn()(self, args.as_mut()).map_err(|err| err.new_position(*pos))
} }

View File

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

View File

@ -5,11 +5,91 @@ use crate::any::{Dynamic, Variant};
use crate::engine::Engine; use crate::engine::Engine;
use crate::fn_native::{CallableFunction, FnAny, FnCallArgs, SendSync}; use crate::fn_native::{CallableFunction, FnAny, FnCallArgs, SendSync};
use crate::parser::FnAccess; use crate::parser::FnAccess;
use crate::plugin::Plugin;
use crate::result::EvalAltResult; use crate::result::EvalAltResult;
use crate::utils::ImmutableString; use crate::utils::ImmutableString;
use crate::stdlib::{any::TypeId, boxed::Box, mem}; use crate::stdlib::{any::TypeId, boxed::Box, mem};
/// A trait to register custom plugins with the `Engine`.
///
/// A plugin consists of a number of functions. All functions will be registered with the engine.
pub trait RegisterPlugin<PL: crate::plugin::Plugin> {
/// Allow extensions of the engine's behavior.
///
/// This can include importing modules, registering functions to the global name space, and
/// more.
///
/// # Example
///
/// ```
/// use rhai::{FLOAT, INT, Module, ModuleResolver, RegisterFn, RegisterPlugin};
/// use rhai::plugin::*;
/// use rhai::module_resolvers::*;
///
/// // A function we want to expose to Rhai.
/// #[derive(Copy, Clone)]
/// struct DistanceFunction();
///
/// impl PluginFunction for DistanceFunction {
/// fn is_method_call(&self) -> bool { false }
/// fn is_varadic(&self) -> bool { false }
///
/// fn call(&self, args: &[&mut Dynamic], pos: Position) -> Result<Dynamic, Box<EvalAltResult>> {
/// let x1: &FLOAT = args[0].downcast_ref::<FLOAT>().unwrap();
/// let y1: &FLOAT = args[1].downcast_ref::<FLOAT>().unwrap();
/// let x2: &FLOAT = args[2].downcast_ref::<FLOAT>().unwrap();
/// let y2: &FLOAT = args[3].downcast_ref::<FLOAT>().unwrap();
/// let square_sum = (y2 - y1).abs().powf(2.0) + (x2 -x1).abs().powf(2.0);
/// Ok(Dynamic::from(square_sum.sqrt()))
/// }
///
/// fn clone_boxed(&self) -> Box<dyn PluginFunction> {
/// Box::new(DistanceFunction())
/// }
/// }
///
/// // A simple custom plugin. This should not usually be done with hand-written code.
/// #[derive(Copy, Clone)]
/// pub struct AdvancedMathPlugin();
///
/// impl Plugin for AdvancedMathPlugin {
/// fn register_contents(self, engine: &mut Engine) {
/// // Plugins are allowed to have side-effects on the engine.
/// engine.register_fn("get_mystic_number", || { 42 as FLOAT });
///
/// // Main purpose: create a module to expose the functions to Rhai.
/// //
/// // This is currently a hack. There needs to be a better API here for "plugin"
/// // modules.
/// let mut m = Module::new();
/// m.set_fn("euclidean_distance".to_string(), FnAccess::Public,
/// &[std::any::TypeId::of::<FLOAT>(),
/// std::any::TypeId::of::<FLOAT>(),
/// std::any::TypeId::of::<FLOAT>(),
/// std::any::TypeId::of::<FLOAT>()],
/// CallableFunction::from_plugin(DistanceFunction()));
/// let mut r = StaticModuleResolver::new();
/// r.insert("Math::Advanced".to_string(), m);
/// engine.set_module_resolver(Some(r));
/// }
/// }
///
///
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
///
/// let mut engine = Engine::new();
/// engine.register_plugin(AdvancedMathPlugin());
///
/// assert_eq!(engine.eval::<FLOAT>(
/// r#"import "Math::Advanced" as math;
/// let x = math::euclidean_distance(0.0, 1.0, 0.0, get_mystic_number()); x"#)?, 41.0);
/// # Ok(())
/// # }
/// ```
fn register_plugin(&mut self, plugin: PL);
}
/// Trait to register custom functions with the `Engine`. /// Trait to register custom functions with the `Engine`.
pub trait RegisterFn<FN, ARGS, RET> { pub trait RegisterFn<FN, ARGS, RET> {
/// Register a custom function with the `Engine`. /// Register a custom function with the `Engine`.
@ -111,6 +191,12 @@ pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
} }
} }
impl<PL: Plugin> RegisterPlugin<PL> for Engine {
fn register_plugin(&mut self, plugin: PL) {
plugin.register_contents(self);
}
}
/// This macro creates a closure wrapping a registered function. /// This macro creates a closure wrapping a registered function.
macro_rules! make_func { macro_rules! make_func {
($fn:ident : $map:expr ; $($par:ident => $convert:expr),*) => { ($fn:ident : $map:expr ; $($par:ident => $convert:expr),*) => {

View File

@ -84,6 +84,10 @@ mod module;
mod optimize; mod optimize;
pub mod packages; pub mod packages;
mod parser; mod parser;
#[cfg(not(feature = "no_module"))]
pub mod plugin;
#[cfg(feature = "no_module")]
mod plugin;
mod result; mod result;
mod scope; mod scope;
mod stdlib; mod stdlib;
@ -95,7 +99,7 @@ pub use any::Dynamic;
pub use engine::Engine; pub use engine::Engine;
pub use error::{ParseError, ParseErrorType}; pub use error::{ParseError, ParseErrorType};
pub use fn_native::IteratorFn; pub use fn_native::IteratorFn;
pub use fn_register::{RegisterFn, RegisterResultFn}; pub use fn_register::{RegisterFn, RegisterPlugin, RegisterResultFn};
pub use module::Module; pub use module::Module;
pub use parser::{ImmutableString, AST, INT}; pub use parser::{ImmutableString, AST, INT};
pub use result::EvalAltResult; pub use result::EvalAltResult;

View File

@ -876,12 +876,10 @@ impl Module {
.functions .functions
.iter() .iter()
.filter(|(_, (_, _, _, v))| match v { .filter(|(_, (_, _, _, v))| match v {
CallableFunction::Pure(_)
| CallableFunction::Method(_)
| CallableFunction::Iterator(_) => true,
CallableFunction::Script(ref f) => { CallableFunction::Script(ref f) => {
filter(f.access, f.name.as_str(), f.params.len()) filter(f.access, f.name.as_str(), f.params.len())
} }
_ => true,
}) })
.map(|(&k, v)| (k, v.clone())), .map(|(&k, v)| (k, v.clone())),
); );
@ -897,10 +895,8 @@ impl Module {
/// Filter out the functions, retaining only some based on a filter predicate. /// Filter out the functions, retaining only some based on a filter predicate.
pub(crate) fn retain_functions(&mut self, filter: impl Fn(FnAccess, &str, usize) -> bool) { pub(crate) fn retain_functions(&mut self, filter: impl Fn(FnAccess, &str, usize) -> bool) {
self.functions.retain(|_, (_, _, _, v)| match v { self.functions.retain(|_, (_, _, _, v)| match v {
CallableFunction::Pure(_)
| CallableFunction::Method(_)
| CallableFunction::Iterator(_) => true,
CallableFunction::Script(ref f) => filter(f.access, f.name.as_str(), f.params.len()), CallableFunction::Script(ref f) => filter(f.access, f.name.as_str(), f.params.len()),
_ => true,
}); });
self.all_functions.clear(); self.all_functions.clear();

42
src/plugin.rs Normal file
View File

@ -0,0 +1,42 @@
//! Module defining plugins in Rhai. Is exported for use by plugin authors.
pub use crate::any::{Dynamic, Variant};
pub use crate::fn_native::{CallableFunction, FnCallArgs, IteratorFn};
pub use crate::parser::{
FnAccess,
FnAccess::{Private, Public},
AST,
};
pub use crate::result::EvalAltResult;
pub use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope};
pub use crate::token::{Position, Token};
pub use crate::utils::StaticVec;
pub use crate::Engine;
#[cfg(features = "sync")]
/// Represents an externally-written plugin for the Rhai interpreter.
///
/// This trait should not be used directly. Use the `#[plugin]` procedural attribute instead.
pub trait Plugin: Send {
fn register_contents(self, engine: &mut Engine);
}
#[cfg(not(features = "sync"))]
/// Represents an externally-written plugin for the Rhai interpreter.
///
/// This trait should not be used directly. Use the `#[plugin]` procedural attribute instead.
pub trait Plugin: Send + Sync {
fn register_contents(self, engine: &mut Engine);
}
/// Represents a function that is statically defined within a plugin.
///
/// This trait should not be used directly. Use the `#[plugin]` procedural attribute instead.
pub trait PluginFunction {
fn is_method_call(&self) -> bool;
fn is_varadic(&self) -> bool;
fn call(&self, args: &[&mut Dynamic], pos: Position) -> Result<Dynamic, Box<EvalAltResult>>;
fn clone_boxed(&self) -> Box<dyn PluginFunction>;
}