Browse Source

allow @:expose(field names) on abstracts

Simon Krajewski 12 years ago
parent
commit
017e1c038e
4 changed files with 20 additions and 3 deletions
  1. 3 3
      std/haxe/macro/Build.hx
  2. 7 0
      tests/unit/MyAbstract.hx
  3. 8 0
      tests/unit/TestType.hx
  4. 2 0
      typeload.ml

+ 3 - 3
std/haxe/macro/Build.hx

@@ -50,7 +50,7 @@ class Build {
 		return fields;
 	}
 
-	macro static public function forwardAbstractFields(fieldExprs:Array<Expr>):Array<Field> {
+	macro static public function exposeUnderlyingFields(fieldExprs:Array<Expr>):Array<Field> {
 		var fields = Context.getBuildFields();
 		var a = switch(Context.getLocalClass().get().kind) {
 			case KAbstractImpl(a): a;
@@ -66,7 +66,7 @@ class Build {
 					return t2;
 				}
 				c;
-			case _: Context.error("Underlying type of forwarding abstract must be a class", Context.currentPos());
+			case _: Context.error("Underlying type of exposing abstract must be a class", Context.currentPos());
 		}
 		function getIdentName(e) return switch(e.expr) {
 			case EConst(CIdent(s)): s;
@@ -110,7 +110,7 @@ class Build {
 			if (cField == null) Context.error('Underlying type has no field $fieldName', fieldExpr.pos);
 			switch(cField.kind) {
 				case FMethod(_):
-				case _: Context.error("Only function fields can be forwarded", fieldExpr.pos);
+				case _: Context.error("Only function fields can be exposed", fieldExpr.pos);
 			}
 			cField.type = map(cField.type);
 			var field = toField(cField);

+ 7 - 0
tests/unit/MyAbstract.hx

@@ -314,4 +314,11 @@ abstract FakeEnumAbstract(Int) {
 	var NotFound = 404;
 	var MethodNotAllowed = 405;
 }
+
+@:expose(push, pop)
+abstract ExposingAbstract<S>(Array<S>) {
+	public inline function new() {
+		this = [];
+	}
+}
 #end

+ 8 - 0
tests/unit/TestType.hx

@@ -860,4 +860,12 @@ class TestType extends Test {
 		var a:Array<unit.MyAbstract.MyInt> = [1, 2, 3];
 		var b:Array<unit.MyAbstract.MyInt2> = a;
 	}
+	
+	function testExposingAbstract() {
+		#if !macro
+		var ea = new unit.MyAbstract.ExposingAbstract();
+		ea.push(12);
+		eq(12, ea.pop());
+		#end
+	}
 }

+ 2 - 0
typeload.ml

@@ -177,6 +177,8 @@ let make_module ctx mpath file tdecls loadp =
 							c.cl_meta <- m :: c.cl_meta;
 						| (Meta.FakeEnum,_,_) ->
 							c.cl_meta <- (Meta.Build,[ECall((EField((EField((EField((EConst(Ident "haxe"),p),"macro"),p),"Build"),p),"buildFakeEnum"),p),[]),p],p) :: c.cl_meta;
+						| (Meta.Expose,el,_) ->
+							c.cl_meta <- (Meta.Build,[ECall((EField((EField((EField((EConst(Ident "haxe"),p),"macro"),p),"Build"),p),"exposeUnderlyingFields"),p),el),p],p) :: c.cl_meta;
 						| _ ->
 							()
 					) a.a_meta;