Skip to content

Instantly share code, notes, and snippets.

View jasononeil's full-sized avatar

Jason O'Neil jasononeil

View GitHub Profile
@jasononeil
jasononeil / MacroHelpTest.hx
Last active December 18, 2015 03:38
Using a macro as the initialization of one field, and having it base itself on an existing member variable.
import haxe.macro.Expr;
import haxe.macro.Context;
class F
{
macro public static function f(a : Expr, b : Int) : Expr
{
// get the field name as a string, use Pattern Matching or tink macros probably...
var fieldName = switch (a.expr) {
case EConst(CIdent(name)): name;
@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 / HaxeScript.hx
Created April 22, 2013 06:18
haxex, a short shell script that can be used so you have Haxe shell scripting
#!/usr/bin/haxex
// Run with "./HaxeScript.hx some args here"
class HaxeScript
{
public static function main()
{
trace('Hi, the following ${Sys.args().length} arguments were passed in:');
for (a in Sys.args())
@jasononeil
jasononeil / ClassicSwitch.hx
Last active March 22, 2018 21:17
ClassicSwitch.hx - a macro which takes a switch statement and turns it into an if/elseif/else chain. This is useful if you want traditional ecmascript "switch" behaviour, not the kick-ass haxe pattern matching. See the README, the implementation, and some tests below.
import haxe.macro.Expr;
import haxe.macro.Context;
class ClassicSwitch
{
macro static public function from(inExpr:Expr):Expr
{
var retExpr:Expr = null;
switch (inExpr.expr)
@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";