axmol/extensions/scripting/lua-bindings/manual/tolua_fix.cpp

278 lines
9.5 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
https://axis-project.github.io/
2021-12-25 10:04:45 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-25 10:04:45 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "scripting/lua-bindings/manual/tolua_fix.h"
#include "base/CCRef.h"
#include "scripting/lua-bindings/manual/LuaBasicConversions.h"
2014-03-10 14:04:58 +08:00
#include <stdlib.h>
using namespace cocos2d;
static int s_function_ref_id = 0;
TOLUA_API void toluafix_open(lua_State* L)
{
lua_pushstring(L, TOLUA_REFID_PTR_MAPPING);
lua_newtable(L);
lua_rawset(L, LUA_REGISTRYINDEX);
lua_pushstring(L, TOLUA_REFID_TYPE_MAPPING);
lua_newtable(L);
lua_rawset(L, LUA_REGISTRYINDEX);
lua_pushstring(L, TOLUA_REFID_FUNCTION_MAPPING);
lua_newtable(L);
lua_rawset(L, LUA_REGISTRYINDEX);
}
2021-12-25 10:04:45 +08:00
TOLUA_API int toluafix_pushusertype_ccobject(lua_State* L, int refid, int* p_refid, void* ptr, const char* type)
2014-03-10 14:04:58 +08:00
{
if (ptr == NULL || p_refid == NULL)
{
lua_pushnil(L);
return -1;
}
2021-12-25 10:04:45 +08:00
Ref* vPtr = static_cast<Ref*>(ptr);
2014-03-10 14:04:58 +08:00
const char* vType = getLuaTypeName(vPtr, type);
if (*p_refid == 0)
{
*p_refid = refid;
lua_pushstring(L, TOLUA_REFID_PTR_MAPPING);
2021-12-25 10:04:45 +08:00
lua_rawget(L, LUA_REGISTRYINDEX); /* stack: refid_ptr */
lua_pushinteger(L, refid); /* stack: refid_ptr refid */
lua_pushlightuserdata(L, vPtr); /* stack: refid_ptr refid ptr */
2014-03-10 14:04:58 +08:00
2021-12-25 10:04:45 +08:00
lua_rawset(L, -3); /* refid_ptr[refid] = ptr, stack: refid_ptr */
lua_pop(L, 1); /* stack: - */
2014-03-10 14:04:58 +08:00
lua_pushstring(L, TOLUA_REFID_TYPE_MAPPING);
2021-12-25 10:04:45 +08:00
lua_rawget(L, LUA_REGISTRYINDEX); /* stack: refid_type */
lua_pushinteger(L, refid); /* stack: refid_type refid */
lua_pushstring(L, vType); /* stack: refid_type refid type */
2014-03-10 14:04:58 +08:00
lua_rawset(L, -3); /* refid_type[refid] = type, stack: refid_type */
2021-12-25 10:04:45 +08:00
lua_pop(L, 1); /* stack: - */
2014-03-10 14:04:58 +08:00
2021-12-25 10:04:45 +08:00
// printf("[LUA] push CCObject OK - refid: %d, ptr: %x, type: %s\n", *p_refid, (int)ptr, type);
2014-03-10 14:04:58 +08:00
}
tolua_pushusertype_and_addtoroot(L, vPtr, vType);
2021-12-25 10:04:45 +08:00
2014-03-10 14:04:58 +08:00
return 0;
}
TOLUA_API int toluafix_remove_ccobject_by_refid(lua_State* L, int refid)
{
2021-12-25 10:04:45 +08:00
void* ptr = NULL;
2014-03-10 14:04:58 +08:00
const char* type = NULL;
2021-12-25 10:04:45 +08:00
void** ud = NULL;
if (refid == 0)
return -1;
2014-03-10 14:04:58 +08:00
// get ptr from tolua_refid_ptr_mapping
lua_pushstring(L, TOLUA_REFID_PTR_MAPPING);
2021-12-25 10:04:45 +08:00
lua_rawget(L, LUA_REGISTRYINDEX); /* stack: refid_ptr */
lua_pushinteger(L, refid); /* stack: refid_ptr refid */
lua_rawget(L, -2); /* stack: refid_ptr ptr */
2014-03-10 14:04:58 +08:00
ptr = lua_touserdata(L, -1);
2021-12-25 10:04:45 +08:00
lua_pop(L, 1); /* stack: refid_ptr */
2014-03-10 14:04:58 +08:00
if (ptr == NULL)
{
lua_pop(L, 1);
// Lua stack has closed, C++ object not in Lua.
// printf("[LUA ERROR] remove CCObject with NULL ptr, refid: %d\n", refid);
return -2;
}
// remove ptr from tolua_refid_ptr_mapping
2021-12-25 10:04:45 +08:00
lua_pushinteger(L, refid); /* stack: refid_ptr refid */
lua_pushnil(L); /* stack: refid_ptr refid nil */
lua_rawset(L, -3); /* delete refid_ptr[refid], stack: refid_ptr */
lua_pop(L, 1); /* stack: - */
2014-03-10 14:04:58 +08:00
// get type from tolua_refid_type_mapping
lua_pushstring(L, TOLUA_REFID_TYPE_MAPPING);
2021-12-25 10:04:45 +08:00
lua_rawget(L, LUA_REGISTRYINDEX); /* stack: refid_type */
lua_pushinteger(L, refid); /* stack: refid_type refid */
lua_rawget(L, -2); /* stack: refid_type type */
2014-03-10 14:04:58 +08:00
if (lua_isnil(L, -1))
{
lua_pop(L, 2);
printf("[LUA ERROR] remove CCObject with NULL type, refid: %d, ptr: %p\n", refid, ptr);
return -1;
}
type = lua_tostring(L, -1);
2021-12-25 10:04:45 +08:00
lua_pop(L, 1); /* stack: refid_type */
2014-03-10 14:04:58 +08:00
// remove type from tolua_refid_type_mapping
2021-12-25 10:04:45 +08:00
lua_pushinteger(L, refid); /* stack: refid_type refid */
lua_pushnil(L); /* stack: refid_type refid nil */
lua_rawset(L, -3); /* delete refid_type[refid], stack: refid_type */
lua_pop(L, 1); /* stack: - */
2014-03-10 14:04:58 +08:00
// get ubox
2021-12-25 10:04:45 +08:00
luaL_getmetatable(L, type); /* stack: mt */
lua_pushstring(L, "tolua_ubox"); /* stack: mt key */
lua_rawget(L, -2); /* stack: mt ubox */
2014-03-10 14:04:58 +08:00
if (lua_isnil(L, -1))
{
// use global ubox
2021-12-25 10:04:45 +08:00
lua_pop(L, 1); /* stack: mt */
lua_pushstring(L, "tolua_ubox"); /* stack: mt key */
lua_rawget(L, LUA_REGISTRYINDEX); /* stack: mt ubox */
2014-03-10 14:04:58 +08:00
};
2021-12-25 10:04:45 +08:00
2014-03-10 14:04:58 +08:00
// cleanup root
tolua_remove_value_from_root(L, ptr);
2021-12-25 10:04:45 +08:00
lua_pushlightuserdata(L, ptr); /* stack: mt ubox ptr */
lua_rawget(L, -2); /* stack: mt ubox ud */
2014-03-10 14:04:58 +08:00
if (lua_isnil(L, -1))
{
// Lua object has released (GC), C++ object not in ubox.
2021-12-25 10:04:45 +08:00
// printf("[LUA ERROR] remove CCObject with NULL ubox, refid: %d, ptr: %x, type: %s\n", refid, (int)ptr, type);
2014-03-10 14:04:58 +08:00
lua_pop(L, 3);
return -3;
}
// cleanup peertable
lua_pushvalue(L, LUA_REGISTRYINDEX);
lua_setfenv(L, -2);
ud = (void**)lua_touserdata(L, -1);
2021-12-25 10:04:45 +08:00
lua_pop(L, 1); /* stack: mt ubox */
2014-03-10 14:04:58 +08:00
if (ud == NULL)
{
printf("[LUA ERROR] remove CCObject with NULL userdata, refid: %d, ptr: %p, type: %s\n", refid, ptr, type);
lua_pop(L, 2);
return -1;
}
// clean userdata
*ud = NULL;
2021-12-25 10:04:45 +08:00
lua_pushlightuserdata(L, ptr); /* stack: mt ubox ptr */
lua_pushnil(L); /* stack: mt ubox ptr nil */
lua_rawset(L, -3); /* ubox[ptr] = nil, stack: mt ubox */
2014-03-10 14:04:58 +08:00
lua_pop(L, 2);
2021-12-25 10:04:45 +08:00
// printf("[LUA] remove CCObject, refid: %d, ptr: %x, type: %s\n", refid, (int)ptr, type);
2014-03-10 14:04:58 +08:00
return 0;
}
TOLUA_API int toluafix_ref_function(lua_State* L, int lo, int def)
{
// function at lo
2021-12-25 10:04:45 +08:00
if (!lua_isfunction(L, lo))
return 0;
2014-03-10 14:04:58 +08:00
s_function_ref_id++;
lua_pushstring(L, TOLUA_REFID_FUNCTION_MAPPING);
2021-12-25 10:04:45 +08:00
lua_rawget(L, LUA_REGISTRYINDEX); /* stack: fun ... refid_fun */
lua_pushinteger(L, s_function_ref_id); /* stack: fun ... refid_fun refid */
lua_pushvalue(L, lo); /* stack: fun ... refid_fun refid fun */
2014-03-10 14:04:58 +08:00
2021-12-25 10:04:45 +08:00
lua_rawset(L, -3); /* refid_fun[refid] = fun, stack: fun ... refid_ptr */
lua_pop(L, 1); /* stack: fun ... */
2014-03-10 14:04:58 +08:00
return s_function_ref_id;
// lua_pushvalue(L, lo); /* stack: ... func */
// return luaL_ref(L, LUA_REGISTRYINDEX);
}
TOLUA_API void toluafix_get_function_by_refid(lua_State* L, int refid)
{
lua_pushstring(L, TOLUA_REFID_FUNCTION_MAPPING);
2021-12-25 10:04:45 +08:00
lua_rawget(L, LUA_REGISTRYINDEX); /* stack: ... refid_fun */
lua_pushinteger(L, refid); /* stack: ... refid_fun refid */
lua_rawget(L, -2); /* stack: ... refid_fun fun */
lua_remove(L, -2); /* stack: ... fun */
2014-03-10 14:04:58 +08:00
}
TOLUA_API void toluafix_remove_function_by_refid(lua_State* L, int refid)
{
lua_pushstring(L, TOLUA_REFID_FUNCTION_MAPPING);
2021-12-25 10:04:45 +08:00
lua_rawget(L, LUA_REGISTRYINDEX); /* stack: ... refid_fun */
lua_pushinteger(L, refid); /* stack: ... refid_fun refid */
lua_pushnil(L); /* stack: ... refid_fun refid nil */
lua_rawset(L, -3); /* refid_fun[refid] = fun, stack: ... refid_ptr */
lua_pop(L, 1); /* stack: ... */
2014-03-10 14:04:58 +08:00
// luaL_unref(L, LUA_REGISTRYINDEX, refid);
}
2016-03-29 09:52:17 +08:00
// check lua value is function
2014-03-10 14:04:58 +08:00
TOLUA_API int toluafix_isfunction(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
{
if (lua_gettop(L) >= abs(lo) && lua_isfunction(L, lo))
{
return 1;
}
err->index = lo;
err->array = 0;
2021-12-25 10:04:45 +08:00
err->type = "[not function]";
2014-03-10 14:04:58 +08:00
return 0;
}
TOLUA_API int toluafix_totable(lua_State* L, int lo, int def)
{
return lo;
}
TOLUA_API int toluafix_istable(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
{
return tolua_istable(L, lo, def, err);
}
TOLUA_API void toluafix_stack_dump(lua_State* L, const char* label)
{
int i;
int top = lua_gettop(L);
printf("Total [%d] in lua stack: %s\n", top, label != 0 ? label : "");
for (i = -1; i >= -top; i--)
{
int t = lua_type(L, i);
switch (t)
{
2021-12-25 10:04:45 +08:00
case LUA_TSTRING:
printf(" [%02d] string %s\n", i, lua_tostring(L, i));
break;
case LUA_TBOOLEAN:
printf(" [%02d] boolean %s\n", i, lua_toboolean(L, i) ? "true" : "false");
break;
case LUA_TNUMBER:
printf(" [%02d] number %g\n", i, lua_tonumber(L, i));
break;
default:
printf(" [%02d] %s\n", i, lua_typename(L, t));
2014-03-10 14:04:58 +08:00
}
}
printf("\n");
}