预编译
- 预编译文件:二进制文件
- 使用luac程序可以进行文件的预编译,生成.lc文件
- 预编译:luac -o prog.lc prog.lua
- 执行:lua prog.lc
- 预编译后的代码不一定更小,但是更快,可以避免源码被意外修改
- 使用luac程序可以进行文件的预编译,生成.lc文件
错误
assert():对参数进行检查
- assert(code[,error]):对参数code进行检查,如果正确则返回code的执行结果,错误则返回错误信息
- code:要检查的代码
- error:可选参数,错误时输出的字符串
- 注:assert函会在调用函数前对参数值进行求值,因此使用显式更好。
- 如:assert(tonumber(n), “input” .. n .. “error”);不存tonumber是否正确,都会执行第二个参数的连接
- 对code进行检查,如果发生错误则输出error的语句
pcall():安全调用
- pacll(function[,level]):使用安全模式执行函数;正确,则返回true和函数正确执行所返回的参数;错误,则返回false和错误信息
- function:要执行的函数,一般是匿名函数
- 返回的错误信息:不代表只能返回字符串,可以返回error函数中的任意类型
```lua
local ok, msg =
pcall(
function()
if unexpected_condition then
end print(a[i]) end )error() --使用error来抛出异常。如果error()中为字符串,则msg为字符串;
--如果error()中为table,则msg为table
if ok then —对函数是否正确做出处理 print(“ok”) else print(“fail”) end
```