Explorar o código

Add lua-standalone compiler define (#11008)

* Add lua_standalone compiler define

If defined unhandled errors will exit the Lua process

* [lua] Handle errors only with -D lua-standalone

* Rename lua_standalone to lua-standalone in changelog

---------

Co-authored-by: tobil4sk <[email protected]>
Sebastian Thomschke %!s(int64=2) %!d(string=hai) anos
pai
achega
459c7d025d

+ 1 - 0
extra/CHANGES.txt

@@ -3,6 +3,7 @@
 	General improvements:
 
 	all : optimize `.bind` for instance methods (#10737)
+	lua : add lua-standalone global compiler flag (#10979)
 
 2021-03-06 4.2.5:
 

+ 6 - 0
src-json/define.json

@@ -397,6 +397,12 @@
 		"doc": "Enable the jit compiler for lua (version 5.2 only).",
 		"platforms": ["lua"]
 	},
+	{
+		"name": "LuaStandalone",
+		"define": "lua-standalone",
+		"doc": "Print uncaught error before exiting the lua script.",
+		"platforms": ["lua"]
+	},
 	{
 		"name": "LuaVanilla",
 		"define": "lua-vanilla",

+ 27 - 16
src/generators/genlua.ml

@@ -47,6 +47,7 @@ type ctx = {
     mutable separator : bool;
     mutable found_expose : bool;
     mutable lua_jit : bool;
+    mutable lua_standalone : bool;
     mutable lua_vanilla : bool;
     mutable lua_ver : float;
 }
@@ -445,7 +446,7 @@ and gen_call ctx e el =
                   if List.length(fields) > 0 then incr count;
               | { eexpr = TConst(TNull)} -> ()
               | _ ->
-				typing_error "__lua_table__ only accepts array or anonymous object arguments" e.epos;
+                typing_error "__lua_table__ only accepts array or anonymous object arguments" e.epos;
              )) el;
          spr ctx "})";
      | TIdent "__lua__", [{ eexpr = TConst (TString code) }] ->
@@ -1876,6 +1877,7 @@ let alloc_ctx com =
         separator = false;
         found_expose = false;
         lua_jit = Common.defined com Define.LuaJit;
+        lua_standalone = Common.defined com Define.LuaStandalone;
         lua_vanilla = Common.defined com Define.LuaVanilla;
         lua_ver = try
                 float_of_string (Common.defined_value com Define.LuaVer)
@@ -2139,25 +2141,35 @@ let generate com =
         print_file (Common.find_file com "lua/_lua/_hx_dyn_add.lua");
     end;
 
+    if ctx.lua_standalone && Option.is_some com.main then begin
+        print_file (Common.find_file com "lua/_lua/_hx_handle_error.lua");
+    end;
+
     println ctx "_hx_static_init();";
 
     List.iter (generate_enumMeta_fields ctx) com.types;
 
     Option.may (fun e ->
-        spr ctx "_G.xpcall(";
-            let luv_run =
-                (* Runs libuv loop if needed *)
-                mk_lua_code ctx.com.basic "_hx_luv.run()" [] ctx.com.basic.tvoid Globals.null_pos
-            in
-            let fn =
-                {
-                    tf_args = [];
-                    tf_type = com.basic.tvoid;
-                    tf_expr = mk (TBlock [e;luv_run]) com.basic.tvoid e.epos;
-                }
-            in
-            gen_value ctx { e with eexpr = TFunction fn; etype = TFun ([],com.basic.tvoid) };
-        spr ctx ", _hx_error)";
+        let luv_run =
+            (* Runs libuv loop if needed *)
+            mk_lua_code ctx.com.basic "_hx_luv.run()" [] ctx.com.basic.tvoid Globals.null_pos
+        in
+        if ctx.lua_standalone then begin
+            spr ctx "_G.xpcall(";
+                let fn =
+                    {
+                        tf_args = [];
+                        tf_type = com.basic.tvoid;
+                        tf_expr = mk (TBlock [e;luv_run]) com.basic.tvoid e.epos;
+                    }
+                in
+                gen_value ctx { e with eexpr = TFunction fn; etype = TFun ([],com.basic.tvoid) };
+            spr ctx ", _hx_handle_error)";
+        end else begin
+            gen_expr ctx e;
+            newline ctx;
+            gen_expr ctx luv_run;
+        end;
         newline ctx
     ) com.main;
 
@@ -2167,4 +2179,3 @@ let generate com =
     let ch = open_out_bin com.file in
     output_string ch (Buffer.contents ctx.buf);
     close_out ch
-

+ 12 - 0
std/lua/_lua/_hx_handle_error.lua

@@ -0,0 +1,12 @@
+function _hx_handle_error(obj)
+    if obj.value then
+        _G.print("runtime error:\n " .. _hx_tostring(obj.value));
+    else
+        _G.print("runtime error:\n " .. tostring(obj));
+    end
+
+    if _G.debug and _G.debug.traceback then
+        _G.print(_G.debug.traceback());
+    end
+    _G.os.exit(1)
+end

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

@@ -99,16 +99,3 @@ function _hx_tostring(obj, depth)
         return ""
     end
 end
-
-function _hx_error(obj)
-    if obj.value then
-        _G.print("runtime error:\n " .. _hx_tostring(obj.value));
-    else
-        _G.print("runtime error:\n " .. tostring(obj));
-    end
-
-    if _G.debug and _G.debug.traceback then
-        _G.print(debug.traceback());
-    end
-end
-