我有一些C++课程,我想向Lua公开.我可以调用Widget:New()以返回带有metatable集的userdata到表中WidgetMeta.WidgetMeta它包含所有C++函数,并且它__index是自己设置的,所以我可以这样做:
w = Widget:New()
w:Foo() -- Foo is defined in C code
Run Code Online (Sandbox Code Playgroud)
这一切都很简单.
现在这是我无法弄清楚的部分.我希望能够将Lua定义的变量和函数放在我的userdata上,就像它是一个表一样.这显然不能直接完成.我不能将它放在userdata上,因为我希望每个用户数据都是唯一的.
w1 = Widget:New()
w2 = Widget:New()
function w1:Bar() print "Hello!" end -- Both functions unique
function w1:Baz() print "World!" end -- to their own userdata
Run Code Online (Sandbox Code Playgroud)
我目前的攻击计划是让metatable在其上有一个特殊的表,它在userdata和table之间进行映射,我可以在其中存储每用户数据函数和变量.问题是我不确定这样做的最佳方法是什么,或者是否有更好的解决方案.所以我的问题是双重的:当我设置我的__index和__newindex元方法时,我是用Lua代码在文本文件中编写它们并在运行剩下的东西之前运行它,还是直接从C语言中放入Lua代码我的程序中的字符串是通过luaL_loadstring,还是我用C接口来处理所有堆栈操作?第二,我该如何编写这个功能......但是一旦我决定采用哪种方式,我就会处理这个问题.
向userdata添加一个函数环境,并通过它重定向访问.
这是我的一些旧代码,描述了这个过程.
static int l_irc_index( lua_State* L )
{
/* object, key */
/* first check the environment */
lua_getfenv( L, -2 );
lua_pushvalue( L, -2 );
lua_rawget( L, -2 );
if( lua_isnoneornil( L, -1 ) == 0 )
{
return 1;
}
lua_pop( L, 2 );
/* second check the metatable */
lua_getmetatable( L, -2 );
lua_pushvalue( L, -2 );
lua_rawget( L, -2 );
/* nil or otherwise, we return here */
return 1;
}
static int l_irc_newindex( lua_State* L )
{
/* object, key, value */
lua_getfenv( L, -3 );
lua_pushvalue( L, -3 );
lua_pushvalue( L, -3 );
lua_rawset( L, -3 );
return 0;
}
Run Code Online (Sandbox Code Playgroud)