Skip to content
This repository
Browse code

extend error handling in pi_lua_dofile(FileData)

  • Loading branch information...
commit 6690cf56b94fc8e5b0b1904c75381f1f8d7c6484 1 parent cb04cb1
John Bartholomew authored May 05, 2012

Showing 1 changed file with 24 additions and 2 deletions. Show diff stats Hide diff stats

  1. 26  src/LuaUtils.cpp
26  src/LuaUtils.cpp
@@ -63,15 +63,37 @@ void pi_lua_protected_call(lua_State* L, int nargs, int nresults) {
63 63
 
64 64
 static void pi_lua_dofile(lua_State *l, const FileSystem::FileData &code)
65 65
 {
  66
+	assert(l);
  67
+	LUA_DEBUG_START(l);
66 68
 	// XXX make this a proper protected call (after working out the implications -- *sigh*)
67 69
 	lua_pushcfunction(l, &pi_lua_panic);
68 70
 	if (luaL_loadbuffer(l, code.GetData(), code.GetSize(), code.GetInfo().GetPath().c_str())) {
69 71
 		pi_lua_panic(l);
70 72
 	} else {
71  
-		lua_pcall(l, 0, 0, -2);
  73
+		int ret = lua_pcall(l, 0, 0, -2);
  74
+		if (ret) {
  75
+			const char *emsg = lua_tostring(l, -1);
  76
+			if (emsg) { fprintf(stderr, "lua error: %s\n", emsg); }
  77
+			switch (ret) {
  78
+				case LUA_ERRRUN:
  79
+					fprintf(stderr, "Lua runtime error in pi_lua_dofile('%s')\n",
  80
+							code.GetInfo().GetAbsolutePath().c_str());
  81
+					break;
  82
+				case LUA_ERRMEM:
  83
+					fprintf(stderr, "Memory allocation error in Lua pi_lua_dofile('%s')\n",
  84
+							code.GetInfo().GetAbsolutePath().c_str());
  85
+					break;
  86
+				case LUA_ERRERR:
  87
+					fprintf(stderr, "Error running error handler in pi_lua_dofile('%s')\n",
  88
+							code.GetInfo().GetAbsolutePath().c_str());
  89
+					break;
  90
+				default: abort();
  91
+			}
  92
+			lua_pop(l, 1);
  93
+		}
72 94
 	}
73 95
 	lua_pop(l, 1);
74  
-
  96
+	LUA_DEBUG_END(l, 0);
75 97
 }
76 98
 
77 99
 void pi_lua_dofile(lua_State *l, const std::string &path)

0 notes on commit 6690cf5

Please sign in to comment.
Something went wrong with that request. Please try again.