-
Hi, I'm trying to do something where this below is the simple case: use mlua::{Lua, Result};
fn main() -> Result<()> {
let lua = Lua::new();
let lua_fn = lua
.load(r#"
t = 22
return t
"#).into_function()?;
eprintln!("before: {}", lua_fn.call::<i32>(())?);
let env = lua_fn.environment().unwrap();
env.set("t", 100)?;
lua_fn.set_environment(env)?;
// this still prints 22
eprintln!("after: {}", lua_fn.call::<i32>(())?);
Ok(())
} update: note that in the example above, it prints 22 both times, when I want it to print 22 first, then 100. in my real use-case, I take some user-defined script (here just set t and return it as an example) and then I would like to modify some known variables in it and run it many times. Since I am running it many times (possibly millions) I don't want to eval each time. Is there some way to modify the env in this case? I get that the obvious way is to .call with |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
What do you mean by "eval each time"? The optimal solution would be to load the function once, get envs table, and then re-run the function updating the environment. |
Beta Was this translation helpful? Give feedback.
Which means you want to modify the original script logic (since the script unconditionally set
t = 22
to prevent any environment modification). The only way to do this is use hooks to hijack the execution flow and modifyt
in the middle.Sort of: