Explorar el Código

handle field init based on platform capabilities

Simon Krajewski hace 13 años
padre
commit
07b03d855f
Se han modificado 3 ficheros con 16 adiciones y 3 borrados
  1. 11 2
      codegen.ml
  2. 2 1
      tests/unit/MyClass.hx
  3. 3 0
      tests/unit/TestType.hx

+ 11 - 2
codegen.ml

@@ -465,8 +465,17 @@ let on_inherit ctx c p h =
 	Adds member field initializations as assignments to the constructor
 *)
 let add_field_inits com c =
+	let rec can_init_inline cf e = match com.platform,e.eexpr with
+		(* Flash8 and As3 can init anything (it seems) *)
+		| Flash8, _ -> true
+		| Flash,_ when Common.defined com "as3" -> true
+		(* Php can init literals when the field has no setter *)
+		| Php, TConst _ when (match cf.cf_kind with Var({v_write = AccCall _}) -> false | _ -> true) -> true
+		| _ -> false
+	in
 	let inits = List.filter (fun cf ->
 		match cf.cf_kind,cf.cf_expr with
+		| Var _, Some e when can_init_inline cf e -> false
 		| Var _, Some _ -> true
 		| _ -> false
 	) c.cl_ordered_fields in
@@ -476,7 +485,7 @@ let add_field_inits com c =
 		let ethis = mk (TConst TThis) (TInst (c,List.map snd c.cl_types)) c.cl_pos in
 		let el = List.map (fun cf ->
 			match cf.cf_expr with
-			| None -> assert false 
+			| None -> assert false
 			| Some e ->
 				let lhs = mk (TField(ethis,cf.cf_name)) e.etype e.epos in
 				cf.cf_expr <- None;
@@ -558,7 +567,7 @@ let on_generate ctx t =
 				c.cl_ordered_fields <- List.filter (fun f2 -> f != f2) c.cl_ordered_fields;
 			end
 		) c.cl_ordered_fields;
-		(match ctx.com.platform with Php | Flash8 -> () | _ -> add_field_inits ctx.com c);
+		add_field_inits ctx.com c;
 		(match build_metadata ctx.com t with
 		| None -> ()
 		| Some e ->

+ 2 - 1
tests/unit/MyClass.hx

@@ -99,7 +99,8 @@ class InitBase {
 	public var i = 2;
 	public var s = "foo";
 	public var b = true;
-	
+	public var a = [true, false];
+	public var complex = { var i = 10; for (v in 0...15) i++; i; };
 	public function new() { }
 }
 

+ 3 - 0
tests/unit/TestType.hx

@@ -345,6 +345,9 @@ class TestType extends Test {
 		eq(c.i, 2);
 		eq(c.s, "foo");
 		eq(c.b, true);
+		t(c.a[0]);
+		f(c.a[1]);
+		eq(c.complex, 25);
 		
 		var c = new InitChild();
 		eq(c.i, 2);