Skip to content

Instantly share code, notes, and snippets.

@ibilon
ibilon / Main.hx
Last active January 28, 2021 11:31
Haxe compile time json
class Main {
macro static function compileJson(path:String) {
return haxe.macro.Context.parseInlineString(sys.io.File.getContent(path), haxe.macro.Context.currentPos());
}
static function main() {
final a = compileJson("test.json");
trace(a);
a.obj.sub += 1;
trace(a);
@ibilon
ibilon / Test.hx
Last active August 25, 2018 14:29
using Test;
class Test {
var A (default, never): Int = 1;
public function new() {
}
public function set(value:Int) {
trace(value);
@ibilon
ibilon / Table.hx
Created June 7, 2017 06:46
Wrapper around lua.Table with from function for array and map
package luautils;
import lua.Lua;
import lua.Table in LuaTable;
abstract Table<K, V> (LuaTable<K, V>) from LuaTable<K, V>
{
function new (table:LuaTable<K, V>) this = table;
@:from static function fromMap<K, V> (map:Map<K, V>) : Table<K, V>
import haxe.macro.Context;
#if !macro
@:genericBuild(GenericClass.build())
#end
class GenericClass<@:const Content> {
public static function build () {
return switch (Context.getLocalType())
{
case TInst(_, [TInst(_.get() => { kind: KExpr(macro $v{(s:String)}) }, _)]):
@ibilon
ibilon / Host.hx
Last active January 2, 2018 12:06
Calling hxcpp app from c++ app
// haxe -main Host -cpp host
// ./host/Host ./lib/Main.dso
class Host {
public static function main () {
var lib = Dl.open(Sys.args()[0], Dl.RTLD_NOW);
if (lib == null) {
trace("can't find library");
Sys.exit(1);
}
@ibilon
ibilon / dep_solve.cpp
Created March 11, 2016 22:30
Dependency resolution
#include <fstream>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <vector>
#include "constraint_solver/constraint_solver.h"
// Unzip https://github.com/google/or-tools/releases/tag/v2015-12 next to this file
@ibilon
ibilon / GenNew.hx
Last active February 23, 2016 12:13
Object construction with macro
import haxe.macro.Expr;
import haxe.macro.Context;
class GenNew
{
macro public static function build () : Array<Field>
{
var fields = Context.getBuildFields();
var args = [];
var assigns = [];
@ibilon
ibilon / Test.hx
Created November 17, 2015 14:47
Compile time macro define information
class Test {
static function main() {
trace(isDemo());
}
macro static function isDemo () : haxe.macro.Expr
{
return macro $v{haxe.macro.Compiler.getDefine("demo") != null};
}
}
@ibilon
ibilon / Test.hx
Last active September 22, 2015 17:04
Array Multi Type
class Test {
static function main() {
var ar = new MyArray<Int>([1, 2, 5]);
trace(ar);
ar.length = 5;
trace(ar);
ar.length = 2;
trace(ar);
var ar2 = new MyArray<Float>([1.1, 2.2, 5.5]);
@ibilon
ibilon / Main.hx
Last active August 29, 2015 14:26
HaxePunk maintain ratio
import com.haxepunk.Engine;
import com.haxepunk.HXP;
import com.haxepunk.utils.Draw;
class Main extends Engine
{
override public function init()
{
#if debug