Browse Source

generate default values in xml (closes #2677)

Simon Krajewski 11 years ago
parent
commit
eaed45b2d1
3 changed files with 36 additions and 5 deletions
  1. 30 3
      genxml.ml
  2. 3 2
      std/haxe/rtti/CType.hx
  3. 3 0
      std/haxe/rtti/XmlParser.hx

+ 30 - 3
genxml.ml

@@ -91,14 +91,37 @@ let gen_meta meta =
 		) meta in
 		[node "meta" [] nodes]
 
-let rec gen_type t =
+let rec gen_type ?(tfunc=None) t =
 	match t with
 	| TMono m -> (match !m with None -> tag "unknown" | Some t -> gen_type t)
 	| TEnum (e,params) -> gen_type_decl "e" (TEnumDecl e) params
 	| TInst (c,params) -> gen_type_decl "c" (TClassDecl c) params
 	| TAbstract (a,params) -> gen_type_decl "x" (TAbstractDecl a) params
 	| TType (t,params) -> gen_type_decl "t" (TTypeDecl t) params
-	| TFun (args,r) -> node "f" ["a",String.concat ":" (List.map gen_arg_name args)] (List.map gen_type (List.map (fun (_,opt,t) -> if opt then follow_param t else t) args @ [r]))
+	| TFun (args,r) ->
+		let s_const ct = match ct with
+			| TString s -> Printf.sprintf "'%s'" (Ast.s_escape s)
+			| _ -> s_const ct
+		in
+		let names = String.concat ":" (List.map gen_arg_name args) in
+		let values = match tfunc with
+			| None ->
+				[]
+			| Some tfunc ->
+				let has_value = ref false in
+				let values = List.map (fun (_,cto) -> match cto with
+					| None ->
+						""
+					| Some ct ->
+						has_value := true;
+						s_const ct
+				) tfunc.tf_args in
+				if !has_value then
+					["v",String.concat ":" values]
+				else
+					[]
+		in
+		node "f" (("a",names) :: values) (List.map gen_type (List.map (fun (_,opt,t) -> if opt then follow_param t else t) args @ [r]))
 	| TAnon a -> node "a" [] (pmap (fun f -> gen_field [] { f with cf_public = false }) a.a_fields)
 	| TDynamic t2 -> node "d" [] (if t == t2 then [] else [gen_type t2])
 	| TLazy f -> gen_type (!f())
@@ -129,7 +152,11 @@ and gen_field att f =
 		| [] -> []
 		| nl -> [node "overloads" [] nl]
 	in
-	node f.cf_name (if f.cf_public then ("public","1") :: att else att) (gen_type f.cf_type :: gen_meta f.cf_meta @ gen_doc_opt f.cf_doc @ overloads)
+	let tfunc = match f.cf_expr with
+		| Some ({eexpr = TFunction tf}) -> Some tf
+		| _ -> None
+	in
+	node f.cf_name (if f.cf_public then ("public","1") :: att else att) (gen_type ~tfunc:tfunc f.cf_type :: gen_meta f.cf_meta @ gen_doc_opt f.cf_doc @ overloads)
 
 let gen_constr e =
 	let doc = gen_doc_opt e.ef_doc in

+ 3 - 2
std/haxe/rtti/CType.hx

@@ -25,12 +25,13 @@ typedef Path = String
 
 typedef Platforms = List<String>
 
+typedef FunctionArgument = { name : String, opt : Bool, t : CType, ?value:String }
 enum CType {
 	CUnknown;
 	CEnum( name : Path, params : List<CType> );
 	CClass( name : Path, params : List<CType> );
 	CTypedef( name : Path, params : List<CType> );
-	CFunction( args : List<{ name : String, opt : Bool, t : CType }>, ret : CType );
+	CFunction( args : List<FunctionArgument>, ret : CType );
 	CAnonymous( fields : List<ClassField> );
 	CDynamic( ?t : CType );
 	CAbstract( name : Path, params : List<CType> );
@@ -205,7 +206,7 @@ class TypeApi {
 		case CFunction(args,ret):
 			switch( t2 ) {
 			case CFunction(args2,ret2):
-				return leq(function(a,b) {
+				return leq(function(a:FunctionArgument,b:FunctionArgument) {
 					return a.name == b.name && a.opt == b.opt && typeEq(a.t,b.t);
 				},args,args2) && typeEq(ret,ret2);
 			default:

+ 3 - 0
std/haxe/rtti/XmlParser.hx

@@ -560,6 +560,7 @@ class XmlParser {
 			var args = new List();
 			var aname = x.att.a.split(":");
 			var eargs = aname.iterator();
+			var evalues = x.has.v ? x.att.v.split(":").iterator() : null;
 			for( e in x.elements ) {
 				var opt = false;
 				var a = eargs.next();
@@ -569,10 +570,12 @@ class XmlParser {
 					opt = true;
 					a = a.substr(1);
 				}
+				var v = evalues == null ? null : evalues.next();
 				args.add({
 					name : a,
 					opt : opt,
 					t : xtype(e),
+					value : v
 				});
 			}
 			var ret = args.last();