Explorar el Código

[cs] optimize Type.getEnumConstructs and mark some enum class fields as protected

Dan Korostelev hace 8 años
padre
commit
5f87427c28
Se han modificado 3 ficheros con 18 adiciones y 21 borrados
  1. 1 1
      src/generators/gencommon/enumToClass2.ml
  2. 9 5
      std/cs/_std/Type.hx
  3. 8 15
      std/cs/internal/HxObject.hx

+ 1 - 1
src/generators/gencommon/enumToClass2.ml

@@ -69,7 +69,7 @@ struct
 
 		(* add constructs field (for reflection) *)
 		let cf_constructs = mk_class_field "__hx_constructs" (gen.gclasses.nativearray basic.tstring) true pos (Var { v_read = AccNormal; v_write = AccNever }) [] in
-		cf_constructs.cf_meta <- (Meta.ReadOnly,[],pos) :: cf_constructs.cf_meta;
+		cf_constructs.cf_meta <- (Meta.ReadOnly,[],pos) :: (Meta.Protected,[],pos) :: cf_constructs.cf_meta;
 		cf_constructs.cf_expr <- Some (mk_nativearray_decl gen basic.tstring (List.map (fun s -> ExprBuilder.make_string gen.gcon s pos) en.e_names) pos);
 		add_static cl_enum cf_constructs;
 

+ 9 - 5
std/cs/_std/Type.hx

@@ -258,11 +258,15 @@ enum ValueType {
 	}
 
 	public static function getEnumConstructs( e : Enum<Dynamic> ) : Array<String> {
-		if (Reflect.hasField(e, "__hx_constructs")) {
-			var ret:Array<String> = cs.Lib.array(untyped e.__hx_constructs);
-			return ret.copy();
-		}
-		return cs.Lib.array(cs.system.Enum.GetNames(untyped e));
+		var t = cs.Lib.as(e, cs.system.Type);
+		var f = t.GetField("__hx_constructs", new cs.Flags(BindingFlags.Static) | BindingFlags.NonPublic);
+		if (f != null) {
+			var values:cs.system.Array = f.GetValue(null);
+			var copy = new cs.NativeArray(values.Length);
+			cs.system.Array.Copy(values, copy, values.Length);
+			return cs.Lib.array(copy);
+		} else
+			return cs.Lib.array(cs.system.Enum.GetNames(t));
 	}
 
 	public static function typeof( v : Dynamic ) : ValueType {

+ 8 - 15
std/cs/internal/HxObject.hx

@@ -85,32 +85,26 @@ class DynamicObject extends HxObject implements Dynamic
 #if core_api_serialize
 @:meta(System.Serializable)
 #end
-class HxEnum
-{
-	@:readOnly private var index(default,never):Int;
+class HxEnum {
+	@:readOnly var index(default,never):Int;
 
-	public function new(index:Int)
-	{
+	@:protected function new(index:Int) {
 		untyped this.index = index;
 	}
 
-	public function getTag():String
-	{
+	public function getTag():String {
 		return throw 'Not Implemented';
 	}
 
-	public function getParams():Array<{}>
-	{
+	public function getParams():Array<{}> {
 		return [];
 	}
 
-	public function toString():String
-	{
+	public function toString():String {
 		return getTag();
 	}
 
-	public static function paramsToString(tag:String, params:Vector<Dynamic>):String
-	{
+	@:protected static function paramsToString(tag:String, params:Vector<Dynamic>):String {
 		var ret = new StringBuf();
 		ret.add(tag);
 		ret.add("(");
@@ -127,8 +121,7 @@ class HxEnum
 		return ret.toString();
 	}
 
-	public static function paramsGetHashCode(index:Int, params:Vector<Dynamic>):Int
-	{
+	@:protected static function paramsGetHashCode(index:Int, params:Vector<Dynamic>):Int {
 		var h:Int = 19;
 		if (params != null) for (p in params)
 		{