Ver código fonte

[cs/java] generate specific getTag methods for enum classes to avoid using reflection

Dan Korostelev 10 anos atrás
pai
commit
7faa1da81d
3 arquivos alterados com 33 adições e 2 exclusões
  1. 31 0
      gencommon.ml
  2. 1 1
      std/cs/internal/HxObject.hx
  3. 1 1
      std/java/internal/HxObject.hx

+ 31 - 0
gencommon.ml

@@ -9059,6 +9059,37 @@ struct
 			cl.cl_ordered_statics <- constructs_cf :: cfs @ cl.cl_ordered_statics ;
 			cl.cl_statics <- PMap.add "constructs" constructs_cf cl.cl_statics;
 
+			let getTag_cf_type = tfun [] basic.tstring in
+			let getTag_cf = mk_class_field "getTag" getTag_cf_type true pos (Method MethNormal) [] in
+			getTag_cf.cf_meta <- [(Meta.Final, [], pos)];
+			getTag_cf.cf_expr <- Some {
+				eexpr = TFunction {
+					tf_args = [];
+					tf_type = basic.tstring;
+					tf_expr = {
+						eexpr = TReturn (Some (
+							let e_constructs = mk_static_field_access_infer cl "constructs" pos [] in
+							let e_this = mk (TConst TThis) (TInst (cl,[])) pos in
+							let e_index = mk_field_access gen e_this "index" pos in
+							let e_unsafe_get = mk_field_access gen e_constructs "__unsafe_get" pos in
+							{
+								eexpr = TCall (e_unsafe_get, [e_index]);
+								etype = basic.tstring;
+								epos = pos;
+							}
+						));
+						epos = pos;
+						etype = basic.tvoid;
+					}
+				};
+				etype = getTag_cf_type;
+				epos = pos;
+			};
+
+			cl.cl_ordered_fields <- getTag_cf :: cl.cl_ordered_fields ;
+			cl.cl_fields <- PMap.add "getTag" getTag_cf cl.cl_fields;
+			cl.cl_overrides <- getTag_cf :: cl.cl_overrides;
+
 			(if should_be_hxgen then
 				cl.cl_meta <- (Meta.HxGen,[],cl.cl_pos) :: cl.cl_meta
 			else begin

+ 1 - 1
std/cs/internal/HxObject.hx

@@ -82,7 +82,7 @@ private class Enum
 		untyped this.index = index;
 		untyped this.params = params;
 	}
-	@:final public function getTag():String
+	public function getTag():String
 	{
 		var cl:Dynamic = StdType.getClass(this);
 		return cl.constructs[index];

+ 1 - 1
std/java/internal/HxObject.hx

@@ -78,7 +78,7 @@ private class Enum
 		untyped this.index = index;
 		untyped this.params = params;
 	}
-	@:final public function getTag():String
+	public function getTag():String
 	{
 		var cl:Dynamic = StdType.getEnum(cast this);
 		return cl.constructs[index];