Kaynağa Gözat

moved can_init_member to global config

Nicolas Cannasse 13 yıl önce
ebeveyn
işleme
1ea6df19a5
2 değiştirilmiş dosya ile 20 ekleme ve 10 silme
  1. 3 10
      codegen.ml
  2. 17 0
      common.ml

+ 3 - 10
codegen.ml

@@ -487,20 +487,13 @@ let add_field_inits com c =
 	let ethis = mk (TConst TThis) (TInst (c,List.map snd c.cl_types)) c.cl_pos in
 	(* TODO: we have to find a variable name which is not used in any of the functions *)
 	let v = alloc_var "_g" ethis.etype in
-	let rec can_init_inline cf e = match com.platform,e.eexpr with
-		| Flash8,_ -> true
-		| Flash,_ when Common.defined com "as3" && (match cf.cf_kind with Var _ -> true | Method _ -> false) -> true
-		| Php, TTypeExpr _ -> false
-		| Php,_ ->
-			(match cf.cf_kind with Var({v_write = AccCall _}) -> false | _ -> true)
-		| _ -> false
-	in
 	let need_this = ref false in
 	let inits,fields = List.fold_left (fun (inits,fields) cf ->
 		match cf.cf_kind,cf.cf_expr with
-		| Var _, Some e when can_init_inline cf e -> (inits, cf :: fields)
-		| Var _, Some _ -> (cf :: inits, cf :: fields)
+		| Var _, Some _ ->
+			if com.config.pf_can_init_member cf then (inits, cf :: fields) else (cf :: inits, cf :: fields)
 		| Method MethDynamic, Some e when Common.defined com "as3" ->
+			(* TODO : this would have a better place in genSWF9 I think - NC *)
 			(* we move the initialization of dynamic functions to the constructor and also solve the
 			   'this' problem along the way *)
 			let rec use_this v e = match e.eexpr with

+ 17 - 0
common.ml

@@ -64,6 +64,8 @@ type platform_config = {
 	pf_captured_scope : bool;
 	(** generated locals must be absolutely unique wrt the current function *)
 	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;
 }
 
 type context = {
@@ -126,6 +128,7 @@ let default_config =
 		pf_locals_scope = true;
 		pf_captured_scope = true;
 		pf_unique_locals = false;
+		pf_can_init_member = (fun _ -> true);
 	}
 
 let get_config com =
@@ -140,6 +143,7 @@ let get_config com =
 			pf_locals_scope = com.flash_version > 6.;
 			pf_captured_scope = false;
 			pf_unique_locals = false;
+			pf_can_init_member = (fun _ -> true);
 		}
 	| Js ->
 		{
@@ -148,6 +152,7 @@ let get_config com =
 			pf_locals_scope = false;		
 			pf_captured_scope = false;
 			pf_unique_locals = false;
+			pf_can_init_member = (fun _ -> false);
 		}
 	| Neko ->
 		{
@@ -156,6 +161,7 @@ let get_config com =
 			pf_locals_scope = true;
 			pf_captured_scope = true;
 			pf_unique_locals = false;
+			pf_can_init_member = (fun _ -> false);
 		}
 	| Flash when defined "as3" ->
 		{
@@ -164,6 +170,7 @@ let get_config com =
 			pf_locals_scope = false;
 			pf_captured_scope = true;
 			pf_unique_locals = true;
+			pf_can_init_member = (fun _ -> true);
 		}
 	| Flash ->
 		{
@@ -172,6 +179,7 @@ let get_config com =
 			pf_locals_scope = true;
 			pf_captured_scope = true; (* handled by genSwf9 *)
 			pf_unique_locals = false;
+			pf_can_init_member = (fun _ -> false);
 		}
 	| Php ->
 		{
@@ -180,6 +188,12 @@ let get_config com =
 			pf_locals_scope = false; (* some duplicate work is done in genPhp *)
 			pf_captured_scope = false;
 			pf_unique_locals = false;
+			pf_can_init_member = (fun cf ->
+				match cf.cf_kind, cf.cf_expr with
+				| Var { v_write = AccCall _ },  _ -> false
+				| _, Some { eexpr = TTypeExpr _ } -> false
+				| _ -> true 
+			);
 		}
 	| Cpp ->
 		{
@@ -188,6 +202,7 @@ let get_config com =
 			pf_locals_scope = true;
 			pf_captured_scope = true;
 			pf_unique_locals = false;
+			pf_can_init_member = (fun _ -> false);
 		}
 	| Cs ->
 		{
@@ -196,6 +211,7 @@ let get_config com =
 			pf_locals_scope = false;
 			pf_captured_scope = true;
 			pf_unique_locals = true;
+			pf_can_init_member = (fun _ -> false);
 		}
 	| Java ->
 		{
@@ -204,6 +220,7 @@ let get_config com =
 			pf_locals_scope = false;
 			pf_captured_scope = true;
 			pf_unique_locals = false;
+			pf_can_init_member = (fun _ -> false);
 		}
 
 let create v args =