gpt4 book ai didi

c - Lua C API 和元表函数

转载 作者:太空狗 更新时间:2023-10-29 15:14:29 24 4
gpt4 key购买 nike

我在 C 应用程序中使用 Lua,并且有两个表。我想创建第三个表,虽然它是空的,但它会索引前两个表中的值。我在 Lua 中编写了以下简单示例 -

a = { one="1", two="2" }
b = { three="3", four="4" }

meta = { __index = function(t,k)
if a[k] == nil then return b[k]
else return a[k] end
end }

c = {}
setmetatable(c, meta)

print(c.one) -- prints "1"
print(c.four) -- prints "4"

我的问题是,从 C API 执行此操作的最有效方法是什么?我已经能够通过创建一个新表,将上面的 Lua 代码块推送到该表,然后在其上调用 setmetatable() 来做到这一点,但这似乎不太理想。有没有更好的办法?

最佳答案

#include <stdio.h>
#include "lua.h"

/* __index metamethod for the 'c' table (stack: 1 = table 'c', 2 = desired index) */
static int
cindex(lua_State *L)
{
/* try the global 'a' table */
lua_getglobal(L, "a");
lua_pushvalue(L, 2);
lua_gettable(L, -2);
if (!lua_isnil(L, -1))
return 1;

/* try the global 'b' table */
lua_getglobal(L, "b");
lua_pushvalue(L, 2);
lua_gettable(L, -2);
if (!lua_isnil(L, -1))
return 1;

/* return nil */
return 0;
}

int
main(int argc, char **argv)
{
lua_State *L;

L = (lua_State *) luaL_newstate();
luaL_openlibs(L);

/* create the global 'a' table */
lua_createtable(L, 0, 2);
lua_pushstring(L, "1");
lua_setfield(L, -2, "one");
lua_pushstring(L, "2");
lua_setfield(L, -2, "two");
lua_setglobal(L, "a");

/* create the global 'b' table */
lua_createtable(L, 0, 2);
lua_pushstring(L, "3");
lua_setfield(L, -2, "three");
lua_pushstring(L, "4");
lua_setfield(L, -2, "four");
lua_setglobal(L, "b");

/* create the global 'c' table and use a C function as the __index metamethod */
lua_createtable(L, 0, 0);
lua_createtable(L, 0, 1);
lua_pushcfunction(L, cindex);
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2);
lua_setglobal(L, "c");

/* run the test script */
luaL_loadstring(L, "print(c.one)\nprint(c.four)");
if (0 != lua_pcall(L, 0, 0, 0)) {
puts(lua_tostring(L, -1));
return 1;
}

return 0;
}

关于c - Lua C API 和元表函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3449759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com