Ver código fonte

[cs] Added unchecked attributes support

Cauê Waneck 11 anos atrás
pai
commit
d7a7c67184
2 arquivos alterados com 75 adições e 0 exclusões
  1. 64 0
      gencs.ml
  2. 11 0
      tests/unit/TestCSharp.hx

+ 64 - 0
gencs.ml

@@ -1435,6 +1435,66 @@ let configure gen =
     expr_s w e
   in
 
+  let rec gen_fpart_attrib w = function
+    | EConst( Ident i ), _ ->
+      write w i
+    | EField( ef, f ), _ ->
+      gen_fpart_attrib w ef;
+      write w ".";
+      write w f
+    | _, p ->
+      gen.gcon.error "Invalid expression inside @:meta metadata" p
+  in
+
+  let rec gen_spart w = function
+    | EConst c, p -> (match c with
+      | Int s | Float s | Ident s ->
+        write w s
+      | String s ->
+        write w "\"";
+        write w (escape s);
+        write w "\""
+      | _ -> gen.gcon.error "Invalid expression inside @:meta metadata" p)
+    | EField( ef, f ), _ ->
+      gen_spart w ef;
+      write w ".";
+      write w f
+    | EBinop( Ast.OpAssign, (EConst (Ident s), _), e2 ), _ ->
+      write w s;
+      write w " = ";
+      gen_spart w e2
+    | EArrayDecl( el ), _ ->
+      write w "new[] {";
+      let fst = ref true in
+      List.iter (fun e ->
+        if !fst then fst := false else write w ", ";
+        gen_spart w e
+      ) el;
+      write w "}"
+    | ECall(fpart,args), _ ->
+      gen_fpart_attrib w fpart;
+      write w "(";
+      let fst = ref true in
+      List.iter (fun e ->
+        if !fst then fst := false else write w ", ";
+        gen_spart w e
+      ) args;
+      write w ")"
+    | _, p ->
+      gen.gcon.error "Invalid expression inside @:meta metadata" p
+  in
+
+  let gen_attributes w metadata =
+    List.iter (function
+      | Meta.Meta, [meta], _ ->
+        write w "[";
+        gen_spart w meta;
+        write w "]";
+        newline w
+      | _ -> ()
+    ) metadata
+  in
+
   let get_string_params cl_types =
     match cl_types with
       | [] ->
@@ -1454,6 +1514,7 @@ let configure gen =
   in
 
 	let rec gen_prop w is_static cl is_final (prop,t,get,set) =
+    gen_attributes w prop.cf_meta;
     let is_interface = cl.cl_interface in
 		let fn_is_final = function
 			| None -> true
@@ -1504,6 +1565,7 @@ let configure gen =
 	in
 
   let rec gen_class_field w ?(is_overload=false) is_static cl is_final cf =
+    gen_attributes w cf.cf_meta;
     let is_interface = cl.cl_interface in
     let name, is_new, is_explicit_iface = match cf.cf_name with
       | "new" -> snd cl.cl_path, true, false
@@ -1825,6 +1887,7 @@ let configure gen =
         begin_block w;
         true
     in
+    gen_attributes w cl.cl_meta;
 
     let is_main =
       match gen.gcon.main_class with
@@ -1959,6 +2022,7 @@ let configure gen =
         begin_block w;
         true
     in
+    gen_attributes w e.e_meta;
 
     print w "public enum %s" (change_clname (snd e.e_path));
     begin_block w;

+ 11 - 0
tests/unit/TestCSharp.hx

@@ -201,6 +201,15 @@ class TestCSharp extends Test
 				});
 	}
 
+	public function testUncheckedAttribute()
+	{
+		var cls = cs.Lib.toNativeType( TestMyClass ),
+				attribType = cs.Lib.toNativeType( cs.system.componentmodel.DescriptionAttribute );
+		var attrib:cs.system.componentmodel.DescriptionAttribute = cast cs.system.Attribute.GetCustomAttribute(cls,attribType,true);
+		t(attrib != null);
+		eq("MyClass Description", attrib.Description);
+	}
+
 #if unsafe
 
 	@:unsafe public function testUnsafe()
@@ -283,6 +292,7 @@ private class HxClass extends NativeClass
 	}
 }
 
+@:meta(System.ComponentModel.Description("MyClass Description"))
 private class TestMyClass extends haxe.test.MyClass
 {
 	@:overload public function new()
@@ -296,6 +306,7 @@ private class TestMyClass extends haxe.test.MyClass
 		alternativeCtorCalled = true;
 	}
 
+	@:meta(System.ComponentModel.Description("Shows if the alternative ctor was called"))
 	public var alternativeCtorCalled:Bool;
 	public var boolCalled:Bool;
 	public var intCalled:Bool;