Prechádzať zdrojové kódy

automatically insert a call to luv.Loop.run if "luv" module is loaded (#10199)

Aleksandr Kuzmenko 4 rokov pred
rodič
commit
be525d0541

+ 9 - 7
src/generators/genlua.ml

@@ -2056,6 +2056,9 @@ let generate com =
     println ctx "_hx_array_mt.__index = Array.prototype";
     newline ctx;
 
+    (* Functions to support auto-run of libuv loop *)
+    print_file (Common.find_file com "lua/_lua/_hx_luv.lua");
+
     let b = open_block ctx in
     (* Localize init variables inside a do-block *)
     println ctx "local _hx_static_init = function()";
@@ -2113,19 +2116,18 @@ let generate com =
 
     Option.may (fun e ->
         spr ctx "_G.xpcall(";
-        (match e.eexpr with
-        | TCall(e2,[]) ->
-            gen_value ctx e2;
-        | _->
+            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]) com.basic.tvoid e.epos;
+                    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) }
-        );
+            gen_value ctx { e with eexpr = TFunction fn; etype = TFun ([],com.basic.tvoid) };
         spr ctx ", _hx_error)";
         newline ctx
     ) com.main;

+ 17 - 0
std/haxe/EntryPoint.hx

@@ -150,6 +150,23 @@ class EntryPoint {
 		flash.Lib.current.stage.addEventListener(flash.events.Event.ENTER_FRAME, function(_) processEvents());
 		#elseif (target.threaded && !cppia)
 		//everything is delegated to sys.thread.EventLoop
+		#elseif lua
+		inline function luvRun(mode:String):Bool
+			return untyped __lua__('_hx_luv.run({0})', mode);
+		while (true) {
+			var nextTick = processEvents();
+			if(untyped __lua__('_hx_luv.loop_alive()')) {
+				if(nextTick < 0)
+					luvRun("once")
+				else
+					luvRun("nowait");
+			} else {
+				if (nextTick < 0)
+					break;
+				if (nextTick > 0)
+					sleepLock.wait(nextTick);
+			}
+		}
 		#elseif sys
 		while (true) {
 			var nextTick = processEvents();

+ 8 - 0
std/lua/_lua/_hx_luv.lua

@@ -0,0 +1,8 @@
+if package.loaded.luv then
+  _hx_luv = _G.require("luv");
+else
+  _hx_luv = {
+    run=function(mode) return false end,
+    loop_alive=function() return false end
+  }
+end

+ 5 - 0
std/lua/lib/luv/Loop.hx

@@ -3,6 +3,11 @@ package lua.lib.luv;
 @:luaRequire("luv")
 extern class Loop {
 	static function loop_close():Bool;
+	/**
+		Runs the event loop of libuv.
+
+		Haxe compiler automatically inserts a call to this function at the end of user's code if needed.
+	**/
 	static function run(?mode:String):Bool;
 	static function loop_alive():Bool;
 	static function stop():Void;