20250111
The point of this article is to start to explore the contents of _G
in lua, specifically the version I happened to have installed at some point on my machine: Lua 5.1.5.
When I type lua
in my terminal, I get the following friendly prompt:
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
>
In looking this version up on the web, I find that it was released in 2014.
I don't recall installing it on my machine, but I must have at some point for some purpose. You have things like that?
I pick it because I have to pick somewhere, and it means I don't have to install anything. I make note of it because that way there are no questions about why your experience differs from mine.
The global _G
contains all of the globals available to you in lua as a lovely table. Yes, this table will include _G
itself.
My main purpose here is to spit out all of the things in _G
, and what sort of something they happen to be, in particular in the version of Lua I am using.
Here's the script I wrote to do this:
for k, v in pairs(_G) do
print(k..": "..type(v))
end
And running that, this is the result I get, a gloriously jumbled list of tables and functions, and one string:
string: table
xpcall: function
package: table
tostring: function
print: function
os: table
unpack: function
require: function
getfenv: function
setmetatable: function
next: function
assert: function
tonumber: function
io: table
rawequal: function
collectgarbage: function
arg: table
getmetatable: function
module: function
rawset: function
math: table
debug: table
pcall: function
table: table
newproxy: function
type: function
coroutine: table
_G: table
select: function
gcinfo: function
pairs: function
rawget: function
loadstring: function
ipairs: function
_VERSION: string
dofile: function
setfenv: function
load: function
error: function
loadfile: function
As you can see, _G
is there. So you can refer to _G
as _G._G
, or _G._G._G
. It is _G
all the way down.
There are forty different things, and that's a lot. My main point here is to get them established, so that I can do a deep dive into them individually/as small groups of related thing in subsequent articles.
But jumbled together, they don't make a whole lot of sense taken as a whole.
There is but one string hanging off of _G, and that is _VERSION. We can take this one on right here and right now. The following code will show us the value of _G._VERSION:
Listing 1.2:
print(_G._VERSION)
And the output is
Lua 5.1
And no one is surprised.
There are 29 of these, nearly 75% of the things inside of _G:
assert, collectgarbage, dofile, error, gcinfo, getfenv, getmetatable, ipairs, load, loadfile, loadstring, module, newproxy, next, pairs, pcall, print, rawget, rawset, rawequal, require, select, setfenv, setmetatable, tonumber, tostring, type, unpack, xpcall
However, most of them fall into logical groups. For example, the various functions that start with raw
make sense to be looked at together. Roughly 2-3 functions are logically grouped together, and expect an article for each group, and approximately 10 articles in total.
There are 9 of these (I'm not counting _G
itself), accounting for the remaining 25% of the things inside of _G
:
arg, coroutine, debug, io, math, os, package, string, table
Each one of these will get covered within their own article.
The contents of _G are the tip of the iceburg, and it lays the groundwork for further investigations. I do this for my own personal understanding and mastery of the mysteries of Lua.