Skip to content

Instantly share code, notes, and snippets.

View jasononeil's full-sized avatar

Jason O'Neil jasononeil

View GitHub Profile
@jasononeil
jasononeil / DispatchTest.hx
Last active December 17, 2015 20:19
A sample to test some more complex routing options of haxe.web.Dispatch. Run with `haxe -x DispatchTest.hx` This is the sample code for this tutorial / blog post: http://jasononeil.com.au/2013/05/29/creating-complex-url-routing-schemes-with-haxe-web-dispatch/
import haxe.ds.StringMap;
import haxe.web.Dispatch;
import haxe.web.Dispatch.DispatchError;
#if sys
import Sys.println;
import Sys.print;
#else
import haxe.Log.trace in println;
import haxe.Log.trace in print;
@jasononeil
jasononeil / CustomSerialisation.hx
Created April 12, 2013 12:54
A test of custom serialisation in Haxe when using sys.db.Object objects. Results in file below * Using a custom serialiser for SPOD can reduce speeds by more than half. * String size is also reduced, which is good if you're sending over HTTP * Have a build macro append a static array containing the names of the fields to serialise.
import sys.db.Types;
using Lambda;
/**
* What this class shows:
*
* Using a custom serialiser for SPOD can reduce speeds by more than half.
* Have a build macro append a static array containing the names of the fields to serialise.
*/
@jasononeil
jasononeil / WebSocketTest.hx
Created April 5, 2013 10:25
A haxe port of the WebSockets demo here: http://www.websocket.org/echo.html Works with Haxe3.
// Compile with: haxe -main WebSocketTest.hx -js WebSocketTest.js
// Doing your imports like this makes it much easier to port with existing Javascript code.
import js.Browser.window;
import js.Browser.document;
import js.html.*;
class WebSocketTest
{
static var wsUri = "ws://echo.websocket.org/";
@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"
@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 / 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 / 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 / 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 / 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 / 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;