-
Notifications
You must be signed in to change notification settings - Fork 181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Imports and the scriptable event handler use case combined with REPL #934
Comments
I'm pretty sure you can call the scripted functions in global... That's how the Rhai REPL does it... If all your imports are static then just load them once into the engine. If they are volatile, then yes you'd want to keep the loaded modules that you imported before... The Rhai REPL currently forces you to reimport the modules on every run. |
Given the following changes to the Rhai REPL: diff --git a/src/bin/rhai-repl.rs b/src/bin/rhai-repl.rs
index 6a1215c7..3a0d161f 100644
--- a/src/bin/rhai-repl.rs
+++ b/src/bin/rhai-repl.rs
@@ -286,6 +286,10 @@ mod sample_functions {
*x += y + (z.len() as INT);
println!("{x} {y} {z}");
}
+
+ pub fn test3(x: INT, y: INT) {
+ println!("{x} {y}");
+ }
}
fn main() {
@@ -596,6 +600,15 @@ fn main() {
// Throw away all the statements, leaving only the functions
main_ast.clear_statements();
+
+ engine
+ .call_fn::<()>(
+ &mut scope,
+ &main_ast,
+ "test3",
+ (1 as rhai::INT, 2 as rhai::INT),
+ )
+ .expect("Failed...");
}
rl.save_history(HISTORY_FILE) The functions in the global module seems to be available from the script, but they do not seem to be directly callable from Rust:
|
You cannot call a native Rust function via |
Ok, here is another example with the function defined in a Rhai script which still does not work: Executing: testing.rhai: fn testing() {
print("Works");
} Changes: diff --git a/src/bin/rhai-repl.rs b/src/bin/rhai-repl.rs
index 6a1215c7..6959e6b7 100644
--- a/src/bin/rhai-repl.rs
+++ b/src/bin/rhai-repl.rs
@@ -596,6 +596,10 @@ fn main() {
// Throw away all the statements, leaving only the functions
main_ast.clear_statements();
+
+ engine
+ .call_fn::<()>(&mut scope, &main_ast, "testing", ())
+ .expect("Failed...");
}
rl.save_history(HISTORY_FILE) Output:
|
That would be interesting. Can you put a Also you can dump the function signatures to see if |
Sure, Updated script: fn testing() {
print("Works");
}
testing(); Executing: Changes: diff --git a/src/bin/rhai-repl.rs b/src/bin/rhai-repl.rs
index 6a1215c7..6959e6b7 100644
--- a/src/bin/rhai-repl.rs
+++ b/src/bin/rhai-repl.rs
@@ -596,6 +596,10 @@ fn main() {
// Throw away all the statements, leaving only the functions
main_ast.clear_statements();
+
+ engine
+ .call_fn::<()>(&mut scope, &main_ast, "testing", ())
+ .expect("Failed...");
}
rl.save_history(HISTORY_FILE) Output:
Conclusions:
|
Can you use Because the function is not in the AST. You can define testing in the REPL and it'll work, I'm pretty sure... |
Yes, using The problem I'm having is combining the event-handler pattern with a REPL wich requires that i use I tried to work around this limitation by registering the source as a global module but then discovered that it was not possible to call any of the event handler functions defined in the registered global module. |
So it seems |
Thank you 🙂 |
I'm sorry it was misleading. /// Call a script function defined in an [`AST`] with multiple arguments.
Functions inside your scripts, however, I believe should be able to call your scripts loaded into global. |
Yes, it would be great however if there was a way to call a function from rust side that first attempts to call the function in the given AST and if the function was not found then attempt to call the function in the registered global modules. |
I fear that may be a breaking change... |
|
I have an issue with an application that implements the scriptable event handler use case and also provides a REPL interface:
The book recommends that imports be placed at the top of the script: https://rhai.rs/book/language/modules/import.html#admonition-place-import-statements-at-the-top
REPL implementations typically use AST::clear_statements() to prevent statements from being executed twice.
After
clear_statements
has been called any imports at top level will be gone from the AST causing event handler functions that depends on the global imports to fail.One approach that I tried to circumvent this is to add the loaded script into a global module similar to how the Rhai REPL does things. However it seems that script functions in a global module cannot be directly accessed from rust side preventing me from calling them using Engine::call_fn().
Am I missing something or is this use-case simply not supported unless the imports are placed inside the event handler functions?
The text was updated successfully, but these errors were encountered: