Skip to content

Instantly share code, notes, and snippets.

@skial
Created March 16, 2015 14:57
Show Gist options
  • Select an option

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

Select an option

Save skial/a5dde7c3f7cd2c997168 to your computer and use it in GitHub Desktop.
Abusing macros causes flat js packages mixed with normal dotted packages.
package;
#if macro
import haxe.macro.Compiler;
import haxe.macro.Context;
import haxe.macro.Expr;
#end
class Macro {
public static macro function build():Array<Field> {
var fields = Context.getBuildFields();
var td = macro class Test {
@:keep public static function nothing() return null;
}
td.name += '1';
td.meta.push( { name:':native', params:[macro 'a.b.Test'], pos:td.pos } );
td.fields = td.fields.concat( fields );
Compiler.exclude('a.b.Test');
Context.defineType( td );
return fields;
}
public static macro function MM():Expr {
return macro $p { ['a', 'b', 'Test', 'method'] } ('hello world');
}
}
package;
class Main {
public static function main() {
a.b.Test.main();
}
}
package a.b;
@:build(Macro.build())
class Test {
public static function main() {
Macro.MM();
}
public static function method(v:String) trace( v );
}
(function (console) { "use strict";
var Macro = function() { };
var Main = function() { };
Main.main = function() {
a.b.Test.main();
};
var a_b_Test = function() { };
a_b_Test.nothing = function() {
return null;
};
a_b_Test.main = function() {
a.b.Test.method("hello world");
};
a_b_Test.method = function(v) {
console.log(v);
};
Main.main();
})(typeof console != "undefined" ? console : {log:function(){}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment