소스 검색

moved capture policy to global config

Nicolas Cannasse 13 년 전
부모
커밋
514758d0a9
2개의 변경된 파일38개의 추가작업 그리고 14개의 파일을 삭제
  1. 12 14
      codegen.ml
  2. 26 0
      common.ml

+ 12 - 14
codegen.ml

@@ -806,9 +806,11 @@ let captured_vars com e =
 					v, o
 			) f.tf_args in
 			let e = { e with eexpr = TFunction { f with tf_args = fargs; tf_expr = !fexpr } } in
-			(match com.platform with
-			| Cpp | Java | Cs -> e
-			| _ ->
+			(*
+				Create a new function scope to make sure that the captured loop variable
+				will not be overwritten in next loop iteration
+			*)
+			if com.config.pf_capture_policy = CPLoopVars then
 				mk (TCall (
 					mk_parent (mk (TFunction {
 						tf_args = List.map (fun v -> v, None) vars;
@@ -816,7 +818,9 @@ let captured_vars com e =
 						tf_expr = mk_block (mk (TReturn (Some e)) e.etype e.epos);
 					}) (TFun (List.map (fun v -> v.v_name,false,v.v_type) vars,e.etype)) e.epos),
 					List.map (fun v -> mk (TLocal v) v.v_type e.epos) vars)
-				) e.etype e.epos)
+				) e.etype e.epos
+			else
+				e
 		| _ ->
 			map_expr (wrap used) e
 
@@ -898,16 +902,10 @@ let captured_vars com e =
 	(* mark all capture variables - also used in rename_local_vars at later stage *)
 	let captured = all_vars e in
 	PMap.iter (fun _ v -> v.v_capture <- true) captured;
-	match com.platform with
-	| Cross | Neko | Php ->
-		e
-	| Cs | Java | Cpp ->
-		(* create temp vars for all captured variables *)
-		do_wrap captured e
-	| Flash8 | Flash | Js ->
-		(* only create temp vars for captured loop variables *)
-
-		out_loop e
+	match com.config.pf_capture_policy with
+	| CPNone -> e
+	| CPWrapRef -> do_wrap captured e
+	| CPLoopVars -> out_loop e
 
 (* -------------------------------------------------------------------------- *)
 (* RENAME LOCAL VARS *)

+ 26 - 0
common.ml

@@ -53,6 +53,20 @@ type stats = {
 	s_macros_called : int ref;
 }
 
+(**
+	The capture policy tells which handling we make of captured locals
+	(the locals which are referenced in local functions)
+
+	See details/implementation in Codegen.captured_vars
+*)
+type capture_policy =
+	(** do nothing, let the platform handle it *)
+	| CPNone 
+	(** wrap all captured variables into a single-element array to allow modifications *)
+	| CPWrapRef
+	(** similar to wrap ref, but will only apply to the locals that are declared in loops *)
+	| CPLoopVars
+
 type platform_config = {
 	(** has a static type system, with not-nullable basic types (Int/Float/Bool) *)
 	pf_static : bool;
@@ -66,6 +80,8 @@ type platform_config = {
 	pf_unique_locals : bool;
 	(** which expressions can be generated to initialize member variables (or will be moved into the constructor *)
 	pf_can_init_member : tclass_field -> bool;
+	(** captured variables handling (see before) *)
+	pf_capture_policy : capture_policy;
 }
 
 type context = {
@@ -129,6 +145,7 @@ let default_config =
 		pf_captured_scope = true;
 		pf_unique_locals = false;
 		pf_can_init_member = (fun _ -> true);
+		pf_capture_policy = CPNone;
 	}
 
 let get_config com =
@@ -144,6 +161,7 @@ let get_config com =
 			pf_captured_scope = false;
 			pf_unique_locals = false;
 			pf_can_init_member = (fun _ -> true);
+			pf_capture_policy = CPLoopVars;
 		}
 	| Js ->
 		{
@@ -153,6 +171,7 @@ let get_config com =
 			pf_captured_scope = false;
 			pf_unique_locals = false;
 			pf_can_init_member = (fun _ -> false);
+			pf_capture_policy = CPLoopVars;
 		}
 	| Neko ->
 		{
@@ -162,6 +181,7 @@ let get_config com =
 			pf_captured_scope = true;
 			pf_unique_locals = false;
 			pf_can_init_member = (fun _ -> false);
+			pf_capture_policy = CPNone;
 		}
 	| Flash when defined "as3" ->
 		{
@@ -171,6 +191,7 @@ let get_config com =
 			pf_captured_scope = true;
 			pf_unique_locals = true;
 			pf_can_init_member = (fun _ -> true);
+			pf_capture_policy = CPLoopVars;
 		}
 	| Flash ->
 		{
@@ -180,6 +201,7 @@ let get_config com =
 			pf_captured_scope = true; (* handled by genSwf9 *)
 			pf_unique_locals = false;
 			pf_can_init_member = (fun _ -> false);
+			pf_capture_policy = CPLoopVars;
 		}
 	| Php ->
 		{
@@ -194,6 +216,7 @@ let get_config com =
 				| _, Some { eexpr = TTypeExpr _ } -> false
 				| _ -> true 
 			);
+			pf_capture_policy = CPNone;
 		}
 	| Cpp ->
 		{
@@ -203,6 +226,7 @@ let get_config com =
 			pf_captured_scope = true;
 			pf_unique_locals = false;
 			pf_can_init_member = (fun _ -> false);
+			pf_capture_policy = CPWrapRef;
 		}
 	| Cs ->
 		{
@@ -212,6 +236,7 @@ let get_config com =
 			pf_captured_scope = true;
 			pf_unique_locals = true;
 			pf_can_init_member = (fun _ -> false);
+			pf_capture_policy = CPWrapRef;
 		}
 	| Java ->
 		{
@@ -221,6 +246,7 @@ let get_config com =
 			pf_captured_scope = true;
 			pf_unique_locals = false;
 			pf_can_init_member = (fun _ -> false);
+			pf_capture_policy = CPWrapRef;
 		}
 
 let create v args =