base
The global functions of Lua 5.1 / LuaJIT 2.1. They are plain globals and are documented here without any prefix.
dofile and loadfile are removed
dofile and loadfile do not exist in this VM. To run a file, read it with the client file API and compile it: load(xiao.fs.read(path)).
Errors raised by scripts are shown in the client console in red, with the file name, the line and a traceback.
Functions
assert(v: any, message?: string): any
assert(v: any, message?: string): any
- Raises an error if
visnilorfalse; otherwise returns all its arguments. - The error message is
messagewhen given, otherwise"assertion failed!". - If
messageis not a string it is used as the error value as is.
local n = assert(tonumber("42"), "not a number")
collectgarbage(opt?: string = "collect", arg?: number): number | boolean
collectgarbage(opt?: string = "collect", arg?: number): number | boolean
"collect": runs a full garbage collection cycle and returns 0."stop"/"restart": stops or restarts the collector."count": returns the memory in use by Lua, in kilobytes (fractional)."step": runs a collection step; returnstrueif the step finished a cycle."setpause"/"setstepmul": sets the collector parameter toarg(percent) and returns the previous value.
error(message: any, level?: number = 1)
error(message: any, level?: number = 1)
- Raises
messageas an error. Never returns. - If
messageis a string, position information is prepended according tolevel: 1 is the function callingerror, 2 is the function that called it, 0 adds nothing.
gcinfo(): number
gcinfo(): number
- Returns the memory in use by Lua, in whole kilobytes. Deprecated form of
collectgarbage("count").
getfenv(f?: function | number = 1): table
getfenv(f?: function | number = 1): table
- Returns the environment table of function
f. - A number is a stack level: 1 is the function calling
getfenv, 2 its caller, and so on. 0returns the global environment of the running thread.
getmetatable(obj: any): table | any
getmetatable(obj: any): table | any
- Returns the metatable of
obj, ornilif it has none. - If the metatable has a
__metatablefield, returns that field instead. - For strings, returns the shared string metatable (whose
__indexisstring).
ipairs(t: table): function, table, number
ipairs(t: table): function, table, number
- Returns an iterator,
tand0, for use in a genericfor. - Iterates
1, t[1],2, t[2], ... and stops at the firstnilvalue.
for i, v in ipairs({ "a", "b", "c" }) do
print(i, v)
end
load(chunk: string | function, chunkname?: string): function | nil, string
load(chunk: string | function, chunkname?: string): function | nil, string
- Compiles a chunk and returns it as a function, or returns
nilplus an error message on a syntax error. - If
chunkis a function, it is called repeatedly to get the pieces of the chunk; each call must return a string, and an empty string ornilends the chunk. chunknameis the name used in error messages and tracebacks (default"=(load)").- Only text source is accepted. Bytecode (binary chunks, for example from
string.dump) is refused:loadreturnsniland the messageattempt to load chunk with wrong mode. - The compiled function runs in the global environment; use
setfenvto change it.
local f = load("return 1 + 2")
print(f())
loadstring(text: string, chunkname?: string): function | nil, string
loadstring(text: string, chunkname?: string): function | nil, string
- Same as
loadfor a string chunk: returns the compiled function, ornilplus an error message. - Only text source is accepted. Bytecode is refused with
attempt to load chunk with wrong mode. chunknamedefaults to the text itself.
newproxy(arg?: boolean | userdata): userdata
newproxy(arg?: boolean | userdata): userdata
- Creates a new empty userdata.
- If
argistrue, the userdata gets a fresh metatable that can be fetched withgetmetatableand filled in. - If
argis another proxy, the new userdata shares its metatable.
next(t: table, key?: any): any, any
next(t: table, key?: any): any, any
- Returns the key following
keyint, and its value. Withkeyomitted ornil, returns the first pair. - Returns
nilat the end of the traversal. - The order is not specified. Assigning to fields that do not exist during a traversal gives undefined behaviour; clearing existing fields is allowed.
pairs(t: table): function, table, nil
pairs(t: table): function, table, nil
- Returns
next,tandnilfor use in a genericfor, visiting every key/value pair in unspecified order. - If
thas a__pairsmetamethod it is not honoured (Lua 5.1 semantics unless the LuaJIT build enables it).
for k, v in pairs({ x = 1, y = 2 }) do
print(k, v)
end
pcall(f: function, ...: any): boolean, any
pcall(f: function, ...: any): boolean, any
- Calls
fwith the given arguments in protected mode. - On success returns
truefollowed by all results off; on error returnsfalseand the error value. - Script time limit errors (see
debug) cannot be caught: the script is stopped regardless ofpcall.
print(...: any)
print(...: any)
- Writes its arguments to the client console window, not to stdout.
- Each argument is converted with
tostringand arguments are separated by a tab; a newline ends the line.
rawequal(a: any, b: any): boolean
rawequal(a: any, b: any): boolean
- Compares
aandbfor primitive equality, ignoring any__eqmetamethod.
rawget(t: table, key: any): any
rawget(t: table, key: any): any
- Reads
t[key]without invoking__index.
rawset(t: table, key: any, value: any): table
rawset(t: table, key: any, value: any): table
- Sets
t[key] = valuewithout invoking__newindex. Returnst. - Errors if
keyisnilor NaN.
require(name: string): any
require(name: string): any
- Loads a module once and returns its value; later calls return the cached value from
package.loaded. - Only looks for
name.luaand thenname/init.luainsideC:\xiaohook\lua. Dots innameare directory separators:a.bmeansa/b.lua. package.pathandpackage.cpathare ignored. Native modules cannot be loaded.package.loadlibandpackage.seeallare removed.package.loadedandpackage.preloadstill work: a function inpackage.preload[name]is used as the loader before any file is searched.- If the module returns a non-nil value it is stored in
package.loaded[name]; if it returns nothing,trueis stored. - Raises an error listing the paths tried if the module is not found.
local util = require("lib.util")
select(index: number | string, ...: any): any
select(index: number | string, ...: any): any
- If
indexis a number, returns all arguments after argument numberindex. Negative indices count from the end. - If
indexis"#", returns the total number of extra arguments.
print(select("#", 1, nil, 3))
print(select(2, "a", "b", "c"))
setfenv(f: function | number, env: table): function
setfenv(f: function | number, env: table): function
- Sets the environment of function
f(or of the function at stack levelf) toenvand returns the function. - Level
0sets the environment of the running thread and returns nothing.
setmetatable(t: table, mt: table | nil): table
setmetatable(t: table, mt: table | nil): table
- Sets the metatable of
ttomt, or removes it whenmtisnil. Returnst. - Errors if the current metatable has a
__metatablefield.
tonumber(v: any, base?: number = 10): number | nil
tonumber(v: any, base?: number = 10): number | nil
- Without
base, converts numbers and numeric strings (including hexadecimal0xand exponent forms); returnsnilif the value cannot be converted. - With
base(2 to 36),vmust be a string holding an integer in that base; letters are digits 10 to 35.
print(tonumber("ff", 16))
print(tonumber("0x10"))
print(tonumber("abc"))
tostring(v: any): string
tostring(v: any): string
- Converts any value to a string. Uses the
__tostringmetamethod when present. - Tables, functions, userdata and threads print as
type: 0xADDRESS.
type(v: any): string
type(v: any): string
- Returns one of
"nil","number","string","boolean","table","function","thread","userdata","cdata".
unpack(t: table, i?: number = 1, j?: number = #t): any
unpack(t: table, i?: number = 1, j?: number = #t): any
- Returns the elements
t[i]tot[j]as multiple values.
print(unpack({ 10, 20, 30 }, 2))
xpcall(f: function, err: function): boolean, any
xpcall(f: function, err: function): boolean, any
- Like
pcall, but calls the handlererrwith the error value before the stack unwinds, so it can build a traceback. - Returns
trueplus the results offon success, orfalseplus the handler's return value on error. - Lua 5.1 form: arguments for
fcannot be passed; wrap the call in a closure.
local ok, trace = xpcall(function() error("boom") end, debug.traceback)
Constants
_G: table
_G: table
- The global environment table.
_G._G == _G.
_VERSION: string
_VERSION: string
- The string
"Lua 5.1".