Skip to content
This repository has been archived by the owner on Apr 30, 2018. It is now read-only.

Commit

Permalink
Added support for injecting super call in fragment constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
misprintt committed Oct 29, 2012
1 parent 0dfddd9 commit c78c1af
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
73 changes: 71 additions & 2 deletions src/mpartial/util/ClassFields.hx
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,84 @@ class ClassFields

if(fieldHash.exists(field.name))
{
Context.error("MPartial does not support partials fragments with super classes.", Context.currentPos());
//Context.error("MPartial does not support partials fragments with super classes.", Context.currentPos());

var superField = fieldHash.get(field.name);
field = appendSuperConstructor(field, superField);

}
fieldHash.set(field.name, field);
}

return Lambda.array(fieldHash);
}


/*
Replaces the call to super() with the contents of the super constructor
*/
static function appendSuperConstructor(field:Field, superField:Field):Field
{
trace(Printer.printField("", field));
trace(Printer.printField("", superField));

var fieldExprs:Array<Expr> = [];
var superFunction:Function = null;

switch(field.kind)
{
case FFun(f):
fieldExprs = toExprArray(f.expr);
default:null;
}

switch(superField.kind)
{
case FFun(f):
superFunction = f;
default:null;
}

for(expr in fieldExprs)
{
trace(expr.expr);
switch(expr.expr)
{
case ECall(e, params):
switch(e.expr)
{
case EConst(c):
switch(c)
{
case CIdent(s):
if(s == "super")
{
//replace
var eSuperfunc = EFunction(null, superFunction).at();
var eSuperVar = "superCall".define(eSuperfunc);
fieldExprs.unshift(eSuperVar);
expr.expr = ECall(EConst(CIdent("superCall")).at(), params);
}
default:null;
}
default:null;
}
default:null;
}
}

trace(Printer.printField("", field));

return field;
}

static function toExprArray(expr:Expr):Array<Expr>
{
switch(expr.expr)
{
case EBlock(exprs): return exprs;
default: return [expr];
}
}
/**
Replaces abstract types (T, TData, etc) with concrete ones
*/
Expand Down
24 changes: 22 additions & 2 deletions test/example/SomeClass.hx
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
package example;

class SomeClass
class SomeClass extends SomeSuperClass
{
public var someProperty:Int;

public function new()
{

super();
someSuperProperty = 2;
someProperty = 1;
}

// override public function someMethod()
// {
// super.someMethod();
// }
}

class SomeSuperClass
{
public var someSuperProperty:Int;

public function new()
{
someSuperProperty = 1;
}

public function someMethod()
{
someSuperProperty *= -1;
}
}

0 comments on commit c78c1af

Please sign in to comment.