TypedExpr reification

86 views
Skip to first unread message

Christian Afonso Ferreira

unread,
Feb 4, 2016, 2:47:53 PM2/4/16
to Haxe
How I create a instance of TypedExpr like Expr reification?

I doing something like a tracer.
In order to inject code in all the static functions of a class (that I can not change the source, in other words, can't use @:build directly), I use a macros to get the ClassType using the class path.

static macro public function inject() {
   
var glFields = TypeTools.getClass(TypeTools.follow(Context.getType('hyper.super.Awesome'))).statics.get();

   
for (field in glFields) {
       
if (field.isPublic)
           
switch (field.kind) {
               
case FMethod(k):
                   
switch (field.expr().expr) {
                       
case TFunction(_.expr.expr => TBlock(block)):
                            block
.unshift(TypedExpr ???);
                       
default:
                   
}
               
default:
           
}
   
}
}

But how I create instances of TypedExpr using Haxe syntax like Expr reification?

Christian Afonso Ferreira

unread,
Feb 4, 2016, 3:02:30 PM2/4/16
to Haxe
Reification of expression like "++calls; trace('stuff");".

Ben Merckx

unread,
Feb 4, 2016, 6:10:25 PM2/4/16
to Haxe
Check typeExpr method on Context: http://api.haxe.org/haxe/macro/Context.html

Christian Afonso Ferreira

unread,
Feb 4, 2016, 6:48:20 PM2/4/16
to Haxe
With that I can create the typed expression but the class don't change at all

static public function print() {
        trace
('yeah');

}

static macro public function inject() {

   
var glFields = TypeTools.getClass(TypeTools.follow(Context.getType('openfl.gl.GL'))).statics.get();


   
for (field in glFields) {
       
if (field.isPublic)
           
switch (field.kind) {
               
case FMethod(k):
                   
switch (field.expr().expr) {
                       
                       
case TFunction(_.expr.expr => TBlock(block)):

                           
var e = macro Macros.print();
                            block
.unshift(Context.typeExpr(e));
                       
default:
                           
                   
}
                   
break;
                    trace
(field.name);
               
default:
           
}
   
}
}

Even if I insert the typed expression on block with "block.unshift(Context.typeExpr(e))" the actual class don't change and Macros.print() is never called.
Class fields can't be changed?


Ben Merckx

unread,
Feb 5, 2016, 2:06:28 AM2/5/16
to Haxe
Sure you can, but you need a build macro. I think you best start here: http://haxe.org/manual/macro-type-building.html

Juraj Kirchheim

unread,
Feb 5, 2016, 4:10:29 AM2/5/16
to haxe...@googlegroups.com


See this ticket for an explanation as to why that is unlikely to change in the foreseeable future: https://github.com/HaxeFoundation/haxe/issues/4817

Best,
Juraj

On Fri, Feb 5, 2016 at 8:06 AM, Ben Merckx <b...@codeurs.be> wrote:
Sure you can, but you need a build macro. I think you best start here: http://haxe.org/manual/macro-type-building.html

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Christian Afonso Ferreira

unread,
Feb 5, 2016, 8:04:05 AM2/5/16
to Haxe
Well, the more issues I have with this the more clear it become to me. The diagram here http://haxe.org/manual/macro.html now makes more sense.
However, I got a way to solve it.
All I have to do is overwrite the original module defining other with the same path in my package. The same way Lime does here https://github.com/openfl/lime/tree/develop/haxe
This way I can add the metadata for type building.

Thank you for the help.

Juraj Kirchheim

unread,
Feb 5, 2016, 9:00:07 AM2/5/16
to haxe...@googlegroups.com
You can use Context.addGlobalMetaData with appropriate filters to add @:build directives to types you don't "own" if that's what you're after.

Best,
Juraj

--

Christian Afonso Ferreira

unread,
Feb 5, 2016, 1:42:00 PM2/5/16
to Haxe
I trying it like --macro addGlobalMetadata("pineapple.pupl.openflavor.Juice", ":build(Macros.build())") but all I get is a

Unexpected :
Build halted with errors.

Christian Afonso Ferreira

unread,
Feb 5, 2016, 2:46:23 PM2/5/16
to Haxe
I got everything working now.
Thank you very much.
Reply all
Reply to author
Forward
0 new messages