Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static function buildBehaviours():Array<Field> {
- var pos = haxe.macro.Context.currentPos();
- var fields = haxe.macro.Context.getBuildFields();
- var behaviours:Array<Dynamic> = [];
- var valueFields = MacroHelpers.getFieldsWithMeta("value", fields);
- var valueField = null;
- if (valueFields != null && valueFields.length > 0) {
- valueField = ExprTools.toString(MacroHelpers.getMeta(valueFields[0], "value").params[0]);
- }
- for (f in MacroHelpers.getFieldsWithMeta("behaviour", fields)) {
- fields.remove(f);
- var type:ComplexType = null;
- switch (f.kind) {
- case FVar(f, _): {
- type = f;
- }
- case _:
- }
- var typeName:String = MacroHelpers.complexTypeToString(type);
- if (TypeTools.findField(Context.getLocalClass().get(), f.name) == null) {
- var kind = FProp("get", "set", type);
- // add getter/setter property
- var meta = [];
- meta.push( { name: ":behaviour", pos: pos, params: [] } );
- meta.push( { name: ":bindable", pos: pos, params: [] } );
- for (m in f.meta) {
- if (m.name != ":behaviour" && m.name != "behaviour" ) {
- meta.push(m);
- }
- }
- fields.push({
- name: f.name,
- doc: f.doc,
- meta: meta,
- access: f.access,
- kind: kind,
- pos: haxe.macro.Context.currentPos()
- });
- // add getter function
- var code = "function ():" + typeName + " {\n";
- if (typeName == "Dynamic") {
- code += "return behaviours.getDynamic('" + f.name + "');\n";
- } else {
- code += "return behaviours.get('" + f.name + "');\n";
- }
- code += "}";
- var fnGetter = switch (Context.parseInlineString(code, haxe.macro.Context.currentPos()) ).expr {
- case EFunction(_, f): f;
- case _: throw "false";
- }
- fields.push({
- name: "get_" + f.name,
- doc: null,
- meta: [],
- access: [APrivate],
- kind: FFun(fnGetter),
- pos: haxe.macro.Context.currentPos()
- });
- // add setter funtion
- var code = "function (value:" + typeName + "):" + typeName + " {\n";
- code += "behaviours.set('" + f.name + "', value);\n";
- if (f.name == valueField) {
- code += "haxe.ui.binding.BindingManager.instance.componentPropChanged(this, 'value');\n";
- }
- code += "return value;\n";
- code += "}";
- var fnSetter = switch (Context.parseInlineString(code, haxe.macro.Context.currentPos()) ).expr {
- case EFunction(_, f): f;
- case _: throw "false";
- }
- fields.push({
- name: "set_" + f.name,
- doc: null,
- meta: [],
- access: [APrivate],
- kind: FFun(fnSetter),
- pos: haxe.macro.Context.currentPos()
- });
- }
- // lets dump info into an array and we'll modify the createDefaults at the end
- var orginalMeta = MacroHelpers.getMeta(f, "behaviour");
- var btype = ExprTools.toString(orginalMeta.params[0]);
- var bparam = null;
- if (orginalMeta.params.length > 1) {
- bparam = ExprTools.toString(orginalMeta.params[1]);
- }
- behaviours.push({
- name: f.name,
- btype: btype,
- bparam: bparam
- });
- }
- for (f in MacroHelpers.getFieldsWithMeta("call", fields)) {
- var fn = MacroHelpers.getFunction(fields, f.name);
- var arg0 = "null";
- var void = true;
- switch (f.kind) {
- case FFun(f):
- if (f.args.length > 0) {
- arg0 = f.args[0].name;
- }
- switch (f.ret) {
- case TPath(p):
- void = (p.name == "Void");
- case _:
- }
- case _:
- }
- if (void == true) {
- fn.expr = macro {
- behaviours.call($v{f.name}, $i{arg0});
- };
- } else {
- fn.expr = macro {
- return behaviours.call($v{f.name}, $i{arg0});
- };
- }
- // lets dump info into an array and we'll modify the createDefaults at the end
- var orginalMeta = MacroHelpers.getMeta(f, "call");
- var btype = ExprTools.toString(orginalMeta.params[0]);
- var bparam = null;
- if (orginalMeta.params.length > 1) {
- bparam = ExprTools.toString(orginalMeta.params[1]);
- }
- behaviours.push({
- name: f.name,
- btype: btype,
- bparam: bparam
- });
- }
- if (behaviours.length > 0) {
- // lets modify the registerBehaviours function
- var parts = [];
- for (b in behaviours) {
- if (b.bparam == null) {
- parts.push('behaviours.register("${b.name}", ${b.btype})');
- } else {
- parts.push('behaviours.register("${b.name}", ${b.btype}, ${b.bparam})');
- }
- }
- var registerBehavioursFn = MacroHelpers.getFunction(fields, "registerBehaviours");
- if (registerBehavioursFn == null) {
- var code = "function() {\n";
- code += 'super.registerBehaviours();\n';
- for (line in parts) {
- code += '${line};\n';
- }
- code += "}\n";
- MacroHelpers.addFunction("registerBehaviours", Context.parseInlineString(code, pos), [APrivate, AOverride], fields, pos);
- } else {
- for (line in parts) {
- MacroHelpers.insertLine(registerBehavioursFn, Context.parseInlineString('${line}', pos), -1);
- }
- }
- }
- for (f in MacroHelpers.getFieldsWithMeta("value", fields)) {
- fields.remove(f);
- var meta:MetadataEntry = MacroHelpers.getMeta(f, "value");
- var param:String = ExprTools.toString(meta.params[0]);
- // add getter function
- var code = "function ():Dynamic {\n";
- code += "return " + param + ";\n";
- code += "}";
- var fnGetter = switch (Context.parseInlineString(code, haxe.macro.Context.currentPos()) ).expr {
- case EFunction(_, f): f;
- case _: throw "false";
- }
- fields.push({
- name: "get_" + f.name,
- doc: null,
- meta: [],
- access: [APrivate, AOverride],
- kind: FFun(fnGetter),
- pos: haxe.macro.Context.currentPos()
- });
- // add setter funtion
- var code = "function (value:Dynamic):Dynamic {\n";
- //code += "super.set_" + f.name + "(value);\n";
- code += "" + param + " = value;\n";
- code += "haxe.ui.binding.BindingManager.instance.componentPropChanged(this, '" + f.name + "');\n";
- code += "return value;\n";
- code += "}";
- var fnSetter = switch (Context.parseInlineString(code, haxe.macro.Context.currentPos()) ).expr {
- case EFunction(_, f): f;
- case _: throw "false";
- }
- fields.push({
- name: "set_" + f.name,
- doc: null,
- meta: [],
- access: [APrivate, AOverride],
- kind: FFun(fnSetter),
- pos: haxe.macro.Context.currentPos()
- });
- }
- return fields;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement