Browse Source

get something working on JVM

Simon Krajewski 1 year ago
parent
commit
b963967ed4

+ 2 - 1
src/coro/coroToTexpr.ml

@@ -362,6 +362,7 @@ let block_to_texpr_coroutine ctx bb vcontinuation vresult verror p =
 	let shared_vars = List.rev (excstate_var :: state_var :: shared_vars) in
 
 	mk (TBlock (shared_vars @ [
-		mk (TVar (vstatemachine, Some estatemachine_def)) com.basic.tvoid p;
+		mk (TVar (vstatemachine, None)) com.basic.tvoid p;
+		binop OpAssign estatemachine estatemachine_def estatemachine.etype p;
 		mk (TReturn (Some estatemachine)) com.basic.tvoid p;
 	])) com.basic.tvoid p

+ 22 - 7
src/generators/genjvm.ml

@@ -166,6 +166,16 @@ let rec jsignature_of_type gctx stack t =
 					end
 				| [],"EnumValue" ->
 					java_enum_sig object_sig
+				| [],"Coroutine" ->
+					begin match tl with
+					| [TFun(args,ret)] ->
+						let tcontinuation = tfun [ret; t_dynamic] gctx.com.basic.tvoid in
+						let args = args @ [("",false,tcontinuation)] in
+						let ret = tfun [t_dynamic; t_dynamic] gctx.com.basic.tvoid in
+						jsignature_of_type (TFun(args,ret))
+					| _ ->
+						die "" __LOC__
+					end
 				| _ ->
 					if Meta.has Meta.CoreType a.a_meta then
 						TObject(a.a_path,List.map jtype_argument_of_type tl)
@@ -196,11 +206,12 @@ let rec jsignature_of_type gctx stack t =
 	| TEnum(en,tl) ->
 		Hashtbl.replace gctx.enum_paths en.e_path ();
 		TObject(en.e_path,List.map jtype_argument_of_type tl)
-	| TFun(tl,tr) -> method_sig (List.map (fun (_,o,t) ->
-		let jsig = jsignature_of_type t in
-		let jsig = if o then get_boxed_type jsig else jsig in
-		jsig
-	) tl) (return_of_type gctx stack tr)
+	| TFun(tl,tr) ->
+		method_sig (List.map (fun (_,o,t) ->
+			let jsig = jsignature_of_type t in
+			let jsig = if o then get_boxed_type jsig else jsig in
+			jsig
+		) tl) (return_of_type gctx stack tr)
 	| TAnon an -> object_sig
 	| TType(td,tl) ->
 		begin match gctx.typedef_interfaces#get_interface_class td.t_path with
@@ -1503,9 +1514,13 @@ class texpr_to_jvm
 	(* calls *)
 
 	method call_arguments ?(cast=true) t el =
-		let tl,tr = match follow t with
-			| TFun(tl,tr) ->
+		let tl,tr = match follow_with_coro t with
+			| NotCoro (TFun(tl,tr)) ->
 				tl,return_of_type gctx tr
+			| Coro(args,ret) ->
+				let args = args @ [("_hx_continuation",false,(tfun [ret; t_dynamic] gctx.com.basic.tvoid))] in
+				let ret = (tfun [t_dynamic; t_dynamic] gctx.com.basic.tvoid) in
+				args,return_of_type gctx ret
 			| _ ->
 				List.map (fun e -> ("",false,e.etype)) el,Some (object_sig)
 		in

+ 4 - 3
src/typing/typer.ml

@@ -1734,9 +1734,10 @@ and type_call_access ctx e el mode with_type p_inline p =
 and type_call_builtin ctx e el mode with_type p =
 	let create_coroutine e args ret p =
 		let args = args @ [("_hx_continuation",false,(tfun [ret; t_dynamic] ctx.com.basic.tvoid))] in
-		let ret = ctx.com.basic.tvoid in
-		let el = unify_call_args ctx el args ret p false false false in
-		mk (TCall (e, el)) (tfun [t_dynamic; t_dynamic] ctx.com.basic.tvoid) p
+		let ret = (tfun [t_dynamic; t_dynamic] ctx.com.basic.tvoid) in
+		let el = unify_call_args ctx el args ctx.t.tvoid p false false false in
+		let e = mk e.eexpr (TFun(args,ret)) p in
+		mk (TCall (e, el)) ret p
 	in
 	match e, el with
 	| (EConst (Ident "trace"),p) , e :: el ->

+ 0 - 2
tests/misc/coroutines/build.hxml → tests/misc/coroutines/build-base.hxml

@@ -2,6 +2,4 @@
 --library utest
 --main Main
 --debug
---js test.js
 -D UTEST-PRINT-TESTS
---cmd node test.js

+ 3 - 0
tests/misc/coroutines/build-js.hxml

@@ -0,0 +1,3 @@
+build-base.hxml
+--js test.js
+--cmd node test.js

+ 3 - 0
tests/misc/coroutines/build-jvm.hxml

@@ -0,0 +1,3 @@
+build-base.hxml
+--jvm test.jar
+--cmd java -jar test.jar

+ 1 - 1
tests/misc/coroutines/src/Main.hx

@@ -2,8 +2,8 @@ function main() {
 	utest.UTest.run([
 		new TestBasic(),
 		new TestControlFlow(),
-		new TestGenerator(),
 		#if js
+		new TestGenerator(),
 		new TestJsPromise(),
 		#end
 	]);

+ 1 - 1
tests/runci/targets/Js.hx

@@ -78,7 +78,7 @@ class Js {
 
 		infoMsg("Test coroutines:");
 		changeDirectory(getMiscSubDir("coroutines"));
-		runCommand("haxe", ["build.hxml"]);
+		runCommand("haxe", ["build-js.hxml"]);
 
 		haxelibInstallGit("HaxeFoundation", "hxnodejs");
 		final env = Sys.environment();

+ 4 - 0
tests/runci/targets/Jvm.hx

@@ -33,6 +33,10 @@ class Jvm {
 			runCommand("java", ["-jar", "bin/unit.jar"]);
 		}
 
+		infoMsg("Test coroutines:");
+		changeDirectory(getMiscSubDir("coroutines"));
+		runCommand("haxe", ["build-jvm.hxml"]);
+
 		changeDirectory(miscJavaDir);
 		runCommand("haxe", ["run.hxml"]);