Kaynağa Gözat

Don't make assumptions about the initial state id

Aidan Lee 5 ay önce
ebeveyn
işleme
cf45a628d5
2 değiştirilmiş dosya ile 19 ekleme ve 19 silme
  1. 16 15
      src/coro/coro.ml
  2. 3 4
      src/coro/coroToTexpr.ml

+ 16 - 15
src/coro/coro.ml

@@ -45,6 +45,21 @@ let fun_to_coro ctx e tf name =
 	let cls_error      = mk_field "_hx_error" ctx.typer.com.basic.texception null_pos null_pos in
 	let cls_captured   = mk_field "_hx_captured" ctx.typer.c.tthis null_pos null_pos in
 
+	(* Generate and assign the continuation variable *)
+	let vcompletion = alloc_var VGenerated "_hx_completion" ctx.typer.com.basic.tcoro_continuation p in
+	let ecompletion = Builder.make_local vcompletion p in
+
+	let vcontinuation = alloc_var VGenerated "_hx_continuation" (TInst (cls, [])) p in
+	let econtinuation = Builder.make_local vcontinuation p in
+
+	let estate = mk (TField(econtinuation,FInstance(cls, [], cls_state))) ctx.typer.com.basic.tint p in
+	let eresult = mk (TField(econtinuation,FInstance(cls, [], cls_result))) ctx.typer.com.basic.tint p in
+
+	let cb_root = make_block ctx (Some(e.etype,p)) in
+
+	ignore(CoroFromTexpr.expr_to_coro ctx eresult cb_root tf.tf_expr);
+	let eloop, initial_state = CoroToTexpr.block_to_texpr_coroutine ctx cb_root econtinuation ecompletion eresult estate e.epos in
+	
 	let ethis = mk (TConst TThis) (TInst (cls, [])) p in
 
 	let cls_ctor =
@@ -60,7 +75,7 @@ let fun_to_coro ctx e tf name =
 
 		let eassignstate =
 			let estatefield = mk (TField(ethis,FInstance(cls, [], cls_state))) ctx.typer.com.basic.tint p in
-			mk_assign estatefield (mk (TConst (TInt (Int32.of_int 1) )) ctx.typer.com.basic.tint p) in
+			mk_assign estatefield (mk (TConst (TInt (Int32.of_int initial_state) )) ctx.typer.com.basic.tint p) in
 
 		let eassigncaptured =
 			let eargcaptured    = Builder.make_local vargcaptured p in
@@ -234,21 +249,8 @@ let fun_to_coro ctx e tf name =
 
 	ctx.typer.m.curmod.m_types <- ctx.typer.m.curmod.m_types @ [ TClassDecl cls ];
 
-	(* Generate and assign the continuation variable *)
-	let vcompletion = alloc_var VGenerated "_hx_completion" ctx.typer.com.basic.tcoro_continuation p in
-	let ecompletion = Builder.make_local vcompletion p in
-
-	let vcontinuation = alloc_var VGenerated "_hx_continuation" (TInst (cls, [])) p in
-	let econtinuation = Builder.make_local vcontinuation p in
-
-	let estate = mk (TField(econtinuation,FInstance(cls, [], cls_state))) ctx.typer.com.basic.tint p in
-	let eresult = mk (TField(econtinuation,FInstance(cls, [], cls_result))) ctx.typer.com.basic.tint p in
-
 	let continuation_var = mk (TVar (vcontinuation, Some (Builder.make_null (TInst (cls, [])) p))) (TInst (cls, [])) p in
 	
-	let cb_root = make_block ctx (Some(e.etype,p)) in
-	ignore(CoroFromTexpr.expr_to_coro ctx eresult cb_root tf.tf_expr);
-	
 	let continuation_assign =
 		let t         = TInst (cls, []) in
 		let tcond     = std_is ecompletion t in
@@ -263,7 +265,6 @@ let fun_to_coro ctx e tf name =
 		mk (TIf (tcond, tif, Some telse)) ctx.typer.com.basic.tvoid p
 	in
 	
-	let eloop   = CoroToTexpr.block_to_texpr_coroutine ctx cb_root econtinuation ecompletion eresult estate e.epos in
 	let tf_expr = mk (TBlock [
 		continuation_var;
 		continuation_assign;

+ 3 - 4
src/coro/coroToTexpr.ml

@@ -104,7 +104,7 @@ let block_to_texpr_coroutine ctx cb econtinuation ecompletion eresult estate p =
 		assert (cb != ctx.cb_unreachable);
 		let el = DynArray.to_list cb.cb_el in
 
-		let ereturn = mk (TReturn None) com.basic.tvoid p in
+		let ereturn = mk (TReturn (Some (make_null com.basic.tany p))) com.basic.tany p in
 
 		let add_state next_id extra_el =
 			let el = current_el @ el @ extra_el in
@@ -130,8 +130,7 @@ let block_to_texpr_coroutine ctx cb econtinuation ecompletion eresult estate p =
 			let ecallcoroutine = mk_suspending_call call in
 			add_state (Some next_state_id) ecallcoroutine;
 		| NextUnknown ->
-			let ecallcontinuation = mk_continuation_call (make_null t_dynamic p) p in
-			add_state (Some (-1)) [ecallcontinuation; ereturn]
+			add_state (Some (-1)) [ereturn]
 		| NextFallThrough cb_next | NextGoto cb_next | NextBreak cb_next | NextContinue cb_next ->
 			let rec skip_loop cb =
 				if DynArray.empty cb.cb_el then begin match cb.cb_next.next_kind with
@@ -329,4 +328,4 @@ let block_to_texpr_coroutine ctx cb econtinuation ecompletion eresult estate p =
 			e_var :: shared_vars
 	in
 
-	eloop
+	eloop, !init_state