c/c++语言开发共享用Lua注册一个闭包

我没有使用lua_CFunction签名来编写从Lua调用的方法,而是想使用我自己的函数签名来简化导出过程。

void foo(call_t *call) { int a; char *b; char *c; table_t *d; /* reading arguments */ a = read_integer(call); b = read_string(call); /* do something... */ /* writing arguments */ write_string(call, c); write_table(call, d); } /* export to Lua */ export("foo", foo); 

到目前为止,我能想到的只有一个lua_CFunction,它从表中调用包装函数。 但是,我不知道如何将Lua函数与C函数和表索引相关联,以便有效地使Lua函数成为闭包。 像这样的东西:

 lua_register_with_data(state, "foo", base_function, FOO_INDEX); 

我怎样才能做到这一点?

    毕竟我想通了。 我想这certificate了橡皮鸭的调试是多么有用。

    我刚刚将基函数与实际函数索引一起注册为upvalue。

     function_t table[FUNCTION_COUNT]; /* lookup function using upvalue */ int base_function(lua_State *state) { int index; call_t call; call.state = state; call.argument_index = 1; call.return_count = 0; index = lua_tointeger(state, lua_upvalueindex(1)); table[index](&call); /* return_count is incremented by write_* functions */ return(call.return_count); } /* register function as closure */ table[FOO_INDEX] = foo; lua_pushinteger(state, FOO_INDEX); lua_pushcclosure(state, base_function, 1); lua_setglobal(state, "foo"); 

      以上就是c/c++开发分享用Lua注册一个闭包相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/560311.html

      (0)
      上一篇 2021年1月27日
      下一篇 2021年1月27日

      精彩推荐