Merge branch 'plugins_dev' into plugins

This commit is contained in:
Stephen Chung 2020-08-19 13:29:03 +08:00
commit 9e7516ba27
3 changed files with 15 additions and 6 deletions

View File

@ -261,7 +261,9 @@ impl CallableFunction {
pub fn is_pure(&self) -> bool {
match self {
Self::Pure(_) => true,
Self::Method(_) | Self::Iterator(_) | Self::Plugin(_) => false,
Self::Method(_) | Self::Iterator(_) => false,
Self::Plugin(p) => !p.is_method_call(),
#[cfg(not(feature = "no_function"))]
Self::Script(_) => false,
@ -271,7 +273,9 @@ impl CallableFunction {
pub fn is_method(&self) -> bool {
match self {
Self::Method(_) => true,
Self::Pure(_) | Self::Iterator(_) | Self::Plugin(_) => false,
Self::Pure(_) | Self::Iterator(_) => false,
Self::Plugin(p) => p.is_method_call(),
#[cfg(not(feature = "no_function"))]
Self::Script(_) => false,

View File

@ -22,12 +22,12 @@ macro_rules! gen_concat_functions {
use super::super::*;
#[export_fn]
pub fn append_func(x: ImmutableString, y: $arg_type) -> String {
pub fn append_func(x: &mut ImmutableString, y: $arg_type) -> String {
super::super::add_append(x, y)
}
#[export_fn]
pub fn prepend_func(x: $arg_type, y: ImmutableString) -> String {
pub fn prepend_func(x: &mut $arg_type, y: ImmutableString) -> String {
super::super::add_prepend(x, y)
}
}
@ -120,10 +120,10 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
);
});
fn add_prepend<T: Display>(x: T, y: ImmutableString) -> String {
fn add_prepend<T: Display>(x: &mut T, y: ImmutableString) -> String {
format!("{}{}", x, y)
}
fn add_append<T: Display>(x: ImmutableString, y: T) -> String {
fn add_append<T: Display>(x: &mut ImmutableString, y: T) -> String {
format!("{}{}", x, y)
}

View File

@ -7,6 +7,10 @@ use rhai::{Engine, EvalAltResult, INT};
mod special_array_package {
use rhai::{Array, INT};
#[rhai_fn(get = "foo", return_raw)]
pub fn foo(array: &mut Array) -> Result<Dynamic, Box<EvalAltResult>> {
Ok(array[0].clone())
}
#[rhai_fn(name = "test")]
pub fn len(array: &mut Array, mul: INT) -> INT {
(array.len() as INT) * mul
@ -53,6 +57,7 @@ fn test_plugins_package() -> Result<(), Box<EvalAltResult>> {
reg_functions!(engine += greet::single(INT, bool, char));
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; a.foo")?, 1);
assert_eq!(engine.eval::<INT>("let a = [1, 2, 3]; test(a, 2)")?, 6);
assert_eq!(engine.eval::<INT>("2 + 2")?, 5);
assert_eq!(