Răsfoiți Sursa

getProperty/setProperty for flash9

Nicolas Cannasse 13 ani în urmă
părinte
comite
666a8135c7
3 a modificat fișierele cu 87 adăugiri și 28 ștergeri
  1. 53 3
      genswf9.ml
  2. 14 4
      std/flash/_std/Reflect.hx
  3. 20 21
      tests/unit/TestReflect.hx

+ 53 - 3
genswf9.ml

@@ -1989,7 +1989,56 @@ let generate_class ctx c =
 		else
 			loop_meta (find_meta c)
 	in
+	let generate_prop f acc alloc_slot =
+		match f.cf_kind with
+		| Method _ -> acc
+		| Var v ->
+			let p = f.cf_pos in
+			let ethis = mk (TConst TThis) (TInst (c,[])) p in
+			let acc = (match v.v_read with
+				| AccCall n when n <> "get_" ^ f.cf_name ->
+					(* generate get_xxx method *)
+					let fk = begin_fun ctx [] f.cf_type [ethis] false p in
+					gen_expr ctx false (mk (TReturn (Some (mk (TCall (mk (TField (ethis,n)) t_dynamic p,[])) f.cf_type p))) t_dynamic p);
+					let m = fk() in
+					{
+						hlf_name = ident ("get_" ^ f.cf_name);
+						hlf_slot = alloc_slot();
+						hlf_kind = HFMethod {
+							hlm_type = m;
+							hlm_final = false;
+							hlm_override = false;
+							hlm_kind = MK3Normal;
+						};
+						hlf_metas = None;
+					} :: acc
+				| _ -> 
+					acc
+			) in
+			let acc = (match v.v_write with
+				| AccCall n when n <> "set_" ^ f.cf_name ->
+					(* generatee set_xxx method *)
+					let v = alloc_var "tmp" f.cf_type in
+					let fk = begin_fun ctx [v,None] f.cf_type [ethis] false p in
+					gen_expr ctx false (mk (TReturn (Some (mk (TCall (mk (TField (ethis,n)) t_dynamic p,[mk (TLocal v) v.v_type p])) f.cf_type p))) t_dynamic p);
+					let m = fk() in
+					{
+						hlf_name = ident ("set_" ^ f.cf_name);
+						hlf_slot = alloc_slot();
+						hlf_kind = HFMethod {
+							hlm_type = m;
+							hlm_final = false;
+							hlm_override = false;
+							hlm_kind = MK3Normal;
+						};
+						hlf_metas = None;
+					} :: acc
+				| _ -> acc
+			) in
+			acc
+	in
 	let fields = PMap.fold (fun f acc ->
+		let acc = generate_prop f acc (fun() -> 0) in
 		match generate_field_kind ctx f c false with
 		| None -> acc
 		| Some k ->
@@ -2025,7 +2074,8 @@ let generate_class ctx c =
 	end in
 	let st_field_count = ref 0 in
 	let st_meth_count = ref 0 in
-	let statics = List.map (fun f ->
+	let statics = List.rev (List.fold_left (fun acc f ->
+		let acc = generate_prop f acc (fun() -> incr st_meth_count; !st_meth_count) in
 		let k = (match generate_field_kind ctx f c true with None -> assert false | Some k -> k) in
 		let count = (match k with HFMethod _ -> st_meth_count | HFVar _ -> st_field_count | _ -> assert false) in
 		incr count;
@@ -2034,8 +2084,8 @@ let generate_class ctx c =
 			hlf_slot = !count;
 			hlf_kind = k;
 			hlf_metas = extract_meta f.cf_meta;
-		}
-	) c.cl_ordered_statics in
+		} :: acc
+	) [] c.cl_ordered_statics) in
 	let statics = if not (need_init ctx c) then statics else
 		{
 			hlf_name = ident "init__";

+ 14 - 4
std/flash/_std/Reflect.hx

@@ -38,12 +38,22 @@
 		o[field] = value;
 	}
 
-	public static inline function getProperty( o : Dynamic, field : String ) : Dynamic {
-		return Reflect.field(o,field);
+	public static function getProperty( o : Dynamic, field : String ) : Dynamic untyped {
+		try {
+			return o["get_" + field]();
+		} catch( e : Dynamic ) {
+			return o[field];
+		} catch( e : Dynamic ) {
+			return null;
+		}
 	}
 
-	public static inline function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void {
-		setField(o,field,value);
+	public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
+		try {
+			o["set_" + field](value);
+		} catch( e : Dynamic ) {
+			o[field] = value;
+		}
 	}
 
 	public inline static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic untyped {

+ 20 - 21
tests/unit/TestReflect.hx

@@ -4,11 +4,11 @@ import Type;
 class ClassWithProp {
 	public var x(getX, setX) : Int;
 	var _x : Int;
-	
+
 	public function new() {
 		_x = 5;
 	}
-	
+
 	function getX() {
 		return _x;
 	}
@@ -16,37 +16,37 @@ class ClassWithProp {
 		_x = v;
 		return v;
 	}
-	
+
 	public static var STAT_X(default, setStatX) : Int;
-	
+
 	static function setStatX(v) {
 		STAT_X = v * 2;
 		return v;
 	}
-	
+
 	static function __init__() {
 		STAT_X = 3;
 	}
-	
-	
+
+
 }
 
 class SubClassWithProp extends ClassWithProp {
 	public var y(default, setY) : Int;
-	
+
 	public function new() {
 		super();
 		y = 10;
 	}
-	
+
 	override function getX() {
 		return _x + 1;
 	}
-	
+
 	function getY() {
 		return y;
 	}
-	
+
 	function setY(v) {
 		y = v;
 		return v;
@@ -238,39 +238,38 @@ class TestReflect extends Test {
 		f( Reflect.compareMethods(a.add,null) );
 		f( Reflect.compareMethods(null,a.add) );
 	}
-	
-	#if haxe3
+
 	function testGetProp() {
-		
+
 		var c = new ClassWithProp();
 		eq( c.x, 5);
+
 		eq( Reflect.getProperty(c, "x"), 5);
 		Reflect.setProperty(c, "x", 10);
 		eq( c.x, 10);
 		eq( Reflect.getProperty(c, "x"), 10);
-		
+
 		var c = new SubClassWithProp();
 		eq( c.x, 6);
 		eq( Reflect.getProperty(c, "x"), 6);
 		eq( c.y, 10);
 		eq( Reflect.getProperty(c, "y"), 10);
-		
+
 		Reflect.setProperty(c, "x", 10);
 		Reflect.setProperty(c, "y", 20);
-		
+
 		eq( c.x, 11);
 		eq( Reflect.getProperty(c, "x"), 11);
 		eq( c.y, 20);
 		eq( Reflect.getProperty(c, "y"), 20);
-		
+
 		eq( ClassWithProp.STAT_X, 6 );
 		eq( Reflect.getProperty(ClassWithProp, "STAT_X"), 6 );
-		
+
 		Reflect.setProperty(ClassWithProp, "STAT_X", 8);
-		
+
 		eq( ClassWithProp.STAT_X, 16 );
 		eq( Reflect.getProperty(ClassWithProp, "STAT_X"), 16 );
 	}
-	#end
 
 }