Skip to content

Instantly share code, notes, and snippets.

@skial
Created March 17, 2015 15:02
Show Gist options
  • Select an option

  • Save skial/df052fd11bb0dc277b7f to your computer and use it in GitHub Desktop.

Select an option

Save skial/df052fd11bb0dc277b7f to your computer and use it in GitHub Desktop.
Change with the output order, but not the execution order of classes using Haxe 3.2.0.rc.1 and node.
-cp src
-main Main
-js bin/test.js
-cmd node bin/test.js
package;
import haxe.ds.StringMap;
import haxe.macro.Type;
import haxe.macro.Expr;
import haxe.macro.Context;
import haxe.macro.Compiler;
using StringTools;
class Macro {
#if macro
public static var counter:Int = 0;
public static var cache:StringMap<String> = new StringMap();
#end
public static macro function mm(key:String):Expr {
var value = cache.get( key );
return value == null ? macro null : macro $p { value.split('.') };
}
public static macro function build():Array<Field> {
var cls = Context.getLocalClass() != null ? Context.getLocalClass().get() : null;
var fields = Context.getBuildFields();
counter++;
cache.set( 'awesome', 'a.b.Test$counter' );
if (cls != null && cls.name.startsWith('Test') && cls.name != 'Test3') {
fields = fields.concat( (macro class Temp {
public static var klasInit = Macro.mm( 'awesome' );
} ).fields );
}
Context.onGenerate(function(types:Array<Type>) {
for (type in types) {
switch (type) {
case TInst(ref, params) if (ref != null && ref.get().name.startsWith('Test')):
for (f in ref.get().statics.get()) if (f.name == 'klasInit') {
trace( f.name );
f.meta.add( ':extern', [], f.pos );
}
case _:
}
}
});
return fields;
}
}
package;
class Main {
static function main() {
a.b.Test1.main();
a.b.Test2.main();
a.b.Test3.main();
}
}
(function (console) { "use strict";
var Macro = function() { };
var Main = function() { };
Main.main = function() {
a_b_Test1.main();
a_b_Test2.main();
a_b_Test3.main();
};
var a_b_Test3 = function() { };
a_b_Test3.main = function() {
console.log("test3");
};
var a_b_Test1 = function() { };
a_b_Test1.main = function() {
console.log("test1");
};
var a_b_Test2 = function() { };
a_b_Test2.main = function() {
console.log("test2");
};
Main.main();
})(typeof console != "undefined" ? console : {log:function(){}});
package a.b;
@:build(Macro.build())
class Test1 {
public static function main() {
trace('test1');
}
}
package a.b;
@:build(Macro.build())
class Test2 {
public static function main() {
trace('test2');
}
}
package a.b;
@:build(Macro.build())
class Test3 {
public static function main() {
trace('test3');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment