lupa icon indicating copy to clipboard operation
lupa copied to clipboard

Using `_ENV` to overwrite a scope

Open bczsalba opened this issue 1 year ago • 6 comments

Is there any way to use _ENV with Lupa? From my testing I've gathered that each call to either .execute or .eval resets/uses its own _ENV, so if you set it in one call it resets by the next.

Reproduction

from lupa import LuaRuntime

lua = LuaRuntime()

code = """
    hidden = 1
    print(hidden)
    
    _ENV = { print = print }
    
    print(hidden)
"""

for line in code.splitlines():
    lua.exec(line)

The Lua code outputs 1 then nil (like on this compiler), but using Lupa results in 1 and 1.

Context

I'm using Lupa to embed Lua into a UI library I'm making. Each widget has its own script, and their _ENV-s create a sort of cascade where each widget within a container has access to things defined above it, but not to its siblings. The end goal is something like Alpine.js.

I suspect the same cascade would be possible using functions instead of inline do ... end scopes, but that massively complicates everything.

Cheers, and enjoy the holidays!

bczsalba avatar Dec 25 '24 19:12 bczsalba