ソースを参照

add MetaAccess.extract function

Dan Korostelev 10 年 前
コミット
af6ee2525d
2 ファイル変更18 行追加0 行削除
  1. 8 0
      interp.ml
  2. 10 0
      std/haxe/macro/Type.hx

+ 8 - 0
interp.ml

@@ -4253,6 +4253,14 @@ let encode_meta m set =
 				failwith "Invalid expression");
 			VNull
 		));
+		"extract", VFunction (Fun1 (fun k ->
+			let k = MetaInfo.from_string (try dec_string k with Invalid_expr -> raise Builtin_error) in
+			try
+				let entry = List.find (fun (m,_,_) -> m = k) (!meta) in
+				encode_meta_entry entry
+			with Not_found ->
+				VNull
+		));
 		"remove", VFunction (Fun1 (fun k ->
 			let k = MetaInfo.from_string (try dec_string k with Invalid_expr -> raise Builtin_error) in
 			meta := List.filter (fun (m,_,_) -> m <> k) (!meta);

+ 10 - 0
std/haxe/macro/Type.hx

@@ -202,6 +202,16 @@ typedef MetaAccess = {
 	**/
 	function get() : Expr.Metadata;
 
+	/**
+		Extract metadata entry by given `name`.
+
+		If there's no metadata with such name, null will be returned.
+		If there's more than one entry with such name, the first one will be returned.
+
+		If `name` is null, compilation fails with an error.
+	**/
+	function extract( name : String ) : Expr.MetadataEntry;
+
 	/**
 		Adds the metadata specified by `name`, `params` and `pos` to the origin
 		of `this` MetaAccess.