lua函數(shù)說明
部分lua函數(shù)在lua的dash文檔中沒找到或者沒有使用例子,特記錄,備忘;
getfenv(f)
-
功能:
返回函數(shù)f的當(dāng)前環(huán)境表;
-
參數(shù):
f可以為函數(shù)或調(diào)用棧的級別;當(dāng)參數(shù)為調(diào)用棧級別時(shí),1表示當(dāng)前的函數(shù),0或其他值將返回全局環(huán)境表_G
-
例子:
a=1 for k,v in pairs(getfenv(string.upper)) do print(k,v) end
輸出:
```lua
a 1
string table: 0xbe12c0
xpcall function: 0xbe09c0
package table: 0xbe1b50
tostring function: 0xbe08a0
print function: 0xbe0a40
os table: 0xbe4140
unpack function: 0xbe0960
require function: 0xbe2340
getfenv function: 0xbe0430
setmetatable function: 0xbe07e0
next function: 0xbe0600
assert function: 0xbe0310
tonumber function: 0xbe0840
io table: 0xbe3760
rawequal function: 0xbe0aa0
collectgarbage function: 0xbe0370
getmetatable function: 0xbe06c0
module function: 0xbe22e0
rawset function: 0xbe0b60
math table: 0xbe5570
debug table: 0xbe6640
pcall function: 0xbe0660
table table: 0xbe0bc0
newproxy function: 0xbe1560
type function: 0xbe0900
coroutine table: 0xbe1600
_G table: 0xbdf6b0
select function: 0xbdf700
gcinfo function: 0xbe03d0
pairs function: 0xbe00b0
rawget function: 0xbe0b00
loadstring function: 0xbe05a0
ipairs function: 0xbe0010
_VERSION Lua 5.1
dofile function: 0xbe0480
setfenv function: 0xbe0780
load function: 0xbe0540
error function: 0xbe04e0
loadfile function: 0xbe0720
```
load (chunk [, chunkname [, mode [, env]]])
功能:
加載一個(gè)塊中的函數(shù);-
參數(shù):
chunk可以是字符串或函數(shù);如果為函數(shù),直到調(diào)用結(jié)果為空串、nil,將調(diào)用結(jié)果連接起來做為chunk的內(nèi)容;mode的可選值包括:- b:二進(jìn)制
- t:文本
- bt:二進(jìn)制或文本[默認(rèn)值]
-
例子:
function test() return "hello,world"; end fn=load(string.dump(test),nil,"b",_ENV) print(fn())
輸出為:hello,world
loadstring(chunk[,chunkname])
-
功能
同load;
參數(shù)
chunk為要裝載的字符串;
-
例子
local user_script = [[ local a = 0 local rand = math.random for i = 1, 200 do a = a + rand(i) end print("hi") ]] local f, err = loadstring(user_script, "=user script") local env = { math = math, print=print, } setfenv(f, env) f()
輸出為:hi
loadfile([filename])
-
功能
同load;
-
參數(shù)
參數(shù)為文件名;如果不傳任何參數(shù),表示從標(biāo)準(zhǔn)輸入加載內(nèi)容;
例子
pcall (f [, arg1, ···])
-
功能
在保護(hù)模式下調(diào)用函數(shù)(即發(fā)生的錯(cuò)誤將不會拋出異常給調(diào)用者)
-
參數(shù)
f為函數(shù)名稱,其他為函數(shù)參數(shù);
例子
xpcall (f, err_handler [, arg1, ···])
-
功能
與pcall類似,但可指定一個(gè)新的錯(cuò)誤處理函數(shù)句柄,當(dāng)調(diào)用函數(shù)成功能返回true,失敗時(shí)將返回false加err_handler返回的結(jié)果;
-
參數(shù)
f為函數(shù)名,err_handller為發(fā)生錯(cuò)誤時(shí)的回調(diào)函數(shù),其他為函數(shù)參數(shù);
-
例子
local user_script = [[ local a = 0 local rand = math.random for i = 1, 200 do a = a + rand(i) end print("hi") ]] local function handle_timeout(typ) return error("user script too hot") end local function handle_error(err) return string.format("%s: %s", err or "", debug.traceback()) end -- 為了讓debug.sethook正常工作,必須關(guān)閉JIT: user_script = [[jit.off(true, true) ]] .. user_script local f, err = loadstring(user_script, "=user script") if not f then ngx.say("ERROR: failed to load user script: ", err) return end -- 只允許math.*和print函數(shù)可以調(diào)用 local env = { math = math, print = print, jit = { off = jit.off }, } setfenv(f, env) --調(diào)用的指令不能超過1000,否則觸發(fā)handle_timeout函數(shù) --debug.sethook的第二個(gè)參數(shù)還可以傳入c,r和l --c表示調(diào)用函數(shù)時(shí)觸發(fā)hook --r表示函數(shù)返回時(shí)觸發(fā)hook --l表示執(zhí)行新的一行代碼觸發(fā)hook local instruction_limit = 1000 debug.sethook(handle_timeout, "", instruction_limit) local ok, err = xpcall(f, handle_error) if not ok then print("failed to run user script: ", err) end debug.sethook() -- turn off the hooks輸出:
failed to run user script: user script:5: user script too hot: stack traceback: 1.lua:15: in function <1.lua:14> [C]: in function '__add' user script:5: in main chunk [C]: in function 'xpcall' 1.lua:37: in main chunk [C]: at 0x010d5c6ec0