From 4f771d904ab5dc944abba237500c90bacd2208b4 Mon Sep 17 00:00:00 2001 From: Ilya Lakhin Date: Fri, 31 Jul 2020 12:08:14 +0700 Subject: [PATCH] Code cleanup --- src/fn_call.rs | 4 +- src/fn_native.rs | 2 +- src/fn_register.rs | 2 +- tests/closures.rs | 184 ++++++++++++++++++++++++--------------------- 4 files changed, 101 insertions(+), 91 deletions(-) diff --git a/src/fn_call.rs b/src/fn_call.rs index 6f82df42..dd56b88d 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -5,7 +5,7 @@ use crate::calc_fn_hash; use crate::engine::{ 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_TYPE_OF, KEYWORD_SHARED, KEYWORD_TAKE + KEYWORD_TYPE_OF, KEYWORD_SHARED, }; use crate::error::ParseErrorType; use crate::fn_native::{FnCallArgs, FnPtr}; @@ -31,7 +31,7 @@ use crate::parser::FLOAT; use crate::engine::{FN_IDX_GET, FN_IDX_SET}; #[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::{ any::{type_name, TypeId}, diff --git a/src/fn_native.rs b/src/fn_native.rs index 54f3da08..de926a47 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -1,6 +1,6 @@ //! Module defining interfaces to native-Rust functions. -use crate::any::{Dynamic, Variant}; +use crate::any::Dynamic; use crate::calc_fn_hash; use crate::engine::Engine; use crate::module::Module; diff --git a/src/fn_register.rs b/src/fn_register.rs index eef4e66b..e01f44a1 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -15,7 +15,7 @@ use crate::stdlib::{ any::TypeId, boxed::Box, mem, - string::{String, ToString}, + string::String, }; /// Trait to register custom functions with the `Engine`. diff --git a/tests/closures.rs b/tests/closures.rs index 6198bc26..4f8c16dd 100644 --- a/tests/closures.rs +++ b/tests/closures.rs @@ -1,6 +1,12 @@ #![cfg(not(feature = "no_function"))] -use rhai::{Dynamic, Engine, EvalAltResult, RegisterFn, FnPtr, Module, INT, Array}; -use std::any::{TypeId, Any}; +use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, Module, INT}; +use std::any::TypeId; + +#[cfg(not(feature = "no_shared"))] +use rhai::RegisterFn; + +#[cfg(not(feature = "no_index"))] +use rhai::Array; #[test] fn test_fn_ptr_curry_call() -> Result<(), Box> { @@ -35,7 +41,7 @@ fn test_fn_ptr_curry_call() -> Result<(), Box> { } #[test] -#[cfg(not(feature = "no_capture"))] +#[cfg(all(not(feature = "no_capture"), not(feature = "no_object")))] fn test_closures() -> Result<(), Box> { let engine = Engine::new(); @@ -110,9 +116,11 @@ fn test_shared() -> Result<(), Box> { 'x' ); - assert_eq!( - engine.eval::( - r#" + #[cfg(not(feature = "no_index"))] + { + assert_eq!( + engine.eval::( + r#" let s = shared("test"); let i = shared(0); i = 2; @@ -120,12 +128,10 @@ fn test_shared() -> Result<(), Box> { s "# - )?, - "teSt" - ); + )?, + "teSt" + ); - #[cfg(not(feature = "no_index"))] - { assert_eq!( engine.eval::( r#" @@ -137,6 +143,7 @@ fn test_shared() -> Result<(), Box> { 5 ); + #[cfg(not(feature = "no_object"))] assert_eq!( engine.eval::( r" @@ -170,6 +177,7 @@ fn test_shared() -> Result<(), Box> { true ); + #[cfg(not(feature = "no_object"))] assert_eq!( engine.eval::( r#" @@ -250,95 +258,97 @@ fn test_shared() -> Result<(), Box> { 42 ); - #[derive(Clone)] - struct TestStruct { - x: INT, - } - - impl TestStruct { - fn update(&mut self) { - self.x += 1000; + #[cfg(not(feature = "no_object"))] + { + #[derive(Clone)] + struct TestStruct { + x: INT, } - fn merge(&mut self, other: Self) { - self.x += other.x; + impl TestStruct { + 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 { - self.x - } + engine + .register_type::() + .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::(), + TypeId::of::(), + TypeId::of::(), + ], + move |engine: &Engine, lib: &Module, args: &mut [&mut Dynamic]| { + let fp = std::mem::take(args[2]).cast::(); + let mut value = args[1].clone(); + { + let mut lock = value.write_lock::().unwrap(); + *lock = *lock + 1; + } + let this_ptr = args.get_mut(0).unwrap(); - fn set_x(&mut self, new_x: INT) { - self.x = new_x; - } + fp.call_dynamic(engine, lib, Some(this_ptr), [value]) + }, + ); - fn new() -> Self { - TestStruct { x: 1 } - } - } + assert_eq!( + engine.eval::( + r" + let a = shared(new_ts()); - engine.register_type::(); + 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); - engine.register_fn("update", TestStruct::update); - engine.register_fn("merge", TestStruct::merge); - engine.register_fn("new_ts", TestStruct::new); - engine. - register_raw_fn( - "mutate_with_cb", - &[ - TypeId::of::(), - TypeId::of::(), - TypeId::of::(), - ], - move |engine: &Engine, lib: &Module, args: &mut [&mut Dynamic]| { - let fp = std::mem::take(args[2]).cast::(); - let mut value = args[1].clone(); - { - let mut lock = value.write_lock::().unwrap(); - *lock = *lock + 1; - } - let this_ptr = args.get_mut(0).unwrap(); - - fp.call_dynamic(engine, lib, Some(this_ptr), [value]) - }, + a.x + " + )?, + 2200 ); - assert_eq!( - engine.eval::( - r" - let a = shared(new_ts()); + assert_eq!( + engine.eval::( + r" + let a = shared(new_ts()); + let b = shared(100); - a.x = 100; - a.update(); - a.merge(a.take()); // take is important to prevent a deadlock + a.mutate_with_cb(b, |param| { + this.x = param; + param = 50; + this.update(); + }); - a.x - " - )?, - 2200 - ); + a.update(); + a.x += b; - assert_eq!( - engine.eval::( - r" - let a = shared(new_ts()); - 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 - ); + a.x + " + )?, + 2151 + ); + } Ok(()) }