LUA_ENV might not be the "_ENV" string

57 views
Skip to first unread message

Jure Bagić

unread,
Dec 3, 2025, 8:29:36 PM (13 days ago) Dec 3
to lua-l
The new way of declaring global variables and maintaining backwards
compatibility is very elegant. Looking a bit at the implementation I stumbled
upon a small detail that might need fixing in lparser.c:502 the
'buildglobal' function:
> if (var->k == VGLOBAL)
> luaK_semerror(ls, "_ENV is global when accessing variable '%s'",
> getstr(varname));

The name of the 1st upvalue of the chunk (the environment) is dictated by the
definition of LUA_ENV, so maybe instead do:
> if (var->k == VGLOBAL)
> luaK_semerror(ls, "%s is global when accessing variable '%s'",
> LUA_ENV, getstr(varname));

This also works:
> if (var->k == VGLOBAL)
> luaK_semerror(ls, "%s is global when accessing variable '%s'",
> getstr(ls->envn), getstr(varname));

-- Jure
signature.asc

Roberto Ierusalimschy

unread,
Dec 4, 2025, 11:20:32 AM (12 days ago) Dec 4
to lu...@googlegroups.com
Thanks for the feedback. I would prefer a third way:

luaK_semerror(ls, LUA_ENV " is global when accessing variable '%s'",

-- Roberto

Jure Bagić

unread,
Dec 4, 2025, 12:27:12 PM (12 days ago) Dec 4
to lu...@googlegroups.com
On 25/12/04 01:20PM, Roberto Ierusalimschy wrote:
>
> Thanks for the feedback. I would prefer a third way:
>
> luaK_semerror(ls, LUA_ENV " is global when accessing variable '%s'",
>
> -- Roberto

Ok, you win.
--
Jure
signature.asc

Roberto Ierusalimschy

unread,
Dec 4, 2025, 1:59:15 PM (12 days ago) Dec 4
to lu...@googlegroups.com
> > Thanks for the feedback. I would prefer a third way:
> >
> > luaK_semerror(ls, LUA_ENV " is global when accessing variable '%s'",
> >
> > -- Roberto
>
> Ok, you win.

Actually, I am not sure I prefer this :-) To know this code is safe we
need to know that LUA_ENV is a valid variable name and, therefore, cannot
contain a '%'. In the end, it is more complex than it looks.

-- Roberto

Sainan

unread,
Dec 4, 2025, 2:03:43 PM (12 days ago) Dec 4
to lu...@googlegroups.com
I'm sure there's a macro metaprogamming exercise here somewhere. :^)

-- Sainan
Reply all
Reply to author
Forward
0 new messages