Skip to content

Instantly share code, notes, and snippets.

View jasononeil's full-sized avatar

Jason O'Neil jasononeil

View GitHub Profile
@jasononeil
jasononeil / Console.hx
Created March 6, 2012 03:00
A really simple proof of concept for a haxe interactive shell
/**
A super basic interactive console for haxe.
Done because someone on the mailing list asked and I got distracted :)
Consider this code Public Domain.
Jason O'Neil
Run using:
haxe -lib hscript -x Console.hx
(you will need hscript installed via haxelib)
@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 / 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"
@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 / 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 / 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)