lua基础之Base

_G

变量是全局环境表。 (_G._G == _G)
您不能通过分配给 _G 来更改环境,而是使用 setfenv。

_VERSION

一个全局变量,它是一个包含当前 Lua 解释器版本的字符串。

print (_VERSION) --> Lua 5.1

v = assert (v, message)

Asserts that condition is not nil and not false

collectgarbage (opt, arg)

  • “stop”:停止垃圾收集器。
  • “restart”:重启垃圾收集器。
  • “collect”:执行完整的垃圾收集周期(如果没有提供选项,这是默认设置)
  • “count”:返回 Lua 使用的总内存(以 KB 为单位)。
  • “step”:执行垃圾收集步骤。 步骤“大小”由 arg 以非指定方式控制(较大的值意味着更多的步骤)。 如果要控制步长,则必须通过实验调整 arg 的值。 如果该步骤完成了一个收集周期,则返回 true。
  • “setpause”:将 arg/100 设置为收集器暂停的新值(见下文)。
  • “setstepmul”:将 arg/100 设置为收集器步长乘数的新值(见下文)。

Lua 实现了一个增量标记清除收集器。它使用两个数字来控制其垃圾收集周期:垃圾收集器暂停和垃圾收集器步骤乘数。

垃圾收集器暂停控制收集器在开始新循环之前等待的时间。较大的值使收集器不那么激进。小于 1 的值意味着收集器不会等待开始新的循环。值为 2 意味着收集器在开始新循环之前等待使用中的总内存翻倍。

step multiplier 控制收集器相对于内存分配的相对速度。较大的值使收集器更具侵略性,但也会增加每个增量步骤的大小。小于 1 的值会使收集器太慢,并可能导致收集器永远不会完成一个循环。默认值 2 表示收集器以内存分配速度的“两倍”运行。

“setpause”和“setstepmul”都将百分比作为参数(因此参数 100 意味着实际值为 1)。当 Lua 启动时,两者都默认为 200(因为它们除以 100,实际上默认值为 2,如上所述)。

collectgarbage (“collect”) --> forces garbage collection

val = dofile (filename)

打开命名文件,将其内容作为 Lua 块进行解析和执行。
如果发生错误则引发错误。 返回块返回的任何值。

dofile ("myfile.lua") 

function dofile (filename)
  local f = assert (loadfile (filename))
  return f ()
end -- dofile

error (message, level)

使用提供的消息引发错误。 永远不会return。 如果提供了一个级别,则错误指向当前函数(级别 1 或 nil)、父函数(级别 2)等。

t = getfenv (f)

返回指定函数 f 使用的当前环境。
f 可以是一个函数或一个代表堆栈级别的数字,其中 1 是当前正在运行的函数,2 是它的父函数,依此类推。

环境是存储“全局”变量的地方。

print (getfenv (1)) --> table: 02072780
print (_G) --> table: 02072780

getmetatable

Returns the metatable for the object

ipairs

Iterates over a numerically keyed table

load

Loads a chunk by calling a function repeatedly

loadfile

Loads a Lua file and parses it

loadstring

Compiles a string of Lua code

module

Creates a Lua module

next

Returns next key / value pair in a table

pairs

Traverse all items in a table

pcall

Calls a function in protected mode

print

Prints its arguments

rawequal

Compares two values for equality without invoking## metamethods

rawget

Gets the value of a table item without invoking## metamethods

rawset

Sets the value of a table item without invoking## metamethods

require

Loads a module

select

Returns items in a list

setfenv

Sets a function’s environment

setmetatable

Sets the metatable for a table

tonumber

Converts a string (of the given base) to a number

tostring

Converts its argument to a string

type

Returns the type of a variable

unpack

Unpacks a table into individual items

xpcall

Calls a function with a custom error handler

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值