Ver código fonte

(Js) generate __empty_constructs__ for enums and use that in Type.allEnums (closes #2775)

Simon Krajewski 11 anos atrás
pai
commit
d771ae3e1f
2 arquivos alterados com 11 adições e 8 exclusões
  1. 10 0
      genjs.ml
  2. 1 8
      std/js/_std/Type.hx

+ 10 - 0
genjs.ml

@@ -1078,6 +1078,16 @@ let generate_enum ctx e =
 		);
 		newline ctx
 	) e.e_names;
+	if has_feature ctx "Type.allEnums" then begin
+		let ctors_without_args = List.filter (fun s ->
+			let ef = PMap.find s e.e_constrs in
+			match follow ef.ef_type with
+				| TFun _ -> false
+				| _ -> true
+		) e.e_names in
+		print ctx "%s.__empty_constructs__ = [%s]" p (String.concat "," (List.map (fun s -> Printf.sprintf "%s.%s" p s) ctors_without_args));
+		newline ctx
+	end;
 	match Codegen.build_metadata ctx.com (TEnumDecl e) with
 	| None -> ()
 	| Some e ->

+ 1 - 8
std/js/_std/Type.hx

@@ -209,14 +209,7 @@ enum ValueType {
 	}
 
 	public static function allEnums<T>( e : Enum<T> ) : Array<T> {
-		var all = [];
-		var cst : Array<String> = untyped e.__constructs__;
-		for( c in cst ) {
-			var v = Reflect.field(e,c);
-			if( !Reflect.isFunction(v) )
-				all.push(v);
-		}
-		return all;
+		return untyped e.__empty_constructs__;
 	}
 
 }