state file name and line number in lua error messages
authorsheepluva
Fri, 13 Jun 2014 06:26:31 +0200
changeset 10293 201ea4989985
parent 10292 1c6639b49afc
child 10294 d98336fcd13d
state file name and line number in lua error messages
hedgewars/LuaPas.pas
hedgewars/uScript.pas
--- a/hedgewars/LuaPas.pas	Fri Jun 13 02:31:30 2014 +0200
+++ b/hedgewars/LuaPas.pas	Fri Jun 13 06:26:31 2014 +0200
@@ -522,17 +522,17 @@
 
 type
     lua_Debug = packed record
-    event : LongInt;
-    name : PChar;          (* (n) *)
-    namewhat : PChar;      (* (n) `global', `local', `field', `method' *)
-    what : PChar;          (* (S) `Lua', `C', `main', `tail' *)
-    source : PChar;        (* (S) *)
-    currentline : LongInt; (* (l) *)
-    nups : LongInt;        (* (u) number of upvalues *)
-    linedefined : LongInt; (* (S) *)
+    event : LUA_INTEGER_;
+    name : PChar;               (* (n) *)
+    namewhat : PChar;           (* (n) `global', `local', `field', `method' *)
+    what : PChar;               (* (S) `Lua', `C', `main', `tail' *)
+    source : PChar;             (* (S) *)
+    currentline : LUA_INTEGER_; (* (l) *)
+    nups : LUA_INTEGER_;        (* (u) number of upvalues *)
+    linedefined : LUA_INTEGER_; (* (S) *)
     short_src : array [0..LUA_IDSIZE-1] of Char; (* (S) *)
     (* private part *)
-    i_ci : LongInt;        (* active function *)
+    i_ci : LUA_INTEGER_;        (* active function *)
     end;
     Plua_Debug = ^lua_Debug;
 
--- a/hedgewars/uScript.pas	Fri Jun 13 02:31:30 2014 +0200
+++ b/hedgewars/uScript.pas	Fri Jun 13 06:26:31 2014 +0200
@@ -105,9 +105,34 @@
 procedure ScriptSetAmmo(ammo : TAmmoType; count, probability, delay, reinforcement: Byte); forward;
 procedure ScriptSetAmmoDelay(ammo : TAmmoType; delay: Byte); forward;
 
-procedure LuaError(s: shortstring);
+var LuaDebugInfo: lua_Debug;
+
+function LuaUpdateDebugInfo(): Boolean;
 begin
-    s:= 'Lua-script error: ' + s;
+    FillChar(LuaDebugInfo, sizeof(LuaDebugInfo), 0);
+
+    if lua_getstack(luaState, 1, @LuaDebugInfo) = 0 then
+        exit(false); // stack not deep enough
+
+    // get source name and line count
+    lua_getinfo(luaState, 'Sl', @LuaDebugInfo);
+    exit(true);
+end;
+
+procedure LuaError(s: shortstring);
+var src: shortstring;
+const
+    maxsrclen = 20;
+begin
+    if LuaUpdateDebugInfo() then
+        begin
+        src:= StrPas(LuaDebugInfo.source);
+        s:= 'LUA ERROR [ ... '
+            + copy(src, Length(src) - maxsrclen, maxsrclen - 3) + ':'
+            + inttostr(LuaDebugInfo.currentLine) + ']: ' + s;
+        end
+    else
+        s:= 'LUA ERROR: ' + s;
     WriteLnToConsole(s);
     AddChatString(#5 + s);
     if cTestLua then
@@ -116,7 +141,8 @@
 
 procedure LuaCallError(error, call, paramsyntax: shortstring);
 begin
-    LuaError(call + ': ' + error + '       function syntax: ' + call + ' ( ' + paramsyntax + ' )');
+    LuaError(call + ': ' + error);
+    LuaError('-- SYNTAX: ' + call + ' ( ' + paramsyntax + ' )');
 end;
 
 procedure LuaParameterCountError(expected, call, paramsyntax: shortstring; wrongcount: LongInt); inline;