diff --git a/CHANGELOG.md b/CHANGELOG.md index ac48a220a..0f328f411 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,9 @@ Bug fixes --------- * Variable resolver now correctly resolves variables that are captured in a closure. -* `NativeCallContext<'_>` (with a lifetime parameter) now parses correctly in the `#[export_module]` macro. This is to allow for `rust_2018_idioms` lints. +* `NativeCallContext<'_>` (with a lifetime parameter) now parses correctly in the `#[export_module]` + macro. This is to allow for `rust_2018_idioms` lints (thanks [`@ltabis`](https://github.com/ltabis) [864](https://github.com/rhaiscript/rhai/issues/864)). +* The `sync` feature now works properly in `no-std` builds (thanks [`@misssonder`](https://github.com/misssonder) [874](https://github.com/rhaiscript/rhai/pull/874)). New features ------------ diff --git a/benches/eval_array.rs b/benches/eval_array.rs index 7287f2224..397defc04 100644 --- a/benches/eval_array.rs +++ b/benches/eval_array.rs @@ -67,7 +67,7 @@ fn bench_eval_array_loop(bench: &mut Bencher) { let script = " let list = []; - for i in 0..10_000 { + for i in 0..1_888 { list.push(i); } diff --git a/benches/eval_expression.rs b/benches/eval_expression.rs index dfcdbb06e..35114effc 100644 --- a/benches/eval_expression.rs +++ b/benches/eval_expression.rs @@ -125,7 +125,7 @@ fn bench_eval_deeply_nested(bench: &mut Bencher) { fn bench_eval_loop_number(bench: &mut Bencher) { let script = " let s = 0; - for x in 0..10000 { + for x in 0..1_888 { s += 1; } "; @@ -142,7 +142,7 @@ fn bench_eval_loop_number(bench: &mut Bencher) { fn bench_eval_loop_strings_build(bench: &mut Bencher) { let script = r#" let s; - for x in 0..10000 { + for x in 0..1_888 { s = "hello, world!" + "hello, world!"; } "#; @@ -159,7 +159,7 @@ fn bench_eval_loop_strings_build(bench: &mut Bencher) { fn bench_eval_loop_strings_no_build(bench: &mut Bencher) { let script = r#" let s; - for x in 0..10000 { + for x in 0..1_888 { s = "hello" + ""; } "#; diff --git a/codegen/src/module.rs b/codegen/src/module.rs index 9aefba25b..92dda1e0f 100644 --- a/codegen/src/module.rs +++ b/codegen/src/module.rs @@ -139,17 +139,13 @@ impl Parse for Module { for item in &*content { if let syn::Item::Const(syn::ItemConst { vis: syn::Visibility::Public(..), - ref expr, ident, attrs, - ty, .. }) = item { consts.push(ExportedConst { name: ident.to_string(), - typ: ty.clone(), - expr: expr.as_ref().clone(), cfg_attrs: crate::attrs::collect_cfg_attr(attrs), }) } diff --git a/codegen/src/rhai_module.rs b/codegen/src/rhai_module.rs index b99084552..a34e399fa 100644 --- a/codegen/src/rhai_module.rs +++ b/codegen/src/rhai_module.rs @@ -13,8 +13,6 @@ use crate::module::Module; #[derive(Debug)] pub struct ExportedConst { pub name: String, - pub typ: Box, - pub expr: syn::Expr, pub cfg_attrs: Vec, } diff --git a/codegen/src/test/module.rs b/codegen/src/test/module.rs index 32c491227..39d2374c6 100644 --- a/codegen/src/test/module.rs +++ b/codegen/src/test/module.rs @@ -183,10 +183,6 @@ mod module_tests { assert!(item_mod.consts().is_empty()); assert_eq!(item_mod.sub_modules().len(), 1); assert_eq!(&item_mod.sub_modules()[0].consts()[0].name, "MYSTIC_NUMBER"); - assert_eq!( - item_mod.sub_modules()[0].consts()[0].expr, - syn::parse2::(quote! { 42 }).unwrap() - ); } #[test] @@ -244,10 +240,6 @@ mod module_tests { assert!(item_mod.fns().is_empty()); assert_eq!(item_mod.consts().len(), 1); assert_eq!(&item_mod.consts()[0].name, "MYSTIC_NUMBER"); - assert_eq!( - item_mod.consts()[0].expr, - syn::parse2::(quote! { 42 }).unwrap() - ); } #[test] diff --git a/no_std/no_std_test/Cargo.toml b/no_std/no_std_test/Cargo.toml index 14ae0f99b..65f3a2818 100644 --- a/no_std/no_std_test/Cargo.toml +++ b/no_std/no_std_test/Cargo.toml @@ -10,8 +10,8 @@ homepage = "https://github.com/rhaiscript/rhai/tree/no_std/no_std_test" repository = "https://github.com/rhaiscript/rhai" [dependencies] -rhai = { path = "../../", features = ["no_std"], default_features = false } -wee_alloc = { version = "0.4.5", default_features = false } +rhai = { path = "../../", features = ["no_std", "sync"], default-features = false } +wee_alloc = { version = "0.4.5", default-features = false } [profile.dev] panic = "abort" diff --git a/src/api/limits.rs b/src/api/limits.rs index ee0e4fad8..c561ba372 100644 --- a/src/api/limits.rs +++ b/src/api/limits.rs @@ -35,8 +35,6 @@ pub mod default_limits { /// Not available under `no_function`. #[cfg(not(feature = "no_function"))] pub const MAX_FUNCTION_EXPR_DEPTH: usize = 32; - /// Maximum number of strings interned. - pub const MAX_STRINGS_INTERNED: usize = 1024; } /// A type containing all the limits imposed by the [`Engine`]. diff --git a/src/func/native.rs b/src/func/native.rs index 446f71d26..5c52e2bfb 100644 --- a/src/func/native.rs +++ b/src/func/native.rs @@ -529,12 +529,36 @@ pub fn locked_read(value: &Locked) -> Option> { return value.try_borrow().ok(); #[cfg(feature = "sync")] - #[cfg(feature = "unchecked")] - return value.read().ok(); + #[cfg(not(feature = "no_std"))] + { + #[cfg(feature = "unchecked")] + return value.read().ok(); + + #[cfg(not(feature = "unchecked"))] + { + // Spin-lock for a short while before giving up + for _ in 0..5 { + match value.try_read() { + Ok(guard) => return Some(guard), + Err(std::sync::TryLockError::WouldBlock) => { + std::thread::sleep(std::time::Duration::from_millis(10)) + } + Err(_) => return None, + } + } + + return None; + } + } #[cfg(feature = "sync")] - #[cfg(not(feature = "unchecked"))] - return value.try_read(); + #[cfg(feature = "no_std")] + { + #[cfg(feature = "unchecked")] + return Some(value.read()); + #[cfg(not(feature = "unchecked"))] + return value.try_read(); + } } /// _(internals)_ Lock a [`Locked`] resource for mutable access. @@ -547,12 +571,36 @@ pub fn locked_write(value: &Locked) -> Option> { return value.try_borrow_mut().ok(); #[cfg(feature = "sync")] - #[cfg(feature = "unchecked")] - return value.write().ok(); + #[cfg(not(feature = "no_std"))] + { + #[cfg(feature = "unchecked")] + return value.write().ok(); + + #[cfg(not(feature = "unchecked"))] + { + // Spin-lock for a short while before giving up + for _ in 0..5 { + match value.try_write() { + Ok(guard) => return Some(guard), + Err(std::sync::TryLockError::WouldBlock) => { + std::thread::sleep(std::time::Duration::from_millis(10)) + } + Err(_) => return None, + } + } + + return None; + } + } #[cfg(feature = "sync")] - #[cfg(not(feature = "unchecked"))] - return value.try_write(); + #[cfg(feature = "no_std")] + { + #[cfg(feature = "unchecked")] + return Some(value.write()); + #[cfg(not(feature = "unchecked"))] + return value.try_write(); + } } /// General Rust function trail object. diff --git a/src/types/dynamic.rs b/src/types/dynamic.rs index b54410adb..a6fe6e3db 100644 --- a/src/types/dynamic.rs +++ b/src/types/dynamic.rs @@ -1701,6 +1701,10 @@ impl Dynamic { #[cfg(not(feature = "sync"))] Ok(value) => value.into_inner().flatten(), #[cfg(feature = "sync")] + #[cfg(not(feature = "no_std"))] + Ok(value) => value.into_inner().unwrap().flatten(), + #[cfg(feature = "sync")] + #[cfg(feature = "no_std")] Ok(value) => value.into_inner().flatten(), // If there are outstanding references, return a cloned copy Err(cell) => {