Code cleanup

This commit is contained in:
Ilya Lakhin 2020-07-31 12:08:14 +07:00
parent ca64668e58
commit 4f771d904a
4 changed files with 101 additions and 91 deletions

View File

@ -5,7 +5,7 @@ use crate::calc_fn_hash;
use crate::engine::{ use crate::engine::{
search_imports, search_namespace, search_scope_only, Engine, Imports, State, KEYWORD_DEBUG, search_imports, search_namespace, search_scope_only, Engine, Imports, State, KEYWORD_DEBUG,
KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_FN_PTR_CALL, KEYWORD_FN_PTR_CURRY, KEYWORD_PRINT, KEYWORD_EVAL, KEYWORD_FN_PTR, KEYWORD_FN_PTR_CALL, KEYWORD_FN_PTR_CURRY, KEYWORD_PRINT,
KEYWORD_TYPE_OF, KEYWORD_SHARED, KEYWORD_TAKE KEYWORD_TYPE_OF, KEYWORD_SHARED,
}; };
use crate::error::ParseErrorType; use crate::error::ParseErrorType;
use crate::fn_native::{FnCallArgs, FnPtr}; use crate::fn_native::{FnCallArgs, FnPtr};
@ -31,7 +31,7 @@ use crate::parser::FLOAT;
use crate::engine::{FN_IDX_GET, FN_IDX_SET}; use crate::engine::{FN_IDX_GET, FN_IDX_SET};
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
use crate::engine::{Map, Target, FN_GET, FN_SET}; use crate::engine::{Map, Target, FN_GET, FN_SET, KEYWORD_TAKE};
use crate::stdlib::{ use crate::stdlib::{
any::{type_name, TypeId}, any::{type_name, TypeId},

View File

@ -1,6 +1,6 @@
//! Module defining interfaces to native-Rust functions. //! Module defining interfaces to native-Rust functions.
use crate::any::{Dynamic, Variant}; use crate::any::Dynamic;
use crate::calc_fn_hash; use crate::calc_fn_hash;
use crate::engine::Engine; use crate::engine::Engine;
use crate::module::Module; use crate::module::Module;

View File

@ -15,7 +15,7 @@ use crate::stdlib::{
any::TypeId, any::TypeId,
boxed::Box, boxed::Box,
mem, mem,
string::{String, ToString}, string::String,
}; };
/// Trait to register custom functions with the `Engine`. /// Trait to register custom functions with the `Engine`.

View File

@ -1,6 +1,12 @@
#![cfg(not(feature = "no_function"))] #![cfg(not(feature = "no_function"))]
use rhai::{Dynamic, Engine, EvalAltResult, RegisterFn, FnPtr, Module, INT, Array}; use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, Module, INT};
use std::any::{TypeId, Any}; use std::any::TypeId;
#[cfg(not(feature = "no_shared"))]
use rhai::RegisterFn;
#[cfg(not(feature = "no_index"))]
use rhai::Array;
#[test] #[test]
fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> { fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> {
@ -35,7 +41,7 @@ fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> {
} }
#[test] #[test]
#[cfg(not(feature = "no_capture"))] #[cfg(all(not(feature = "no_capture"), not(feature = "no_object")))]
fn test_closures() -> Result<(), Box<EvalAltResult>> { fn test_closures() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new(); let engine = Engine::new();
@ -110,9 +116,11 @@ fn test_shared() -> Result<(), Box<EvalAltResult>> {
'x' 'x'
); );
assert_eq!( #[cfg(not(feature = "no_index"))]
engine.eval::<String>( {
r#" assert_eq!(
engine.eval::<String>(
r#"
let s = shared("test"); let s = shared("test");
let i = shared(0); let i = shared(0);
i = 2; i = 2;
@ -120,12 +128,10 @@ fn test_shared() -> Result<(), Box<EvalAltResult>> {
s s
"# "#
)?, )?,
"teSt" "teSt"
); );
#[cfg(not(feature = "no_index"))]
{
assert_eq!( assert_eq!(
engine.eval::<Array>( engine.eval::<Array>(
r#" r#"
@ -137,6 +143,7 @@ fn test_shared() -> Result<(), Box<EvalAltResult>> {
5 5
); );
#[cfg(not(feature = "no_object"))]
assert_eq!( assert_eq!(
engine.eval::<INT>( engine.eval::<INT>(
r" r"
@ -170,6 +177,7 @@ fn test_shared() -> Result<(), Box<EvalAltResult>> {
true true
); );
#[cfg(not(feature = "no_object"))]
assert_eq!( assert_eq!(
engine.eval::<INT>( engine.eval::<INT>(
r#" r#"
@ -250,95 +258,97 @@ fn test_shared() -> Result<(), Box<EvalAltResult>> {
42 42
); );
#[derive(Clone)] #[cfg(not(feature = "no_object"))]
struct TestStruct { {
x: INT, #[derive(Clone)]
} struct TestStruct {
x: INT,
impl TestStruct {
fn update(&mut self) {
self.x += 1000;
} }
fn merge(&mut self, other: Self) { impl TestStruct {
self.x += other.x; fn update(&mut self) {
self.x += 1000;
}
fn merge(&mut self, other: Self) {
self.x += other.x;
}
fn get_x(&mut self) -> INT {
self.x
}
fn set_x(&mut self, new_x: INT) {
self.x = new_x;
}
fn new() -> Self {
TestStruct { x: 1 }
}
} }
fn get_x(&mut self) -> INT { engine
self.x .register_type::<TestStruct>()
} .register_get_set("x", TestStruct::get_x, TestStruct::set_x)
.register_fn("update", TestStruct::update)
.register_fn("merge", TestStruct::merge)
.register_fn("new_ts", TestStruct::new)
.register_raw_fn(
"mutate_with_cb",
&[
TypeId::of::<TestStruct>(),
TypeId::of::<INT>(),
TypeId::of::<FnPtr>(),
],
move |engine: &Engine, lib: &Module, args: &mut [&mut Dynamic]| {
let fp = std::mem::take(args[2]).cast::<FnPtr>();
let mut value = args[1].clone();
{
let mut lock = value.write_lock::<INT>().unwrap();
*lock = *lock + 1;
}
let this_ptr = args.get_mut(0).unwrap();
fn set_x(&mut self, new_x: INT) { fp.call_dynamic(engine, lib, Some(this_ptr), [value])
self.x = new_x; },
} );
fn new() -> Self { assert_eq!(
TestStruct { x: 1 } engine.eval::<INT>(
} r"
} let a = shared(new_ts());
engine.register_type::<TestStruct>(); a.x = 100;
a.update();
a.merge(a.take()); // take is important to prevent a deadlock
engine.register_get_set("x", TestStruct::get_x, TestStruct::set_x); a.x
engine.register_fn("update", TestStruct::update); "
engine.register_fn("merge", TestStruct::merge); )?,
engine.register_fn("new_ts", TestStruct::new); 2200
engine.
register_raw_fn(
"mutate_with_cb",
&[
TypeId::of::<TestStruct>(),
TypeId::of::<INT>(),
TypeId::of::<FnPtr>(),
],
move |engine: &Engine, lib: &Module, args: &mut [&mut Dynamic]| {
let fp = std::mem::take(args[2]).cast::<FnPtr>();
let mut value = args[1].clone();
{
let mut lock = value.write_lock::<INT>().unwrap();
*lock = *lock + 1;
}
let this_ptr = args.get_mut(0).unwrap();
fp.call_dynamic(engine, lib, Some(this_ptr), [value])
},
); );
assert_eq!( assert_eq!(
engine.eval::<INT>( engine.eval::<INT>(
r" r"
let a = shared(new_ts()); let a = shared(new_ts());
let b = shared(100);
a.x = 100; a.mutate_with_cb(b, |param| {
a.update(); this.x = param;
a.merge(a.take()); // take is important to prevent a deadlock param = 50;
this.update();
});
a.x a.update();
" a.x += b;
)?,
2200
);
assert_eq!( a.x
engine.eval::<INT>( "
r" )?,
let a = shared(new_ts()); 2151
let b = shared(100); );
}
a.mutate_with_cb(b, |param| {
this.x = param;
param = 50;
this.update();
});
a.update();
a.x += b;
a.x
"
)?,
2151
);
Ok(()) Ok(())
} }