因为需求,最近又重新回忆了下tolua++的相关流程, 以 3.9的 lua stack 的初始化相关代码开始解析:
_state = lua_open();
luaL_openlibs(_state);
toluafix_open(_state);
// Register our version of the global "print" function
const luaL_reg global_functions [] = {
{"print", lua_print},
{"release_print",lua_release_print},
{nullptr, nullptr}
};
luaL_register(_state, "_G", global_functions);
g_luaType.clear();
register_all_cocos2dx(_state);
register_all_cocos2dx 如下:
<pre name="code" class="cpp">TOLUA_API int register_all_cocos2dx(lua_State* tolua_S)
{
tolua_open(tolua_S);
tolua_module(tolua_S,"cc",0);
tolua_beginmodule(tolua_S,"cc");
lua_register_cocos2dx_Ref(tolua_S);
<span style="white-space:pre"> </span>...
<span style="white-space:pre"> </span>
tolua_endmodule(tolua_S);
<span style="white-space:pre"> return 1;</span>
}
TOLUA_API void tolua_open (lua_State* L)
{
int top = lua_gettop(L);
lua_pushstring(L,"tolua_opened");
lua_rawget(L,LUA_REGISTRYINDEX);
if (!lua_isboolean(L,-1))
{
lua_pushstring(L,"to