加载编译:loadfile() & load()
loadfile(filename) 做的就是加载并编译代码,然后返回filename对应chunk的函数,注意不运行的话无法加载其中的代码dofile可以等价于local f = assert(loadfile(filename))后f()
load(str) 等价于加载编译这个字符串中的代码,但是操作的是全局变量预编译
$ luac -o prog.lc prog.lua
错误
assert(assetionValue, errorMessage) error(errorMessage) 处理错误: pcall()
local ok, msg = pcall(function() -- some code if unexpected_code then error(err) end -- some code print(a[i]) -- potential error may happen -- some code end ) if ok then -- regular code else -- error-handling code end