Created
June 26, 2014 14:36
-
-
Save dpeek/15fec53d7befe1984f04 to your computer and use it in GitHub Desktop.
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
-x Test | |
--macro Test.Macro.keep() | |
--next | |
-x Test | |
-dce full | |
--macro Test.Macro.keep() |
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
import haxe.macro.Type; | |
using haxe.macro.Tools; | |
using Lambda; | |
class Test | |
{ | |
static function main() new Test(); | |
function new() | |
{ | |
check("build"); | |
check("dead"); | |
check("keep"); | |
} | |
function check(field:String) trace('$field: ' + (Reflect.field(this, field) != null)); | |
function build() null; | |
function dead() null; | |
@:keep function keep() null; | |
} | |
class Macro | |
{ | |
static function keep() haxe.macro.Context.onGenerate(process); | |
static function process(types:Array<Type>) types.iter(processType); | |
static function processType(type:Type) switch (type) | |
{ | |
case TInst(t, _): | |
if (t.get().name == 'Test') processFields(t.get().fields.get()); | |
case _: | |
} | |
static function processFields(fields:Array<ClassField>) | |
{ | |
for (field in fields) | |
if (field.name == 'build') | |
field.meta.add('@:keep', [], haxe.macro.Context.currentPos()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment