Sfoglia il codice sorgente

hijack haxe.EntryPoint to deal with UV initialization

Simon Krajewski 6 anni fa
parent
commit
0fdd3e8e11
4 ha cambiato i file con 28 aggiunte e 22 eliminazioni
  1. 8 3
      src/typing/finalization.ml
  2. 18 2
      std/haxe/EntryPoint.hx
  3. 2 6
      std/haxe/Timer.hx
  4. 0 11
      tests/asys/Main.hx

+ 8 - 3
src/typing/finalization.ml

@@ -33,11 +33,16 @@ let get_main ctx types =
 		let main = (try
 			let et = List.find (fun t -> t_path t = (["haxe"],"EntryPoint")) types in
 			let ec = (match et with TClassDecl c -> c | _ -> assert false) in
-			let ef = PMap.find "run" ec.cl_statics in
 			let p = null_pos in
 			let et = mk (TTypeExpr et) (TAnon { a_fields = PMap.empty; a_status = ref (Statics ec) }) p in
-			let call = mk (TCall (mk (TField (et,FStatic (ec,ef))) ef.cf_type p,[])) ctx.t.tvoid p in
-			mk (TBlock [main;call]) ctx.t.tvoid p
+			let mk_call name =
+				let ef = PMap.find name ec.cl_statics in
+				mk (TCall (mk (TField (et,FStatic (ec,ef))) ef.cf_type p,[])) ctx.t.tvoid p
+			in
+			let init = mk_call "init" in
+			let run = mk_call "run" in
+			let close = mk_call "close" in
+			mk (TBlock [init;main;run;close]) ctx.t.tvoid p
 		with Not_found ->
 			main
 		) in

+ 18 - 2
std/haxe/EntryPoint.hx

@@ -102,6 +102,12 @@ class EntryPoint {
 		return time;
 	}
 
+	@:keep public static function init() {
+		#if eval
+		eval.Uv.init();
+		#end
+	}
+
 	/**
 		Start the main loop. Depending on the platform, this can return immediately or will only return when the application exits.
 	**/
@@ -116,10 +122,10 @@ class EntryPoint {
 		#if nodejs
 		setTimeoutNextTick();
 		#else
-		if(js.Lib.typeof(js.Browser.window) != 'undefined') {
+		if (js.Lib.typeof(js.Browser.window) != 'undefined') {
 			var window:Dynamic = js.Browser.window;
 			var rqf:Dynamic = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame;
-			if(rqf != null) {
+			if (rqf != null) {
 				rqf(run);
 			} else {
 				setTimeoutNextTick();
@@ -130,6 +136,10 @@ class EntryPoint {
 		#end
 		#elseif flash
 		flash.Lib.current.stage.addEventListener(flash.events.Event.ENTER_FRAME, function(_) processEvents());
+		#elseif target.asys
+		#if eval
+		eval.Uv.run(RunDefault);
+		#end
 		#elseif sys
 		while (true) {
 			var nextTick = processEvents();
@@ -142,4 +152,10 @@ class EntryPoint {
 		// no implementation available, let's exit immediately
 		#end
 	}
+
+	@:keep static public function close() {
+		#if eval
+		eval.Uv.close();
+		#end
+	}
 }

+ 2 - 6
std/haxe/Timer.hx

@@ -23,12 +23,7 @@
 package haxe;
 
 #if (target.asys)
-private typedef Native =
-	#if eval
-	eval.uv.Timer;
-	#else
-	#error "Missing asys implementation"
-	#end
+private typedef Native = #if eval eval.uv.Timer; #else #error "Missing asys implementation" #end
 #end
 
 /**
@@ -81,6 +76,7 @@ class Timer {
 		timer = new java.util.Timer();
 		timer.scheduleAtFixedRate(task = new TimerTask(this), haxe.Int64.ofInt(time_ms), haxe.Int64.ofInt(time_ms));
 		#elseif (target.asys)
+		(null : haxe.EntryPoint); // TODO: Have to reference EntryPoint - cleaner way?
 		native = new Native(time_ms, () -> run());
 		#else
 		var dt = time_ms / 1000;

+ 0 - 11
tests/asys/Main.hx

@@ -3,17 +3,8 @@ import utest.ui.Report;
 
 import sys.FileSystem;
 
-#if hl
-import hl.Uv;
-#elseif eval
-import eval.Uv;
-#elseif neko
-import neko.Uv;
-#end
-
 class Main {
 	public static function main():Void {
-		Uv.init();
 		if (FileSystem.exists("resources-rw")) {
 			function walk(path:String):Void {
 				for (f in FileSystem.readDirectory(path)) {
@@ -34,7 +25,5 @@ class Main {
 		runner.onTestStart.add(test -> trace("running", Type.getClassName(Type.getClass(test.fixture.target)), test.fixture.method));
 		Report.create(runner);
 		runner.run();
-		Uv.run(RunDefault);
-		Uv.close();
 	}
 }