rhai: Cannot create recursive function in module
Seems like I cannot create a recursive function inside a module.
extern crate rhai;
use rhai::{Engine, Module, Scope};
const MODULE_TEXT: &str = r#"
fn factorial(n) {
if n == 1 {
return 1;
} else {
return factorial(n-1);
}
}
"#;
const SCRIPT: &str = r#"
import "test_module" as test;
print(test::factorial(5));
"#;
fn main() {
let mut engine = Engine::new();
let module_ast = engine.compile(MODULE_TEXT).unwrap();
let module = Module::eval_ast_as_new(Scope::new(), &module_ast, &engine).unwrap();
let mut static_modules = rhai::module_resolvers::StaticModuleResolver::new();
static_modules.insert("test_module", module);
engine.set_module_resolver(Some(static_modules));
engine.consume(SCRIPT).unwrap();
}
Results in: ErrorInFunctionCall("factorial", ErrorFunctionNotFound("factorial (i64)", 6:16), 3:17)
Upon further testing, it also looks like I cannot access other functions defined in the same module either, even though I expect all of those to be in scope. Am I misunderstanding something, or is this a bug?
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 28
Just tested it out, looks like it’s working exactly like I expect.
I’d add the change to “breaking changes” in RELEASES.md by the way since it’s technically one.
One more thing - say I want a custom resolver that has the same functionality, I think this should be made more of a first-class feature by adding a method that makes it “just work” on
ASTorModule, so that implementing a custom resolver can focus on actually resolving and not just copying theiter_functionsloop fromFileModuleResolver.Maybe
Module::eval_ast_encapsulatedor something?I left some comments on commit c4ec93080e58eb1f48ca4042e35f018ef209e1d3 for now