From cf8808bec66f666af120b9c4fad666a558d0e989 Mon Sep 17 00:00:00 2001 From: dshaligram Date: Sat, 9 Aug 2008 09:42:53 +0000 Subject: Report the right line numbers for Lua errors in .crawlrc Lua. git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@6799 c06c8d41-db1a-0410-9941-cceddc491573 --- crawl-ref/source/initfile.cc | 50 +++++++++++++++++++++++--------------------- crawl-ref/source/luadgn.cc | 9 ++++++++ crawl-ref/source/luadgn.h | 11 +++++----- 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/crawl-ref/source/initfile.cc b/crawl-ref/source/initfile.cc index d160635440..4b5c252c9a 100644 --- a/crawl-ref/source/initfile.cc +++ b/crawl-ref/source/initfile.cc @@ -24,6 +24,7 @@ #include "chardump.h" #include "clua.h" +#include "luadgn.h" #include "delay.h" #include "directn.h" #include "Kills.h" @@ -1337,8 +1338,9 @@ void game_options::read_options(InitLineInput &il, bool runscript, if (clear_aliases) aliases.clear(); - std::string luacond; - std::string luacode; + dlua_chunk luacond(filename); + dlua_chunk luacode(filename); + while (!il.eof()) { std::string s = il.getline(); @@ -1359,12 +1361,12 @@ void game_options::read_options(InitLineInput &il, bool runscript, if (!str.empty() && runscript) { // If we're in the middle of an option block, close it. - if (luacond.length() && l_init) + if (!luacond.empty() && l_init) { - luacond += "]] )\n"; + luacond.add(line - 1, "]] )"); l_init = false; } - luacond += str + "\n"; + luacond.add(line, str); } continue; } @@ -1385,12 +1387,12 @@ void game_options::read_options(InitLineInput &il, bool runscript, if (!str.empty() && runscript) { // If we're in the middle of an option block, close it. - if (luacond.length() && l_init) + if (!luacond.empty() && l_init) { - luacond += "]] )\n"; + luacond.add(line - 1, "]] )"); l_init = false; } - luacond += str + "\n"; + luacond.add(line, str); } continue; } @@ -1400,13 +1402,13 @@ void game_options::read_options(InitLineInput &il, bool runscript, inscriptcond = false; str = str.substr(0, str.length() - 1); if (!str.empty() && runscript) - luacond += str + "\n"; + luacond.add(line, str); continue; } else if (inscriptcond) { if (runscript) - luacond += s + "\n"; + luacond.add(line, s); continue; } @@ -1415,6 +1417,7 @@ void game_options::read_options(InitLineInput &il, bool runscript, { inscriptblock = true; luacode.clear(); + luacode.set_file(filename); // Strip leading Lua[ str = str.substr( str.find("Lua{") == 0? 4 : 1 ); @@ -1426,14 +1429,14 @@ void game_options::read_options(InitLineInput &il, bool runscript, } if (!str.empty()) - luacode += str + "\n"; + luacode.add(line, str); if (!inscriptblock && runscript) { #ifdef CLUA_BINDINGS - clua.execstring(luacode.c_str()); - if (!clua.error.empty()) - mprf(MSGCH_ERROR, "Lua error: %s", clua.error.c_str()); + if (luacode.run(clua)) + mprf(MSGCH_ERROR, "Lua error: %s", + luacode.orig_error().c_str()); luacode.clear(); #endif } @@ -1446,9 +1449,9 @@ void game_options::read_options(InitLineInput &il, bool runscript, #ifdef CLUA_BINDINGS if (runscript) { - clua.execstring(luacode.c_str()); - if (!clua.error.empty()) - mprf(MSGCH_ERROR, "Lua error: %s", clua.error.c_str()); + if (luacode.run(clua)) + mprf(MSGCH_ERROR, "Lua error: %s", + luacode.orig_error().c_str()); } #endif luacode.clear(); @@ -1456,7 +1459,7 @@ void game_options::read_options(InitLineInput &il, bool runscript, } else if (inscriptblock) { - luacode += s + "\n"; + luacode.add(line, s); continue; } @@ -1464,11 +1467,11 @@ void game_options::read_options(InitLineInput &il, bool runscript, { if (!l_init) { - luacond += "crawl.setopt( [[\n"; + luacond.add(line, "crawl.setopt( [["); l_init = true; } - luacond += s + "\n"; + luacond.add(line, s); continue; } @@ -1479,10 +1482,9 @@ void game_options::read_options(InitLineInput &il, bool runscript, if (runscript && !luacond.empty()) { if (l_init) - luacond += "]] )\n"; - clua.execstring(luacond.c_str()); - if (!clua.error.empty()) - mprf(MSGCH_ERROR, "Lua error: %s", clua.error.c_str()); + luacond.add(line, "]] )"); + if (luacond.run(clua)) + mprf(MSGCH_ERROR, "Lua error: %s", luacond.orig_error().c_str()); } #endif diff --git a/crawl-ref/source/luadgn.cc b/crawl-ref/source/luadgn.cc index e3b6459268..8ad3fc37ac 100644 --- a/crawl-ref/source/luadgn.cc +++ b/crawl-ref/source/luadgn.cc @@ -207,6 +207,15 @@ int dlua_chunk::load(CLua &interp) return (err); } +int dlua_chunk::run(CLua &interp) +{ + int err = load(interp); + if (err) + return (err); + // callfn returns true on success, but we want to return 0 on success. + return (check_op(interp, !interp.callfn(NULL, 0, 0))); +} + int dlua_chunk::load_call(CLua &interp, const char *fn) { int err = load(interp); diff --git a/crawl-ref/source/luadgn.h b/crawl-ref/source/luadgn.h index 8dd447c175..746f80be60 100644 --- a/crawl-ref/source/luadgn.h +++ b/crawl-ref/source/luadgn.h @@ -36,7 +36,7 @@ private: CT_SOURCE, CT_COMPILED }; - + private: int check_op(CLua &, int); std::string rewrite_chunk_prefix(const std::string &line, @@ -51,23 +51,24 @@ public: dlua_chunk(lua_State *ls); static dlua_chunk precompiled(const std::string &compiled); - + void clear(); void add(int line, const std::string &line2); void set_chunk(const std::string &s); - + int load(CLua &interp); + int run(CLua &interp); int load_call(CLua &interp, const char *function); void set_file(const std::string &s); const std::string &lua_string() const { return chunk; } std::string orig_error() const; bool rewrite_chunk_errors(std::string &err) const; - + bool empty() const; const std::string &compiled_chunk() const { return compiled; } - + void write(writer&) const; void read(reader&); }; -- cgit v1.2.3-54-g00ecf