Skip to content

Commit

Permalink
Merge pull request #875 from schungx/master
Browse files Browse the repository at this point in the history
Fix builds.
  • Loading branch information
schungx committed May 15, 2024
2 parents 521bedc + da1b662 commit e9cac35
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 31 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------
Expand Down
2 changes: 1 addition & 1 deletion benches/eval_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions benches/eval_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
";
Expand All @@ -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!";
}
"#;
Expand All @@ -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" + "";
}
"#;
Expand Down
4 changes: 0 additions & 4 deletions codegen/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
}
Expand Down
2 changes: 0 additions & 2 deletions codegen/src/rhai_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use crate::module::Module;
#[derive(Debug)]
pub struct ExportedConst {
pub name: String,
pub typ: Box<syn::Type>,
pub expr: syn::Expr,
pub cfg_attrs: Vec<syn::Attribute>,
}

Expand Down
8 changes: 0 additions & 8 deletions codegen/src/test/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<syn::Expr>(quote! { 42 }).unwrap()
);
}

#[test]
Expand Down Expand Up @@ -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::<syn::Expr>(quote! { 42 }).unwrap()
);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions no_std/no_std_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 0 additions & 2 deletions src/api/limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down
64 changes: 56 additions & 8 deletions src/func/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,12 +529,36 @@ pub fn locked_read<T>(value: &Locked<T>) -> Option<LockGuard<T>> {
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.
Expand All @@ -547,12 +571,36 @@ pub fn locked_write<T>(value: &Locked<T>) -> Option<LockGuardMut<T>> {
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.
Expand Down
4 changes: 4 additions & 0 deletions src/types/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit e9cac35

Please sign in to comment.