[Oddity] _G in package.loaded

87 views
Skip to first unread message

Martin Eden

unread,
Jun 12, 2026, 9:34:51 AMJun 12
to lu...@googlegroups.com
Hello list,

While working on my deploy generator I discovered that
"_G" is present in "package.loaded":

  $ lua
  Lua 5.5.0  Copyright (C) 1994-2025 Lua.org, PUC-Rio
  > package.loaded._G
  table: 0x55fb5ddbcbc0

I expect this table be name map for modules loaded via require().
(And no, I have no "require('_G')" in my code.)

Any ideas why it's so?

-- Martin

Luiz Henrique de Figueiredo

unread,
Jun 12, 2026, 9:42:37 AMJun 12
to lu...@googlegroups.com
> "_G" is present in "package.loaded":
> Any ideas why it's so?

https://www.lua.org/source/5.5/linit.c.html
https://www.lua.org/source/5.5/lauxlib.h.html#LUA_GNAME

Martin Eden

unread,
Jun 12, 2026, 10:06:39 AMJun 12
to lu...@googlegroups.com
Yeah but my "why" was aimed for different plane.

-- Martin

ppp vvv

unread,
Jun 12, 2026, 10:39:26 AMJun 12
to lua-l
> And no, I have no "require('_G')" in my code
you do have luaL_openlibs before your code.

string library is loaded into table called  'string'  -> package.loaded.string
os library is loaded into table called  'os' -> package.loaded.os
base library loaded directly into _G table


пятница, 12 июня 2026 г. в 16:06:39 UTC+2, Martin Eden:

Martin Eden

unread,
Jun 12, 2026, 10:28:13 PMJun 12
to lu...@googlegroups.com
Thanks for the replies

I was perceiving _G like global namespace alias. But it's wrong.
_G is just another table special is that it's default package when
resolving name.

Tangentially I appreciate style in code listings on Lua site.
Comparing with github it's like modern and baroque, One is centered
on content, other on frame.

-- Martin

ppp vvv

unread,
Jun 14, 2026, 5:28:57 AMJun 14
to lua-l
Does this also mean that it is not a good idea to use luaL_requiref(L, "_G", ...) to load another library as "global" into _G, because package.loaded._G is already occupied by baselib and it would not actually load it because "package not already loaded"?
суббота, 13 июня 2026 г. в 04:28:13 UTC+2, Martin Eden:
Reply all
Reply to author
Forward
0 new messages