From 9ea18abb49a760fe1eda197c02cbdcd680b47204 Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Sun, 22 Aug 2010 13:32:24 +0200 Subject: Pass a script name to Lua for proper reporting of error locations Now the Lua file name shows up in the error message and stack traceback, or the map file and object name in case of a script embedded in a map file. --- src/game-server/mapreader.cpp | 3 ++- src/scripting/luascript.cpp | 10 +++++----- src/scripting/luascript.hpp | 2 +- src/scripting/script.cpp | 4 ++-- src/scripting/script.hpp | 5 ++++- 5 files changed, 14 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/game-server/mapreader.cpp b/src/game-server/mapreader.cpp index 80a0646d..16e4660e 100644 --- a/src/game-server/mapreader.cpp +++ b/src/game-server/mapreader.cpp @@ -369,7 +369,8 @@ Map* MapReader::readMap(xmlNodePtr node, const std::string &path, } else if (!scriptText.empty()) { - s->load(scriptText.c_str()); + const std::string name = "'" + objName + "'' in " + path; + s->load(scriptText.c_str(), name.c_str()); } else { diff --git a/src/scripting/luascript.cpp b/src/scripting/luascript.cpp index ee727060..23e4fdb9 100644 --- a/src/scripting/luascript.cpp +++ b/src/scripting/luascript.cpp @@ -19,14 +19,14 @@ * along with The Mana Server. If not, see . */ -#include - #include "luascript.hpp" #include "game-server/being.hpp" - #include "utils/logger.h" +#include +#include + LuaScript::~LuaScript() { lua_close(mState); @@ -83,9 +83,9 @@ int LuaScript::execute() mCurFunction = ""; } -void LuaScript::load(const char *prog) +void LuaScript::load(const char *prog, const char *name) { - int res = luaL_loadstring(mState, prog); + int res = luaL_loadbuffer(mState, prog, std::strlen(prog), name); if (res) { switch (res) { diff --git a/src/scripting/luascript.hpp b/src/scripting/luascript.hpp index 5e4deea1..292c35ad 100644 --- a/src/scripting/luascript.hpp +++ b/src/scripting/luascript.hpp @@ -45,7 +45,7 @@ class LuaScript: public Script */ ~LuaScript(); - void load(const char *); + void load(const char *prog, const char *name); void prepare(const std::string &); diff --git a/src/scripting/script.cpp b/src/scripting/script.cpp index 3d16a7d1..334895c3 100644 --- a/src/scripting/script.cpp +++ b/src/scripting/script.cpp @@ -88,7 +88,7 @@ bool Script::loadFile(const std::string &name) if (buffer) { mScriptFile = name; - load(skipPotentialBom(buffer)); + load(skipPotentialBom(buffer), name.c_str()); free(buffer); return true; } else { @@ -99,7 +99,7 @@ bool Script::loadFile(const std::string &name) void Script::loadNPC(const std::string &name, int id, int x, int y, const char *prog) { - load(prog); + load(prog, name.c_str()); prepare("create_npc_delayed"); push(name); push(id); diff --git a/src/scripting/script.hpp b/src/scripting/script.hpp index dfba06f0..6f5dded6 100644 --- a/src/scripting/script.hpp +++ b/src/scripting/script.hpp @@ -61,8 +61,11 @@ class Script /** * Loads a chunk of text into script context and executes its global * statements. + * + * @param prog the program text to load + * @param name the name of the text, used for error reporting */ - virtual void load(const char *) = 0; + virtual void load(const char *prog, const char *name) = 0; /** * Loads a text file into script context and executes its global -- cgit v1.2.3-70-g09d2