c++ 與 lua 簡單交互參數介紹
原文http://blog.csdn.net/johnice/article/details/5517431
一、第一個例子 Hello World !
[c-sharp] view plain copy
- #include <stdio.h>
- #include <string.h>
- #include "lua.h"
- #include "lauxlib.h"
- #include "lualib.h"
- int main (void)
- {
- char buff[256];
- int error;
- lua_State *L = lua_open(); /* opens Lua */
- // 5.1.4 版本加載庫 方法
- luaL_openlibs(L);
- // 5.1.4 版本之前 加載庫 方式
- //luaopen_base(L); /* opens the basic library */
- //luaopen_table(L); /* opens the table library */
- //luaopen_io(L); /* opens the I/O library */
- //luaopen_string(L); /* opens the string lib. */
- //luaopen_math(L); /* opens the math lib. */
- while (fgets(buff, sizeof(buff), stdin) != NULL) {
- error = luaL_loadbuffer(L, buff, strlen(buff),
- "line") || lua_pcall(L, 0, 0, 0);
- if (error) {
- fprintf(stderr, "%s", lua_tostring(L, -1));
- lua_pop(L, 1);/* pop error message from the stack */
- }
- }
- lua_close(L);
- return 0;
- }
註意一下幾點:
1.需要lua庫的 .dll 和 .lib 文件
2.在include “lua.h lauxlib.h lualib.h” 時,註意區分是否需要將這些include用 extern "C" { ... } 包含起來
3.初始化lua虛擬機函數已改成 luaL_openlibs(L);
二、堆棧
2.1:壓入元素
將每種可以用C 來描述的Lua 類型壓棧
[c-sharp] view plain copy
- void lua_pushnil (lua_State *L);
- void lua_pushboolean (lua_State *L, int bool);
- void lua_pushnumber (lua_State *L, double n);
- void lua_pushlstring (lua_State *L, const char *s, size_t length);
- void lua_pushstring (lua_State *L, const char *s);
將字符串壓入串的正式函數是lua_pushlstring,它要求一個明確的長度作為參數。對於以零結束的字符串,你可以用lua_pushstring
2.2:查詢元素
各個類型的這些函數都有同一個原型: int lua_is... (lua_State *L, int index);
這些函數中使用 lua_type, 並對結果(幾種宏)進行判斷,返回0 or 1
[c-sharp] view plain copy
- #define LUA_TNIL 0
- #define LUA_TBOOLEAN 1
- #define LUA_TLIGHTUSERDATA 2
- #define LUA_TNUMBER 3
- #define LUA_TSTRING 4
- #define LUA_TTABLE 5
- #define LUA_TFUNCTION 6
- #define LUA_TUSERDATA 7
- #define LUA_TTHREAD 8
2.3:從棧中獲取值,lua_to... ()函數:
[c-sharp] view plain copy
- int lua_toboolean (lua_State *L, int index);
- double lua_tonumber (lua_State *L, int index);
- const char * lua_tostring (lua_State *L, int index);
- size_t lua_strlen (lua_State *L, int index);
即使給定的元素的類型不正確,調用上面這些函數也沒有什麽問題。在這種情況下,lua_toboolean、lua_tonumber 和lua_strlen 返回0,其他函數返回NULL。
lua 允許 string 中包含‘/0‘,所以下面的語句總是有效的:
[c-sharp] view plain copy
- const char *s = lua_tostring(L, -1); /* any Lua string */
- size_t l = lua_strlen(L, -1); /* its length */
- assert(s[l] == ‘/0‘);
- assert(strlen(s) <= l);
2.4:其他堆棧操作
[c-sharp] view plain copy
- int lua_gettop (lua_State *L);
- void lua_settop (lua_State *L, int index);
- void lua_pushvalue (lua_State *L, int index);
- void lua_remove (lua_State *L, int index);
- void lua_insert (lua_State *L, int index);
- void lua_replace (lua_State *L, int index);
lua_gettop:返回堆棧中的元素個數,它也是棧頂元素的索引(註意一個負數索引-x 對應於正數索引gettop-x+1)
lua_settop:設置棧頂(也就是堆棧中的元素個數)為一個指定的值
如果開始的棧頂高於新的棧頂,頂部的值被丟棄。否則,為了得到指定的大小這個函數壓入相應個數的空值(nil)到棧上
lua_settop(L,0); 清空堆棧
也可以用負數索引作為調用lua_settop 的參數,那將會設置棧頂到指定的索引。利用這種技巧,API 提供了下面這個宏,它從堆棧中彈出n 個元素:
#define lua_pop(L,n) lua_settop(L, -(n)-1)
lua_pushvalue:壓入堆棧上指定索引的一個摶貝到棧頂
lua_remove:移除指定索引位置的元素,並將其上面所有的元素下移來填補這個位置的空白
lua_insert:移動棧頂元素到指定索引的位置,並將這個索引位置上面的元素全部上移至棧頂被移動留下的空隔;
lua_replace 從棧頂彈出元素值並將其設置到指定索引位置,沒有任何移動操作。
2.5:表操作
lua_getglobal:其中一參數為變量名稱,每調用一次就把相應的變量值壓入棧頂
lua_gettable:他接受table在棧中的位置為參數,調用前需要先將要取的key(string)壓入棧,並位於棧頂,
調用lua_gettable 後對應key 值出棧,返回與key 對應的value(棧頂)
lua_newtable:創建一個新的空table 然後將其入棧
lua_settable:以table 在棧中的索引作為參數(key先入棧,value後(頂)),並將棧中的key 和value出棧,用這兩個值修改table的相應key值。
lua_setglobal:將棧頂元素出棧,並將其賦給一個全局變量名
[c-sharp] view plain copy
- void setfield (const char *index, int value) {
- lua_pushstring(L, index);
- lua_pushnumber(L, (double)value/MAX_COLOR);
- lua_settable(L, -3);
- }
- void setcolor (struct ColorTable *ct) {
- lua_newtable(L); /* creates a table */
- setfield("r", ct->red); /* table.r = ct->r */
- setfield("g", ct->green); /* table.g = ct->g */
- setfield("b", ct->blue); /* table.b = ct->b */
- lua_setglobal(ct->name); /* ‘name‘ = table */
- }
c++ 與 lua 簡單交互參數介紹