?
本文檔使用 php中文網(wǎng)手冊 發(fā)布
本節(jié)描述 Lua 的 C API,即主機(jī)程序可用于與 Lua 通信的一組 C 函數(shù)。所有的 API 函數(shù)以及相關(guān)的類型和常量都在頭文件中聲明lua.h
。
即使我們使用術(shù)語“功能”,API中的任何工具都可以作為宏提供。除非另有說明,否則所有這些宏都只使用它們的每個(gè)參數(shù)一次(除了第一個(gè)參數(shù),它總是一個(gè)Lua狀態(tài)),所以不會(huì)產(chǎn)生任何隱藏的副作用。
和大多數(shù)C庫一樣,Lua API函數(shù)也不檢查參數(shù)的有效性或一致性。但是,您可以通過使用LUA_USE_APICHECK
定義的宏編譯Lua來更改此行為。
Lua庫是完全可重入的:它沒有全局變量。它將所需的所有信息保存在一個(gè)稱為Lua狀態(tài)的動(dòng)態(tài)結(jié)構(gòu)中。
每個(gè)Lua狀態(tài)都有一個(gè)或多個(gè)線程,這些線程對應(yīng)于獨(dú)立的合作執(zhí)行線。類型lua_State
(盡管它的名稱)是指一個(gè)線程。(間接地,通過線程,它也指與線程關(guān)聯(lián)的Lua狀態(tài)。)
一個(gè)指向線程的指針必須作為第一個(gè)參數(shù)傳遞給庫中的每個(gè)函數(shù),除了lua_newstate
從頭創(chuàng)建一個(gè)Lua狀態(tài)并返回一個(gè)指向新狀態(tài)主線程的指針。
Lua使用虛擬堆棧向C傳遞值和從C傳遞值。此堆棧中的每個(gè)元素表示一個(gè)Lua值(零,數(shù)字,字符串等)。API中的函數(shù)可以通過它們接收的Lua狀態(tài)參數(shù)訪問此堆棧。
每當(dāng)Lua調(diào)用C時(shí),被調(diào)用的函數(shù)都會(huì)得到一個(gè)新的堆棧,它獨(dú)立于先前的堆棧和仍然處于活動(dòng)狀態(tài)的C函數(shù)堆棧。此堆棧最初包含C函數(shù)的任何參數(shù),并且它是C函數(shù)可以存儲(chǔ)臨時(shí)Lua值的位置,并且必須將其結(jié)果返回給調(diào)用方(請參閱參考資料lua_CFunction
)。
為方便起見,API中的大多數(shù)查詢操作都不遵循嚴(yán)格的堆棧規(guī)則。相反,他們可以使用索引引用堆棧中的任何元素:正數(shù)索引表示絕對堆棧位置(從1開始); 負(fù)索引表示相對于堆棧頂部的偏移量。更具體地說,如果堆棧有n個(gè)元素,則索引1表示第一個(gè)元素(即,先推入堆棧的元素),而索引n表示最后一個(gè)元素; 索引-1也表示最后一個(gè)元素(即頂部的元素)和索引-n表示第一個(gè)元素。
當(dāng)您與 Lua API 交互時(shí),您有責(zé)任確保一致性。特別是,你負(fù)責(zé)控制堆棧溢出。您可以使用該函數(shù)lua_checkstack
確保堆棧有足夠的空間來推送新元素。
每當(dāng) Lua 調(diào)用 C 時(shí),它都會(huì)確保堆棧至少有LUA_MINSTACK
額外的插槽空間。LUA_MINSTACK
被定義為20,所以通常你不必?fù)?dān)心堆??臻g,除非你的代碼有循環(huán)將元素推入堆棧。
當(dāng)你調(diào)用沒有固定數(shù)量結(jié)果的Lua函數(shù)時(shí)(參見lua_call
參考資料),Lua確保堆棧為所有結(jié)果提供了足夠的空間,但它不能確保有任何額外的空間。所以,在這樣的通話之后,在推動(dòng)任何東西之前,你應(yīng)該使用lua_checkstack
。
接收堆棧索引的API中的任何函數(shù)只能使用有效的索引或可接受的索引。
有效的索引是指存儲(chǔ)可修改的Lua值的位置的索引。它包含1和堆棧頂部(1 ≤ abs(index) ≤ top
)之間的堆棧索引以及偽代碼,它們代表C代碼可訪問但不在堆棧中的某些位置。偽索引用于訪問注冊表(請參閱第4.5節(jié))和C函數(shù)的高價(jià)值(請參閱第4.4節(jié))。
函數(shù)不需要特定的可變位置,但只有一個(gè)值(例如查詢函數(shù)),可以用可接受的索引來調(diào)用。一個(gè)可接受的索引可以是任何有效的索引,但它也可以是在為該堆棧分配的空間內(nèi)的堆棧頂部之后的任何正數(shù)索引,即,索引直至堆棧大小。(請注意,0永遠(yuǎn)不是可接受的索引。)除非另有說明,API中的函數(shù)可以使用可接受的索引。
查詢堆棧時(shí),可接受的索引用于避免對堆棧頂部進(jìn)行額外的測試。例如,一個(gè)C函數(shù)可以查詢它的第三個(gè)參數(shù),而不需要先檢查是否有第三個(gè)參數(shù),也就是說,不需要檢查3是否是一個(gè)有效的索引。
對于可以用可接受索引調(diào)用的函數(shù),任何無效索引都被視為包含虛擬類型的值LUA_TNONE
,其行為類似于零值。
當(dāng)創(chuàng)建一個(gè)C函數(shù)時(shí),可以將一些值與它關(guān)聯(lián),從而創(chuàng)建一個(gè)C閉包(參見lua_pushcclosure
); 這些值被稱為upvalues,并且只要被調(diào)用就可以被該函數(shù)訪問。
無論何時(shí)調(diào)用C函數(shù),它的upvalues都位于特定的偽索引處。這些偽指數(shù)是由宏產(chǎn)生的lua_upvalueindex
。與函數(shù)關(guān)聯(lián)的第一個(gè)upvalue位于索引處lua_upvalueindex(1)
,依此類推。任何訪問,其中n大于當(dāng)前函數(shù)的upvalues數(shù)(但不大于256,這是一個(gè)加上閉包中upvalues的最大數(shù)),產(chǎn)生一個(gè)可接受但無效的索引。lua_upvalueindex(n)
Lua提供了一個(gè)注冊表,這是一個(gè)預(yù)定義的表,任何C代碼都可以使用它來存儲(chǔ)它需要存儲(chǔ)的任何Lua值。注冊表表格始終位于偽索引處LUA_REGISTRYINDEX
。任何C庫都可以將數(shù)據(jù)存儲(chǔ)到此表中,但必須注意選擇與其他庫所使用的不同的密鑰,以避免沖突。通常,您應(yīng)該使用包含您的庫名稱的字符串或代碼中具有C對象地址的輕型用戶數(shù)據(jù)或由您的代碼創(chuàng)建的任何Lua對象作為關(guān)鍵字。與變量名一樣,以下劃線開頭的字符串鍵后跟大寫字母保留給Lua。
注冊表中的整數(shù)鍵由引用機(jī)制(請參閱luaL_ref
)和一些預(yù)定義的值使用。因此,整數(shù)鍵不能用于其他目的。
當(dāng)你創(chuàng)建一個(gè)新的Lua狀態(tài)時(shí),它的注冊表帶有一些預(yù)定義的值。這些預(yù)定義值使用定義為常量的整數(shù)鍵索引lua.h
。定義了以下常量:
LUA_RIDX_MAINTHREAD
: 在這個(gè)索引處,注冊表具有狀態(tài)的主線程。(主線程是與狀態(tài)一起創(chuàng)建的主線程。)
LUA_RIDX_GLOBALS
: 在這個(gè)索引處,注冊管理機(jī)構(gòu)擁有全球環(huán)境。
在內(nèi)部,Lua 使用C longjmp
工具來處理錯(cuò)誤。(如果將它編譯為C ++,Lua將使用異常; LUAI_THROW
有關(guān)詳細(xì)信息,請?jiān)谠创a中進(jìn)行搜索。)當(dāng)Lua面臨任何錯(cuò)誤(例如內(nèi)存分配錯(cuò)誤或類型錯(cuò)誤)時(shí),會(huì)引發(fā)錯(cuò)誤; 也就是說,它跳遠(yuǎn)了。一個(gè)受保護(hù)的環(huán)境使用setjmp
設(shè)置一個(gè)恢復(fù)點(diǎn); 任何錯(cuò)誤都會(huì)跳轉(zhuǎn)到最近的活動(dòng)恢復(fù)點(diǎn)。
在 C 函數(shù)內(nèi)部,您可以通過調(diào)用來引發(fā)錯(cuò)誤lua_error
。
API中的大多數(shù)函數(shù)都可能引發(fā)錯(cuò)誤,例如由于內(nèi)存分配錯(cuò)誤。每個(gè)函數(shù)的文檔都指出它是否會(huì)引發(fā)錯(cuò)誤。
如果在任何受保護(hù)的環(huán)境之外發(fā)生錯(cuò)誤,Lua會(huì)調(diào)用panic函數(shù)(請參閱lua_atpanic
)然后調(diào)用abort
,從而退出主機(jī)應(yīng)用程序。你的恐慌功能可以通過永不返回來避免這種退出(例如,在Lua之外跳轉(zhuǎn)到自己的恢復(fù)點(diǎn))。
正如其名稱所暗示的,恐慌功能是一種最后的手段。程序應(yīng)該避免它。一般來說,當(dāng)Lua調(diào)用一個(gè)Lua狀態(tài)的C函數(shù)時(shí),它可以在Lua狀態(tài)下做任何事情,因?yàn)樗鼞?yīng)該已經(jīng)被保護(hù)了。但是,當(dāng)C代碼運(yùn)行在其他Lua狀態(tài)(例如,函數(shù)的Lua參數(shù),存儲(chǔ)在注冊表中的Lua狀態(tài)或結(jié)果lua_newthread
)時(shí),它應(yīng)該只在不會(huì)引發(fā)錯(cuò)誤的API調(diào)用中使用它們。
恐慌函數(shù)就像是一個(gè)消息處理程序一樣運(yùn)行(請參閱第2.3節(jié)); 特別是,錯(cuò)誤對象位于堆棧的頂部。但是,堆棧空間不能保證。要推動(dòng)堆棧中的任何內(nèi)容,恐慌功能必須首先檢查可用空間(請參閱第4.2節(jié))。
在內(nèi)部,Lua 使用C longjmp
工具來產(chǎn)生協(xié)程。因此,如果一個(gè)C函數(shù)foo
調(diào)用一個(gè)API函數(shù)并且這個(gè)API函數(shù)產(chǎn)生(通過調(diào)用另一個(gè)產(chǎn)生的函數(shù)直接或間接產(chǎn)生),Lua不能再返回foo
,因?yàn)?code>longjmp它從C棧中移除了它的框架。
為了避免這樣的問題,提出的Lua每當(dāng)它試圖跨越API調(diào)用來產(chǎn)生,除了三個(gè)功能錯(cuò)誤:lua_yieldk
,lua_callk
,和lua_pcallk
。所有這些函數(shù)都會(huì)收到一個(gè)繼續(xù)函數(shù)(作為一個(gè)名為參數(shù)k
)以在yield之后繼續(xù)執(zhí)行。
我們需要設(shè)置一些術(shù)語來解釋延續(xù)。我們有一個(gè)從 Lua 調(diào)用的 C 函數(shù),我們將其稱為原始函數(shù)。這個(gè)原始函數(shù)然后調(diào)用 C API 中的三個(gè)函數(shù)之一,我們將調(diào)用被調(diào)用函數(shù),然后產(chǎn)生當(dāng)前線程。(這可能發(fā)生在被調(diào)用函數(shù)時(shí)lua_yieldk
,或者當(dāng)被調(diào)用函數(shù)是lua_callk
或者lua_pcallk
并且由它們調(diào)用的函數(shù)產(chǎn)生時(shí))。
假設(shè)正在運(yùn)行的線程在執(zhí)行被調(diào)用函數(shù)時(shí)產(chǎn)生。線程恢復(fù)后,它最終將完成運(yùn)行被調(diào)用函數(shù)。但是,被調(diào)用函數(shù)無法返回到原始函數(shù),因?yàn)镃堆棧中的框架被yield損壞了。相反,Lua調(diào)用一個(gè)繼續(xù)函數(shù),它被作為被調(diào)用函數(shù)的參數(shù)給出。顧名思義,繼續(xù)功能應(yīng)該繼續(xù)原有功能的任務(wù)。
作為說明,請考慮以下功能:
int original_function (lua_State *L) { ... /* code 1 */ status = lua_pcall(L, n, m, h); /* calls Lua */ ... /* code 2 */}
現(xiàn)在我們要允許運(yùn)行的Lua代碼lua_pcall
生成。首先,我們可以像這樣重寫我們的功能:
int k (lua_State *L, int status, lua_KContext ctx) { ... /* code 2 */}int original_function (lua_State *L) { ... /* code 1 */ return k(L, lua_pcall(L, n, m, h), ctx);}
在上面的代碼中,新函數(shù)k
是一個(gè)繼承函數(shù)(帶有類型lua_KFunction
),它應(yīng)該完成原函數(shù)在調(diào)用之后所做的所有工作lua_pcall
?,F(xiàn)在,我們必須告訴Lua,k
如果被執(zhí)行的Lua代碼lua_pcall
以某種方式被中斷(錯(cuò)誤或屈服),它必須調(diào)用,所以我們在這里重寫代碼,替換lua_pcall
為lua_pcallk
:
int original_function (lua_State *L) { ... /* code 1 */ return k(L, lua_pcallk(L, n, m, h, ctx2, k), ctx1);}
請注意對延續(xù)的外部顯式調(diào)用:只有在需要時(shí),Lua才會(huì)調(diào)用延續(xù),也就是說,如果發(fā)生錯(cuò)誤或者在yield之后恢復(fù)。如果被調(diào)用的函數(shù)正常返回而沒有屈服,lua_pcallk
(和lua_callk
)也將正常返回。(當(dāng)然,不是在這種情況下調(diào)用延續(xù),而是直接在原始函數(shù)內(nèi)執(zhí)行等效工作。)
除了Lua狀態(tài)之外,continuation函數(shù)還有兩個(gè)其他參數(shù):調(diào)用的最終狀態(tài)和ctx
最初傳遞給的上下文值()lua_pcallk
。(Lua沒有使用這個(gè)上下文值,它只是將原始函數(shù)中的值傳遞給繼續(xù)函數(shù))。因?yàn)?code>lua_pcallk,狀態(tài)與返回的值相同lua_pcallk
,只不過它是LUA_YIELD
在yield之后執(zhí)行的(而不是的LUA_OK
)。對于lua_yieldk
和lua_callk
,LUA_YIELD
當(dāng)Lua稱為延續(xù)時(shí),狀態(tài)總是如此。(對于這兩個(gè)函數(shù),如果發(fā)生錯(cuò)誤,Lua將不會(huì)調(diào)用延續(xù),因?yàn)樗鼈儾粫?huì)處理錯(cuò)誤。)類似地,使用時(shí)lua_callk
,您應(yīng)該LUA_OK
以狀態(tài)的形式調(diào)用延續(xù)函數(shù)。(對于lua_yieldk
,直接調(diào)用繼續(xù)函數(shù)沒有太大意義,因?yàn)?code>lua_yieldk通常不會(huì)返回。)
Lua 將延續(xù)功能看作是原始功能。continuation函數(shù)從原始函數(shù)接收相同的Lua堆棧,處于與被調(diào)用函數(shù)返回時(shí)相同的狀態(tài)。(例如,lua_callk
函數(shù)及其參數(shù)從堆棧中移除并由調(diào)用的結(jié)果替換后)。它也具有相同的upvalues。無論它返回的是由Lua處理,就好像它是原始函數(shù)的返回一樣。
這里我們按字母順序列出C API中的所有函數(shù)和類型。每個(gè)功能都有一個(gè)像這樣的指示器:
第一個(gè)字段o
是函數(shù)從堆棧中彈出多少個(gè)元素。第二個(gè)字段p
是函數(shù)將多少元素壓入堆棧。(任何函數(shù)在彈出其參數(shù)后總是推送其結(jié)果。)表單中的字段x|y
表示該函數(shù)可以推送(或彈出)x
或y
元素,具體取決于具體情況; 一個(gè)詢問標(biāo)記“ ?
'意味著我們無法通過僅查看其參數(shù)(例如,它們可能取決于棧上的內(nèi)容)來知道該函數(shù)彈出/推動(dòng)多少個(gè)元素。第三個(gè)字段x
告訴函數(shù)是否會(huì)引發(fā)錯(cuò)誤:' -
'表示函數(shù)不會(huì)引發(fā)任何錯(cuò)誤; ' m
'表示該函數(shù)可能引發(fā)運(yùn)行元方法的內(nèi)存__gc
不足錯(cuò)誤和錯(cuò)誤; “e
'表示該函數(shù)可能引發(fā)任何錯(cuò)誤(它可以直接或通過元方法運(yùn)行任意Lua代碼); ' v
'表示該功能可能會(huì)故意提出錯(cuò)誤。
lua_absindex
-0, +0, –int lua_absindex (lua_State *L, int idx);
將可接受的索引idx
轉(zhuǎn)換為等價(jià)的絕對索引(即,不依賴于堆棧頂部的索引)。
lua_Alloc
typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
Lua 狀態(tài)使用的內(nèi)存分配函數(shù)的類型。分配器函數(shù)必須提供類似的功能realloc
,但不完全一樣。它的論點(diǎn)是ud
,一個(gè)不透明的指針傳遞給lua_newstate
; ptr
,一個(gè)指向被分配/重新分配/釋放的塊的指針; osize
,塊的原始大小或有關(guān)正在分配什么的代碼; 和nsize
塊的新大小。
如果ptr
不是NULL
,osize
指定塊的大小ptr
,即分配或重新分配時(shí)給定的大小。
如果ptr
是NULL
,osize
編碼Lua是分配類型的對象。osize
是任何的LUA_TSTRING
,LUA_TTABLE
,LUA_TFUNCTION
,LUA_TUSERDATA
,或LUA_TTHREAD
當(dāng)(且僅當(dāng))Lua是創(chuàng)建該類型的新對象。何時(shí)osize
有其他價(jià)值,Lua會(huì)為其他內(nèi)容分配內(nèi)存。
Lua 假定分配器函數(shù)具有以下行為:
當(dāng)nsize
為零時(shí),分配器必須表現(xiàn)得像free
并返回NULL
。
當(dāng)nsize
不為零時(shí),分配器必須表現(xiàn)得像realloc
。NULL
當(dāng)且僅當(dāng)它不能滿足請求時(shí),分配器才返回。Lua認(rèn)為分配器從不失敗時(shí)osize >= nsize
。
這是分配器函數(shù)的一個(gè)簡單實(shí)現(xiàn)。它被用于輔助庫中luaL_newstate
。
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { (void)ud; (void)osize; /* not used */ if (nsize == 0) { free(ptr); return NULL; } else return realloc(ptr, nsize);}
請注意,標(biāo)準(zhǔn)C確保free(NULL)
沒有任何影響,這realloc(NULL,size)
相當(dāng)于malloc(size)
。此代碼假定realloc
縮小塊時(shí)不會(huì)失敗。(雖然標(biāo)準(zhǔn)C不能確保這種行為,但這似乎是一個(gè)安全的假設(shè)。)
lua_arith
-(2|1), +1, evoid lua_arith (lua_State *L, int op);
對堆棧頂部的兩個(gè)值(或一個(gè),如果是否定)執(zhí)行算術(shù)運(yùn)算或按位運(yùn)算,頂部的值為第二個(gè)操作數(shù),彈出這些值并推送操作結(jié)果。該函數(shù)遵循相應(yīng)的Lua運(yùn)算符的語義(也就是說,它可能調(diào)用metamethods)。
值op
必須是以下常量之一:
LUA_OPADD
: 執(zhí)行addition(+
)
LUA_OPSUB
: 執(zhí)行減法(-
)
LUA_OPMUL
: 執(zhí)行乘法(*
)
LUA_OPDIV
: 執(zhí)行浮動(dòng)除法(/
)
LUA_OPIDIV
: 執(zhí)行地板劃分(//
)
LUA_OPMOD
: 執(zhí)行modulo(%
)
LUA_OPPOW
: 執(zhí)行冪(^
)
LUA_OPUNM
: 執(zhí)行數(shù)學(xué)否定(一元-
)
LUA_OPBNOT
: 按位執(zhí)行NOT(~
)
LUA_OPBAND
: 執(zhí)行按位AND(&
)
LUA_OPBOR
: 執(zhí)行按位OR(|
)
LUA_OPBXOR
: 執(zhí)行按位異或(~
)
LUA_OPSHL
: 執(zhí)行左移(<<
)
LUA_OPSHR
: 執(zhí)行右移(>>
)
lua_atpanic
-0, +0, –lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);
設(shè)置一個(gè)新的恐慌函數(shù)并返回舊函數(shù)(參見§4.6)。
lua_call
-(nargs+1), +nresults, evoid lua_call (lua_State *L, int nargs, int nresults);
調(diào)用一個(gè)函數(shù)。
要調(diào)用函數(shù),您必須使用以下協(xié)議:首先,要調(diào)用的函數(shù)被壓入堆棧; 然后,函數(shù)的參數(shù)按直接順序推入; 也就是說,第一個(gè)參數(shù)是先推。最后你打電話lua_call
; nargs
是您推入堆棧的參數(shù)的數(shù)量。函數(shù)被調(diào)用時(shí),所有參數(shù)和函數(shù)值都從堆棧彈出。當(dāng)函數(shù)返回時(shí),函數(shù)結(jié)果被壓入堆棧。結(jié)果的數(shù)量調(diào)整為nresults
,除非nresults
是LUA_MULTRET
。在這種情況下,該功能的所有結(jié)果都會(huì)被推送; Lua注意返回值適合堆??臻g,但它不能確保堆棧中有任何額外的空間。函數(shù)結(jié)果按直接順序壓入堆棧(第一個(gè)結(jié)果先被壓入),這樣在調(diào)用后最后的結(jié)果位于堆棧的頂部。
被調(diào)用函數(shù)中的任何錯(cuò)誤都向上傳播(使用a longjmp
)。
以下示例顯示主機(jī)程序如何執(zhí)行與此Lua代碼等效的操作:
a = f("how", t.x, 14)
這里是在C:
lua_getglobal(L, "f"); /* function to be called */lua_pushliteral(L, "how"); /* 1st argument */lua_getglobal(L, "t"); /* table to be indexed */lua_getfield(L, -1, "x"); /* push result of t.x (2nd arg) */lua_remove(L, -2); /* remove 't' from the stack */lua_pushinteger(L, 14); /* 3rd argument */lua_call(L, 3, 1); /* call 'f' with 3 arguments and 1 result */lua_setglobal(L, "a"); /* set global 'a' */
請注意,上面的代碼是平衡的:在最后,堆?;謴?fù)到原來的配置。這被認(rèn)為是很好的編程習(xí)慣。
lua_callk
-(nargs + 1), +nresults, evoid lua_callk (lua_State *L, int nargs, int nresults, lua_KContext ctx, lua_KFunction k);
這個(gè)函數(shù)的行為完全一樣lua_call
,但允許被調(diào)用函數(shù)產(chǎn)生(見§4.7)。
lua_CFunction
typedef int (*lua_CFunction) (lua_State *L);
輸入C函數(shù)。
為了與Lua進(jìn)行正確的通信,C函數(shù)必須使用以下協(xié)議,該協(xié)議定義了傳遞參數(shù)和結(jié)果的方式:C函數(shù)以直接順序(第一個(gè)參數(shù)先被壓入)從Lua堆棧中直接接收它的參數(shù)。所以,當(dāng)函數(shù)啟動(dòng)時(shí),lua_gettop(L)
返回函數(shù)收到的參數(shù)個(gè)數(shù)。第一個(gè)參數(shù)(如果有)位于索引1,最后一個(gè)參數(shù)位于索引處lua_gettop(L)
。為了將值返回給Lua,C函數(shù)只是將它們按順序推送到堆棧中(第一個(gè)結(jié)果被首先推送),然后返回結(jié)果的數(shù)目。結(jié)果下方的任何其他值將被Lua正確丟棄。像Lua函數(shù)一樣,Lua調(diào)用的C函數(shù)也可以返回許多結(jié)果。
作為一個(gè)例子,下面的函數(shù)接收可變數(shù)量的數(shù)字參數(shù)并返回它們的平均值和它們的總和:
static int foo (lua_State *L) { int n = lua_gettop(L); /* number of arguments */ lua_Number sum = 0.0; int i; for (i = 1; i <= n; i++) { if (!lua_isnumber(L, i)) { lua_pushliteral(L, "incorrect argument"); lua_error(L); } sum += lua_tonumber(L, i); } lua_pushnumber(L, sum/n); /* first result */ lua_pushnumber(L, sum); /* second result */ return 2; /* number of results */}
lua_checkstack
-0, +0, –int lua_checkstack (lua_State *L, int n);
確保堆棧至少有n
額外的插槽空間(也就是說,您可以安全地將n
值放入其中)。如果它不能滿足請求,它會(huì)返回false,因?yàn)樗鼤?huì)導(dǎo)致堆棧大于一個(gè)固定的最大大?。ㄍǔV辽儆袔浊€(gè)元素),或者因?yàn)樗荒転轭~外的空間分配內(nèi)存。這個(gè)函數(shù)永遠(yuǎn)不會(huì)縮小堆棧; 如果堆棧已經(jīng)有空間用于額外的插槽,則保持不變。
lua_close
-0, +0, –void lua_close (lua_State *L);
銷毀給定Lua狀態(tài)下的所有對象(調(diào)用相應(yīng)的垃圾收集元方法,如果有的話)并釋放此狀態(tài)使用的所有動(dòng)態(tài)內(nèi)存。在多個(gè)平臺上,您可能不需要調(diào)用此函數(shù),因?yàn)樗匈Y源在主機(jī)程序結(jié)束時(shí)自然釋放。另一方面,創(chuàng)建多個(gè)狀態(tài)的長時(shí)間運(yùn)行的程序(如守護(hù)程序或Web服務(wù)器)可能需要在不需要時(shí)立即關(guān)閉狀態(tài)。
lua_compare
-0, +0, eint lua_compare (lua_State *L, int index1, int index2, int op);
比較兩個(gè) Lua 值。如果在索引中的值,則返回1 index1
滿足op
時(shí)與索引的值進(jìn)行比較index2
,以下對應(yīng)的Lua操作符的語義(即,它可以調(diào)用元方法)。否則返回0.如果任何索引無效,則返回0。
值op
必須是以下常量之一:
LUA_OPEQ
: 比較平等(==
)
LUA_OPLT
: 比較小于(<
)
LUA_OPLE
: 比較少或相等(<=
)
lua_concat
-n, +1, evoid lua_concat (lua_State *L, int n);
連接n
堆棧頂部的值,彈出它們,并將結(jié)果保留在頂部。如果n
是1,結(jié)果是堆棧中的單個(gè)值(即該函數(shù)不執(zhí)行任何操作); 如果n
是0,結(jié)果是空字符串。按照Lua的通常語義執(zhí)行連接(請參閱第3.4.6節(jié))。
lua_copy
-0, +0, –void lua_copy (lua_State *L, int fromidx, int toidx);
將索引處的元素復(fù)制fromidx
到有效索引中toidx
,替換該位置處的值。其他位置的值不受影響。
lua_createtable
-0, +1, mvoid lua_createtable (lua_State *L, int narr, int nrec);
創(chuàng)建一個(gè)新的空表并將其壓入堆棧。參數(shù)narr
是提示表格將具有多少個(gè)元素作為序列; 參數(shù)nrec
是表格中將包含多少其他元素的提示。Lua可以使用這些提示為新表預(yù)分配內(nèi)存。當(dāng)您事先知道表中有多少元素時(shí),此預(yù)分配對性能很有用。否則,您可以使用該功能lua_newtable
。
lua_dump
-0, +0, –int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip);
將函數(shù)轉(zhuǎn)儲(chǔ)為二進(jìn)制塊。在堆棧頂部接收一個(gè)Lua函數(shù),并生成一個(gè)二進(jìn)制塊,如果再次加載,則會(huì)生成一個(gè)等同于所轉(zhuǎn)儲(chǔ)的函數(shù)的函數(shù)。由于它產(chǎn)生塊的一部分,lua_dump
調(diào)用函數(shù)writer
(參見lua_Writer
)與給定data
的寫入它們。
如果strip
為真,則二進(jìn)制表示可能不包含有關(guān)該函數(shù)的所有調(diào)試信息,以節(jié)省空間。
返回的值是上次調(diào)用writer時(shí)返回的錯(cuò)誤代碼; 0表示沒有錯(cuò)誤。
該函數(shù)不會(huì)從堆棧中彈出Lua函數(shù)。
lua_error
-1, +0, vint lua_error (lua_State *L);
生成一個(gè) Lua 錯(cuò)誤,使用堆棧頂部的值作為錯(cuò)誤對象。此功能會(huì)跳遠(yuǎn),因此不會(huì)返回(請參閱luaL_error
)。
lua_gc
-0, +0, mint lua_gc (lua_State *L, int what, int data);
控制垃圾收集器。
根據(jù)參數(shù)的值,該功能執(zhí)行幾項(xiàng)任務(wù)what
:
LUA_GCSTOP
:停止垃圾收集器。
LUA_GCRESTART
:重新啟動(dòng)垃圾收集器。
LUA_GCCOLLECT
:執(zhí)行完整的垃圾收集循環(huán)。
LUA_GCCOUNT
:返回Lua使用的當(dāng)前內(nèi)存量(以KB為單位)。
LUA_GCCOUNTB
:將由Lua使用的當(dāng)前內(nèi)存字節(jié)數(shù)除以1024的余數(shù)。
LUA_GCSTEP
:執(zhí)行垃圾收集的增量步驟。
LUA_GCSETPAUSE
:設(shè)置data
為收集器暫停的新值(請參閱§2.5)并返回暫停的前一個(gè)值。
LUA_GCSETSTEPMUL
:設(shè)置data
為收集器步驟乘數(shù)的新值(見§2.5)并返回步驟乘數(shù)的前一個(gè)值。
LUA_GCISRUNNING
:返回一個(gè)布爾值,告訴收集器是否正在運(yùn)行(即不停止)。
有關(guān)這些選項(xiàng)的更多詳細(xì)信息,請參閱collectgarbage
。
lua_getallocf
-0, +0, –lua_Alloc lua_getallocf (lua_State *L, void **ud);
返回給定狀態(tài)的內(nèi)存分配函數(shù)。如果ud
不是NULL
,Lua將存儲(chǔ)在*ud
設(shè)置了內(nèi)存分配器函數(shù)時(shí)給出的不透明指針上。
lua_getfield
-0, +1, eint lua_getfield (lua_State *L, int index, const char *k);
將值推到堆棧上t[k]
,其中t
是給定索引處的值。和Lua一樣,這個(gè)函數(shù)可能觸發(fā)“索引”事件的元方法(見§2.4)。
返回推送值的類型。
lua_getextraspace
-0, +0, –void *lua_getextraspace (lua_State *L);
返回指向與給定的Lua狀態(tài)相關(guān)的原始內(nèi)存區(qū)域的指針。應(yīng)用程序可以將此區(qū)域用于任何目的; Lua沒有使用它。
每個(gè)新線程都使用主線程區(qū)域的副本初始化該區(qū)域。
默認(rèn)情況下,該區(qū)域的指針大小為void,但您可以使用該區(qū)域的不同大小重新編譯Lua。(參見LUA_EXTRASPACE
在luaconf.h
)。
lua_getglobal
-0, +1, eint lua_getglobal (lua_State *L, const char *name);
將全局值推向堆棧name
。返回該值的類型。
lua_geti
-0, +1, eint lua_geti (lua_State *L, int index, lua_Integer i);
將值推到堆棧上t[i]
,其中t
是給定索引處的值。和Lua一樣,這個(gè)函數(shù)可能觸發(fā)“索引”事件的元方法(見§2.4)。
返回推送值的類型。
lua_getmetatable
-0, +(0|1), –int lua_getmetatable (lua_State *L, int index);
如果給定索引處的值具有metatable,則該函數(shù)會(huì)將metatable推送到堆棧并返回1.否則,該函數(shù)將返回0,并且不會(huì)在堆棧上執(zhí)行任何操作。
lua_gettable
-1, +1, eint lua_gettable (lua_State *L, int index);
將值推入堆棧t[k]
,其中t
是給定索引k
處的值,并且是堆棧頂部的值。
該函數(shù)從堆棧彈出鍵,將結(jié)果值推到原位。和Lua一樣,這個(gè)函數(shù)可能觸發(fā)“索引”事件的元方法(見§2.4)。
返回推送值的類型。
lua_gettop
-0, +0, –int lua_gettop (lua_State *L);
返回堆棧中頂層元素的索引。因?yàn)樗饕龔?開始,所以這個(gè)結(jié)果等于堆棧中元素的數(shù)量; 特別是,0表示空棧。
lua_getuservalue
-0, +1, –int lua_getuservalue (lua_State *L, int index);
將與給定索引處的完整用戶數(shù)據(jù)關(guān)聯(lián)的Lua值推入堆棧。
返回推送值的類型。
lua_insert
-1, +1, –void lua_insert (lua_State *L, int index);
將頂層元素移動(dòng)到給定的有效索引中,將該索引上方的元素向上移動(dòng)以打開空間。該函數(shù)不能用偽索引調(diào)用,因?yàn)閭嗡饕皇菍?shí)際的堆棧位置。
lua_Integer
typedef ... lua_Integer;
Lua中的整數(shù)類型。
默認(rèn)情況下,這個(gè)類型是long long
(通常是一個(gè)64位二補(bǔ)數(shù)整數(shù)),但可以改為long
或int
(通常是一個(gè)32位二補(bǔ)數(shù)整數(shù))。(參見LUA_INT_TYPE
在luaconf.h
)。
Lua還定義了常量,LUA_MININTEGER
并LUA_MAXINTEGER
用最小值和最大值適合這種類型。
lua_isboolean
-0, +0, –int lua_isboolean (lua_State *L, int index);
如果給定索引處的值是布爾值,則返回1,否則返回0。
lua_iscfunction
-0, +0, –int lua_iscfunction (lua_State *L, int index);
如果給定索引處的值是C函數(shù),則返回1,否則返回0。
lua_isfunction
-0, +0, –int lua_isfunction (lua_State *L, int index);
如果給定索引處的值是一個(gè)函數(shù)(C或Lua),則返回1,否則返回0。
lua_isinteger
-0, +0, –int lua_isinteger (lua_State *L, int index);
如果給定索引處的值是整數(shù)(即,該值是數(shù)字并以整數(shù)表示),則返回1,否則返回0。
lua_islightuserdata
-0, +0, –int lua_islightuserdata (lua_State *L, int index);
如果給定索引處的值為 light userdata,則返回1,否則返回0。
lua_isnil
-0, +0, –int lua_isnil (lua_State *L, int index);
如果給定索引處的值為零,則返回1,否則返回0。
lua_isnone
-0, +0, –int lua_isnone (lua_State *L, int index);
如果給定索引無效,則返回1,否則返回0。
lua_isnoneornil
-0, +0, –int lua_isnoneornil (lua_State *L, int index);
如果給定索引無效或者此索引處的值為零,則返回1;否則返回0。
lua_isnumber
-0, +0, –int lua_isnumber (lua_State *L, int index);
如果給定索引處的值是可轉(zhuǎn)換為數(shù)字的數(shù)字或字符串,則返回1,否則返回0。
lua_isstring
-0, +0, –int lua_isstring (lua_State *L, int index);
Returns 1 if the value at the given index is a string or a number (which is always convertible to a string), and 0 otherwise.
lua_istable
-0, +0, –int lua_istable (lua_State *L, int index);
Returns 1 if the value at the given index is a table, and 0 otherwise.
lua_isthread
-0, +0, –int lua_isthread (lua_State *L, int index);
Returns 1 if the value at the given index is a thread, and 0 otherwise.
lua_isuserdata
-0, +0, –int lua_isuserdata (lua_State *L, int index);
Returns 1 if the value at the given index is a userdata (either full or light), and 0 otherwise.
lua_isyieldable
-0, +0, –int lua_isyieldable (lua_State *L);
Returns 1 if the given coroutine can yield, and 0 otherwise.
lua_KContext
typedef ... lua_KContext;
The type for continuation-function contexts. It must be a numeric type. This type is defined as intptr_t
when intptr_t
is available, so that it can store pointers too. Otherwise, it is defined as ptrdiff_t
.
lua_KFunction
typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx);
Type for continuation functions (see §4.7).
lua_len
-0, +1, evoid lua_len (lua_State *L, int index);
Returns the length of the value at the given index. It is equivalent to the '#
' operator in Lua (see §3.4.7) and may trigger a metamethod for the "length" event (see §2.4). The result is pushed on the stack.
lua_load
-0, +1, –int lua_load (lua_State *L, lua_Reader reader, void *data, const char *chunkname, const char *mode);
Loads a Lua chunk without running it. If there are no errors, lua_load
pushes the compiled chunk as a Lua function on top of the stack. Otherwise, it pushes an error message.
The return values of lua_load
are:
LUA_OK
: no errors;
LUA_ERRSYNTAX
: syntax error during precompilation;
LUA_ERRMEM
: memory allocation (out-of-memory) error;
LUA_ERRGCMM
: error while running a __gc
metamethod. (This error has no relation with the chunk being loaded. It is generated by the garbage collector.)
The lua_load
function uses a user-supplied reader
function to read the chunk (see lua_Reader
). The data
argument is an opaque value passed to the reader function.
The chunkname
argument gives a name to the chunk, which is used for error messages and in debug information (see §4.9).
lua_load
automatically detects whether the chunk is text or binary and loads it accordingly (see program luac
). The string mode
works as in function load
, with the addition that a NULL
value is equivalent to the string "bt
".
lua_load
uses the stack internally, so the reader function must always leave the stack unmodified when returning.
If the resulting function has upvalues, its first upvalue is set to the value of the global environment stored at index LUA_RIDX_GLOBALS
in the registry (see §4.5). When loading main chunks, this upvalue will be the _ENV
variable (see §2.2). Other upvalues are initialized with nil.
lua_newstate
-0, +0, –lua_State *lua_newstate (lua_Alloc f, void *ud);
Creates a new thread running in a new, independent state. Returns NULL
if it cannot create the thread or the state (due to lack of memory). The argument f
is the allocator function; Lua does all memory allocation for this state through this function (see lua_Alloc
). The second argument, ud
, is an opaque pointer that Lua passes to the allocator in every call.
lua_newtable
-0, +1, mvoid lua_newtable (lua_State *L);
Creates a new empty table and pushes it onto the stack. It is equivalent to lua_createtable(L, 0, 0)
.
lua_newthread
-0, +1, mlua_State *lua_newthread (lua_State *L);
Creates a new thread, pushes it on the stack, and returns a pointer to a lua_State
that represents this new thread. The new thread returned by this function shares with the original thread its global environment, but has an independent execution stack.
There is no explicit function to close or to destroy a thread. Threads are subject to garbage collection, like any Lua object.
lua_newuserdata
-0, +1, mvoid *lua_newuserdata (lua_State *L, size_t size);
This function allocates a new block of memory with the given size, pushes onto the stack a new full userdata with the block address, and returns this address. The host program can freely use this memory.
lua_next
-1, +(2|0), eint lua_next (lua_State *L, int index);
Pops a key from the stack, and pushes a key–value pair from the table at the given index (the "next" pair after the given key). If there are no more elements in the table, then lua_next
returns 0 (and pushes nothing).
A typical traversal looks like this:
/* table is in the stack at index 't' */lua_pushnil(L); /* first key */while (lua_next(L, t) != 0) { /* uses 'key' (at index -2) and 'value' (at index -1) */ printf("%s - %s\n", lua_typename(L, lua_type(L, -2)), lua_typename(L, lua_type(L, -1))); /* removes 'value'; keeps 'key' for next iteration */ lua_pop(L, 1);}
While traversing a table, do not call lua_tolstring
directly on a key, unless you know that the key is actually a string. Recall that lua_tolstring
may change the value at the given index; this confuses the next call to lua_next
.
See function next
for the caveats of modifying the table during its traversal.
lua_Number
typedef ... lua_Number;
The type of floats in Lua.
By default this type is double, but that can be changed to a single float or a long double. (See LUA_FLOAT_TYPE
in luaconf.h
.)
lua_numbertointeger
int lua_numbertointeger (lua_Number n, lua_Integer *p);
Converts a Lua float to a Lua integer. This macro assumes that n
has an integral value. If that value is within the range of Lua integers, it is converted to an integer and assigned to *p
. The macro results in a boolean indicating whether the conversion was successful. (Note that this range test can be tricky to do correctly without this macro, due to roundings.)
This macro may evaluate its arguments more than once.
lua_pcall
-(nargs + 1), +(nresults|1), –int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);
Calls a function in protected mode.
Both nargs
and nresults
have the same meaning as in lua_call
. If there are no errors during the call, lua_pcall
behaves exactly like lua_call
. However, if there is any error, lua_pcall
catches it, pushes a single value on the stack (the error object), and returns an error code. Like lua_call
, lua_pcall
always removes the function and its arguments from the stack.
If msgh
is 0, then the error object returned on the stack is exactly the original error object. Otherwise, msgh
is the stack index of a message handler. (This index cannot be a pseudo-index.) In case of runtime errors, this function will be called with the error object and its return value will be the object returned on the stack by lua_pcall
.
Typically, the message handler is used to add more debug information to the error object, such as a stack traceback. Such information cannot be gathered after the return of lua_pcall
, since by then the stack has unwound.
The lua_pcall
function returns one of the following constants (defined in lua.h
):
LUA_OK
(0): success.
LUA_ERRRUN
: a runtime error.
LUA_ERRMEM
: memory allocation error. For such errors, Lua does not call the message handler.
LUA_ERRERR
: error while running the message handler.
LUA_ERRGCMM
: error while running a __gc
metamethod. For such errors, Lua does not call the message handler (as this kind of error typically has no relation with the function being called).
lua_pcallk
-(nargs + 1), +(nresults|1), –int lua_pcallk (lua_State *L, int nargs, int nresults, int msgh, lua_KContext ctx, lua_KFunction k);
This function behaves exactly like lua_pcall
, but allows the called function to yield (see §4.7).
lua_pop
-n, +0, –void lua_pop (lua_State *L, int n);
Pops n
elements from the stack.
lua_pushboolean
-0, +1, –void lua_pushboolean (lua_State *L, int b);
Pushes a boolean value with value b
onto the stack.
lua_pushcclosure
-n, +1, mvoid lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
Pushes a new C closure onto the stack.
When a C function is created, it is possible to associate some values with it, thus creating a C closure (see §4.4); these values are then accessible to the function whenever it is called. To associate values with a C function, first these values must be pushed onto the stack (when there are multiple values, the first value is pushed first). Then lua_pushcclosure
is called to create and push the C function onto the stack, with the argument n
telling how many values will be associated with the function. lua_pushcclosure
also pops these values from the stack.
The maximum value for n
is 255.
When n
is zero, this function creates a light C function, which is just a pointer to the C function. In that case, it never raises a memory error.
lua_pushcfunction
-0, +1, –void lua_pushcfunction (lua_State *L, lua_CFunction f);
Pushes a C function onto the stack. This function receives a pointer to a C function and pushes onto the stack a Lua value of type function
that, when called, invokes the corresponding C function.
Any function to be callable by Lua must follow the correct protocol to receive its parameters and return its results (see lua_CFunction
).
lua_pushfstring
-0, +1, econst char *lua_pushfstring (lua_State *L, const char *fmt, ...);
Pushes onto the stack a formatted string and returns a pointer to this string. It is similar to the ISO C function sprintf
, but has some important differences:
You do not have to allocate space for the result: the result is a Lua string and Lua takes care of memory allocation (and deallocation, through garbage collection).
The conversion specifiers are quite restricted. There are no flags, widths, or precisions. The conversion specifiers can only be '%%
' (inserts the character '%
'), '%s
' (inserts a zero-terminated string, with no size restrictions), '%f
' (inserts a lua_Number
), '%I
' (inserts a lua_Integer
), '%p
' (inserts a pointer as a hexadecimal numeral), '%d
' (inserts an int
), '%c
' (inserts an int
as a one-byte character), and '%U
' (inserts a long int
as a UTF-8 byte sequence).
Unlike other push functions, this function checks for the stack space it needs, including the slot for its result.
lua_pushglobaltable
-0, +1, –void lua_pushglobaltable (lua_State *L);
Pushes the global environment onto the stack.
lua_pushinteger
-0, +1, –void lua_pushinteger (lua_State *L, lua_Integer n);
Pushes an integer with value n
onto the stack.
lua_pushlightuserdata
-0, +1, –void lua_pushlightuserdata (lua_State *L, void *p);
將輕量級用戶數(shù)據(jù)推入堆棧。
用戶數(shù)據(jù)表示 Lua 中的C值。一個(gè)light userdata代表一個(gè)指針,一個(gè)void*
。它是一個(gè)值(像一個(gè)數(shù)字):你不創(chuàng)建它,它沒有單獨(dú)的metatable,它不被收集(因?yàn)樗鼜膩頉]有被創(chuàng)建)。輕的用戶數(shù)據(jù)等于具有相同C地址的“任何”輕用戶數(shù)據(jù)。
lua_pushliteral
-0, +1, mconst char *lua_pushliteral (lua_State *L, const char *s);
這個(gè)宏相當(dāng)于lua_pushstring
,但只有在s
是文字字符串時(shí)才應(yīng)該使用。
lua_pushlstring
-0, +1, mconst char *lua_pushlstring (lua_State *L, const char *s, size_t len);
將s
尺寸指向的字符串len
推入堆棧。Lua創(chuàng)建(或重新使用)給定字符串的內(nèi)部副本,因此s
可以在函數(shù)返回后立即釋放或重用內(nèi)存。該字符串可以包含任何二進(jìn)制數(shù)據(jù),包括嵌入的零。
返回一個(gè)指向字符串內(nèi)部副本的指針。
lua_pushnil
-0, +1, –void lua_pushnil (lua_State *L);
將一個(gè)零值推入堆棧。
lua_pushnumber
-0, +1, –void lua_pushnumber (lua_State *L, lua_Number n);
將具有值的浮動(dòng)值n
推入堆棧。
lua_pushstring
-0, +1, mconst char *lua_pushstring (lua_State *L, const char *s);
將指向的由零結(jié)尾的字符串s
推入堆棧。Lua創(chuàng)建(或重新使用)給定字符串的內(nèi)部副本,因此s
可以在函數(shù)返回后立即釋放或重用內(nèi)存。
返回一個(gè)指向字符串內(nèi)部副本的指針。
如果s
是NULL
,推零和返回NULL
。
lua_pushthread
-0, +1, –int lua_pushthread (lua_State *L);
將由代表的線程L
推入堆棧。如果此線程是其狀態(tài)的主線程,則返回1。
lua_pushvalue
-0, +1, –void lua_pushvalue (lua_State *L, int index);
將給定索引處元素的副本壓入堆棧。
lua_pushvfstring
-0, +1, mconst char *lua_pushvfstring (lua_State *L, const char *fmt, va_list argp);
相當(dāng)于lua_pushfstring
,除了它接收一個(gè)va_list
而不是可變數(shù)量的參數(shù)。
lua_rawequal
-0, +0, –int lua_rawequal (lua_State *L, int index1, int index2);
如果在索引的兩個(gè)值,則返回1 index1
和index2
是原始地等于(即,不調(diào)用__eq
元方法)。否則返回0.如果任何索引無效,則返回0。
lua_rawget
-1, +1, –int lua_rawget (lua_State *L, int index);
類似于lua_gettable
,但沒有原始訪問(即沒有metamethods)。
lua_rawgeti
-0, +1, –int lua_rawgeti (lua_State *L, int index, lua_Integer n);
將值推入堆棧t[n]
,其中t
是給定索引處的表。訪問是原始的,也就是說,它不會(huì)調(diào)用__index
metamethod。
返回推送值的類型。
lua_rawgetp
-0, +1, –int lua_rawgetp (lua_State *L, int index, const void *p);
將值推入堆棧t[k]
,其中t
是給定索引處的表,并且k
是指示p
為輕型用戶數(shù)據(jù)的指針。訪問是生的; 也就是說,它不會(huì)調(diào)用__index
metamethod。
返回推送值的類型。
lua_rawlen
-0, +0, –size_t lua_rawlen (lua_State *L, int index);
返回給定索引處的值的原始“長度”:對于字符串,這是字符串長度; 對于表,這是長度運(yùn)算符(' #
')的結(jié)果,沒有metamethods; 對于用戶數(shù)據(jù),這是為用戶數(shù)據(jù)分配的內(nèi)存塊的大小; 對于其他值,它是0。
lua_rawset
-2, +0, mvoid lua_rawset (lua_State *L, int index);
類似lua_settable
,但做了原始分配(即沒有metamethods)。
lua_rawseti
-1, +0, mvoid lua_rawseti (lua_State *L, int index, lua_Integer i);
是否相當(dāng)于t[i] = v
,其中t
是表中的給定索引處并且v
是在堆棧的頂部的值。
該函數(shù)從堆棧中彈出值。該任務(wù)是原始的,也就是說,它不會(huì)調(diào)用__newindex
metamethod。
lua_rawsetp
-1, +0, mvoid lua_rawsetp (lua_State *L, int index, const void *p);
是否等價(jià)于給定索引處的表在t[p] = v
哪里t
被p
編碼為輕型用戶數(shù)據(jù),并且v
是堆棧頂部的值。
該函數(shù)從堆棧中彈出值。該任務(wù)是原始的,也就是說,它不會(huì)調(diào)用__newindex
metamethod。
lua_Reader
typedef const char * (*lua_Reader) (lua_State *L, void *data, size_t *size);
讀者功能使用lua_load
。每次它需要另一塊時(shí),lua_load
調(diào)用讀者,傳遞其data
參數(shù)。閱讀器必須返回一個(gè)指向一塊內(nèi)存塊的指針并將其設(shè)置size
為塊大小。該塊必須存在,直到再次調(diào)用閱讀器功能。為了表示塊的結(jié)束,閱讀器必須返回NULL
或設(shè)置size
為零。閱讀器功能可以返回任何大于零的尺寸的部分。
lua_register
-0, +0, evoid lua_register (lua_State *L, const char *name, lua_CFunction f);
將C函數(shù)設(shè)置f
為全局的新值name
。它被定義為一個(gè)宏:
#define lua_register(L,n,f) \ (lua_pushcfunction(L, f), lua_setglobal(L, n))
lua_remove
-1, +0, –void lua_remove (lua_State *L, int index);
刪除給定有效索引處的元素,向下移動(dòng)此索引上方的元素以填補(bǔ)空白。該函數(shù)不能用偽索引調(diào)用,因?yàn)閭嗡饕皇菍?shí)際的堆棧位置。
lua_replace
-1, +0, –void lua_replace (lua_State *L, int index);
將頂層元素移動(dòng)到給定的有效索引中,而不移動(dòng)任何元素(因此替換該給定索引處的值),然后彈出頂層元素。
lua_resume
-?, +?, –int lua_resume (lua_State *L, lua_State *from, int nargs);
在給定的線程中啟動(dòng)并恢復(fù)一個(gè)協(xié)程L
。
要啟動(dòng)一個(gè)協(xié)程,你可以將線程堆棧的主函數(shù)加上任何參數(shù); 然后你打電話lua_resume
,nargs
作為參數(shù)的數(shù)量。這個(gè)調(diào)用在協(xié)程暫?;蚪Y(jié)束執(zhí)行時(shí)返回。當(dāng)它返回時(shí),堆棧包含傳遞給的lua_yield
所有值,或者正文函數(shù)返回的所有值。lua_resume
返回LUA_YIELD
如果協(xié)程產(chǎn)率,LUA_OK
如果所述協(xié)程完成它的執(zhí)行沒有錯(cuò)誤,或在錯(cuò)誤的情況下的錯(cuò)誤代碼(參見lua_pcall
)。
如果發(fā)生錯(cuò)誤,堆棧不會(huì)解開,因此您可以使用調(diào)試 API 覆蓋它。錯(cuò)誤對象位于堆棧的頂部。
要恢復(fù)一個(gè)協(xié)程,你可以從最后一個(gè)結(jié)果中刪除任何結(jié)果lua_yield
,只將其作為結(jié)果傳入的值放在棧上yield
,然后調(diào)用lua_resume
。
該參數(shù)from
表示正在恢復(fù)的協(xié)程L
。如果沒有這樣的協(xié)程,這個(gè)參數(shù)可以NULL
。
lua_rotate
-0, +0, –void lua_rotate (lua_State *L, int idx, int n);
在有效索引idx
和堆棧頂部之間旋轉(zhuǎn)堆棧元素。這些元素是n
在頂部方向旋轉(zhuǎn)的位置,對于正面n
,或-n
對底部方向的位置,對于負(fù)面n
。絕對值n
不得大于正在旋轉(zhuǎn)的切片的大小。該函數(shù)不能用偽索引調(diào)用,因?yàn)閭嗡饕皇菍?shí)際的堆棧位置。
lua_setallocf
-0, +0, –void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);
f
用用戶數(shù)據(jù)改變給定狀態(tài)的分配器功能ud
。
lua_setfield
-1, +0, evoid lua_setfield (lua_State *L, int index, const char *k);
是否為等效的t[k] = v
,在那里t
是給定索引處的值并且v
是在堆棧的頂部的值。
該函數(shù)從堆棧中彈出值。和Lua一樣,這個(gè)函數(shù)可能觸發(fā)“newindex”事件的元方法(參見§2.4)。
lua_setglobal
-1, +0, evoid lua_setglobal (lua_State *L, const char *name);
從堆棧中彈出一個(gè)值并將其設(shè)置為全局的新值name
。
lua_seti
-1, +0, evoid lua_seti (lua_State *L, int index, lua_Integer n);
是否為等效的t[n] = v
,在那里t
是給定索引處的值并且v
是在堆棧的頂部的值。
該函數(shù)從堆棧中彈出值。和Lua一樣,這個(gè)函數(shù)可能觸發(fā)“newindex”事件的元方法(參見§2.4)。
lua_setmetatable
-1, +0, –void lua_setmetatable (lua_State *L, int index);
從堆棧中彈出一個(gè)表并將其設(shè)置為給定索引處的值的新元數(shù)據(jù)。
lua_settable
-2, +0, evoid lua_settable (lua_State *L, int index);
是否等同于t[k] = v
,這里t
是給定索引處的值,v
是在堆棧的頂部的值,k
略低于前值。
該函數(shù)彈出堆棧中的鍵和值。和Lua一樣,這個(gè)函數(shù)可能觸發(fā)“newindex”事件的元方法(參見§2.4)。
lua_settop
-?, +?, –void lua_settop (lua_State *L, int index);
接受任何索引或0,并將堆棧頂部設(shè)置為該索引。如果新的頂部比舊的頂部大,那么新的元素被填充為零。如果index
為0,則所有堆棧元素都將被刪除。
lua_setuservalue
-1, +0, –void lua_setuservalue (lua_State *L, int index);
從堆棧中彈出一個(gè)值,并將其設(shè)置為與給定索引處的完整用戶數(shù)據(jù)關(guān)聯(lián)的新值。
lua_State
typedef struct lua_State lua_State;
一個(gè)不透明的結(jié)構(gòu),指向一個(gè)線程并間接(通過線程)到一個(gè) Lua 解釋器的整個(gè)狀態(tài)。Lua庫是完全可重入的:它沒有全局變量。所有關(guān)于國家的信息都可以通過這個(gè)結(jié)構(gòu)來訪問。
一個(gè)指向這個(gè)結(jié)構(gòu)的指針必須作為第一個(gè)參數(shù)傳遞給庫中的每個(gè)函數(shù),除了lua_newstate
從頭創(chuàng)建一個(gè)Lua狀態(tài)。
lua_status
-0, +0, –int lua_status (lua_State *L);
返回線程的狀態(tài)L
。
The status can be 0 (LUA_OK
) for a normal thread, an error code if the thread finished the execution of a lua_resume
with an error, or LUA_YIELD
if the thread is suspended.
您只能在具有狀態(tài)的線程中調(diào)用函數(shù)LUA_OK
。您可以恢復(fù)具有狀態(tài)的線程LUA_OK
(啟動(dòng)新協(xié)程)或LUA_YIELD
(恢復(fù)協(xié)程)。
lua_stringtonumber
-0, +1, –size_t lua_stringtonumber (lua_State *L, const char *s);
將以零結(jié)尾的字符串s
轉(zhuǎn)換為數(shù)字,將該數(shù)字推入堆棧,并返回字符串的總大小,即其長度加1。根據(jù)Lua的詞匯慣例,轉(zhuǎn)換可以產(chǎn)生整數(shù)或浮點(diǎn)數(shù)(參見§3.1)。該字符串可能有前導(dǎo)和尾隨空格以及符號。如果字符串不是有效的數(shù)字,則返回0并不推送任何內(nèi)容。(注意,如果轉(zhuǎn)換成功,結(jié)果可以用作布爾值,true。)
lua_toboolean
-0, +0, –int lua_toboolean (lua_State *L, int index);
將給定索引處的Lua值轉(zhuǎn)換為C布爾值(0或1)。像Lua中的所有測試一樣,lua_toboolean
對于與false和nil不同的任何Lua值都返回true ; 否則返回false。(如果您只想接受實(shí)際布爾值,請使用lua_isboolean
測試值的類型。)
lua_tocfunction
-0, +0, –lua_CFunction lua_tocfunction (lua_State *L, int index);
將給定索引處的值轉(zhuǎn)換為C函數(shù)。該值必須是C函數(shù); 否則,返回NULL
。
lua_tointeger
-0, +0, –lua_Integer lua_tointeger (lua_State *L, int index);
相當(dāng)于lua_tointegerx
用isnum
等于NULL
。
lua_tointegerx
-0, +0, –lua_Integer lua_tointegerx (lua_State *L, int index, int *isnum);
將給定索引處的Lua值轉(zhuǎn)換為帶符號整型lua_Integer
。Lua值必須是一個(gè)整數(shù),或者是一個(gè)可轉(zhuǎn)換為整數(shù)的數(shù)字或字符串(參見§3.4.3); 否則,lua_tointegerx
返回0。
如果isnum
不是NULL
,則為其指示對象分配一個(gè)布爾值,指示操作是否成功。
lua_tolstring
-0, +0, mconst char *lua_tolstring (lua_State *L, int index, size_t *len);
將給定索引處的Lua值轉(zhuǎn)換為C字符串。如果len
不是NULL
,則設(shè)置*len
字符串長度。Lua值必須是字符串或數(shù)字; 否則,函數(shù)返回NULL
。如果該值是一個(gè)數(shù)字,那么lua_tolstring
也會(huì)將堆棧中的實(shí)際值更改為一個(gè)字符串。(這種改變在表遍歷過程中被應(yīng)用于鍵lua_next
時(shí)lua_tolstring
會(huì)產(chǎn)生混淆。)
lua_tolstring
返回一個(gè)指向Lua狀態(tài)內(nèi)的字符串的指針。該字符串\0
在最后一個(gè)字符后面(如C中)始終有一個(gè)零(' '),但在其正文中可以包含其他零。
由于 Lua 具有垃圾回收功能,因此不能保證lua_tolstring
在將相應(yīng)的Lua值從堆棧中移除后,返回的指針將會(huì)有效。
lua_tonumber
-0, +0, –lua_Number lua_tonumber (lua_State *L, int index);
相當(dāng)于lua_tonumberx
用isnum
等于NULL
。
lua_tonumberx
-0, +0, –lua_Number lua_tonumberx (lua_State *L, int index, int *isnum);
將給定索引處的Lua值轉(zhuǎn)換為C類型lua_Number
(請參閱參考資料lua_Number
)。Lua值必須是可轉(zhuǎn)換為數(shù)字的數(shù)字或字符串(請參閱第3.4.3節(jié)); 否則,lua_tonumberx
返回0。
如果isnum
不是NULL
,則為其指示對象分配一個(gè)布爾值,指示操作是否成功。
lua_topointer
-0, +0, –const void *lua_topointer (lua_State *L, int index);
將給定索引處的值轉(zhuǎn)換為通用C指針(void*
)。該值可以是用戶數(shù)據(jù),表格,線程或函數(shù); 否則,lua_topointer
返回NULL
。不同的對象會(huì)給出不同的指針。沒有辦法將指針轉(zhuǎn)換回原始值。
通常這個(gè)函數(shù)只用于哈希和調(diào)試信息。
lua_tostring
-0, +0, mconst char *lua_tostring (lua_State *L, int index);
相當(dāng)于lua_tolstring
用len
等于NULL
。
lua_tothread
-0, +0, –lua_State *lua_tothread (lua_State *L, int index);
將給定索引處的值轉(zhuǎn)換為Lua線程(表示為lua_State*
)。這個(gè)值必須是一個(gè)線程; 否則,函數(shù)返回NULL
。
lua_touserdata
-0, +0, –void *lua_touserdata (lua_State *L, int index);
如果給定索引處的值是完整的用戶數(shù)據(jù),則返回其塊地址。如果該值是一個(gè)light userdata,則返回其指針。否則,退貨NULL
。
lua_type
-0, +0, –int lua_type (lua_State *L, int index);
返回給定有效索引中的值的類型,或者返回LUA_TNONE
無效(但可接受)索引中的值的類型。返回的類型lua_type
是通過在定義的以下常量編碼lua.h
:LUA_TNIL
(0), , ,LUA_TNUMBER
,LUA_TBOOLEAN
,LUA_TSTRING
,,LUA_TTABLE
,和。LUA_TFUNCTIONLUA_TUSERDATALUA_TTHREADLUA_TLIGHTUSERDATA
lua_typename
-0, +0, –const char *lua_typename (lua_State *L, int tp);
返回由該值編碼的類型的名稱,該值tp
必須是由返回的值之一lua_type
。
lua_Unsigned
typedef ... lua_Unsigned;
未簽名的版本lua_Integer
。
lua_upvalueindex
-0, +0, –int lua_upvalueindex (int i);
返回表示i
運(yùn)行函數(shù)的第 - 個(gè)最高值的偽索引(請參閱第4.4節(jié))。
lua_version
-0, +0, –const lua_Number *lua_version (lua_State *L);
返回存儲(chǔ)在Lua核中的版本號(一個(gè)C靜態(tài)變量)的地址。當(dāng)用有效的方式調(diào)用時(shí)lua_State
,返回用于創(chuàng)建該狀態(tài)的版本的地址。呼叫時(shí)NULL
,返回運(yùn)行呼叫的版本的地址。
lua_Writer
typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud);
編寫器函數(shù)使用的類型lua_dump
。每次它產(chǎn)生另一塊時(shí),lua_dump
調(diào)用寫入器,傳遞緩沖區(qū)以寫入(p
),其大?。?code>sz)和data
提供給的參數(shù)lua_dump
。
作者返回一個(gè)錯(cuò)誤代碼:0意味著沒有錯(cuò)誤; 任何其他值意味著錯(cuò)誤,并停止lua_dump
再次調(diào)用作者。
lua_xmove
-?, +?, –void lua_xmove (lua_State *from, lua_State *to, int n);
交換相同狀態(tài)的不同線程之間的值。
該函數(shù)n
從堆棧中彈出值from
,并將它們推入堆棧to
。
lua_yield
-?, +?, eint lua_yield (lua_State *L, int nresults);
這個(gè)功能相當(dāng)于lua_yieldk
,但沒有延續(xù)(見§4.7)。因此,當(dāng)線程恢復(fù)時(shí),它繼續(xù)調(diào)用函數(shù)調(diào)用的函數(shù)lua_yield
。
lua_yieldk
-?, +?, eint lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, lua_KFunction k);
產(chǎn)生一個(gè)協(xié)程(線程)。
當(dāng)C函數(shù)調(diào)用時(shí)lua_yieldk
,正在運(yùn)行的協(xié)程暫停執(zhí)行,并且lua_resume
啟動(dòng)該協(xié)程的調(diào)用返回。該參數(shù)nresults
是堆棧中將作為結(jié)果傳遞給的值的數(shù)量lua_resume
。
當(dāng)協(xié)程再次恢復(fù)時(shí),Lua調(diào)用給定的繼續(xù)函數(shù)k
繼續(xù)執(zhí)行產(chǎn)生的C函數(shù)(見§4.7)。這個(gè)繼續(xù)函數(shù)從前一個(gè)函數(shù)接收相同的堆棧,并將n
結(jié)果刪除并由傳遞給它的參數(shù)替換lua_resume
。而且,繼續(xù)函數(shù)接收ctx
傳遞給的值lua_yieldk
。
通常,這個(gè)函數(shù)不會(huì)返回; 當(dāng)協(xié)程最終恢復(fù)時(shí),它繼續(xù)執(zhí)行延續(xù)功能。但是,有一種特殊情況,即在線內(nèi)或計(jì)數(shù)掛鉤中調(diào)用此函數(shù)時(shí)(請參閱第4.9節(jié))。在這種情況下,lua_yieldk
應(yīng)該在沒有延續(xù)的情況下(可能以)的形式被調(diào)用,lua_yield
并且沒有結(jié)果,并且鉤子應(yīng)該在調(diào)用之后立即返回。Lua將屈服,并且當(dāng)協(xié)程再次恢復(fù)時(shí),它將繼續(xù)正常執(zhí)行觸發(fā)鉤子的(Lua)函數(shù)。
如果從具有掛起C調(diào)用而沒有連續(xù)功能的線程調(diào)用該函數(shù),或者該函數(shù)是從未在簡歷中運(yùn)行的線程(例如主線程)調(diào)用的,則此函數(shù)可能會(huì)引發(fā)錯(cuò)誤。
Lua 沒有內(nèi)置的調(diào)試工具。相反,它通過函數(shù)和鉤子提供了一個(gè)特殊的接口。該接口允許構(gòu)建不同類型的調(diào)試器,分析器和其他需要解釋器“內(nèi)部信息”的工具。
lua_Debug
typedef struct lua_Debug { int event; const char *name; /* (n) */ const char *namewhat; /* (n) */ const char *what; /* (S) */ const char *source; /* (S) */ int currentline; /* (l) */ int linedefined; /* (S) */ int lastlinedefined; /* (S) */ unsigned char nups; /* (u) number of upvalues */ unsigned char nparams; /* (u) number of parameters */ char isvararg; /* (u) */ char istailcall; /* (t) */ char short_src[LUA_IDSIZE]; /* (S) */ /* private part */ other fields} lua_Debug;
用于承載有關(guān)功能或激活記錄的不同信息的結(jié)構(gòu)。lua_getstack
只填充這個(gè)結(jié)構(gòu)的私有部分,供以后使用。要填充lua_Debug
有用信息的其他字段,請致電lua_getinfo
。
這些領(lǐng)域lua_Debug
具有以下含義:
source
:創(chuàng)建函數(shù)的塊的名稱。如果source
以“ @
' 開頭,則意味著該函數(shù)是在文件名稱跟在” @
“ 之后的文件中定義的。如果source
以' =
' 開頭,則其余內(nèi)容以依賴于用戶的方式描述源。否則,函數(shù)被定義在字符串的source
字符串中。
short_src
:source
用于錯(cuò)誤消息的“可打印”版本。
linedefined
:函數(shù)定義開始的行號。
lastlinedefined
:函數(shù)定義結(jié)束的行號。
what
:"Lua"
函數(shù)是Lua函數(shù)的字符串,"C"
如果是C函數(shù),"main"
則它是塊的主要部分。
currentline
:給定函數(shù)正在執(zhí)行的當(dāng)前行。當(dāng)沒有行信息可用時(shí),currentline
設(shè)置為-1。
name
:給定函數(shù)的合理名稱。因?yàn)長ua中的函數(shù)是一流的值,所以它們沒有固定的名稱:一些函數(shù)可以是多個(gè)全局變量的值,而另一些函數(shù)可以僅存儲(chǔ)在表字段中。該lua_getinfo
函數(shù)檢查如何調(diào)用該函數(shù)以找到合適的名稱。如果找不到名稱,則name
設(shè)置為NULL
。
namewhat
:解釋這個(gè)name
領(lǐng)域。的值namewhat
可以是"global"
,"local"
,"method"
,"field"
,"upvalue"
,或""
(空字符串),根據(jù)功能如何被調(diào)用。(當(dāng)沒有其他選項(xiàng)似乎適用時(shí),Lua使用空字符串。)
istailcall
:如果通過尾部調(diào)用調(diào)用此函數(shù)調(diào)用,則為true。在這種情況下,此級別的調(diào)用者不在堆棧中。
nups
:函數(shù)的upvalues數(shù)量。
nparams
:函數(shù)的固定參數(shù)數(shù)量(C函數(shù)始終為0)。
isvararg
:如果函數(shù)是可變參數(shù)函數(shù),則為true(對于C函數(shù)總是如此)。
lua_gethook
-0, +0, –lua_Hook lua_gethook (lua_State *L);
返回當(dāng)前的掛鉤函數(shù)。
lua_gethookcount
-0, +0, –int lua_gethookcount (lua_State *L);
返回當(dāng)前掛鉤計(jì)數(shù)。
lua_gethookmask
-0, +0, –int lua_gethookmask (lua_State *L);
返回當(dāng)前的鉤子掩碼。
lua_getinfo
-(0|1), +(0|1|2), eint lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
獲取有關(guān)特定函數(shù)或函數(shù)調(diào)用的信息。
要獲得關(guān)于函數(shù)調(diào)用的信息,參數(shù)ar
必須是一個(gè)有效的激活記錄,該記錄由前一次調(diào)用lua_getstack
或作為參數(shù)給出(參見lua_Hook
)。
要獲得關(guān)于某個(gè)函數(shù)的信息,請將其推入堆棧并what
使用字符“ >
' 開始字符串。(在這種情況下,lua_getinfo
從堆棧頂部彈出該函數(shù)。)例如,要知道在哪一行f
中定義了函數(shù),可以編寫以下代碼:
lua_Debug ar;lua_getglobal(L, "f"); /* get global 'f' */lua_getinfo(L, ">S", &ar);printf("%d\n", ar.linedefined);
字符串中的每個(gè)字符都what
選擇ar
要填充的結(jié)構(gòu)的某些字段或要在堆棧中壓入的值:
' n
':填寫字段name
并且namewhat
;
' S
“:填補(bǔ)了領(lǐng)域source
,short_src
,linedefined
,lastlinedefined
,和what
;
' l
':填寫字段currentline
;
' t
':填寫字段istailcall
;
' u
“:填補(bǔ)了領(lǐng)域nups
,nparams
以及isvararg
;
' f
':將在給定級別運(yùn)行的函數(shù)壓入堆棧;
' L
':將一個(gè)表格索引到函數(shù)上有效的行號上。(一個(gè)有效的行是一個(gè)包含相關(guān)代碼的行,也就是說,可以放置一個(gè)斷點(diǎn)的行,無效的行包含空行和注釋)。如果此選項(xiàng)與選項(xiàng)“ f
' 一起給出,其表是在功能后推動(dòng)。
該函數(shù)在出錯(cuò)時(shí)返回0(例如,一個(gè)無效的選項(xiàng)what
)。
lua_getlocal
-0, +(0|1), –const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);
獲取有關(guān)給定激活記錄或給定函數(shù)的局部變量的信息。
在第一種情況下,該參數(shù)ar
必須是一個(gè)有效的激活記錄,該記錄由前一次調(diào)用lua_getstack
或作為參數(shù)給出(參見lua_Hook
)。該索引n
選擇要檢查的局部變量; 查看debug.getlocal
有關(guān)變量索引和名稱的詳細(xì)信息。
lua_getlocal
將變量的值推入堆棧并返回其名稱。
在第二種情況下,ar
必須是,NULL
并且要檢查的功能必須位于堆棧的頂部。在這種情況下,只有Lua函數(shù)的參數(shù)可見(因?yàn)闆]有關(guān)于哪些變量處于活動(dòng)狀態(tài)的信息)并且沒有值被壓入堆棧。
NULL
當(dāng)索引大于活動(dòng)局部變量的數(shù)量時(shí),返回(并且不會(huì)執(zhí)行任何操作)。
lua_getstack
-0, +0, –int lua_getstack (lua_State *L, int level, lua_Debug *ar);
獲取有關(guān)解釋器運(yùn)行時(shí)堆棧的信息。
該函數(shù)填充了一個(gè)lua_Debug
結(jié)構(gòu)的一部分,并標(biāo)識了在給定級別執(zhí)行的函數(shù)的激活記錄。0級是當(dāng)前運(yùn)行的函數(shù),而n + 1級是已調(diào)用級別n的函數(shù)(tail 函數(shù)除外,它不計(jì)入堆棧)。當(dāng)沒有錯(cuò)誤時(shí),lua_getstack
返回1; 當(dāng)用大于堆棧深度的級別調(diào)用時(shí),它返回0。
lua_getupvalue
-0, +(0|1), –const char *lua_getupvalue (lua_State *L, int funcindex, int n);
獲取有關(guān)n
索引處閉包的最新價(jià)值的信息funcindex
。它將upvalue的值推入堆棧并返回其名稱。NULL
當(dāng)索引n
大于upvalues的數(shù)量時(shí),返回(并且不推送)。
對于 C 函數(shù),此函數(shù)使用空字符串""
作為所有upvalues的名稱。(對于Lua函數(shù),upvalues是函數(shù)使用的外部局部變量,因此包含在它的閉包中。)
Upvalues 沒有特定的順序,因?yàn)樗鼈冊谡麄€(gè)功能中處于活動(dòng)狀態(tài)。它們以任意順序編號。
lua_Hook
typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
鍵入調(diào)試掛鉤函數(shù)。
每當(dāng)調(diào)用一個(gè)鉤子時(shí),它的ar
參數(shù)都會(huì)將其字段event
設(shè)置為觸發(fā)鉤子的特定事件。Lua中識別這些事件與以下常量:LUA_HOOKCALL
,LUA_HOOKRET
,LUA_HOOKTAILCALL
,LUA_HOOKLINE
,和LUA_HOOKCOUNT
。而且,對于線路事件,該字段currentline
也被設(shè)置。要獲取其他字段的值,ar
必須調(diào)用該鉤子lua_getinfo
。
對于呼叫事件,event
可以是LUA_HOOKCALL
正常值,或者LUA_HOOKTAILCALL
對于尾部呼叫; 在這種情況下,將不會(huì)有相應(yīng)的回報(bào)事件。
當(dāng)Lua運(yùn)行一個(gè)鉤子時(shí),它會(huì)禁止其他鉤子調(diào)用。因此,如果一個(gè)鉤子回叫Lua執(zhí)行一個(gè)函數(shù)或一個(gè)塊,這個(gè)執(zhí)行就會(huì)發(fā)生,不會(huì)有任何鉤子調(diào)用。
鉤子函數(shù)不能延續(xù),也就是說,他們不能打電話lua_yieldk
,lua_pcallk
或lua_callk
與非空k
。
鉤子函數(shù)可以在下列條件下產(chǎn)生:只有計(jì)數(shù)和行事件可以產(chǎn)生; 到產(chǎn)量,鉤函數(shù)必須完成其執(zhí)行主叫lua_yield
與nresults
等于零(即,沒有值)。
lua_sethook
-0, +0, –void lua_sethook (lua_State *L, lua_Hook f, int mask, int count);
設(shè)置調(diào)試掛鉤功能。
參數(shù)f
是鉤子函數(shù)。mask
指定要在其事件的鉤將被稱為:它是由常量的位OR形成LUA_MASKCALL
,LUA_MASKRET
,LUA_MASKLINE
,和LUA_MASKCOUNT
。該count
參數(shù)僅在掩碼包含時(shí)才有意義LUA_MASKCOUNT
。對于每個(gè)事件,掛鉤被調(diào)用如下所述:
調(diào)用鉤子: 當(dāng)解釋器調(diào)用一個(gè)函數(shù)時(shí)被調(diào)用。鉤子在Lua在函數(shù)獲取其參數(shù)之前輸入新函數(shù)之后立即調(diào)用。
返回鉤子: 當(dāng)解釋器從函數(shù)返回時(shí)被調(diào)用。掛鉤在Lua離開函數(shù)之前被調(diào)用。沒有標(biāo)準(zhǔn)的方法來訪問函數(shù)返回的值。
線鉤子: 當(dāng)解釋器即將開始執(zhí)行新的代碼行時(shí),或者在代碼中跳回時(shí)(甚至是同一行),將調(diào)用它。(這個(gè)事件只發(fā)生在Lua執(zhí)行Lua函數(shù)時(shí)。)
計(jì)數(shù)掛鉤: 在解釋器執(zhí)行每條count
指令后調(diào)用。(這個(gè)事件只發(fā)生在Lua執(zhí)行Lua函數(shù)時(shí)。)
掛鉤通過設(shè)置mask
為零來禁用。
lua_setlocal
-(0|1), +0, –const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);
設(shè)置給定激活記錄的局部變量的值。它將堆棧頂部的值賦給變量并返回其名稱。它也彈出堆棧中的值。
Returns NULL
(and pops nothing) when the index is greater than the number of active local variables.
參數(shù)ar
和n
功能一樣lua_getlocal
。
lua_setupvalue
-(0|1), +0, –const char *lua_setupvalue (lua_State *L, int funcindex, int n);
設(shè)置封閉的upvalue的值。它將堆棧頂部的值賦給upvalue并返回它的名字。它也彈出堆棧中的值。
NULL
當(dāng)索引n
大于upvalues的數(shù)量時(shí),返回(并且不會(huì)彈出)。
參數(shù)funcindex
和n
功能一樣lua_getupvalue
。
lua_upvalueid
-0, +0, –void *lua_upvalueid (lua_State *L, int funcindex, int n);
n
從索引處的閉包返回編號為upvalue的唯一標(biāo)識符funcindex
。
這些唯一標(biāo)識符允許程序檢查不同的關(guān)閉是否共享upvalue。共享一個(gè)upvalue(也就是說,訪問一個(gè)相同的外部局部變量)的Lua閉包將為這些upvalue索引返回相同的id。
參數(shù)funcindex
和n
功能一樣lua_getupvalue
,但n
不能大于upvalues的數(shù)量。
lua_upvaluejoin
-0, +0, –void lua_upvaluejoin (lua_State *L, int funcindex1, int n1, int funcindex2, int n2);
使n1
指數(shù)中Lua收盤價(jià)的第 - 高價(jià)值funcindex1
指n2
的是指數(shù)中Lua收盤價(jià)的第 - 高價(jià)值funcindex2
。