Skip to content

Implement Lua 5.3 _ENV semantics for global access #186

Closed

Description

@davydog187

Goal

Make user-level reassignment of _ENV actually redirect subsequent
"global" reads and writes through the named environment table, matching
Lua 5.3 semantics.

Background

In Lua 5.3, every "global" name reference is syntactic sugar for
_ENV.name, where _ENV is an implicit upvalue in every function. A
user can swap their environment with _ENV = setmetatable({}, ...) or
local _ENV = ... and all subsequent free-name accesses go through
that table (and its metamethods).

This implementation does not honour that today. Free-name resolution in
lib/lua/compiler/scope.ex produces a {:global, name} tag, codegen
emits dedicated :get_global / :set_global opcodes, and the executor
reads/writes a flat state.globals map that no Lua-level table
participates in. _G is a metatable-backed proxy whose data map is
empty and whose __index/__newindex route to state.globals. _ENV
is set once to that same proxy at stdlib install and never consulted
again.

Impact

This blocks suite test files that exercise real _ENV semantics:

  • events.lua — swaps _ENV on line 10, before any metamethod tests
    run. Surfaced this gap during plan A8 triage.
  • attrib.lua — does _ENV = _G.
  • locals.lua — local _ENV = ... patterns.
  • errors.lua — also uses _ENV reassignment.

Scope

Substantial: touches scope.ex, codegen.ex, executor.ex, and
stdlib.ex. Free-name resolution must rewrite to _ENV.name lookups,
the main chunk needs an implicit _ENV upvalue, and globals storage
needs to live where the _ENV table can see it.

Plan

See .agents/plans/A16-env-semantics.md for the full plan, success
criteria, implementation notes, and risks.

Tests

Documenting tests already merged (currently @tag :skip):
test/lua/vm/env_semantics_test.exs. Removing the skip tags is a
success criterion of the plan.

Related

  • Plan A8 (events.lua) is blocked on this — see
    .agents/plans/A8-events-suite.md.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions