Advertisement
Guest User

Untitled

a guest
Apr 12th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 8.69 KB | None | 0 0
  1.     public static function buildBehaviours():Array<Field> {
  2.         var pos = haxe.macro.Context.currentPos();
  3.         var fields = haxe.macro.Context.getBuildFields();
  4.        
  5.         var behaviours:Array<Dynamic> = [];
  6.        
  7.         var valueFields = MacroHelpers.getFieldsWithMeta("value", fields);
  8.         var valueField = null;
  9.         if (valueFields != null && valueFields.length > 0) {
  10.             valueField = ExprTools.toString(MacroHelpers.getMeta(valueFields[0], "value").params[0]);
  11.         }
  12.        
  13.         for (f in MacroHelpers.getFieldsWithMeta("behaviour", fields)) {
  14.             fields.remove(f);
  15.            
  16.             var type:ComplexType = null;
  17.             switch (f.kind) {
  18.                 case FVar(f, _): {
  19.                     type = f;
  20.                 }
  21.                 case _:
  22.             }
  23.             var typeName:String = MacroHelpers.complexTypeToString(type);
  24.             if (TypeTools.findField(Context.getLocalClass().get(), f.name) == null) {
  25.                 var kind = FProp("get", "set", type);
  26.                
  27.                 // add getter/setter property
  28.                 var meta = [];
  29.                 meta.push( { name: ":behaviour", pos: pos, params: [] } );
  30.                 meta.push( { name: ":bindable", pos: pos, params: [] } );
  31.                 for (m in f.meta) {
  32.                     if (m.name != ":behaviour" && m.name != "behaviour" ) {
  33.                         meta.push(m);
  34.                     }
  35.                 }
  36.                
  37.                 fields.push({
  38.                     name: f.name,
  39.                     doc: f.doc,
  40.                     meta: meta,
  41.                     access: f.access,
  42.                     kind: kind,
  43.                     pos: haxe.macro.Context.currentPos()
  44.                 });
  45.                 // add getter function
  46.                 var code = "function ():" + typeName + " {\n";
  47.                 if (typeName == "Dynamic") {
  48.                     code += "return behaviours.getDynamic('" + f.name + "');\n";
  49.                 } else {
  50.                     code += "return behaviours.get('" + f.name + "');\n";
  51.                 }
  52.                 code += "}";
  53.                 var fnGetter = switch (Context.parseInlineString(code, haxe.macro.Context.currentPos()) ).expr {
  54.                     case EFunction(_, f): f;
  55.                     case _: throw "false";
  56.                 }
  57.                 fields.push({
  58.                     name: "get_" + f.name,
  59.                     doc: null,
  60.                     meta: [],
  61.                     access: [APrivate],
  62.                     kind: FFun(fnGetter),
  63.                     pos: haxe.macro.Context.currentPos()
  64.                 });
  65.                      
  66.                 // add setter funtion
  67.                 var code = "function (value:" + typeName + "):" + typeName + " {\n";
  68.                 code += "behaviours.set('" + f.name + "', value);\n";
  69.                 if (f.name == valueField) {
  70.                     code += "haxe.ui.binding.BindingManager.instance.componentPropChanged(this, 'value');\n";
  71.                 }
  72.                 code += "return value;\n";
  73.                 code += "}";
  74.  
  75.                 var fnSetter = switch (Context.parseInlineString(code, haxe.macro.Context.currentPos()) ).expr {
  76.                     case EFunction(_, f): f;
  77.                     case _: throw "false";
  78.                 }
  79.                 fields.push({
  80.                     name: "set_" + f.name,
  81.                     doc: null,
  82.                     meta: [],
  83.                     access: [APrivate],
  84.                     kind: FFun(fnSetter),
  85.                     pos: haxe.macro.Context.currentPos()
  86.                 });
  87.             }
  88.            
  89.            
  90.             // lets dump info into an array and we'll modify the createDefaults at the end            
  91.             var orginalMeta = MacroHelpers.getMeta(f, "behaviour");
  92.             var btype = ExprTools.toString(orginalMeta.params[0]);
  93.             var bparam = null;
  94.             if (orginalMeta.params.length > 1) {
  95.                 bparam = ExprTools.toString(orginalMeta.params[1]);
  96.             }
  97.            
  98.             behaviours.push({
  99.                name: f.name,
  100.                btype: btype,
  101.                bparam: bparam
  102.             });
  103.         }
  104.        
  105.         for (f in MacroHelpers.getFieldsWithMeta("call", fields)) {
  106.             var fn = MacroHelpers.getFunction(fields, f.name);
  107.             var arg0 = "null";
  108.             var void = true;
  109.             switch (f.kind) {
  110.                 case FFun(f):
  111.                     if (f.args.length > 0) {
  112.                         arg0 = f.args[0].name;
  113.                     }
  114.                     switch (f.ret) {
  115.                         case TPath(p):
  116.                             void = (p.name == "Void");
  117.                         case _:  
  118.                     }
  119.                 case _:
  120.             }
  121.            
  122.             if (void == true) {
  123.                 fn.expr = macro {
  124.                     behaviours.call($v{f.name}, $i{arg0});
  125.                 };
  126.             } else {
  127.                 fn.expr = macro {
  128.                     return behaviours.call($v{f.name}, $i{arg0});
  129.                 };
  130.             }
  131.            
  132.             // lets dump info into an array and we'll modify the createDefaults at the end            
  133.             var orginalMeta = MacroHelpers.getMeta(f, "call");
  134.             var btype = ExprTools.toString(orginalMeta.params[0]);
  135.             var bparam = null;
  136.             if (orginalMeta.params.length > 1) {
  137.                 bparam = ExprTools.toString(orginalMeta.params[1]);
  138.             }
  139.  
  140.             behaviours.push({
  141.                name: f.name,
  142.                btype: btype,
  143.                bparam: bparam
  144.             });
  145.         }
  146.        
  147.         if (behaviours.length > 0) {
  148.             // lets modify the registerBehaviours function
  149.            
  150.             var parts = [];
  151.             for (b in behaviours) {
  152.                 if (b.bparam == null) {
  153.                     parts.push('behaviours.register("${b.name}", ${b.btype})');
  154.                 } else {
  155.                     parts.push('behaviours.register("${b.name}", ${b.btype}, ${b.bparam})');
  156.                 }
  157.             }
  158.            
  159.             var registerBehavioursFn = MacroHelpers.getFunction(fields, "registerBehaviours");
  160.             if (registerBehavioursFn == null) {
  161.                 var code = "function() {\n";
  162.                 code += 'super.registerBehaviours();\n';
  163.                 for (line in parts) {
  164.                     code += '${line};\n';
  165.                 }
  166.                 code += "}\n";
  167.                 MacroHelpers.addFunction("registerBehaviours", Context.parseInlineString(code, pos), [APrivate, AOverride], fields, pos);
  168.             } else {
  169.                 for (line in parts) {
  170.                     MacroHelpers.insertLine(registerBehavioursFn, Context.parseInlineString('${line}', pos), -1);    
  171.                 }
  172.             }
  173.         }
  174.        
  175.         for (f in MacroHelpers.getFieldsWithMeta("value", fields)) {
  176.             fields.remove(f);
  177.             var meta:MetadataEntry = MacroHelpers.getMeta(f, "value");
  178.             var param:String = ExprTools.toString(meta.params[0]);
  179.  
  180.             // add getter function
  181.             var code = "function ():Dynamic {\n";
  182.             code += "return " + param + ";\n";
  183.             code += "}";
  184.             var fnGetter = switch (Context.parseInlineString(code, haxe.macro.Context.currentPos()) ).expr {
  185.                 case EFunction(_, f): f;
  186.                 case _: throw "false";
  187.             }
  188.             fields.push({
  189.                 name: "get_" + f.name,
  190.                 doc: null,
  191.                 meta: [],
  192.                 access: [APrivate, AOverride],
  193.                 kind: FFun(fnGetter),
  194.                 pos: haxe.macro.Context.currentPos()
  195.             });
  196.            
  197.             // add setter funtion
  198.             var code = "function (value:Dynamic):Dynamic {\n";
  199.             //code += "super.set_" + f.name + "(value);\n";
  200.             code += "" + param + " = value;\n";
  201.             code += "haxe.ui.binding.BindingManager.instance.componentPropChanged(this, '" + f.name + "');\n";
  202.             code += "return value;\n";
  203.             code += "}";
  204.  
  205.             var fnSetter = switch (Context.parseInlineString(code, haxe.macro.Context.currentPos()) ).expr {
  206.                 case EFunction(_, f): f;
  207.                 case _: throw "false";
  208.             }
  209.             fields.push({
  210.                 name: "set_" + f.name,
  211.                 doc: null,
  212.                 meta: [],
  213.                 access: [APrivate, AOverride],
  214.                 kind: FFun(fnSetter),
  215.                 pos: haxe.macro.Context.currentPos()
  216.             });
  217.         }
  218.        
  219.         return fields;
  220.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement