base

The global functions of Lua 5.1 / LuaJIT 2.1. They are plain globals and are documented here without any prefix.

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 v is nil or false; otherwise returns all its arguments.
  • The error message is message when given, otherwise "assertion failed!".
  • If message is 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; returns true if the step finished a cycle.
  • "setpause" / "setstepmul": sets the collector parameter to arg (percent) and returns the previous value.

error(message: any, level?: number = 1)

error(message: any, level?: number = 1)

  • Raises message as an error. Never returns.
  • If message is a string, position information is prepended according to level: 1 is the function calling error, 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.
  • 0 returns the global environment of the running thread.

getmetatable(obj: any): table | any

getmetatable(obj: any): table | any

  • Returns the metatable of obj, or nil if it has none.
  • If the metatable has a __metatable field, returns that field instead.
  • For strings, returns the shared string metatable (whose __index is string).

ipairs(t: table): function, table, number

ipairs(t: table): function, table, number

  • Returns an iterator, t and 0, for use in a generic for.
  • Iterates 1, t[1], 2, t[2], ... and stops at the first nil value.
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 nil plus an error message on a syntax error.
  • If chunk is a function, it is called repeatedly to get the pieces of the chunk; each call must return a string, and an empty string or nil ends the chunk.
  • chunkname is 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: load returns nil and the message attempt to load chunk with wrong mode.
  • The compiled function runs in the global environment; use setfenv to 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 load for a string chunk: returns the compiled function, or nil plus an error message.
  • Only text source is accepted. Bytecode is refused with attempt to load chunk with wrong mode.
  • chunkname defaults to the text itself.

newproxy(arg?: boolean | userdata): userdata

newproxy(arg?: boolean | userdata): userdata

  • Creates a new empty userdata.
  • If arg is true, the userdata gets a fresh metatable that can be fetched with getmetatable and filled in.
  • If arg is 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 key in t, and its value. With key omitted or nil, returns the first pair.
  • Returns nil at 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, t and nil for use in a generic for, visiting every key/value pair in unspecified order.
  • If t has a __pairs metamethod 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 f with the given arguments in protected mode.
  • On success returns true followed by all results of f; on error returns false and the error value.
  • Script time limit errors (see debug) cannot be caught: the script is stopped regardless of pcall.

print(...: any)

  • Writes its arguments to the client console window, not to stdout.
  • Each argument is converted with tostring and arguments are separated by a tab; a newline ends the line.

rawequal(a: any, b: any): boolean

rawequal(a: any, b: any): boolean

  • Compares a and b for primitive equality, ignoring any __eq metamethod.

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] = value without invoking __newindex. Returns t.
  • Errors if key is nil or 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.lua and then name/init.lua inside C:\xiaohook\lua. Dots in name are directory separators: a.b means a/b.lua.
  • package.path and package.cpath are ignored. Native modules cannot be loaded. package.loadlib and package.seeall are removed.
  • package.loaded and package.preload still work: a function in package.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, true is 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 index is a number, returns all arguments after argument number index. Negative indices count from the end.
  • If index is "#", 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 level f) to env and returns the function.
  • Level 0 sets 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 t to mt, or removes it when mt is nil. Returns t.
  • Errors if the current metatable has a __metatable field.

tonumber(v: any, base?: number = 10): number | nil

tonumber(v: any, base?: number = 10): number | nil

  • Without base, converts numbers and numeric strings (including hexadecimal 0x and exponent forms); returns nil if the value cannot be converted.
  • With base (2 to 36), v must 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 __tostring metamethod 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] to t[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 handler err with the error value before the stack unwinds, so it can build a traceback.
  • Returns true plus the results of f on success, or false plus the handler's return value on error.
  • Lua 5.1 form: arguments for f cannot 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".