Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental @bind support [Discussion] #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 64 additions & 4 deletions domkit/Macros.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package domkit;
#if macro
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
import domkit.Error;

using haxe.macro.Tools;
using haxe.macro.ExprTools;

typedef ComponentData = {
var declaredIds : Map<String,Bool>;
Expand All @@ -27,6 +30,10 @@ class Macros {
public static dynamic function processMacro( id : String, args : Null<Array<haxe.macro.Expr>>, pos : haxe.macro.Expr.Position ) : MarkupParser.Markup {
return null;
}

public static dynamic function processBind( e : haxe.macro.Expr ) : haxe.macro.Expr {
return null;
}

public static function registerComponentsPath( path : String ) {
if( componentsSearchPath.indexOf(path) < 0 )
Expand Down Expand Up @@ -104,9 +111,46 @@ class Macros {
}
}

static function remapBind( rootExpr : haxe.macro.Expr ) {
var isBound = false;
var bname:String = null;
var bindExprs:Array<haxe.macro.Expr> = [];
function _remap(e : haxe.macro.Expr) {
switch( e.expr ) {
case EMeta(m, group) if( m.name == "bind" ):
if(m.params.length > 0)
switch(m.params[0].expr) {
case EConst(CIdent(name)):
bname = name;
default:
}
isBound = true;
_remap(group);
e.expr = group.expr;
return;
case EConst(CIdent(name)) if(isBound):
var b = macro domkit.Macros.bindVar($i{name});
bindExprs.push(b);
return;
case EField(obj, name) if(isBound):
var b = macro domkit.Macros.bindVar($obj.$name);
bindExprs.push(b);
return;
default:
}
e.iter(_remap);
}
_remap(rootExpr);
return {
isBound: isBound,
name: bname,
exprs: bindExprs
};
}

static var latestBlock : Array<Expr> = null;

static function remapBuild( e : haxe.macro.Expr ) {
static function remapBuild( e : haxe.macro.Expr) {
switch( e.expr ) {
case EMeta(m, expr) if( m.name == "rebuild" ):
switch( expr.expr ) {
Expand Down Expand Up @@ -261,10 +305,23 @@ class Macros {
} else
error("Unknown property "+comp.name+"."+p.name, attr.vmin, attr.pmax);
} else {
aexprs.push(macro var __attrib = $e);
var binding = remapBind(e);
var eattrib = { expr : EConst(CIdent("__attrib")), pos : e.pos };
aexprs.push({ expr : EMeta({ pos : e.pos, name : ":privateAccess" }, { expr : ECall(withPos(eset,e.pos),[macro cast tmp.obj,eattrib]), pos : e.pos }), pos : e.pos });
aexprs.push(macro @:privateAccess tmp.initStyle($v{p.name},$eattrib));
var setter = { expr : ECall(withPos(eset,e.pos),[macro cast tmp.obj,eattrib]), pos : e.pos };
aexprs.push(macro {
function __onVarChanged() {
var __attrib = $e;
@:privateAccess $setter;
@:privateAccess tmp.initStyle($v{p.name},$eattrib);
}
$b{binding.exprs};
$e{
if(binding.isBound)
macro registerBind(__onVarChanged, $v{binding.name})
else macro {}
}
__onVarChanged();
});
}
}
}
Expand Down Expand Up @@ -596,4 +653,7 @@ class Macros {

#end

public static macro function bindVar(e : haxe.macro.Expr) : haxe.macro.Expr {
return processBind(e);
}
}