Skip to content

Instantly share code, notes, and snippets.

View jasononeil's full-sized avatar

Jason O'Neil jasononeil

View GitHub Profile
@jasononeil
jasononeil / MethodTyper.hx
Created February 19, 2014 05:11
Method used in a macro to figure out the return type of a member method. Uses tink_macro
/**
Figure out return type of function.
Will first to check if it is explicitly defined.
If not, it attempts to set the function up as a local function expression (EFunction), and then use `Context.typeof( fnExpr )` to evaluate the type.
Will generate a compiler error if member was not a method or method type could not be determined.
**/
static function figureOutReturnTypeOfMemberMethod( member:Member ) {
switch member.getFunction() {
@jasononeil
jasononeil / Http.hx
Created March 5, 2014 22:37
Workaround for Hxssl being broken for Http requests. Drop these 4 files into a package called "haxe", and you can use this as a drop-in workaround for HxSSL breaking regular HTTP port 80 requests
package haxe;
@:access( haxe.Https )
@:access( haxe.Http80 )
class Http {
public var h:IHttp;
public var url(get, set) : String;
function get_url() return h.url;
@jasononeil
jasononeil / ObjectInit.hx
Created December 2, 2014 01:19
Object initialisations syntax sugar
import haxe.macro.Expr;
import haxe.macro.Context;
using haxe.macro.ExprTools;
class ObjectInit {
macro public static function init( expr:Expr, varsToSet:ExprOf<Dynamic<Dynamic>> ) {
var lines:Array<Expr> = [];
lines.push( macro var __obj_init_tmp = $expr );
switch varsToSet.expr {
case EObjectDecl(fields):
@jasononeil
jasononeil / NekoScript.hx
Created May 23, 2012 13:13
NekoScript proof of concept - simple scripting language using hscript
/**
NekoScript
By Jason O'Neil, 2011. Licensed under the GPL.
*/
import hscript.Parser;
import neko.Sys;
import neko.FileSystem;
import neko.io.File;
import haxe.Http;
@jasononeil
jasononeil / Main.hx
Created June 18, 2012 13:47
Xirsys_std and erazor do not co-operate
import erazor.Template;
class Main
{
public static function main()
{
var template = new Template("hello @file");
trace(template.execute({ "file": "world" }));
}
}
@jasononeil
jasononeil / JS Callback to Future
Last active November 10, 2015 23:14 — forked from kevinresol/ JS Callback to Future
Convert js-style callback to Future, with macro
Convert js-style callback to Future, with macro
@jasononeil
jasononeil / Builder.hx
Last active December 11, 2015 08:08 — forked from Simn/Builder.hx
import haxe.macro.Context;
import haxe.macro.Expr;
class Builder {
// Build macros are called using @:build() or @:autoBuild() metadata
// They must return an array containing all of the fields of the new type
macro static function build():Array<Field> {
// Create expression representing the test() function
var funcExpr = macro function():String {
return "test";
@jasononeil
jasononeil / InitialisationChecker.hx
Last active December 14, 2015 06:58
Very simple proof of concept for a macro which checks that all member variables and static variables have been initialised. Currently it checks for every single type... so it finds a bunch of errors in the standard library too. Someone may want to fork it and make it a little bit more clever... UPDATE: modified it to only check Int, Float, Strin…
// Only tested on Neko, Haxe3RC
// Compile and run with:
// haxe -x InitialisationChecker.hx
// Created by Jason O'Neil, 2013. Release public domain.
#if macro
import haxe.macro.Context;
using Lambda;
#end
@jasononeil
jasononeil / DOMReady.hx
Last active December 15, 2015 03:19
A lot of the time in JS we have to wait for the page to finish loading, or at least, for the DOM to be loaded, before we can begin our various operations. Detox makes this easy with the Detox.ready() function, which fires the given function as soon as the DOM is loaded.
using Detox;
public class DOMReady
{
static function main()
{
Detox.ready(function (e) {
Detox.document.title = "Page is ready";
});
}
@jasononeil
jasononeil / MacroDefine.hx
Created March 25, 2013 02:25
haxe.macro.Compiler.define() example
/**
* Macro Compiler Define example
*
* Shows how to use a compiler configuration macro to set up more "defines" based
* on an existing define.
*
* Jason O'Neil 2013. Use as you please.
*
* haxe -D mydebug --macro "MacroDefine.setDefines()" -x MacroDefine.hx
* -- traces "A" and "B"