Skip to content

Instantly share code, notes, and snippets.

@skial
Created January 17, 2011 09:45
Show Gist options
  • Save skial/782661 to your computer and use it in GitHub Desktop.
Save skial/782661 to your computer and use it in GitHub Desktop.
haxe macro test
package ;
import js.Lib;
/**
* ...
* @author Skial Bainn
*/
class Main {
var bobby:Bobby;
static function main() {
var m = new Main();
m.notNative();
}
public function new() {
trace('hello 1');
bobby = new Bobby();
}
@native({fun:'bobby.shakeThat'})
public function notNative():Void {
trace('hello 2');
}
}
class Bobby {
public function new() {
}
public function shakeThat():Void {
trace('...');
}
}
-js bin/MacroTest.js
-cp src/
-main Main
-debug
--macro Replace.nativeMethod()
/**
* ...
* @author Skial Bainn
*/
package ;
import haxe.macro.Expr;
import haxe.macro.Context;
import haxe.macro.Compiler;
// code originally from haxe mailing list - http://haxe.1354130.n2.nabble.com/Some-Ideas-for-Macros-and-Context-onGenerate-new-on-svn-td5847221.html#a5848099
@:macro
class Replace {
public static function nativeMethod():Expr {
var methods:Hash<String> = new Hash<String>();
Context.onGenerate(function(types) {
var fout = neko.io.File.write('macrolog.txt', false);
var meta = null;
for (t in types) {
switch (t) {
case TInst(cT, _):
fout.writeString('\n' + cT.get().name);
/*fout.writeString('\n' + cT.get().fields.get());
fout.writeString('\n' + cT.get().fields.get()[1].meta.get());*/
for (f in cT.get().fields.get()) {
for (m in f.meta.get()) {
meta = m;
if (meta.name == 'native') {
fout.writeString('\n' + meta);
}
}
}
default:
}
}
fout.close();
});
return {expr:EConst(CString('Void')), pos:haxe.macro.Context.currentPos()}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment