haxetink / tink_await

Haxe async/await
MIT License
58 stars 15 forks source link

Use with macro that generates a block #10

Closed kevinresol closed 8 years ago

kevinresol commented 8 years ago
@:await
class Main
{

    @:await static function main()
    {
        var b = @:await bool();
        trace(b);
        Macro.tryCatch({
          var i = int();
          i *= i;
          trace(i);
        });
    }

    @:async static function bool() return true;
    static function int() return 1;
}

class Macro {
    public static macro function tryCatch(e:haxe.macro.Expr) {
        return macro try $e catch(e:Dynamic) trace(e);
    }
}

The above code outputs something like this:

console.log(__t0_result);
var i = Main["int"]();
i *= i;
try {
    console.log(i);
} catch( e ) {
    if (e instanceof js__$Boot_HaxeError) e = e.val;
    console.log(e);
}

Note that var i = Main.int(); i *= i; got moved out of the try block, I think that shouldn't happen.

benmerckx commented 8 years ago

Thanks for the example, I'll have a look at it. It could be fairly complex, or simply the way I handle blocks, can't tell at this time.

benmerckx commented 8 years ago

This should work with the last update. No processing is done on blocks that do not contain @:await or @:async. This will fix this issue, but will fail if you're using @:await inside of Macro.tryCatch. There's not much I can do to fix that. I've tried running those expression macros before transforming, but I can't find a reliable way to do so and the haxe docs are clear that the build order is unspecified and shouldn't be relied on.

kevinresol commented 8 years ago

This seem to work for now. Thanks!