Skip to content

Using _ENV to overwrite a scope #275

@bczsalba

Description

@bczsalba

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions