Skip to content

Instantly share code, notes, and snippets.

@back2dos
back2dos / Main.hx
Last active December 24, 2015 21:19
Promising benchmarks. Surprising results.
package ;
import tink.core.Future;
using tink.core.Outcome;
import haxe.Timer.*;
class Main {
macro static function benchmark(name:String, makeTrigger, getFuture, watch, trigger)
return macro {
name: $v{name},
#if macro
import haxe.macro.Expr;
#else
class Image {
static public function img_tag(rr:RR, image: {path:String, w:Int, h:Int}, target_size):String {
return '<img alt="whoops">';
}
}
#end
typedef RR = Dynamic;
@back2dos
back2dos / Foo.hx
Last active December 31, 2015 22:59
Stringly fail.
package ;
#if macro
import haxe.macro.Expr;
import haxe.macro.Context;
using haxe.macro.ExprTools;
#end
class Foo {
public function new() {
@back2dos
back2dos / DevTools.hx
Last active January 2, 2016 09:39
Measure execution time with macros.
package ;
class DevTools {
macro static public function measure(e, ?msg:String) {
if (msg == null)
msg = haxe.macro.ExprTools.toString(e);
return macro @:pos(e.pos) {
var start = haxe.Timer.stamp(),
result = [$e];//wrapping in the array skews the result a bit, but avoids issues with Void
var duration = haxe.Timer.stamp() - start;
@back2dos
back2dos / HaxeCompletion.hx
Last active August 29, 2015 13:57
Completion interface
typedef CompletionPoint = {
file:String,
offset:Int
}
interface HaxeCompletion {
function field(at:CompletionPoint):Array<haxe.macro.ClassField>;
function usage(at:CompletionPoint):Array<haxe.macro.Position>;
function toplevel(at:CompletionPoint):Array<TopLevel>;
}
@back2dos
back2dos / Int.hx
Created May 19, 2014 16:58
Strict ints for Oleg.
package strict;
import StdTypes.Int in HxInt;
abstract Int(HxInt) from HxInt to HxInt {
@:op(this + b) inline function add(b:Int):Int return this + (b : HxInt);
@:op(this - b) inline function subtract(b:Int):Int return this - (b : HxInt);
@:op(this * b) inline function multiply(b:Int):Int return this * (b : HxInt);
@:op(this >>> b) inline function urshift(b:Int):Int return this >>> (b : HxInt);
@:op(this >> b) inline function rshift(b:Int):Int return this >> (b : HxInt);
@back2dos
back2dos / Helper.hx
Created May 25, 2014 13:15
Custom indexOf
package ;
class Helper {
static public function indexOf<A>(i:Iterable<A>, filter:A->Bool):Int {
var i = 0;
for (x in a) {
i++;
if (filter(x)) return i;
}
@back2dos
back2dos / Gen.hx
Last active August 29, 2015 14:02
Generate es5 style properties. Use with --macro Gen.use().
package ;
import haxe.macro.*;
import haxe.macro.Type;
using haxe.macro.Tools;
private typedef Accessor = {
name: String,
read: Bool,
@back2dos
back2dos / EventDispatcher.hx
Last active July 9, 2020 03:20
Typesafe version of `flash.events.EventDispatcher` to make the Flash API suck a little less. Would also work for `js.html.EventTarget`.
package flash.events;
extern class EventDispatcher implements IEventDispatcher {
function new(?target:IEventDispatcher) : Void;
function addEventListener<T:Event>(type:EventType<T>, listener: T->Void, useCapture:Bool = false, priority:Int = 0, useWeakReference:Bool = false):Void;
function dispatchEvent(event:Event):Bool;
function hasEventListener(type:String):Bool;
function removeEventListener<T:Event>(type:EventType<T>, listener: T->Void, useCapture:Bool = false):Void;
function toString():String;
function willTrigger(type:String):Bool;
abstract Flags(Int) {
public inline function new(i = 0) {
this = i;
}
public inline function has( index : Int ) : Bool {
return this & (1 << index) != 0;
}