int luaQ_tracebackskip(struct lua_State *L, int skip) { // stack: msg luaQ_getfield(L, LUA_GLOBALSINDEX, "debug"); luaQ_getfield(L, -1, "traceback"); // stack: traceback debug msg lua_remove(L, -2); // stack: traceback msg if (! lua_isfunction(L, -1)) { lua_pop(L, 1); } else { lua_pushvalue(L, -2); lua_pushinteger(L, skip+1); // save hook lua_Hook hf = lua_gethook(L); int hm = lua_gethookmask(L); int hc = lua_gethookcount(L); lua_sethook(L, 0, 0, 0); // stack: skip msg traceback msg if (lua_pcall(L, 2, 1, 0)) // stack: err msg lua_remove(L, -1); else // stack: msg msg lua_remove(L, -2); // restore hook lua_sethook(L, hf, hm, hc); } // stack: msg return 1; }
static const char *pushfilename (lua_State *L, const char *name) { const char *path; const char *filename; luaQ_getfield(L, LUA_GLOBALSINDEX, "package"); luaQ_getfield(L, -1, "cpath"); lua_remove(L, -2); if (! (path = lua_tostring(L, -1))) luaL_error(L, LUA_QL("package.cpath") " must be a string"); lua_pushliteral(L, ""); while ((path = pushnexttemplate(L, path))) { filename = luaL_gsub(L, lua_tostring(L, -1), "?", name); lua_remove(L, -2); if (readable(filename)) { // stack: cpath errmsg filename lua_remove(L, -3); lua_remove(L, -2); return lua_tostring(L, -1); } lua_pushfstring(L, "\n\tno file " LUA_QS, filename); lua_remove(L, -2); /* remove file name */ lua_concat(L, 2); /* add entry to possible error message */ } lua_pushfstring(L, "module" LUA_QS "not found", name); lua_replace(L, -3); lua_concat(L, 2); lua_error(L); return 0; }
LUA_EXTERNC QTUILOADER_API int luaopen_libqtuiloader(lua_State *L) { // load module 'qt' if (luaL_dostring(L, "require 'qt'")) lua_error(L); // enrichs class QUiLoader. luaQ_pushmeta(L, &QUiLoader::staticMetaObject); luaQ_getfield(L, -1, "__metatable"); luaQ_register(L, qtuiloader_lib, QCoreApplication::instance()); return 0; }
int luaopen_libqttorch(lua_State *L) { // load module 'qt' if (luaL_dostring(L, "require 'qt'")) lua_error(L); // load modules 'torch' if (luaL_dostring(L, "require 'torch'")) lua_error(L); torch_Tensor_id = luaT_checktypename2id(L, "torch.Tensor"); // enrichs QImage luaQ_pushmeta(L, QMetaType::QImage); luaQ_getfield(L, -1, "__metatable"); luaL_register(L, 0, qttorch_qimage_lib); return 0; }
int luaQ_complete(struct lua_State *L) { int k = 0; int loop = 0; const char *stem = luaL_checkstring(L, 1); lua_pushvalue(L, LUA_GLOBALSINDEX); for(;;) { const char *s = stem; while (*s && *s != '.' && *s != ':') s++; if (*s == 0) break; // stack: table str lua_pushlstring(L, stem, s-stem); lua_gettable(L, -2); // stack: ntable table str lua_replace(L, -2); // stack: ntable str stem = s + 1; } lua_createtable(L, 0, 0); lua_insert(L, -2); // stack: maybetable anstable str if (lua_isuserdata(L, -1) && lua_getmetatable(L, -1)) { lua_replace(L, -2); lua_pushliteral(L, "__index"); lua_rawget(L, -2); if (lua_isfunction(L, -1)) { lua_pop(L, 1); lua_pushliteral(L, "__metatable"); lua_rawget(L, -2); } lua_replace(L, -2); } if (! lua_istable(L, -1)) { lua_pop(L, 1); return 1; } // stack: table anstable str size_t stemlen = strlen(stem); for(;;) { lua_pushnil(L); while (lua_next(L, -2)) { // stack: value key table anstable str bool ok = false; size_t keylen; const char *key = lua_tolstring(L, -2, &keylen); if (key && keylen > 0 && keylen >= stemlen) if (!strncmp(key, stem, stemlen)) ok = true; if (ok && !isalpha(key[0])) ok = false; if (ok) for (int i=0; ok && i<(int)keylen; i++) if (!isalpha(key[i]) && !isdigit(key[i]) && key[i]!='_') ok = false; if (ok) { const char *suffix = ""; switch (lua_type(L, -1)) { case LUA_TFUNCTION: suffix = "("; break; case LUA_TTABLE: suffix = "."; luaQ_getfield(L, -1, "_C"); if (lua_istable(L, -1)) suffix = ":"; lua_pop(L, 1); break; case LUA_TUSERDATA: { QVariant v = luaQ_toqvariant(L, -1); const char *s = QMetaType::typeName(v.userType()); if (s && !strcmp(s,"QtLuaMethodInfo")) suffix = "("; else if (s && !strcmp(s, "QtLuaPropertyInfo")) suffix = ""; else if (lua_getmetatable(L, -1)) { lua_pop(L, 1); suffix = ":"; } else suffix = ""; } break; default: break; } // stack: value key table anstable str lua_pushfstring(L, "%s%s", key+stemlen, suffix); lua_rawseti(L, -5, ++k); } lua_pop(L, 1); } // stack: table anstable str if (! lua_getmetatable(L, -1)) break; lua_replace(L, -2); lua_pushliteral(L, "__index"); lua_rawget(L, -2); lua_replace(L, -2); if (! lua_istable(L, -1)) break; if (++loop > 100) luaL_error(L, "complete: infinite loop in metatables"); } // stack: something anstable str lua_pop(L, 1); lua_replace(L, -2); return 1; }