36 m_cppCallbackDepth = 0;
57 assert(stdext::demangle_class<LuaObject>() ==
"LuaObject");
60 registerClass<LuaObject>();
67 obj->luaGetFieldsTable();
76 assert(m_totalFuncRefs == 0);
77 assert(m_totalObjRefs == 0);
94 const int klass =
getTop();
100 int klass_fieldmethods =
getTop();
125 if (!className.empty() && className !=
"LuaObject") {
152 const std::string& functionName,
159 const std::string& functionName,
169 const std::string& field,
198 std::string key = lua->
toString(-1);
217 obj->luaGetField(key);
241 std::string key = lua->
toString(-2);
263 obj->luaSetField(key);
267 int LuaInterface::luaObjectEqualEvent(
LuaInterface* lua)
276 assert(objPtr1 && objPtr2);
277 if (*objPtr1 == *objPtr2)
287 int LuaInterface::luaObjectCollectEvent(
LuaInterface* lua)
295 g_lua.m_totalObjRefs--;
330 std::string filePath = fileName;
337 std::string source =
"@" + filePath;
343 if (buffer.empty()) {
368 if (!expression.empty()) {
447 int errorFuncIndex = previousStackSize - numArgs;
452 int ret =
pcall(numArgs, LUA_MULTRET, errorFuncIndex);
460 int rets = (
stackSize() + numArgs + 1) - previousStackSize;
461 while (numRets != -1 && rets != numRets) {
462 if (rets < numRets) {
479 int funcIndex = -numArgs - 1;
488 throw LuaException(
"function call didn't return the expected number of results", 0);
496 while (
next(funcIndex - 1)) {
499 for (
int i = 0; i < numArgs; ++i)
511 throw LuaException(
"function call didn't return the expected number of results", 0);
514 throw LuaException(
"attempt to call a non function", 0);
519 if (numRets == 1 || numRets == -1) {
525 else if (
isNil(funcIndex)) {
530 throw LuaException(
"attempt to call a non function value", 0);
538 while (numRets != -1 && rets < numRets) {
560 int LuaInterface::luaScriptLoader(lua_State*)
575 int LuaInterface::lua_dofile(lua_State*)
591 int LuaInterface::lua_dofiles(lua_State*)
593 std::string contains =
"";
598 bool recursive =
false;
609 int LuaInterface::lua_loadfile(lua_State*)
625 int LuaInterface::luaErrorHandler(lua_State*)
631 if (
error.find(
"stack traceback:") == std::string::npos)
639 int LuaInterface::luaCppFunctionCallback(lua_State*)
649 g_lua.m_cppCallbackDepth++;
650 numRets = (*(funcPtr->get()))(&
g_lua);
651 g_lua.m_cppCallbackDepth--;
666 int LuaInterface::luaCollectCppFunction(lua_State*)
671 g_lua.m_totalFuncRefs--;
698 m_weakTableRef =
ref();
732 static bool collecting =
false;
738 for (
int i = 0; i < 2; ++i)
739 lua_gc(L, LUA_GCCOLLECT, 0);
748 int ret = luaL_loadbuffer(L, buffer.c_str(), buffer.length(), source.c_str());
756 return lua_pcall(L, numArgs, numRets, errorFuncIndex);
762 lua_call(L, numArgs, numRets);
773 int ref = luaL_ref(L, LUA_REGISTRYINDEX);
774 assert(
ref != LUA_NOREF);
775 assert(
ref < 2147483647);
785 if (
id == 2147483647)
803 if (
ref >= 0 && L !=
nullptr)
804 luaL_unref(L, LUA_REGISTRYINDEX,
ref);
810 int type = lua_type(L, index);
811 return lua_typename(L, type);
820 memset(&ar, 0,
sizeof(ar));
821 lua_getinfo(L,
">Sn", &ar);
824 if (ar.source[0] ==
'@') {
826 path = path.substr(1, path.find_last_of(
"/") - 1);
827 path = path.substr(0, path.find_last_of(
":"));
837 lua_insert(L, index);
843 lua_remove(L, index);
849 return lua_next(L, index);
855 if (lua_getstack(L, level, &ar) == 1)
856 lua_getinfo(L,
"f", &ar);
863 lua_rawgeti(L, LUA_REGISTRYINDEX,
ref);
886 lua_setmetatable(L, index);
892 lua_getmetatable(L, index);
899 lua_getfield(L, index, key);
906 lua_setfield(L, index, key);
912 lua_getfenv(L, index);
918 lua_setfenv(L, index);
924 lua_gettable(L, index);
930 lua_settable(L, index);
939 while (
next(index - 1)) {
942 if (
next(index - 2)) {
958 lua_getglobal(L, key.c_str());
974 lua_setglobal(L, key.c_str());
980 lua_rawget(L, index);
986 lua_rawgeti(L, index, n);
992 lua_rawset(L, index);
998 lua_rawseti(L, index, n);
1008 lua_createtable(L, narr, nrec);
1013 return lua_newuserdata(L, size);
1074 return lua_touserdata(L, lua_upvalueindex(1));
1085 lua_pushinteger(L, v);
1091 lua_pushnumber(L, v);
1097 lua_pushboolean(L, v);
1104 lua_pushstring(L, v);
1110 lua_pushlstring(L, v.c_str(), v.length());
1116 lua_pushlightuserdata(L, p);
1132 obj->luaGetMetatable();
1140 lua_pushcclosure(L, func, n);
1163 lua_pushvalue(L, index);
1170 return lua_isnil(L, index);
1176 return lua_isboolean(L, index);
1182 return lua_isnumber(L, index);
1188 return lua_isstring(L, index);
1194 return lua_istable(L, index);
1200 return lua_isfunction(L, index);
1206 return lua_iscfunction(L, index);
1212 return lua_isuserdata(L, index);
1218 return static_cast<bool>(lua_toboolean(L, index));
1224 return lua_tointeger(L, index);
1230 return lua_tonumber(L, index);
1236 return lua_tostring(L, index);
1244 const char* c_str = lua_tolstring(L, index, &len);
1245 if (c_str && len > 0)
1246 str.assign(c_str, len);
1253 return lua_touserdata(L, index);
1261 if (objRef && *objRef)
1269 return lua_gettop(L);
1275 std::string fullPath = directory +
"/" + fileName;
1285 if (!contains.empty() && fileName.find(contains) == std::string::npos)