Add plugins test.
This commit is contained in:
parent
675c4eb606
commit
5eed5fe6a3
@ -204,7 +204,11 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Run external function
|
// Run external function
|
||||||
let result = func.get_native_fn()(self, lib, args)?;
|
let result = if func.is_plugin_fn() {
|
||||||
|
func.get_plugin_fn().call(args, Position::none())?
|
||||||
|
} else {
|
||||||
|
func.get_native_fn()(self, lib, args)?
|
||||||
|
};
|
||||||
|
|
||||||
// Restore the original reference
|
// Restore the original reference
|
||||||
restore_first_arg(old_this_ptr, args);
|
restore_first_arg(old_this_ptr, args);
|
||||||
|
@ -2,18 +2,11 @@
|
|||||||
|
|
||||||
use crate::stdlib::{any::TypeId, boxed::Box};
|
use crate::stdlib::{any::TypeId, boxed::Box};
|
||||||
|
|
||||||
pub use crate::any::{Dynamic, Variant};
|
use crate::any::Dynamic;
|
||||||
pub use crate::fn_native::{CallableFunction, FnCallArgs, IteratorFn};
|
use crate::engine::Engine;
|
||||||
pub use crate::parser::{
|
pub use crate::fn_native::CallableFunction;
|
||||||
FnAccess,
|
use crate::result::EvalAltResult;
|
||||||
FnAccess::{Private, Public},
|
use crate::token::Position;
|
||||||
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")]
|
#[cfg(features = "sync")]
|
||||||
/// Represents an externally-written plugin for the Rhai interpreter.
|
/// Represents an externally-written plugin for the Rhai interpreter.
|
||||||
@ -38,7 +31,8 @@ pub trait PluginFunction {
|
|||||||
fn is_method_call(&self) -> bool;
|
fn is_method_call(&self) -> bool;
|
||||||
fn is_varadic(&self) -> bool;
|
fn is_varadic(&self) -> bool;
|
||||||
|
|
||||||
fn call(&self, args: &mut[&mut Dynamic], pos: Position) -> Result<Dynamic, Box<EvalAltResult>>;
|
fn call(&self, args: &mut [&mut Dynamic], pos: Position)
|
||||||
|
-> Result<Dynamic, Box<EvalAltResult>>;
|
||||||
|
|
||||||
fn clone_boxed(&self) -> Box<dyn PluginFunction>;
|
fn clone_boxed(&self) -> Box<dyn PluginFunction>;
|
||||||
|
|
||||||
|
@ -18,9 +18,9 @@ impl Engine {
|
|||||||
///
|
///
|
||||||
/// When searching for functions, packages loaded later are preferred.
|
/// When searching for functions, packages loaded later are preferred.
|
||||||
/// In other words, loaded packages are searched in reverse order.
|
/// In other words, loaded packages are searched in reverse order.
|
||||||
pub fn load_package(&mut self, package: PackageLibrary) -> &mut Self {
|
pub fn load_package(&mut self, package: impl Into<PackageLibrary>) -> &mut Self {
|
||||||
// Push the package to the top - packages are searched in reverse order
|
// Push the package to the top - packages are searched in reverse order
|
||||||
self.packages.push(package);
|
self.packages.push(package.into());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let mut engine = Engine::new();
|
let mut engine = Engine::new();
|
||||||
engine.load_package(module.into());
|
engine.load_package(module);
|
||||||
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
use rhai::{export_module, exported_module};
|
use rhai::{
|
||||||
|
export_fn, export_module, exported_module,
|
||||||
|
plugin::{CallableFunction, PluginFunction},
|
||||||
|
register_exported_fn,
|
||||||
|
};
|
||||||
use rhai::{Engine, EvalAltResult, INT};
|
use rhai::{Engine, EvalAltResult, INT};
|
||||||
|
|
||||||
#[export_module]
|
#[export_module]
|
||||||
pub mod array_package {
|
mod special_array_package {
|
||||||
use rhai::{Array, INT};
|
use rhai::{Array, INT};
|
||||||
|
|
||||||
pub fn len(array: &mut Array, mul: INT) -> INT {
|
pub fn len(array: &mut Array, mul: INT) -> INT {
|
||||||
@ -10,15 +14,25 @@ pub mod array_package {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[export_fn]
|
||||||
|
fn make_greeting(n: INT) -> String {
|
||||||
|
format!("{} {}", n, if n > 1 { "kitties" } else { "kitty" }).into()
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_plugins() -> Result<(), Box<EvalAltResult>> {
|
fn test_plugins_package() -> Result<(), Box<EvalAltResult>> {
|
||||||
let mut engine = Engine::new();
|
let mut engine = Engine::new();
|
||||||
|
|
||||||
let m = exported_module!(array_package);
|
let mut m = exported_module!(special_array_package);
|
||||||
|
register_exported_fn!(m, "greet", make_greeting);
|
||||||
|
|
||||||
engine.load_package(m.into());
|
engine.load_package(m);
|
||||||
|
|
||||||
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; a.len(2)")?, 6);
|
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; len(a, 2)")?, 6);
|
||||||
|
assert_eq!(
|
||||||
|
engine.eval::<String>("let a = [1, 2, 3]; greet(len(a, 2))")?,
|
||||||
|
"6 kitties"
|
||||||
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user