36 m_cppCallbackDepth = 0;
57 assert(stdext::demangle_class<LuaObject>() ==
"LuaObject");
60 registerClass<LuaObject>();
66 obj->luaGetFieldsTable();
75 assert(m_totalFuncRefs == 0);
76 assert(m_totalObjRefs == 0);
93 const int klass =
getTop();
99 int klass_fieldmethods =
getTop();
124 if(!className.empty() && className !=
"LuaObject") {
151 const std::string& functionName,
158 const std::string& functionName,
168 const std::string& field,
197 std::string key = lua->
toString(-1);
216 obj->luaGetField(key);
240 std::string key = lua->
toString(-2);
262 obj->luaSetField(key);
266 int LuaInterface::luaObjectEqualEvent(
LuaInterface* lua)
275 assert(objPtr1 && objPtr2);
276 if(*objPtr1 == *objPtr2)
285 int LuaInterface::luaObjectCollectEvent(
LuaInterface* lua)
293 g_lua.m_totalObjRefs--;
327 std::string filePath = fileName;
334 std::string source =
"@" + filePath;
365 if(!expression.empty()) {
440 int errorFuncIndex = previousStackSize - numArgs;
445 int ret =
pcall(numArgs, LUA_MULTRET, errorFuncIndex);
453 int rets = (
stackSize() + numArgs + 1) - previousStackSize;
454 while(numRets != -1 && rets != numRets) {
471 int funcIndex = -numArgs-1;
480 throw LuaException(
"function call didn't return the expected number of results", 0);
488 while(
next(funcIndex-1)) {
491 for(
int i=0;i<numArgs;++i)
502 throw LuaException(
"function call didn't return the expected number of results", 0);
504 throw LuaException(
"attempt to call a non function", 0);
509 if(numRets == 1 || numRets == -1) {
515 else if(
isNil(funcIndex)) {
520 throw LuaException(
"attempt to call a non function value", 0);
527 while(numRets != -1 && rets < numRets) {
549 int LuaInterface::luaScriptLoader(lua_State* L)
563 int LuaInterface::lua_dofile(lua_State* L)
578 int LuaInterface::lua_dofiles(lua_State* L)
580 std::string contains =
"";
585 bool recursive =
false;
596 int LuaInterface::lua_loadfile(lua_State* L)
611 int LuaInterface::luaErrorHandler(lua_State* L)
617 if(
error.find(
"stack traceback:") == std::string::npos)
625 int LuaInterface::luaCppFunctionCallback(lua_State* L)
635 g_lua.m_cppCallbackDepth++;
636 numRets = (*(funcPtr->get()))(&
g_lua);
637 g_lua.m_cppCallbackDepth--;
651 int LuaInterface::luaCollectCppFunction(lua_State* L)
656 g_lua.m_totalFuncRefs--;
683 m_weakTableRef =
ref();
717 static bool collecting =
false;
724 lua_gc(L, LUA_GCCOLLECT, 0);
733 int ret = luaL_loadbuffer(L, buffer.c_str(), buffer.length(), source.c_str());
741 return lua_pcall(L, numArgs, numRets, errorFuncIndex);
747 lua_call(L, numArgs, numRets);
758 int ref = luaL_ref(L, LUA_REGISTRYINDEX);
759 assert(
ref != LUA_NOREF);
760 assert(
ref < 2147483647);
788 if(
ref >= 0 && L !=
nullptr)
789 luaL_unref(L, LUA_REGISTRYINDEX,
ref);
795 int type = lua_type(L, index);
796 return lua_typename(L, type);
805 memset(&ar, 0,
sizeof(ar));
806 lua_getinfo(L,
">Sn", &ar);
809 if(ar.source[0] ==
'@') {
811 path = path.substr(1, path.find_last_of(
"/") - 1);
812 path = path.substr(0, path.find_last_of(
":"));
822 lua_insert(L, index);
828 lua_remove(L, index);
834 return lua_next(L, index);
840 if(lua_getstack(L, level, &ar) == 1)
841 lua_getinfo(L,
"f", &ar);
848 lua_rawgeti(L, LUA_REGISTRYINDEX,
ref);
871 lua_setmetatable(L, index);
877 lua_getmetatable(L, index);
884 lua_getfield(L, index, key);
891 lua_setfield(L, index, key);
897 lua_getfenv(L, index);
903 lua_setfenv(L, index);
909 lua_gettable(L, index);
915 lua_settable(L, index);
923 while(
next(index-1)) {
941 lua_getglobal(L, key.c_str());
957 lua_setglobal(L, key.c_str());
963 lua_rawget(L, index);
969 lua_rawgeti(L, index, n);
975 lua_rawset(L, index);
981 lua_rawseti(L, index, n);
991 lua_createtable(L, narr, nrec);
996 return lua_newuserdata(L, size);
1057 return lua_touserdata(L, lua_upvalueindex(1));
1068 lua_pushinteger(L, v);
1074 lua_pushnumber(L, v);
1080 lua_pushboolean(L, v);
1087 lua_pushstring(L, v);
1093 lua_pushlstring(L, v.c_str(), v.length());
1099 lua_pushlightuserdata(L, p);
1115 obj->luaGetMetatable();
1123 lua_pushcclosure(L, func, n);
1146 lua_pushvalue(L, index);
1153 return lua_isnil(L, index);
1159 return lua_isboolean(L, index);
1165 return lua_isnumber(L, index);
1171 return lua_isstring(L, index);
1177 return lua_istable(L, index);
1183 return lua_isfunction(L, index);
1189 return lua_iscfunction(L, index);
1195 return lua_isuserdata(L, index);
1201 return (
bool)lua_toboolean(L, index);
1207 return lua_tointeger(L, index);
1213 return lua_tonumber(L, index);
1219 return lua_tostring(L, index);
1227 const char *c_str = lua_tolstring(L, index, &len);
1228 if(c_str && len > 0)
1229 str.assign(c_str, len);
1236 return lua_touserdata(L, index);
1244 if(objRef && *objRef)
1252 return lua_gettop(L);
1258 std::string fullPath = directory +
"/" + fileName;
1268 if(!contains.empty() && fileName.find(contains) == std::string::npos)