diff --git a/CHANGELOG.md b/CHANGELOG.md index a49d97d43..dde2b4149 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ Bug fixes * Native functions that call back into the engine (e.g. `==` or `to_string` on arrays) now count towards `max_call_levels`, so comparing or printing a deeply-nested array returns an error instead of overflowing the stack (thanks [`@Rockyrok77`](https://github.com/Rockyrok77) [`#1208`](https://github.com/rhaiscript/rhai/pull/1208)). * `AST::clone_functions_only_filtered` now applies its filter, as does `AST::merge_filtered` when the first `AST` has no functions. Previously every function was kept (thanks [`@ImTheSquid`](https://github.com/ImTheSquid) [`#1192`](https://github.com/rhaiscript/rhai/pull/1192)). * Rhai Grain no longer merges distinct constants of a custom type (such as two custom syntax states) in the constant pool because they print the same (thanks [`@luispedro`](https://github.com/luispedro) [`#1196`](https://github.com/rhaiscript/rhai/issues/1196)). -* A function called inside a module now always resolves within that module. Previously, when two modules defined a function with the same name and parameters, a bare call could silently run the other module's function, because the call into the module shared the caller's function resolution cache. +* A function called inside a module now resolves correctly instead of incorrectly using a cached resolution (thanks [`@yuvalrakavy`](https://github.com/yuvalrakavy) [`#1211`](https://github.com/rhaiscript/rhai/pull/1211)). * Rhai and Rhai Grain now properly enforce object map size limits in all mutation scenarios ([`#1214`](https://github.com/rhaiscript/rhai/issues/1214)). * (Fuzzing) Fixed panic when assigning to a property of a constant object. * Fixed undefined behavior (an aliasing violation detected by Miri) when a variable is passed to a function that does not take its first argument by `&mut`, e.g. `x.contains("a")` or `f(x)` for a script function. This was a regression in 1.27.0. @@ -31,6 +31,8 @@ Enhancements * `Engine::set_max_call_levels` and `Engine::max_call_levels` are now available under `no_function`, since native functions calling back into the engine are also bound by this limit (thanks [`@Rockyrok77`](https://github.com/Rockyrok77) [`#1208`](https://github.com/rhaiscript/rhai/pull/1208)). * Large object map literals now run faster by storing constant property values in their templates, only computed properties are evaluated at runtime ([`#1207`](https://github.com/rhaiscript/rhai/pull/1207)). * Rhai Grain now compiles scripts with many constants, such as large object map literals, in linear instead of quadratic time, by indexing the constant pool by hash (thanks [`@luispedro`](https://github.com/luispedro) [`#1196`](https://github.com/rhaiscript/rhai/issues/1196)). +* Rhai Grain `Program::iter_functions` now exposes metadata for compiled functions, similar to `AST::iter_functions` ([`#1212`](https://github.com/rhaiscript/rhai/pull/1212)). +* Rhai Grain `Program::has_functions` checks whether a program contains compiled functions ([`#1212`](https://github.com/rhaiscript/rhai/pull/1212)). Version 1.27.0 diff --git a/src/func/script.rs b/src/func/script.rs index 3b31491c7..096ef4a99 100644 --- a/src/func/script.rs +++ b/src/func/script.rs @@ -144,6 +144,8 @@ impl fmt::Display for ScriptFuncDef { /// Not available under `no_function`. /// /// Created by [`AST::iter_functions`][crate::AST::iter_functions]. +/// +/// With the `grain` feature, also created by [`Program::iter_functions`][crate::grain::Program::iter_functions] for compiled functions. #[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Clone, Hash)] #[cfg_attr( feature = "serde", diff --git a/src/grain/bytecode/verify.rs b/src/grain/bytecode/verify.rs index 277adbee0..c1b617d97 100644 --- a/src/grain/bytecode/verify.rs +++ b/src/grain/bytecode/verify.rs @@ -283,6 +283,21 @@ pub fn verify( }); } + for function in functions { + for index in std::iter::once(function.name) + .chain(function.params.iter().copied()) + .chain(function.this_type) + { + if pools.names.get(index).is_none() { + return Err(VerifyError::BadIndex { + at: function.chunk.entry() as usize, + what: "name", + index, + }); + } + } + } + chunks .iter() .map(|chunk| verify_chunk(caps, code, chunk, &starts, pools)) diff --git a/src/grain/program.rs b/src/grain/program.rs index 76e426dab..3cb7192e9 100644 --- a/src/grain/program.rs +++ b/src/grain/program.rs @@ -445,6 +445,52 @@ impl<'a> Program<'a> { &self.functions } + /// Iterate through all compiled script function definitions. + /// + /// Not available under `no_function`. + /// + /// Like [`AST::iter_functions`][crate::AST::iter_functions], this includes + /// private functions, typed methods and anonymous functions. + /// + /// Fallback functions retained only in AST form are _excluded_. + /// + /// Iteration order is unspecified. + /// + /// Names, parameters and receiver types borrow from this [`Program`]. + /// + /// Doc-comments are not retained, so `comments` (under `metadata`) is always empty. + #[cfg(not(feature = "no_function"))] + #[inline] + pub fn iter_functions(&self) -> impl Iterator> { + self.functions + .iter() + .map(|function| crate::ScriptFnMetadata { + name: self.name(function.name).unwrap(), + params: function + .params + .iter() + .map(|&p| self.name(p).unwrap()) + .collect(), + access: function.access, + #[cfg(not(feature = "no_object"))] + this_type: function.this_type.map(|t| self.name(t).unwrap()), + #[cfg(feature = "metadata")] + comments: Vec::new(), + }) + } + + /// Does this [`Program`] contain compiled script functions? + /// + /// Not available under `no_function`. + /// + /// Fallback functions retained only in AST form are _excluded_. + #[cfg(not(feature = "no_function"))] + #[inline(always)] + #[must_use] + pub fn has_functions(&self) -> bool { + !self.functions.is_empty() + } + /// Number of compiled script functions in this[`Program`]. #[inline(always)] #[must_use] @@ -864,6 +910,68 @@ mod tests { ) } + #[cfg(not(feature = "no_function"))] + #[test] + fn discovery_of_compiled_functions() { + let empty = program_of(&[]); + assert!(!empty.has_functions()); + assert_eq!(empty.iter_functions().count(), 0); + + let program = program_of(&[(0, None, 2)]); + assert!(program.has_functions()); + assert_eq!(program.iter_functions().count(), program.num_functions()); + let function = program.iter_functions().next().unwrap(); + assert_eq!(function.name, "f"); + assert_eq!(function.params, ["f", "f"]); + assert_eq!(function.access, FnAccess::Public); + #[cfg(not(feature = "no_object"))] + assert_eq!(function.this_type, None); + #[cfg(feature = "metadata")] + assert!(function.comments.is_empty()); + + let bytes = program.write().unwrap(); + let loaded = Program::read(&bytes).unwrap(); + assert_eq!(loaded.iter_functions().next().unwrap(), function); + let owned = loaded.into_owned(); + assert_eq!(owned.iter_functions().next().unwrap(), function); + } + + #[cfg(not(any(feature = "no_function", feature = "no_object")))] + #[test] + fn discovery_of_typed_functions() { + let program = program_of(&[(0, Some(1), 0), (0, Some(2), 0)]); + let functions: Vec<_> = program.iter_functions().collect(); + assert_eq!(functions.len(), 2); + assert_eq!(functions[0].this_type, Some("i64")); + assert_eq!(functions[1].this_type, Some("string")); + } + + #[cfg(not(feature = "no_function"))] + #[test] + fn discovery_rejects_invalid_function_metadata_on_load() { + use crate::grain::bytecode::VerifyError; + use crate::grain::ReadError; + + for field in 0..3 { + let mut program = program_of(&[(0, None, 1)]); + let function = &mut program.functions[0]; + match field { + 0 => function.name = 3, + 1 => function.params[0] = 3, + _ => function.this_type = Some(3), + } + let bytes = program.write().unwrap(); + assert!(matches!( + Program::read(&bytes), + Err(ReadError::Unverifiable(VerifyError::BadIndex { + what: "name", + index: 3, + .. + })) + )); + } + } + /// Rhai tries the receiver's type first and falls back to the untyped /// function of the same name and arity. #[test] diff --git a/tests/grain/functions.rs b/tests/grain/functions.rs new file mode 100644 index 000000000..042d85d58 --- /dev/null +++ b/tests/grain/functions.rs @@ -0,0 +1,154 @@ +#[cfg(not(feature = "no_ast"))] +mod ast { + use rhai::grain::{Compiler, Program, WriteError}; + use rhai::{Engine, FnAccess, ScriptFnMetadata, AST}; + + fn metadata(ast: &AST) -> Vec> { + let mut functions: Vec<_> = ast.iter_functions().collect(); + #[cfg(feature = "metadata")] + for function in &mut functions { + function.comments.clear(); + } + functions.sort(); + functions + } + + fn assert_discovery(program: &Program, expected: &[ScriptFnMetadata<'_>]) { + let mut actual: Vec<_> = program.iter_functions().collect(); + actual.sort(); + assert_eq!(actual, expected); + assert_eq!(actual.len(), program.num_functions()); + assert_eq!(program.has_functions(), !actual.is_empty()); + } + + #[test] + fn compiled_metadata_matches_ast() { + let ast = Engine::new() + .compile("fn foo() { 1 } fn foo(first, second) { first + second } private fn hidden(value) { value }") + .unwrap(); + let expected = metadata(&ast); + assert_eq!(expected.len(), 3); + assert!(expected.iter().any(|f| f.access == FnAccess::Private)); + assert!(expected.iter().any(|f| f.params == ["first", "second"])); + let program = Compiler::new().compile(&ast); + assert_discovery(&program, &expected); + + let bytes = program.write().unwrap(); + let loaded = Program::read(&bytes).unwrap(); + assert_discovery(&loaded, &expected); + let owned = loaded.into_owned(); + assert_discovery(&owned, &expected); + let shared = owned.into_shared(); + assert_discovery(&shared, &expected); + + let stripped = program.write_stripped().unwrap(); + let loaded = Program::read(&stripped.artifact).unwrap(); + assert_discovery(&loaded, &expected); + } + + #[test] + fn empty_and_fallback_only_programs_have_no_compiled_functions() { + let engine = Engine::new(); + let empty = Compiler::new().compile(&engine.compile("42").unwrap()); + assert_discovery(&empty, &[]); + let ast = engine.compile(r#"fn fallback(value) { eval("1"); value }"#).unwrap(); + assert!(ast.has_functions()); + let program = Compiler::new().compile(&ast); + assert!(matches!(program.write(), Err(WriteError::HasScriptFunctions))); + assert_discovery(&program, &[]); + } + + #[test] + fn retained_ast_library_is_excluded() { + let ast = Engine::new() + .compile(r#"fn fallback(value) { eval("1"); value } fn compiled(first, second) { first + second }"#) + .unwrap(); + let expected: Vec<_> = metadata(&ast).into_iter().filter(|f| f.name == "compiled").collect(); + assert_eq!(expected.len(), 1); + let program = Compiler::new().compile(&ast); + assert!(matches!(program.write(), Err(WriteError::HasScriptFunctions))); + assert_discovery(&program, &expected); + } + + #[cfg(not(feature = "no_object"))] + #[test] + fn typed_overloads_remain_distinct() { + let ast = Engine::new() + .compile("fn foo(value) { value } fn int.foo(value) { this + value } private fn string.foo(value) { value }") + .unwrap(); + let expected = metadata(&ast); + assert_eq!(expected.len(), 3); + assert_eq!(expected.iter().filter(|f| f.this_type.is_some()).count(), 2); + let program = Compiler::new().compile(&ast); + assert_discovery(&program, &expected); + let bytes = program.write().unwrap(); + assert_discovery(&Program::read(&bytes).unwrap(), &expected); + } + + #[cfg(not(feature = "no_closure"))] + #[test] + fn anonymous_functions_are_discoverable() { + let ast = Engine::new().compile("let callback = |value| value + 1;").unwrap(); + let expected = metadata(&ast); + assert_eq!(expected.len(), 1); + let program = Compiler::new().compile(&ast); + assert_discovery(&program, &expected); + let bytes = program.write().unwrap(); + assert_discovery(&Program::read(&bytes).unwrap(), &expected); + } + + #[cfg(feature = "metadata")] + #[test] + fn compiled_doc_comments_are_empty_even_with_a_retained_library() { + let ast = Engine::new() + .compile( + r#" + /// Compiled function documentation. + fn compiled(value) { value } + /// Fallback function documentation. + fn fallback() { eval("1"); } + "#, + ) + .unwrap(); + assert!(ast.iter_functions().all(|f| !f.comments.is_empty())); + let expected: Vec<_> = metadata(&ast).into_iter().filter(|f| f.name == "compiled").collect(); + let program = Compiler::new().compile(&ast); + assert!(matches!(program.write(), Err(WriteError::HasScriptFunctions))); + assert_discovery(&program, &expected); + assert!(program.iter_functions().all(|f| f.comments.is_empty())); + } +} + +#[cfg(feature = "grain")] +#[cfg(feature = "no_ast")] +#[cfg(not(feature = "no_float"))] +#[cfg(not(feature = "no_index"))] +#[cfg(not(feature = "no_object"))] +#[cfg(not(feature = "no_closure"))] +#[cfg(not(feature = "only_i32"))] +#[cfg(not(feature = "f32_float"))] +#[cfg(not(feature = "decimal"))] +#[test] +fn discover_functions_without_an_ast() { + use rhai::grain::Program; + use rhai::FnAccess; + + let bytes = include_bytes!("grain/fixtures/golden.rgrn"); + let program = Program::read(bytes).unwrap(); + assert!(program.has_functions()); + assert_eq!(program.iter_functions().count(), program.num_functions()); + assert_eq!(program.num_functions(), 2); + + let tally = program.iter_functions().find(|f| f.name == "tally").unwrap(); + assert_eq!(tally.params, ["items", "bias"]); + assert_eq!(tally.access, FnAccess::Public); + assert_eq!(tally.this_type, None); + #[cfg(feature = "metadata")] + assert!(tally.comments.is_empty()); + + let expected: Vec<_> = program.iter_functions().collect(); + let owned = Program::read(bytes).unwrap().into_owned(); + assert_eq!(owned.iter_functions().collect::>(), expected); + let shared = owned.into_shared(); + assert_eq!(shared.iter_functions().collect::>(), expected); +} diff --git a/tests/mod.rs b/tests/mod.rs index 48f2a98d3..eb27c127f 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -29,6 +29,8 @@ mod grain { mod custom_syntax; mod differential; mod format; + #[cfg(not(feature = "no_function"))] + mod functions; mod lazy; // Both are about execution staying inside a bound, which `unchecked` // removes outright — and the artifact fuzzer needs `max_operations` to stop