Merge branch 'master' into plugins

This commit is contained in:
Stephen Chung
2020-08-06 10:31:15 +08:00
37 changed files with 431 additions and 404 deletions

View File

@@ -83,6 +83,7 @@ fn test_closures() -> Result<(), Box<EvalAltResult>> {
#[test]
#[cfg(not(feature = "no_closure"))]
#[cfg(not(feature = "no_object"))]
#[cfg(not(feature = "sync"))]
fn test_closures_data_race() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();

View File

@@ -5,16 +5,16 @@ use std::sync::{Arc, Mutex, RwLock};
/// Simulate a command object.
struct Command {
/// Simulate an external state.
state: i64,
state: INT,
}
impl Command {
/// Do some action.
pub fn action(&mut self, val: i64) {
pub fn action(&mut self, val: INT) {
self.state = val;
}
/// Get current value.
pub fn get(&self) -> i64 {
pub fn get(&self) -> INT {
self.state
}
}
@@ -39,7 +39,7 @@ fn test_side_effects_command() -> Result<(), Box<EvalAltResult>> {
// Register type.
engine.register_type_with_name::<API>("CommandType");
engine.register_fn("action", |api: &mut API, x: i64| {
engine.register_fn("action", |api: &mut API, x: INT| {
let mut command = api.lock().unwrap();
let val = command.get();
command.action(val + x);

View File

@@ -1,5 +1,5 @@
#![cfg(not(feature = "unchecked"))]
use rhai::{Engine, EvalAltResult, ParseError, ParseErrorType};
use rhai::{Engine, EvalAltResult, ParseError, ParseErrorType, INT};
#[test]
#[cfg(not(feature = "no_function"))]
@@ -7,7 +7,7 @@ fn test_stack_overflow_fn_calls() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert_eq!(
engine.eval::<i64>(
engine.eval::<INT>(
r"
fn foo(n) { if n <= 1 { 0 } else { n + foo(n-1) } }
foo(8)

View File

@@ -1,6 +1,4 @@
use rhai::{
Engine, EvalAltResult, EvalContext, Expression, ParseError, ParseErrorType, Scope, INT,
};
use rhai::{Engine, EvalAltResult, EvalContext, Expression, ParseErrorType, Scope, INT};
#[test]
fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
@@ -19,43 +17,35 @@ fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
ParseErrorType::Reserved(err) if err == "while"
));
engine
.register_custom_syntax(
&[
"do", "|", "$ident$", "|", "->", "$block$", "while", "$expr$",
],
1,
|engine: &Engine,
context: &mut EvalContext,
scope: &mut Scope,
inputs: &[Expression]| {
let var_name = inputs[0].get_variable_name().unwrap().to_string();
let stmt = inputs.get(1).unwrap();
let expr = inputs.get(2).unwrap();
engine.register_custom_syntax(
&[
"do", "|", "$ident$", "|", "->", "$block$", "while", "$expr$",
],
1,
|engine: &Engine, context: &mut EvalContext, scope: &mut Scope, inputs: &[Expression]| {
let var_name = inputs[0].get_variable_name().unwrap().to_string();
let stmt = inputs.get(1).unwrap();
let expr = inputs.get(2).unwrap();
scope.push(var_name, 0 as INT);
scope.push(var_name, 0 as INT);
loop {
engine.eval_expression_tree(context, scope, stmt)?;
loop {
engine.eval_expression_tree(context, scope, stmt)?;
if !engine
.eval_expression_tree(context, scope, expr)?
.as_bool()
.map_err(|_| {
EvalAltResult::ErrorBooleanArgMismatch(
"do-while".into(),
expr.position(),
)
})?
{
break;
}
if !engine
.eval_expression_tree(context, scope, expr)?
.as_bool()
.map_err(|_| {
EvalAltResult::ErrorBooleanArgMismatch("do-while".into(), expr.position())
})?
{
break;
}
}
Ok(().into())
},
)
.unwrap();
Ok(().into())
},
)?;
// 'while' is now a custom keyword so this it can no longer be a variable
engine.consume("let while = 0").expect_err("should error");
@@ -71,10 +61,13 @@ fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
);
// The first symbol must be an identifier
assert!(matches!(
*engine.register_custom_syntax(&["!"], 0, |_, _, _, _| Ok(().into())).expect_err("should error"),
ParseError(err, _) if *err == ParseErrorType::BadInput("Improper symbol for custom syntax: '!'".to_string())
));
assert_eq!(
*engine
.register_custom_syntax(&["!"], 0, |_, _, _, _| Ok(().into()))
.expect_err("should error")
.0,
ParseErrorType::BadInput("Improper symbol for custom syntax: '!'".to_string())
);
Ok(())
}