From 0bfb94a0e0f618a2d8032d1b275e65fdd65ebe8a Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 21 Mar 2024 12:26:09 +0800 Subject: [PATCH 1/6] Remove print line. --- src/bin/rhai-repl.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/bin/rhai-repl.rs b/src/bin/rhai-repl.rs index 9c8e6c911..65b826b7b 100644 --- a/src/bin/rhai-repl.rs +++ b/src/bin/rhai-repl.rs @@ -563,7 +563,6 @@ fn main() { .compile_with_scope(&scope, &input) .map_err(Into::into) .and_then(|r| { - println!("AST: {r:#?}"); #[cfg(not(feature = "no_optimize"))] { ast_u = r.clone(); From 875bf25acbb8d7739736100733c389e76a49a1ed Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 23 Mar 2024 17:30:11 +0800 Subject: [PATCH 2/6] Fix 32-bit --- src/tests.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/tests.rs b/src/tests.rs index 4de884ef9..c0b0f9110 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -15,7 +15,16 @@ fn check_struct_sizes() { )); const WORD_SIZE: usize = size_of::(); - assert_eq!(size_of::(), if PACKED { 8 } else { 16 }); + assert_eq!( + size_of::(), + if PACKED { + 8 + } else if IS_32_BIT { + 12 + } else { + 16 + } + ); assert_eq!(size_of::>(), if PACKED { 8 } else { 16 }); assert_eq!( size_of::(), From d920568dbf9935c3d8c779f362c0e2cbd319457f Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 24 Mar 2024 12:01:28 +0800 Subject: [PATCH 3/6] Fix crash. --- CHANGELOG.md | 1 + src/eval/chaining.rs | 24 +++++++----------------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b34604863..4e6ba2488 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Bug fixes * The position of an undefined operation call now points to the operator instead of the first operand. * The `optimize` command in `rhai-repl` now works properly and cycles through `None`->`Simple`->`Full`. * `Engine::call_fn_XXX` no longer return errors unnecessarily wrapped in `EvalAltResult::ErrorInFunctionCall`. +* Some tests that panic on 32-bit architecture are fixed. Deprecated API's ---------------- diff --git a/src/eval/chaining.rs b/src/eval/chaining.rs index 40da1156a..1b704d830 100644 --- a/src/eval/chaining.rs +++ b/src/eval/chaining.rs @@ -482,29 +482,19 @@ impl Engine { } // Short-circuit for indexing with literal: {expr}[1] #[cfg(not(feature = "no_index"))] - (_, ChainType::Indexing) if rhs.is_constant() => { - idx_values.push(rhs.get_literal_value().unwrap()) - } - #[cfg(not(feature = "no_index"))] - (Expr::FnCall(fnc, _), ChainType::Indexing) - if fnc.op_token == Some(crate::tokenizer::Token::InclusiveRange) - || fnc.op_token == Some(crate::tokenizer::Token::ExclusiveRange) => - { + (_, ChainType::Indexing) if rhs.get_literal_value().is_some() => { idx_values.push(rhs.get_literal_value().unwrap()) } // Short-circuit for simple method call: {expr}.func() #[cfg(not(feature = "no_object"))] (Expr::MethodCall(x, ..), ChainType::Dotting) if x.args.is_empty() => (), // All other patterns - evaluate the arguments chain - _ => self.eval_dot_index_chain_arguments( - global, - caches, - scope, - this_ptr.as_deref_mut(), - expr, - rhs, - idx_values, - )?, + _ => { + let this_ptr = this_ptr.as_deref_mut(); + self.eval_dot_index_chain_arguments( + global, caches, scope, this_ptr, expr, rhs, idx_values, + )? + } } match (lhs, new_val) { From e42d75e63aa41b51aaf697f988784e3fc44c502c Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 24 Mar 2024 12:08:57 +0800 Subject: [PATCH 4/6] Fix struct size tests on 32-bit. --- src/tests.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index c0b0f9110..fc4778ea6 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -25,12 +25,32 @@ fn check_struct_sizes() { 16 } ); - assert_eq!(size_of::>(), if PACKED { 8 } else { 16 }); + assert_eq!( + size_of::>(), + if PACKED { + 8 + } else if IS_32_BIT { + 12 + } else { + 16 + } + ); assert_eq!( size_of::(), if cfg!(feature = "no_position") { 0 } else { 4 } ); - assert_eq!(size_of::(), 2 * WORD_SIZE); + assert_eq!( + size_of::(), + if IS_32_BIT { + if cfg!(feature = "only_i32") { + 2 * WORD_SIZE + } else { + 3 * WORD_SIZE + } + } else { + 2 * WORD_SIZE + } + ); assert_eq!(size_of::(), if PACKED { 12 } else { 16 }); assert_eq!(size_of::>(), if PACKED { 12 } else { 16 }); assert_eq!(size_of::(), if IS_32_BIT { 12 } else { 16 }); From 263296519f74ed6271d778164c94b5e605875a00 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 24 Mar 2024 12:25:01 +0800 Subject: [PATCH 5/6] Update test. --- codegen/ui_tests/rhai_mod_inner_cfg_false.stderr | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/codegen/ui_tests/rhai_mod_inner_cfg_false.stderr b/codegen/ui_tests/rhai_mod_inner_cfg_false.stderr index b0feeb47b..2c6d5659e 100644 --- a/codegen/ui_tests/rhai_mod_inner_cfg_false.stderr +++ b/codegen/ui_tests/rhai_mod_inner_cfg_false.stderr @@ -3,3 +3,10 @@ error[E0433]: failed to resolve: could not find `test_mod` in `test_module` | 24 | if test_module::test_mod::test_fn(n) { | ^^^^^^^^ could not find `test_mod` in `test_module` + | +note: found an item that was configured out + --> ui_tests/rhai_mod_inner_cfg_false.rs:12:13 + | +12 | pub mod test_mod { + | ^^^^^^^^ + = note: the item is gated behind the `unset_feature` feature From ef2a63d0d3a0f50339ce642a5f58bba43b31a7a0 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 25 Mar 2024 20:27:01 +0800 Subject: [PATCH 6/6] Replace update_fn_comments with potentially more useful get_fn_metadata_mut. --- CHANGELOG.md | 1 + src/api/build_type.rs | 47 +++++++++++++++++++++++++++---------------- src/module/mod.rs | 20 +++++++----------- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e6ba2488..bc670c710 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ New features * New options `Engine::set_max_strings_interned` and `Engine::max_strings_interned` are added to limit the maximum number of strings interned in the `Engine`'s string interner. * A new advanced callback, `Engine::on_invalid_array_index`, is added (gated under the `internals` feature) to handle access to missing properties in object maps. * A new advanced callback, `Engine::on_missing_map_property`, is added (gated under the `internals` feature) to handle out-of-bound index into arrays. +* Doc-comments are now automatically added to function registrations and custom types via the `CustomType` derive macro. Enhancements ------------ diff --git a/src/api/build_type.rs b/src/api/build_type.rs index ecb5a808b..ef4664c42 100644 --- a/src/api/build_type.rs +++ b/src/api/build_type.rs @@ -2,8 +2,8 @@ use crate::func::SendSync; use crate::module::FuncMetadata; use crate::packages::string_basic::{FUNC_TO_DEBUG, FUNC_TO_STRING}; -use crate::FuncRegistration; -use crate::{types::dynamic::Variant, Engine, Identifier, RhaiNativeFunc}; +use crate::types::dynamic::Variant; +use crate::{Engine, FuncRegistration, Identifier, RhaiNativeFunc, StaticVec}; use std::marker::PhantomData; #[cfg(feature = "no_std")] @@ -104,7 +104,7 @@ impl Engine { pub struct TypeBuilder<'a, T: Variant + Clone> { engine: &'a mut Engine, /// Keep the latest registered function(s) in cache to add additional metadata. - hashes: Vec, + hashes: StaticVec, _marker: PhantomData, } @@ -114,7 +114,7 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> { fn new(engine: &'a mut Engine) -> Self { Self { engine, - hashes: vec![], + hashes: StaticVec::new_const(), _marker: PhantomData, } } @@ -155,7 +155,8 @@ impl TypeBuilder<'_, T> { ) -> &mut Self { let FuncMetadata { hash, .. } = FuncRegistration::new(FUNC_TO_STRING).register_into_engine(self.engine, on_print); - self.hashes = vec![*hash]; + self.hashes.clear(); + self.hashes.push(*hash); self } @@ -167,7 +168,8 @@ impl TypeBuilder<'_, T> { ) -> &mut Self { let FuncMetadata { hash, .. } = FuncRegistration::new(FUNC_TO_DEBUG).register_into_engine(self.engine, on_debug); - self.hashes = vec![*hash]; + self.hashes.clear(); + self.hashes.push(*hash); self } @@ -180,19 +182,22 @@ impl TypeBuilder<'_, T> { ) -> &mut Self { let FuncMetadata { hash, .. } = FuncRegistration::new(name).register_into_engine(self.engine, method); - self.hashes = vec![*hash]; + self.hashes.clear(); + self.hashes.push(*hash); self } - /// Add comments to the last registered function. - /// Available under the metadata feature only. + /// _(metadata)_ Add comments to the last registered function. + /// Available under the `metadata` feature only. #[cfg(feature = "metadata")] #[inline(always)] pub fn and_comments(&mut self, comments: &[&str]) -> &mut Self { let module = self.engine.global_namespace_mut(); - for hash in &self.hashes { - module.update_fn_comments(*hash, comments); + for &hash in &self.hashes { + if let Some(f) = module.get_fn_metadata_mut(hash) { + f.comments = comments.into_iter().map(|&s| s.into()).collect(); + } } self @@ -228,7 +233,8 @@ impl TypeBuilder<'_, T> { ) -> &mut Self { let FuncMetadata { hash, .. } = FuncRegistration::new_getter(name).register_into_engine(self.engine, get_fn); - self.hashes = vec![*hash]; + self.hashes.clear(); + self.hashes.push(*hash); self } @@ -244,7 +250,8 @@ impl TypeBuilder<'_, T> { ) -> &mut Self { let FuncMetadata { hash, .. } = FuncRegistration::new_setter(name).register_into_engine(self.engine, set_fn); - self.hashes = vec![*hash]; + self.hashes.clear(); + self.hashes.push(*hash); self } @@ -273,7 +280,9 @@ impl TypeBuilder<'_, T> { let hash_2 = FuncRegistration::new_setter(&name) .register_into_engine(self.engine, set_fn) .hash; - self.hashes = vec![hash_1, hash_2]; + self.hashes.clear(); + self.hashes.push(hash_1); + self.hashes.push(hash_2); self } @@ -298,7 +307,8 @@ impl TypeBuilder<'_, T> { ) -> &mut Self { let FuncMetadata { hash, .. } = FuncRegistration::new_index_getter().register_into_engine(self.engine, get_fn); - self.hashes = vec![*hash]; + self.hashes.clear(); + self.hashes.push(*hash); self } @@ -318,7 +328,8 @@ impl TypeBuilder<'_, T> { ) -> &mut Self { let FuncMetadata { hash, .. } = FuncRegistration::new_index_setter().register_into_engine(self.engine, set_fn); - self.hashes = vec![*hash]; + self.hashes.clear(); + self.hashes.push(*hash); self } @@ -345,7 +356,9 @@ impl TypeBuilder<'_, T> { let hash_2 = FuncRegistration::new_index_setter() .register_into_engine(self.engine, set_fn) .hash; - self.hashes = vec![hash_1, hash_2]; + self.hashes.clear(); + self.hashes.push(hash_1); + self.hashes.push(hash_2); self } diff --git a/src/module/mod.rs b/src/module/mod.rs index 44c8c42bd..4c8ba66ce 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -1483,20 +1483,14 @@ impl Module { self } - /// _(metadata)_ Update the comments of a registered function. - /// Exported under the `metadata` feature only. - #[cfg(feature = "metadata")] + /// Get a registered function's metadata. #[inline] - pub(crate) fn update_fn_comments>( - &mut self, - hash_fn: u64, - comments: impl IntoIterator, - ) -> &mut Self { - if let Some((_, f)) = self.functions.as_mut().and_then(|m| m.get_mut(&hash_fn)) { - f.comments = comments.into_iter().map(|s| s.as_ref().into()).collect(); - } - - self + #[allow(dead_code)] + pub(crate) fn get_fn_metadata_mut(&mut self, hash_fn: u64) -> Option<&mut FuncMetadata> { + self.functions + .as_mut() + .and_then(|m| m.get_mut(&hash_fn)) + .map(|(_, f)| f.as_mut()) } /// Remap type ID.