Browse Source

introduce haxe.coro package

Simon Krajewski 1 year ago
parent
commit
04d73ebf5e

+ 1 - 1
src/core/tFunctions.ml

@@ -627,7 +627,7 @@ type maybe_coro =
 	| NotCoro of t
 
 let follow_with_coro t = match follow t with
-	| TAbstract({a_path = ([],"Coroutine")},[t]) ->
+	| TAbstract({a_path = (["haxe";"coro"],"Coroutine")},[t]) ->
 		begin match follow t with
 			| TFun(args,ret) ->
 				Coro (args,ret)

+ 1 - 1
src/generators/genjvm.ml

@@ -166,7 +166,7 @@ let rec jsignature_of_type gctx stack t =
 					end
 				| [],"EnumValue" ->
 					java_enum_sig object_sig
-				| [],"Coroutine" ->
+				| ["haxe";"coro"],"Coroutine" ->
 					begin match tl with
 					| [TFun(args,ret)] ->
 						let tcontinuation = tfun [ret; t_dynamic] gctx.com.basic.tvoid in

+ 0 - 9
src/typing/typeload.ml

@@ -420,15 +420,6 @@ and load_instance' ctx ptp get_params mode =
 			| [] -> t_dynamic
 			| [TPType t] -> TDynamic (Some (load_complex_type ctx true LoadNormal t))
 			| _ -> raise_typing_error "Too many parameters for Dynamic" ptp.pos_full
-		(* else if info.build_path = ([],"Coroutine") then
-			match t.tparams with
-			| [TPType t] ->
-				begin match load_complex_type ctx true t with
-				| TFun(args,ret,_) -> TFun(args,ret,true)
-				| _ -> raise_typing_error "Argument type should be function" ptp.pos_full
-				end
-			| _ ->
-				raise_typing_error "Wrong number of arguments for Coroutine<T>" ptp.pos_full *)
 		else if info.build_params = [] then begin match t.tparams with
 			| [] ->
 				info.build_apply []

+ 10 - 5
src/typing/typerEntry.ml

@@ -110,11 +110,6 @@ let create com macros =
 						TLazy r
 				in
 				ctx.t.tnull <- mk_null;
-			| "Coroutine" ->
-				let mk_coro args ret =
-					TAbstract(a,[TFun(args,ret)])
-				in
-				ctx.t.tcoro <- mk_coro
 			| _ -> ())
 		| TEnumDecl _ | TClassDecl _ | TTypeDecl _ ->
 			()
@@ -152,6 +147,16 @@ let create com macros =
 		| [TClassDecl c2 ] -> ctx.g.global_using <- (c1,c1.cl_pos) :: (c2,c2.cl_pos) :: ctx.g.global_using
 		| _ -> die "" __LOC__);
 	| _ -> die "" __LOC__);
+	let m = TypeloadModule.load_module ctx (["haxe";"coro"],"Coroutine") null_pos in
+	List.iter (function
+		| TAbstractDecl({a_path = (["haxe";"coro"],"Coroutine")} as a) ->
+			let mk_coro args ret =
+				TAbstract(a,[TFun(args,ret)])
+			in
+			ctx.t.tcoro <- mk_coro
+		| _ ->
+			()
+	) m.m_types;
 	ignore(TypeloadModule.load_module ctx (["haxe"],"Exception") null_pos);
 	ctx.g.complete <- true;
 	ctx

+ 1 - 29
std/StdTypes.hx

@@ -168,32 +168,4 @@ typedef KeyValueIterable<K, V> = {
 
 	@see https://haxe.org/manual/types-abstract-array-access.html
 **/
-extern interface ArrayAccess<T> {}
-
-/**
-	Coroutine function.
-**/
-@:callable
-@:coreType
-abstract Coroutine<T:haxe.Constraints.Function> {
-	/**
-		Suspend running coroutine and expose the continuation callback
-		for resuming coroutine execution.
-	**/
-	@:coroutine
-	public static extern function suspend<T>(f:(cont:(T, Null<Dynamic>) -> Void)->Void):T;
-
-	#if (jvm || eval)
-	@:native("suspend")
-	@:ifFeature("_StdTypes.Coroutine_Impl_.suspend")
-	static function nativeSuspend<T>(f, cont:(T, Null<Dynamic>) -> Void) {
-		return (_, _) -> f(cont);
-	}
-	#end
-
-	#if js // TODO: implement this all properly for all the targets
-	static function __init__():Void {
-		js.Syntax.code("{0} = {1}", Coroutine.suspend, cast function(f, cont) return (_, _) -> f(cont));
-	}
-	#end
-}
+extern interface ArrayAccess<T> {}

+ 29 - 0
std/haxe/coro/Coroutine.hx

@@ -0,0 +1,29 @@
+package haxe.coro;
+
+/**
+	Coroutine function.
+**/
+@:callable
+@:coreType
+abstract Coroutine<T:haxe.Constraints.Function> {
+	/**
+		Suspend running coroutine and expose the continuation callback
+		for resuming coroutine execution.
+	**/
+	@:coroutine
+	public static extern function suspend<T>(f:(cont:(T, Null<Dynamic>) -> Void)->Void):T;
+
+	#if (jvm || eval)
+	@:native("suspend")
+	@:ifFeature("_StdTypes.Coroutine_Impl_.suspend")
+	static function nativeSuspend<T>(f, cont:(T, Null<Dynamic>) -> Void) {
+		return (_, _) -> f(cont);
+	}
+	#end
+
+	#if js // TODO: implement this all properly for all the targets
+	static function __init__():Void {
+		js.Syntax.code("{0} = {1}", Coroutine.suspend, cast function(f, cont) return (_, _) -> f(cont));
+	}
+	#end
+}

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

@@ -116,7 +116,7 @@ class TestControlFlow extends utest.Test {
 }
 
 @:coroutine
-private function mapCalls<TArg,TRet>(args:Array<TArg>, f:Coroutine<TArg->TRet>):Array<TRet> {
+private function mapCalls<TArg,TRet>(args:Array<TArg>, f:haxe.coro.Coroutine<TArg->TRet>):Array<TRet> {
 	return [for (arg in args) f(arg)];
 }
 

+ 2 - 0
tests/misc/coroutines/src/TestGenerator.hx

@@ -1,3 +1,5 @@
+import haxe.coro.Coroutine;
+
 class TestGenerator extends utest.Test {
 	function testSimple() {
 		var iter = sequence(yield -> {

+ 1 - 0
tests/misc/coroutines/src/TestJsPromise.hx

@@ -1,5 +1,6 @@
 import js.lib.Error;
 import js.lib.Promise;
+import haxe.coro.Coroutine;
 
 @:coroutine
 private function await<T>(p:Promise<T>):T {