[macro] TemplateMacro: move init to constructor
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
SDK_PATH=~/sdk
|
||||
echo "`pwd`/target" > ~/.macromedia/Flash_Player/#Security/FlashPlayerTrust/haxework_demo.cfg
|
||||
. /opt/sdk/neko/2.2.0/activate
|
||||
. /opt/sdk/haxe/3.4.7/activate
|
||||
haxe build.hxml && flashplayerdebugger target/demo.swf &
|
||||
. ${SDK_PATH}/neko/2.2.0/activate
|
||||
. ${SDK_PATH}/haxe/3.4.7/activate
|
||||
haxe build.hxml && ${SDK_PATH}/flashplayer/32/flashplayerdebugger target/demo.swf &
|
||||
tail -f ~/.macromedia/Flash_Player/Logs/flashlog.txt
|
||||
|
||||
@@ -17,7 +17,8 @@ import haxework.view.group.VGroupView;
|
||||
@:view var switcher:FrameSwitcher;
|
||||
@:view var tabs:ButtonGroup<String>;
|
||||
|
||||
private function init():Void {
|
||||
public function new():Void {
|
||||
super();
|
||||
switcher.change("list");
|
||||
}
|
||||
|
||||
|
||||
@@ -10,9 +10,6 @@ import haxework.view.SpriteView;
|
||||
|
||||
public function new() {
|
||||
super("test_layout");
|
||||
}
|
||||
|
||||
public function init():Void {
|
||||
resize();
|
||||
content.addEventListener(MouseEvent.CLICK, function(_) {
|
||||
resize();
|
||||
|
||||
@@ -23,7 +23,8 @@ class FontLabelView extends LabelListItem<ThemeFont> {
|
||||
|
||||
@:view var fonts:ListView<ThemeFont>;
|
||||
|
||||
private function init():Void {
|
||||
private function new():Void {
|
||||
super();
|
||||
var values:Array<ThemeFont> = Font.enumerateFonts(true).map(function(font:Font) {
|
||||
return {
|
||||
name: font.fontName,
|
||||
|
||||
@@ -64,4 +64,21 @@ class MacroUtil {
|
||||
case _: null;
|
||||
}
|
||||
}
|
||||
|
||||
public static function upgradeField(field:Field, expr:Expr, position:Int = 0):Field {
|
||||
switch field.kind {
|
||||
case FFun(f):
|
||||
var fieldExpr = f.expr;
|
||||
switch fieldExpr.expr {
|
||||
case EBlock(exprs):
|
||||
exprs.insert(position, expr);
|
||||
fieldExpr = macro $b{exprs};
|
||||
case _:
|
||||
fieldExpr = macro $b{[fieldExpr, expr]}
|
||||
}
|
||||
f.expr = fieldExpr;
|
||||
case _:
|
||||
}
|
||||
return field;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,30 +154,22 @@ class TemplateMacro extends ClassTypeMacro {
|
||||
}
|
||||
}
|
||||
|
||||
private function buildConstructor(init:Bool, constructor:Field = null):Field {
|
||||
var contstrExprs = [];
|
||||
if (constructor != null) {
|
||||
switch constructor.kind {
|
||||
case FFun(f): contstrExprs.push(f.expr);
|
||||
case _:
|
||||
private function upgradeConstructor(constructor:Field = null):Field {
|
||||
if (constructor == null) {
|
||||
constructor = {
|
||||
name: "new",
|
||||
access: [Access.APublic],
|
||||
pos: getPosition(),
|
||||
kind: FieldType.FFun({
|
||||
args: [],
|
||||
expr: macro super(),
|
||||
params: [],
|
||||
ret: null
|
||||
})
|
||||
}
|
||||
} else {
|
||||
contstrExprs.push(macro super());
|
||||
}
|
||||
contstrExprs.push(macro build());
|
||||
if (init) contstrExprs.push(macro init());
|
||||
|
||||
return {
|
||||
name: "new",
|
||||
access: [Access.APublic],
|
||||
pos: getPosition(),
|
||||
kind: FieldType.FFun({
|
||||
args: [],
|
||||
expr: macro $b{contstrExprs},
|
||||
params: [],
|
||||
ret: null
|
||||
})
|
||||
};
|
||||
MacroUtil.upgradeField(constructor, macro build(), 1);
|
||||
return constructor;
|
||||
}
|
||||
|
||||
private static function findViewsBindings(fields:Array<Field>):Map<String, String> {
|
||||
@@ -206,14 +198,13 @@ class TemplateMacro extends ClassTypeMacro {
|
||||
bindings = findViewsBindings(fields);
|
||||
var result:Array<Field> = fields.slice(0);
|
||||
var exprs:Array<Expr> = [];
|
||||
var init = Lambda.exists(result, function(f) return f.name == "init");
|
||||
createElement("this", template, exprs);
|
||||
result.push(buildBuild(exprs));
|
||||
var constructor = Lambda.find(result, function(f) return f.name == "new");
|
||||
if (constructor != null) {
|
||||
result.remove(constructor);
|
||||
}
|
||||
result.push(buildConstructor(init, constructor));
|
||||
result.push(upgradeConstructor(constructor));
|
||||
if (Lambda.count(bindings) > 0) {
|
||||
var keys = Lambda.map({iterator: bindings.keys}, function(k) return '"${k}"').join(",");
|
||||
Context.error('Invalid @:view bindings: $keys', getPosition());
|
||||
|
||||
Reference in New Issue
Block a user