diff --git a/src/bindx/Bind.hx b/src/bindx/Bind.hx index c755e32..3de4fe0 100644 --- a/src/bindx/Bind.hx +++ b/src/bindx/Bind.hx @@ -7,22 +7,22 @@ import bindx.macro.BindMacros; class Bind { @:noUsing macro static public function bind(field:Expr, listener:Expr):Expr { - return BindMacros.internalBind(field, listener, true); + return BindMacros.bind(field, listener, true); } @:noUsing macro static public function bindTo(field:Expr, target:Expr):Expr { - return BindMacros.internalBindTo(field, target); + return BindMacros.bindTo(field, target); } @:noUsing macro static public function unbind(field:Expr, ?listener:Expr):Expr { - return BindMacros.internalBind(field, listener, false); + return BindMacros.bind(field, listener, false); } @:noUsing macro static public function notify(field:Expr, ?oldValue:Expr, ?newValue:Expr):Expr { - return BindMacros.internalNotify(field, oldValue, newValue); + return BindMacros.notify(field, oldValue, newValue); } @:noUsing macro static public function unbindAll(object:ExprOf):Expr { - return BindMacros.internalUnbindAll(object); + return BindMacros.unbindAll(object); } } \ No newline at end of file diff --git a/src/bindx/BindExt.hx b/src/bindx/BindExt.hx index f479168..246df25 100644 --- a/src/bindx/BindExt.hx +++ b/src/bindx/BindExt.hx @@ -5,25 +5,26 @@ import haxe.macro.Context; import bindx.macro.BindExtMacros; using haxe.macro.Tools; +using bindx.macro.MacroUtils; @:access(bindx.macro.BindxExtMacro) class BindExt { @:noUsing macro static public function expr(expr:ExprOf, listener:ExprOf->Null->Void>):ExprOfVoid> { - return BindxExtMacro.internalBindExpr(expr, listener); + return BindxExtMacro.bindExpr(expr, listener); } @:noUsing macro static public function exprTo(expr:ExprOf, target:ExprOf):ExprOfVoid> { - var type = Context.typeof(expr).toComplexType(); - return BindxExtMacro.internalBindExpr(expr, macro function (_, to:Null<$type>) $target = to); + var type = expr.getComplexType(); + return BindxExtMacro.bindExpr(expr, macro function (_, to:Null<$type>) $target = to); } @:noUsing macro static public function chain(expr:ExprOf, listener:Expr):ExprOfVoid> { - return BindxExtMacro.internalBindChain(expr, listener); + return BindxExtMacro.bindChain(expr, listener); } @:noUsing macro static public function chainTo(expr:ExprOf, target:ExprOf):ExprOfVoid> { - var type = Context.typeof(expr).toComplexType(); - return BindxExtMacro.internalBindChain(expr, macro function (_, to:Null<$type>) $target = to); + var type = expr.getComplexType(); + return BindxExtMacro.bindChain(expr, macro function (_, to:Null<$type>) $target = to); } } \ No newline at end of file diff --git a/src/bindx/macro/BindExtMacros.hx b/src/bindx/macro/BindExtMacros.hx index 545c047..2be00d3 100644 --- a/src/bindx/macro/BindExtMacros.hx +++ b/src/bindx/macro/BindExtMacros.hx @@ -11,6 +11,7 @@ import bindx.macro.BindMacros; using Lambda; using StringTools; +using bindx.macro.MacroUtils; using haxe.macro.Tools; private typedef FieldExpr = { @@ -31,7 +32,7 @@ private typedef Chain = { @:access(bindx.macro.BindMacros) class BindxExtMacro { - static inline function internalBindChain(expr:Expr, listener:Expr):Expr { + static inline function bindChain(expr:Expr, listener:Expr):Expr { var zeroListener = listenerName(0, ""); var chain = null; try { chain = warnPrepareChain(expr); } catch (e:GenericError) e.contextError(); @@ -42,18 +43,8 @@ class BindxExtMacro { return res; } - static inline function unwrapFormatedString(expr:Expr):Expr { - return if (MacroStringTools.isFormatExpr(expr)) { - var f = switch (expr.expr) { - case EConst(CString(s)): s; - case _: null; - } - if (f != null) MacroStringTools.formatString(f, expr.pos) else expr; - } else expr; - } - - static function internalBindExpr(expr:Expr, listener:Expr):Expr { - var type = Context.typeof(expr).toComplexType(); + static function bindExpr(expr:Expr, listener:Expr):Expr { + var type = expr.getComplexType(); var listenerNameExpr = macro listener; var fieldListenerName = "fieldListener"; var fieldListenerNameExpr = macro $i{fieldListenerName}; @@ -259,7 +250,7 @@ class BindxExtMacro { while (++i < fields.length - 1) { var field = fields[i + 1]; var prev = fields[i]; - var type = Context.typeof(field.e).toComplexType(); + var type = field.e.getComplexType(); var listenerName = listenerName(i+1, prefix); var listenerNameExpr = macro $i { listenerName }; @@ -299,8 +290,7 @@ class BindxExtMacro { if (field.params != null) { fieldListenerBody.unshift(macro $i { oldValue } = n); - fieldListenerBody.unshift(macro try { n = $e; } catch (e:Dynamic) { }); - fieldListenerBody.unshift(macro var n:Null < $type > = null); + fieldListenerBody.unshift(macro var n:Null<$type> = try { $e; } catch (_:Dynamic) { null; }); fieldListenerBody.unshift(macro var o:Null<$type> = $i{oldValue} ); res.init.push(macro var $oldValue:Null<$type> = null); @@ -322,7 +312,7 @@ class BindxExtMacro { prevListenerNameExpr = listenerNameExpr; } - if (zeroListener == null || zeroListener.f.bindable == false) + if (zeroListener == null || !zeroListener.f.bindable) throw new GenericError('${expr.toString()} is not bindable.', expr.pos); var zeroName = zeroListener.f.e.toString(); @@ -340,5 +330,15 @@ class BindxExtMacro { } return res; } + + static inline function unwrapFormatedString(expr:Expr):Expr { + return if (MacroStringTools.isFormatExpr(expr)) { + var f = switch (expr.expr) { + case EConst(CString(s)): s; + case _: null; + } + if (f != null) MacroStringTools.formatString(f, expr.pos) else expr; + } else expr; + } } #end \ No newline at end of file diff --git a/src/bindx/macro/BindMacros.hx b/src/bindx/macro/BindMacros.hx index dca2e3a..d828eb9 100644 --- a/src/bindx/macro/BindMacros.hx +++ b/src/bindx/macro/BindMacros.hx @@ -8,30 +8,30 @@ import haxe.macro.Type; import haxe.macro.Context; using haxe.macro.Tools; -using bindx.macro.MetaUtils; +using bindx.macro.MacroUtils; using Lambda; @:access(bindx.macro.BindableMacros) class BindMacros { - static inline function internalBind(field:Expr, listener:Expr, doBind:Bool):Expr { + static inline function bind(field:Expr, listener:Expr, doBind:Bool):Expr { var fieldData = warnCheckField(field); return if (doBind) BindableMacros.bindingSignalProvider.getClassFieldBindExpr(fieldData.e, fieldData.field, listener); else BindableMacros.bindingSignalProvider.getClassFieldUnbindExpr(fieldData.e, fieldData.field, listener); } - static inline function internalBindTo(field:Expr, target:Expr):Expr { + static inline function bindTo(field:Expr, target:Expr):Expr { var fieldData = warnCheckField(field); return BindableMacros.bindingSignalProvider.getClassFieldBindToExpr(fieldData.e, fieldData.field, target); } - static inline function internalNotify(field:Expr, ?oldValue:Expr, ?newValue:Expr):Expr { + static inline function notify(field:Expr, ?oldValue:Expr, ?newValue:Expr):Expr { var fieldData = warnCheckField(field); return BindableMacros.bindingSignalProvider.getClassFieldChangedExpr(fieldData.e, fieldData.field, oldValue, newValue); } - static inline function internalUnbindAll(object:ExprOf):Expr { - var type = Context.typeof(object).follow(); + static inline function unbindAll(object:ExprOf):Expr { + var type = Context.typeof(object); if (!isBindable(type.getClass())) { Context.error('\'${object.toString()}\' must be bindx.IBindable', object.pos); } diff --git a/src/bindx/macro/BindSignalProvider.hx b/src/bindx/macro/BindSignalProvider.hx index bd47262..736867b 100644 --- a/src/bindx/macro/BindSignalProvider.hx +++ b/src/bindx/macro/BindSignalProvider.hx @@ -6,7 +6,7 @@ import haxe.macro.Type; import haxe.macro.Context; import bindx.BindSignal; -using bindx.macro.MetaUtils; +using bindx.macro.MacroUtils; using haxe.macro.Tools; using Lambda; diff --git a/src/bindx/macro/BindableMacros.hx b/src/bindx/macro/BindableMacros.hx index 766ff68..58cf0e7 100644 --- a/src/bindx/macro/BindableMacros.hx +++ b/src/bindx/macro/BindableMacros.hx @@ -8,7 +8,7 @@ import haxe.macro.Context; using haxe.macro.Tools; using Lambda; using StringTools; -using bindx.macro.MetaUtils; +using bindx.macro.MacroUtils; class BindableMacros { diff --git a/src/bindx/macro/MetaUtils.hx b/src/bindx/macro/MacroUtils.hx similarity index 93% rename from src/bindx/macro/MetaUtils.hx rename to src/bindx/macro/MacroUtils.hx index eb02fd4..6c5a218 100644 --- a/src/bindx/macro/MetaUtils.hx +++ b/src/bindx/macro/MacroUtils.hx @@ -1,5 +1,6 @@ package bindx.macro; +#if macro import haxe.macro.Type; import haxe.macro.Expr; import haxe.macro.Context; @@ -62,7 +63,7 @@ class ClassTypeMetaUtils { return bindableMeta(classType) != null; } -class ExprMetaUtils { +class ExprUtils { static public inline function isTrue(expr:Expr):Bool return expr.expr.match(EConst(CIdent("true"))); @@ -78,4 +79,9 @@ class ExprMetaUtils { static public inline function isNullOrTrue(expr:Expr):Bool return expr == null || isTrue(expr); -} \ No newline at end of file + + static public inline function getComplexType(expr:Expr):ComplexType { + return Context.typeof(expr).toComplexType(); + } +} +#end \ No newline at end of file