Skip to content

set env after into_function? #555

Answered by khvzak
brentp asked this question in Q&A
Apr 7, 2025 · 1 comments · 5 replies

You must be logged in to vote

that makes sense. I wanted a way to do it so that the original user-script, which must define the variable (in this case t) would still be valid without any modifications.

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 modify t in the middle.

Sort of:

    let env = lua.create_table()?;

    let env2 = env.clone();
    lua.set_hook(HookTriggers::EVERY_LINE, move |lua, _| {
        lua.globals().set("t", env2.get::<Value>("t")?)?;
        Ok(mlua::VmState::Continue)
    })?;

    for t in 0..2 {
        env.set("t", t)?;

Replies: 1 comment 5 replies

You must be logged in to vote
5 replies
@brentp

@khvzak

@brentp

@khvzak

Answer selected by brentp
@brentp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants