LuaSTG-x Core API
Lua_Builtin

Classes

class  _G
 cc.RenderTexture More...
 
class  any
 
class  bit
 
class  boolean
 
class  coroutine
 
class  debug
 
class  ffi
 
class  file
 
class  function
 
class  io
 
class  jit
 
class  local
 
class  math
 
class  nil
 
class  number
 
class  jit::opt
 
class  os
 
class  string
 
class  table
 
class  thread
 
class  userdata
 

Functions

static function _G::assert (local v, local message)
 
static function _G::collectgarbage (local opt, local arg)
 
static function _G::dofile (local filename)
 
static function _G::error (local message, local level)
 
static function _G::getfenv (local f)
 
static function _G::getmetatable (local object)
 
static function _G::ipairs (local t)
 
static function _G::load (local chunk, local chunkname)
 
static function _G::loadfile (local filename, local mode, local env)
 
static function package::loadlib (local libname, local funcname)
 
static function _G::loadstring (local string, local chunkname)
 
static function _G::module (local name, local ...)
 
static function _G::next (local table, local index)
 
static function _G::pairs (local t)
 
static function _G::pcall (local f, local arg1, local ...)
 
static function _G::print (local ...)
 
static function _G::rawequal (local v1, local v2)
 
static function _G::rawget (local table, local index)
 
static function _G::rawset (local table, local index, local value)
 
static function _G::require (local modname)
 
static function _G::select (local index, local ...)
 
static function _G::setfenv (local f, local table)
 
static function _G::setmetatable (local table, local metatable)
 
static function _G::tonumber (local e, local base)
 
static function _G::tostring (local v)
 
static function _G::type (local v)
 
static function _G::unpack (local list, local i, local j)
 
static function _G::xpcall (local f, local msgh, local arg1, local ...)
 

Detailed Description

Function Documentation

◆ assert()

static function assert ( local  v,
local  message 
)

如果其参数 v 的值为假( nilfalse )它就调用 error

否则,返回所有的参数。在错误情况时,message 指那个错误对象;

如果不提供这个参数,参数默认为 **"assertion failed!"** 。

重载
fun(v:any):any
Parameters
vany
messagestring
Returns
any

◆ collectgarbage()

static function collectgarbage ( local  opt,
local  arg 
)

这个函数是垃圾收集器的通用接口。

通过参数 opt 它提供了一组不同的功能:

**"collect"**: 做一次完整的垃圾收集循环。这是默认选项。

**"stop"**: 停止垃圾收集器的运行。在调用重启前,收集器只会因显式的调用运行。

**"restart"**: 重启垃圾收集器的自动运行。

**"count"**: 以 K 字节数为单位返回 Lua 使用的总内存数。这个值有小数部分,

所以只需要乘上 1024 就能得到 Lua 使用的准确字节数(除非溢出)。

**"step"**: 单步运行垃圾收集器。步长“大小”由 arg 控制。传入 0 时,

收集器步进(不可分割的)一步。传入非 0 值,收集器收集相当于 Lua 分配这些多(K 字节)

内存的工作。如果收集器结束一个循环将返回 true

**"setpause"**: 将 arg 设为收集器的 间歇率 。返回 间歇率 的前一个值。

**"incremental"**: Change the collector mode to incremental. This option can

be followed by three numbers: the garbage-collector pause, the step

multiplier, and the step size.

**"generational"**: Change the collector mode to generational. This option

can be followed by two numbers: the garbage-collector minor multiplier and

the major multiplier.

**"isrunning"**: 返回表示收集器是否在工作的布尔值(即未被停止)。

重载
fun():any
Parameters
optstring
argstring
Returns
any

◆ dofile()

static function dofile ( local  filename)

打开该名字的文件,并执行文件中的 Lua 代码块。

不带参数调用时, dofile 执行标准输入的内容(stdin)。

返回该代码块的所有返回值。

对于有错误的情况,dofile 将错误反馈给调用者

(即,dofile 没有运行在保护模式下)。

重载
fun():table
Parameters
filenamestring
Returns
table

◆ error()

static function error ( local  message,
local  level 
)

中止上一次保护函数调用,将错误对象 message 返回。函数 error 永远不会返回。

当 message 是一个字符串时,通常 error 会把一些有关出错位置的信息附加在消息的前头。

level 参数指明了怎样获得出错位置。对于 level 1 (默认值),出错位置指 error

函数调用的位置。Level 2 将出错位置指向调用 error的函数的函数;以此类推。

传入 level 0 可以避免在消息前添加出错位置信息。

重载
fun(message:string)
Parameters
messagestring
levelnumber

◆ getfenv()

static function getfenv ( local  f)

Returns the current environment in use by the function. f can be a Lua

function or a number that specifies the function at that stack level:

Level 1 is the function calling getfenv. If the given function is not a

Lua function, or if f is 0, getfenv returns the global environment. The

default for f is 1.

重载
fun():function
Parameters
ffunction|number
Returns
any

◆ getmetatable()

static function getmetatable ( local  object)

如果 object 不包含元表,返回 nil 。否则,如果在该对象的元表中有

"__metatable" 域时返回其关联值,没有时返回该对象的元表。

Parameters
objectany
Returns
any

◆ ipairs()

static function ipairs ( local  t)

返回三个值(迭代函数、表 t 以及 0 ),如此,以下代码

for i,v in ipairs(t) do body end

将迭代键值对(1,t[1]) ,(2,t[2]), ... ,直到第一个空值。

@generic V

Parameters
ttable<number, V>|V[]
Returns
fun(tbl: table<number, V>):number, V

◆ load()

static function load ( local  chunk,
local  chunkname 
)

加载一个代码块。

如果 chunk 是一个字符串,代码块指这个字符串。

如果 chunk 是一个函数,load 不断地调用它获取代码块的片断。

每次对 chunk 的调用都必须返回一个字符串紧紧连接在上次调用的返回串之后。

当返回空串、**nil**、或是不返回值时,都表示代码块结束。

如果没有语法错误,则以函数形式返回编译好的代码块;

否则,返回 nil 加上错误消息。

chunkname 在错误消息和调试消息中,用于代码块的名字。

Lua 不会对二进制代码块做健壮性检查。恶意构造一个二进制块有可能把解释器弄崩溃。

重载
fun(chunk:fun():string):any
Parameters
chunkfun():string
chunknamestring
modestring
envnay

◆ loadfile()

static function loadfile ( local  filename,
local  mode,
local  env 
)

load 类似,不过是从文件 filename 或标准输入(如果文件名未提供)中获取代码块。

重载
fun()
Parameters
filenamestring
modestring
envany

◆ loadlib()

static function loadlib ( local  libname,
local  funcname 
)

这个路径被 require 在 C 加载器中做搜索时用到。

Lua 用和初始化 Lua 路径 package.path

相同的方式初始化 C 路径 package.cpath

它会使用环境变量 LUA_CPATH 初始化。

要么就采用 luaconf.h 中定义的默认路径。 用于 require 控制哪些模块已经被加载的表。

当你请求一个 modname 模块,且

package.loaded[modname] 不为假时,

require 简单返回储存在内的值。

这个变量仅仅是对真正那张表的引用;

改变这个值并不会改变 require 使用的表。 让宿主程序动态链接 C 库 libname

funcname 为 "`*`",

它仅仅连接该库,让库中的符号都导出给其它动态链接库使用。

否则,它查找库中的函数 funcname ,以 C 函数的形式返回这个函数。

因此,funcname 必须遵循原型 lua_CFunction

(参见 lua_CFunction)。

这是一个低阶函数。

它完全绕过了包模块系统。

require 不同,

它不会做任何路径查询,也不会自动加扩展名。

libname 必须是一个 C 库需要的完整的文件名,如果有必要,需要提供路径和扩展名。

funcname 必须是 C 库需要的准确名字

(这取决于使用的 C 编译器和链接器)。

这个函数在标准 C 中不支持。因此,它只在部分平台有效

( Windows ,Linux ,Mac OS X, Solaris, BSD, 加上支持

dlfcn 标准的 Unix 系统)。

Parameters
libnamestring
funcnamestring
Returns
fun():nil

◆ loadstring()

static function loadstring ( local  string,
local  chunkname 
)

Similar to load, but gets the chunk from the given string.

To load and run a given string, use the idiom

  `assert(loadstring(s))()`

When absent, chunkname defaults to the given string.

重载
fun(string:string):any
Parameters
stringstring
chunknamestring
Returns
any

◆ module()

static function module ( local  name,
local ...   
)

Creates a module. If there is a table in package.loaded[name], this table

is the module. Otherwise, if there is a global table t with the given name,

this table is the module. Otherwise creates a new table t and sets it as the

value of the global name and the value of package.loaded[name]. This function

also initializes t._NAME with the given name, t._M with the module (t itself),

and t._PACKAGE with the package name (the full module name minus last component;

see below). Finally, module sets t as the new environment of the current function

and the new value of package.loaded[name], so that require returns t.

If name is a compound name (that is, one with components separated by dots),

module creates (or reuses, if they already exist) tables for each component.

For instance, if name is a.b.c, then module stores the module table in field c

of field b of global a.

This function can receive optional options after the module name, where each

option is a function to be applied over the module.

Parameters
namestring
Returns
any

◆ next()

static function next ( local  table,
local  index 
)

运行程序来遍历表中的所有域。

第一个参数是要遍历的表,第二个参数是表中的某个键。

next 返回该键的下一个键及其关联的值。

如果用 nil 作为第二个参数调用 next

将返回初始键及其关联值。

当以最后一个键去调用,或是以 nil 调用一张空表时,

next 返回 **nil**。

如果不提供第二个参数,将认为它就是 **nil**。

特别指出,你可以用 next(t) 来判断一张表是否是空的。

索引在遍历过程中的次序无定义,

即使是数字索引也是这样

(如果想按数字次序遍历表,可以使用数字形式的 for 。)

当在遍历过程中你给表中并不存在的域赋值,

next 的行为是未定义的。

然而你可以去修改那些已存在的域。

特别指出,你可以清除一些已存在的域。

重载
fun(table:table):any
Parameters
tabletable
indexany
Returns
any

◆ pairs()

static function pairs ( local  t)

返回三个值:next 函数,表 t,以及 **nil**。

因此以下代码

for k,v in pairs(t) do *body* end

能迭代表 t 中的所有键值对。

参见函数 next 中关于迭代过程中修改表的风险。

@generic V

Parameters
ttable<string, V>|V[]
Returns
fun(tbl: table<string, V>):string, V

◆ pcall()

static function pcall ( local  f,
local  arg1,
local ...   
)

传入参数,以 保护模式 调用函数 f

这意味着 f 中的任何错误不会抛出;

取而代之的是,pcall 会将错误捕获到,并返回一个状态码。

第一个返回值是状态码(一个布尔量),

当没有错误时,其为真。

此时,pcall 同样会在状态码后返回所有调用的结果。

在有错误时,pcall 返回 false 加错误消息。

重载
fun(f:fun():any):boolean|table
Parameters
ffun():any
arg1table
Returns
boolean|table

◆ print()

static function print ( local ...  )

接收任意数量的参数,并将它们的值打印到 stdout

它用 tostring 函数将每个参数都转换为字符串。

print 不用于做格式化输出。仅作为看一下某个值的快捷方式。

多用于调试。

完整的对输出的控制,请使用 io.write

◆ rawequal()

static function rawequal ( local  v1,
local  v2 
)

在不触发任何元方法的情况下检查 v1 是否和 v2 相等。

返回一个布尔量。

Parameters
v1any
v2any
Returns
boolean

◆ rawget()

static function rawget ( local  table,
local  index 
)

在不触发任何元方法的情况下获取 table[index] 的值。

table 必须是一张表;index 可以是任何值。

Parameters
tabletable
indexany
Returns
any

◆ rawset()

static function rawset ( local  table,
local  index,
local  value 
)

在不触发任何元方法的情况下

table[index] 设为 value

table 必须是一张表,

index 可以是 nil 与 NaN 之外的任何值。

value 可以是任何 Lua 值。

这个函数返回 table

Parameters
tabletable
indexany
valueany

◆ require()

static function require ( local  modname)

加载一个模块。

这个函数首先查找 package.loaded 表,

检测 modname 是否被加载过。

如果被加载过,require 返回 package.loaded[modname] 中保存的值。

否则,它试着为模块寻找 加载器

首先 require 查找 package.preload[modname]

如果这里有一个值,这个值(必须是一个函数)就是那个加载器。

否则 require 使用 Lua 加载器去查找

package.path 的路径。

如果查找失败,接着使用 C 加载器去查找

package.cpath 的路径。

如果都失败了,再尝试 一体化 加载器。

When loading a C library, require first uses a dynamic link facility to link the application with the library. Then it tries to find a C function inside this library to be used as the loader. The name of this C function is the string "luaopen_" concatenated with a copy of the module name where each dot is replaced by an underscore. Moreover, if the module name has a hyphen, its prefix up to (and including) the first hyphen is removed. For instance, if the module name is a.v1-b.c, the function name will be luaopen_b_c.

If require finds neither a Lua library nor a C library for a module, it calls the all-in-one loader. This loader searches the C path for a library for the root name of the given module. For instance, when requiring a.b.c, it will search for a C library for a. If found, it looks into it for an open function for the submodule; in our example, that would be luaopen_a_b_c. With this facility, a package can pack several C submodules into one single library, with each submodule keeping its original open function.

每次找到一个加载器,require 都用两个参数调用加载器:

modname 和一个在获取加载器过程中得到的参数。

(如果通过查找文件得到的加载器,这个额外参数是文件名。)

如果加载器返回非空值,

require 将这个值赋给 package.loaded[modname]

如果加载器没能返回一个非空值用于赋给 package.loaded[modname]

require 会在那里设入 true

无论是什么情况,require 都会返回

package.loaded[modname] 的最终值。

如果在加载或运行模块时有错误,

或是无法为模块找到加载器,

require 都会抛出错误。

Parameters
modnamestring
Returns
any

◆ select()

static function select ( local  index,
local ...   
)

如果 index 是个数字,

那么返回参数中第 index 个之后的部分;

负的数字会从后向前索引(-1 指最后一个参数)。

否则,index 必须是字符串 "#"

此时 select 返回参数的个数。

@generic T

Parameters
indexnumber|string

@vararg T

Returns
T

◆ setfenv()

static function setfenv ( local  f,
local  table 
)

Sets the environment to be used by the given function. f can be a Lua

function or a number that specifies the function at that stack level:

Level 1 is the function calling setfenv. setfenv returns the given function.

Parameters
ffunction|number
tableany
Returns
function

◆ setmetatable()

static function setmetatable ( local  table,
local  metatable 
)

给指定表设置元表。

(你不能在 Lua 中改变其它类型值的元表,那些只能在 C 里做。)

如果 metatable 是 **nil**,

将指定表的元表移除。

如果原来那张元表有 "__metatable" 域,抛出一个错误。

这个函数返回 table

Parameters
tabletable
metatabletable
Returns
table

◆ tonumber()

static function tonumber ( local  e,
local  base 
)

如果调用的时候没有 base

tonumber 尝试把参数转换为一个数字。

如果参数已经是一个数字,或是一个可以转换为数字的字符串,

tonumber 就返回这个数字;

否则返回 **nil**。

(字符串可以有前置和后置的空格,可以带符号。)

当传入 base 调用它时,

e 必须是一个以该进制表示的整数字符串。

进制可以是 2 到 36 (包含 2 和 36)之间的任何整数。

大于 10 进制时,字母 'A' (大小写均可)表示 10 ,

'B' 表示 11,依次到 'Z' 表示 35 。

如果字符串 e 不是该进制下的合法数字,

函数返回 **nil**。

重载
fun(e:string):any
Parameters
estring
basenumber
Returns
any

◆ tostring()

static function tostring ( local  v)

可以接收任何类型,它将其转换为人可阅读的字符串形式。

浮点数总被转换为浮点数的表现形式(小数点形式或是指数形式)。

(如果想完全控制数字如何被转换,可以使用 string.format。)

如果 v"__tostring" 域的元表,

tostring 会以 v 为参数调用它。

并用它的结果作为返回值。

Parameters
vany
Returns
string

◆ type()

static function type ( local  v)

将参数的类型编码为一个字符串返回。

函数可能的返回值有

**"nil"** (一个字符串,而不是 nil 值),

**"number"** ,

**"string"** ,

**"boolean"** ,

**"table"** ,

**"function"** ,

**"thread"** ,

**"userdata"** 。

Parameters
vany
Returns
string

◆ unpack()

static function unpack ( local  list,
local  i,
local  j 
)

Returns the elements from the given table. This function is equivalent to

`return list[i], list[i+1], ···, list[j]`

except that the above code can be written only for a fixed number of elements.

By default, i is 1 and j is the length of the list, as defined by the length

operator (see §2.5.5).

重载
fun(list:table, i:number):any
重载
fun(list:table, i:number, j:number):any
Returns
any

◆ xpcall()

static function xpcall ( local  f,
local  msgh,
local  arg1,
local ...   
)

一个包含有当前解释器版本号的全局变量(并非函数)。

当前这个变量的值为 **"Lua 5.1"** 。 这个函数和 pcall 类似。

不过它可以额外设置一个消息处理器 msgh

Parameters
ffun():any
msghfun():string
Returns
any