From d96cd236f3e983ff35ef16f7c9979c8341d5ae0c Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sat, 3 Dec 2011 02:45:55 +0200 Subject: Enforced mod global naming convention and better error reporting --- src/script.cpp | 46 ++++++++++++++++++++- src/script.h | 11 ++--- src/scriptapi.cpp | 118 ++++++++++++++++++++++++++++++++++++++++-------------- src/server.cpp | 2 +- 4 files changed, 138 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/script.cpp b/src/script.cpp index 5a6c98026..8b1b7013e 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -31,6 +31,27 @@ extern "C" { #include } +LuaError::LuaError(lua_State *L, const std::string &s) +{ + m_s = "LuaError: "; + m_s += s + "\n"; + lua_getfield(L, LUA_GLOBALSINDEX, "debug"); + if(lua_istable(L, -1)){ + lua_getfield(L, -1, "traceback"); + if(lua_isfunction(L, -1)){ + lua_call(L, 0, 1); + if(lua_isstring(L, -1)){ + m_s += lua_tostring(L, -1); + } + lua_pop(L, 1); + } + else{ + lua_pop(L, 1); + } + } + lua_pop(L, 1); +} + void script_error(lua_State *L, const char *fmt, ...) { va_list argp; @@ -39,13 +60,34 @@ void script_error(lua_State *L, const char *fmt, ...) vsnprintf(buf, 10000, fmt, argp); va_end(argp); //errorstream<<"SCRIPT ERROR: "< #include +typedef struct lua_State lua_State; + class LuaError : public std::exception { public: - LuaError(const std::string &s) - { - m_s = "LuaError: "; - m_s += s; - } + LuaError(lua_State *L, const std::string &s); + virtual ~LuaError() throw() {} virtual const char * what() const throw() @@ -40,8 +39,6 @@ public: std::string m_s; }; -typedef struct lua_State lua_State; - lua_State* script_init(); void script_deinit(lua_State *L); void script_error(lua_State *L, const char *fmt, ...); diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index db6b7e86e..be1366737 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -107,6 +107,59 @@ public: } }; +class ModNameStorer +{ +private: + lua_State *L; +public: + ModNameStorer(lua_State *L_, const std::string modname): + L(L_) + { + // Store current modname in registry + lua_pushstring(L, modname.c_str()); + lua_setfield(L, LUA_REGISTRYINDEX, "minetest_current_modname"); + } + ~ModNameStorer() + { + // Clear current modname in registry + lua_pushnil(L); + lua_setfield(L, LUA_REGISTRYINDEX, "minetest_current_modname"); + } +}; + +std::string get_current_modname(lua_State *L) +{ + lua_getfield(L, LUA_REGISTRYINDEX, "minetest_current_modname"); + std::string modname = ""; + if(lua_type(L, -1) == LUA_TSTRING) + modname = lua_tostring(L, -1); + lua_pop(L, 1); + return modname; +} + +void check_modname_prefix(lua_State *L, std::string &name) +{ + if(name.size() == 0) + throw LuaError(L, std::string("Name is empty")); + + if(name[0] == ':'){ + name = name.substr(1); + return; + } + + std::string modname = get_current_modname(L); + assert(modname != ""); + + // For __builtin, anything goes + if(modname == "__builtin") + return; + + if(name.substr(0, modname.size()+1) != modname + "_") + throw LuaError(L, std::string("Name \"")+name + +"\" does not follow naming conventions: " + +"\"modname_\" or \":\" prefix required)"); +} + static v3f readFloatPos(lua_State *L, int index) { v3f pos; @@ -470,7 +523,7 @@ static void inventory_get_list_to_lua(Inventory *inv, const char *name, lua_pushstring(L, item->getItemString().c_str()); } if(lua_pcall(L, 2, 0, 0)) - script_error(L, "error: %s\n", lua_tostring(L, -1)); + script_error(L, "error: %s", lua_tostring(L, -1)); } } @@ -611,8 +664,9 @@ static int l_register_nodedef_defaults(lua_State *L) // register_entity(name, prototype) static int l_register_entity(lua_State *L) { - const char *name = luaL_checkstring(L, 1); - infostream<<"register_entity: "< stack top // registered_entities[name] = object - lua_setfield(L, registered_entities, name); + lua_setfield(L, registered_entities, name.c_str()); // Get registered object to top of stack lua_pushvalue(L, 2); @@ -703,14 +757,14 @@ public: lua_pushnumber(L, active_object_count); lua_pushnumber(L, active_object_count_wider); if(lua_pcall(L, 4, 0, 0)) - script_error(L, "error: %s\n", lua_tostring(L, -1)); + script_error(L, "error: %s", lua_tostring(L, -1)); } }; // register_abm({...}) static int l_register_abm(lua_State *L) { - infostream<<"register_abm"<registerCraftItem(name, def); lua_pushvalue(L, table); - scriptapi_add_craftitem(L, name); + scriptapi_add_craftitem(L, name.c_str()); return 0; /* number of results */ } @@ -814,8 +870,9 @@ static int l_register_craftitem(lua_State *L) // register_node(name, {lots of stuff}) static int l_register_node(lua_State *L) { - const char *name = luaL_checkstring(L, 1); - infostream<<"register_node: "<