Skip to content

Instantly share code, notes, and snippets.

@ccapndave
Created June 5, 2012 11:54
Show Gist options
  • Select an option

  • Save ccapndave/2874561 to your computer and use it in GitHub Desktop.

Select an option

Save ccapndave/2874561 to your computer and use it in GitHub Desktop.
package ember;
import haxe.macro.Expr;
import haxe.macro.Context;
@:autoBuild(ember.GetterSetterBuilder.build())
@:native("Ember.Object")
extern class Object {
public function new():Void;
public function init():Void;
public function get(field:String):Dynamic;
public function set(field:String, value:Dynamic):Dynamic;
// TODO: We want a macro that intercepts properties and makes them use get and set
}
class GetterSetterBuilder {
@:macro static public function build():Array<Field> {
var fields = Context.getBuildFields();
var cls = Context.getLocalClass().get();
var pos = haxe.macro.Context.currentPos();
var newFields = [];
for (field in fields) {
var isPublic = false, isStatic = false;
switch (field.kind) {
case FVar(t, e):
for (a in field.access) {
switch (a) {
case APublic: isPublic = true;
case AStatic: isStatic = true;
default:
}
}
if (isPublic && !isStatic) {
trace("HERE!!!" + cls.module + "." + field.name);
var name = "hello" + field.name + "" + Math.abs(Math.floor(Math.random() * 999999999999999));
var tint = TPath( { pack : [], name : "Int", params : [], sub : null } );
//var f = ;
newFields.push({
name: name,
doc: null,
meta: [],
access: [APublic],
kind: FFun( {
args: [],
ret: null,
expr: { expr: EBlock([Context.parse('trace(1)', pos)]), pos: pos },
params: []
}),
pos: pos
});
}
default:
}
}
for (newField in newFields)
fields.push(newField);
return fields;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment