Created
March 16, 2015 14:57
-
-
Save skial/a5dde7c3f7cd2c997168 to your computer and use it in GitHub Desktop.
Abusing macros causes flat js packages mixed with normal dotted packages.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package; | |
| class Main { | |
| public static function main() { | |
| a.b.Test.main(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package a.b; | |
| @:build(Macro.build()) | |
| class Test { | |
| public static function main() { | |
| Macro.MM(); | |
| } | |
| public static function method(v:String) trace( v ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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