فهرست منبع

[lua] custom global error handler

Justin Donaldson 5 سال پیش
والد
کامیت
bde35a20d9
3فایلهای تغییر یافته به همراه24 افزوده شده و 6 حذف شده
  1. 10 5
      src/generators/genlua.ml
  2. 13 0
      std/lua/_lua/_hx_tostring.lua
  3. 1 1
      std/lua/_std/haxe/Exception.hx

+ 10 - 5
src/generators/genlua.ml

@@ -146,9 +146,7 @@ let spr ctx s =
 
 let print ctx =
     ctx.separator <- false;
-    Printf.kprintf (fun s -> begin
-            Buffer.add_string ctx.buf s
-        end)
+    Printf.kprintf (fun s -> Buffer.add_string ctx.buf s)
 
 let newline ctx = print ctx "\n%s" ctx.tabs
 
@@ -2083,9 +2081,16 @@ let generate com =
 
     List.iter (generate_enumMeta_fields ctx) com.types;
 
-    (match com.main with
+    match com.main with
      | None -> ()
-     | Some e -> gen_expr ctx e; newline ctx);
+     | Some e -> (match e.eexpr with
+         | TCall(e2,el) -> begin
+                spr ctx "_G.xpcall(";
+                gen_value ctx e2;
+                spr ctx ", _hx_error)";
+                newline ctx;
+            end
+         | _-> ());
 
     if anyExposed then
         println ctx "return _hx_exports";

+ 13 - 0
std/lua/_lua/_hx_tostring.lua

@@ -95,3 +95,16 @@ function _hx_tostring(obj, depth)
         return ""
     end
 end
+
+function _hx_error(obj)
+    if obj.value then
+        "Runtime Error: " + _G.print(_hx_tostring(obj.value));
+    else
+        "Runtime Error: " + _G.print(tostring(obj))
+    end
+
+    if _G.debug and _G.debug.traceback then
+        _G.print(debug.traceback());
+    end
+end
+

+ 1 - 1
std/lua/_std/haxe/Exception.hx

@@ -82,4 +82,4 @@ class Exception {
 			case s: s;
 		}
 	}
-}
+}