/* Type casting */ static int tolua_bnd_cast (lua_State* L) { /* // old code void* v = tolua_tousertype(L,1,NULL); const char* s = tolua_tostring(L,2,NULL); if (v && s) tolua_pushusertype(L,v,s); else lua_pushnil(L); return 1; */ void* v; const char* s; if (lua_islightuserdata(L, 1)) { v = tolua_touserdata(L, 1, NULL); } else { v = tolua_tousertype(L, 1, 0); }; s = tolua_tostring(L,2,NULL); if (v && s) tolua_pushusertype(L,v,s); else lua_pushnil(L); return 1; }
template<> void* ToluaToPODVector<Vector2>(lua_State* L, int narg, void* def) { if (!lua_istable(L, narg)) return 0; static PODVector<Vector2> result; result.Clear(); tolua_Error tolua_err; int length = lua_objlen(L, narg); for (int i = 1; i <= length; ++i) { lua_pushinteger(L, i); lua_gettable(L, narg); if (!tolua_isusertype(L, -1, "Vector2", 0, &tolua_err)) { lua_pop(L, 1); return 0; } Vector2* value = (Vector2*)tolua_touserdata(L, -1, 0); result.Push(*value); lua_pop(L, 1); } return &result; }
int cLuaState::CopyStackFrom(cLuaState & a_SrcLuaState, int a_SrcStart, int a_SrcEnd) { /* // DEBUG: LOGD("Copying stack values from %d to %d", a_SrcStart, a_SrcEnd); a_SrcLuaState.LogStack("Src stack before copying:"); LogStack("Dst stack before copying:"); */ for (int i = a_SrcStart; i <= a_SrcEnd; ++i) { int t = lua_type(a_SrcLuaState, i); switch (t) { case LUA_TNIL: { lua_pushnil(m_LuaState); break; } case LUA_TSTRING: { AString s; a_SrcLuaState.ToString(i, s); Push(s); break; } case LUA_TBOOLEAN: { bool b = (tolua_toboolean(a_SrcLuaState, i, false) != 0); Push(b); break; } case LUA_TNUMBER: { lua_Number d = tolua_tonumber(a_SrcLuaState, i, 0); Push(d); break; } case LUA_TUSERDATA: { // Get the class name: const char * type = nullptr; if (lua_getmetatable(a_SrcLuaState, i) == 0) { LOGWARNING("%s: Unknown class in pos %d, cannot copy.", __FUNCTION__, i); lua_pop(m_LuaState, i - a_SrcStart); return -1; } lua_rawget(a_SrcLuaState, LUA_REGISTRYINDEX); // Stack +1 type = lua_tostring(a_SrcLuaState, -1); lua_pop(a_SrcLuaState, 1); // Stack -1 // Copy the value: void * ud = tolua_touserdata(a_SrcLuaState, i, nullptr); tolua_pushusertype(m_LuaState, ud, type); break; } default: { LOGWARNING("%s: Unsupported value: '%s' at stack position %d. Can only copy numbers, strings, bools and classes!", __FUNCTION__, lua_typename(a_SrcLuaState, t), i ); a_SrcLuaState.LogStack("Stack where copying failed:"); lua_pop(m_LuaState, i - a_SrcStart); return -1; } } } return a_SrcEnd - a_SrcStart + 1; }
bool cLuaState::CopySingleValueFrom(cLuaState & a_SrcLuaState, int a_StackIdx, int a_NumAllowedNestingLevels) { int t = lua_type(a_SrcLuaState, a_StackIdx); switch (t) { case LUA_TNIL: { lua_pushnil(m_LuaState); return true; } case LUA_TSTRING: { AString s; a_SrcLuaState.ToString(a_StackIdx, s); Push(s); return true; } case LUA_TBOOLEAN: { bool b = (tolua_toboolean(a_SrcLuaState, a_StackIdx, false) != 0); Push(b); return true; } case LUA_TNUMBER: { lua_Number d = tolua_tonumber(a_SrcLuaState, a_StackIdx, 0); Push(d); return true; } case LUA_TUSERDATA: { // Get the class name: const char * type = nullptr; if (lua_getmetatable(a_SrcLuaState, a_StackIdx) == 0) { LOGWARNING("%s: Unknown class in pos %d, cannot copy.", __FUNCTION__, a_StackIdx); return false; } lua_rawget(a_SrcLuaState, LUA_REGISTRYINDEX); // Stack +1 type = lua_tostring(a_SrcLuaState, -1); lua_pop(a_SrcLuaState, 1); // Stack -1 // Copy the value: void * ud = tolua_touserdata(a_SrcLuaState, a_StackIdx, nullptr); tolua_pushusertype(m_LuaState, ud, type); return true; } case LUA_TTABLE: { if (!CopyTableFrom(a_SrcLuaState, a_StackIdx, a_NumAllowedNestingLevels - 1)) { LOGWARNING("%s: Failed to copy table in pos %d.", __FUNCTION__, a_StackIdx); return false; } return true; } default: { LOGWARNING("%s: Unsupported value: '%s' at stack position %d. Can only copy numbers, strings, bools, classes and simple tables!", __FUNCTION__, lua_typename(a_SrcLuaState, t), a_StackIdx ); return false; } } }
int collect_baseobject(lua_State* L) { CBaseObject *self = (CBaseObject*)tolua_touserdata(L,1,0); delete self; return 0; }
int collect_player_trade(lua_State* L) { CPlayerTrade *self = (CPlayerTrade*)tolua_touserdata(L,1,0); delete self; return 0; }