3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 LuaError::LuaError(lua_State *L, const std::string &s)
38 m_s += script_get_backtrace(L);
41 std::string script_get_backtrace(lua_State *L)
44 lua_getfield(L, LUA_GLOBALSINDEX, "debug");
45 if(lua_istable(L, -1)){
46 lua_getfield(L, -1, "traceback");
47 if(lua_isfunction(L, -1)){
49 if(lua_isstring(L, -1)){
50 s += lua_tostring(L, -1);
62 void script_error(lua_State *L, const char *fmt, ...)
67 vsnprintf(buf, 10000, fmt, argp);
69 //errorstream<<"SCRIPT ERROR: "<<buf;
70 throw LuaError(L, buf);
73 int luaErrorHandler(lua_State *L) {
74 lua_getfield(L, LUA_GLOBALSINDEX, "debug");
75 if (!lua_istable(L, -1)) {
79 lua_getfield(L, -1, "traceback");
80 if (!lua_isfunction(L, -1)) {
85 lua_pushinteger(L, 2);
90 bool script_load(lua_State *L, const char *path)
92 verbosestream<<"Loading and running script from "<<path<<std::endl;
94 lua_pushcfunction(L, luaErrorHandler);
95 int errorhandler = lua_gettop(L);
97 int ret = luaL_loadfile(L, path) || lua_pcall(L, 0, 0, errorhandler);
99 errorstream<<"========== ERROR FROM LUA ==========="<<std::endl;
100 errorstream<<"Failed to load and run script from "<<std::endl;
101 errorstream<<path<<":"<<std::endl;
102 errorstream<<std::endl;
103 errorstream<<lua_tostring(L, -1)<<std::endl;
104 errorstream<<std::endl;
105 errorstream<<"=======END OF ERROR FROM LUA ========"<<std::endl;
106 lua_pop(L, 1); // Pop error message from stack
107 lua_pop(L, 1); // Pop the error handler from stack
110 lua_pop(L, 1); // Pop the error handler from stack
114 lua_State* script_init()
116 lua_State *L = luaL_newstate();
121 void script_deinit(lua_State *L)