首先,helloworld
您的代碼工作:
/* file: hw.c
* on Debian/Ubuntu compile with:
* `gcc -I/usr/include/lua5.2 -fpic -shared -o hw.so hw.c`
*/
#include <lua.h>
#include <lauxlib.h>
struct SomethingWrapper {
void *object;
};
static int l_helloworld(lua_State *L) {
lua_pushliteral(L, "Hello World!");
return 1;
}
static luaL_Reg const some_funcs[] = {
{ "helloworld", l_helloworld },
{ NULL, NULL }
};
int push_Something(lua_State *L, void *object) {
struct SomethingWrapper *w = lua_newuserdata(L, sizeof(*w));
w->object = object;
luaL_setmetatable(L, "Something");
return 1;
}
int luaopen_hw(lua_State *L) {
luaL_newmetatable(L, "Something");
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_setfuncs(L, some_funcs, 0);
lua_pop(L, 1);
push_Something(L, NULL);
return 1;
}
和測試腳本:
-- file: hwtest.lua
local x = require("hw")
print(x.helloworld())
輸出是:
Hello World!
有關您需要將用戶數據訪問性能將__index
設置爲函數而不是表格。每當您嘗試訪問userdata上的字段時,都會使用兩個參數(userdata和key)調用該函數,並且您可以查詢您的C對象並推送所需的結果。
如果您打算同時支持方法和屬性,它會變得稍微複雜一些,但基本方法如下:使用函數__index
metamethod。這個函數可以訪問一個方法表(例如通過一個upvalue或者註冊表等),並且試圖查找該表中給定的鍵。如果你成功了,你會返回這個價值。如果沒有提供任何內容,則將給定鍵與C對象的有效屬性名稱進行比較,如果得到匹配則返回相應的值。 (如果你沒有得到一場比賽,你可以返回nil
或提出錯誤 - 這取決於你。)
這是一個可重用的實現,方法(從moon toolkit提取):
static int moon_dispatch(lua_State* L) {
lua_CFunction pindex;
/* try method table first */
lua_pushvalue(L, 2); /* duplicate key */
lua_rawget(L, lua_upvalueindex(1));
if(!lua_isnil(L, -1))
return 1;
lua_pop(L, 1);
pindex = lua_tocfunction(L, lua_upvalueindex(2));
return pindex(L);
}
MOON_API void moon_propindex(lua_State* L, luaL_Reg const methods[],
lua_CFunction pindex, int nups) {
if(methods != NULL) {
luaL_checkstack(L, nups+2, "not enough stack space available");
lua_newtable(L);
for(; methods->func; ++methods) {
int i = 0;
for(i = 0; i < nups; ++i)
lua_pushvalue(L, -nups-1);
lua_pushcclosure(L, methods->func, nups);
lua_setfield(L, -2, methods->name);
}
if(pindex) {
lua_pushcfunction(L, pindex);
if(nups > 0) {
lua_insert(L, -nups-2);
lua_insert(L, -nups-2);
}
lua_pushcclosure(L, moon_dispatch, 2+nups);
} else if(nups > 0) {
lua_replace(L, -nups-1);
lua_pop(L, nups-1);
}
} else if(pindex) {
lua_pushcclosure(L, pindex, nups);
} else {
lua_pop(L, nups);
lua_pushnil(L);
}
}
用法:
/* [ -nup, +1, e ] */
void moon_propindex(lua_State* L,
luaL_Reg const* methods,
lua_CFunction index,
int nup);
This function is used for creating an __index metafield. If index is NULL but methods is not, a table containing all the functions in methods is created and pushed to the top of the stack. If index is not NULL, but methods is, the index function pointer is simply pushed to the top of the stack. In case both are non NULL, a new C closure is created and pushed to the stack, which first tries to lookup a key in the methods table, and if unsuccessful then calls the original index function. If both are NULL, nil is pushed to the stack. If nup is non-zero, the given number of upvalues is popped from the top of the stack and made available to all registered functions. (In case index and methods are not NULL, the index function receives two additional upvalues at indices 1 and 2.) This function is used in the implementation of moon_defobject, but maybe it is useful to you independently.
如果試圖存儲在用戶數據任意的Lua值(如標題所示) - 你不能。但是您可以將一個額外的表(稱爲「用戶值」)與每個用戶數據關聯並在其中存儲任意值。該方法類似於上面的方法,但不是與預定義的屬性名稱匹配並直接訪問C對象,而是首先推送用戶值表(使用lua_getuservalue
),然後查找您的字段。
您在使用'push_Something'之前註冊/創建了metatable? –
正確,首先創建metatable – 010110110101