Skip to content

Instantly share code, notes, and snippets.

@danielo515
Forked from kLabz/Job.hx
Created December 11, 2022 10:53
Show Gist options
  • Save danielo515/38c30b46b70d94d2d22060dc8429c9bb to your computer and use it in GitHub Desktop.
Save danielo515/38c30b46b70d94d2d22060dc8429c9bb to your computer and use it in GitHub Desktop.
Lua test
#if macro
import haxe.macro.Expr;
#else
import lua.Table;
#end
typedef Job_opts = {
final command:String;
final cwd:Null<String>;
final arguments:Array<String>;
}
extern class Job {
#if !macro
static inline function create(args:Table<String, Dynamic>):Job {
return untyped __lua__("{0}:new({1})", Job, args);
}
#end
}
// Note that ExprOf<T> isn't really checking that types do match
macro function makeJob(args:ExprOf<Job_opts>):ExprOf<Job> {
var fieldExprs = switch (args.expr) {
case EObjectDecl(fields):
[for (f in fields) f.field => f.expr];
case _:
throw "Must be called with an anonymous object";
};
switch (haxe.macro.Context.typeof(args)) {
case TAnonymous(_.get().fields => fields):
var objFields:Array<ObjectField> = [
for (f in fields) {
switch (f.type) {
case TInst(_.toString() => "Array", [TInst(_.toString() => "String", [])]):
{field: f.name, expr: macro Table.create(${fieldExprs.get(f.name)})};
case _:
{field: f.name, expr: macro ${fieldExprs.get(f.name)}};
}
}
];
var obj = {expr: EObjectDecl(objFields), pos: args.pos};
return macro Job.create(Table.create(null, $obj));
case _:
throw "Must be called with an anonymous object";
}
}
#if !macro
// Used like this:
function main() {
var job = makeJob({
command: "chezmoi",
cwd: "/Users/danielo/",
arguments: ['-v']
});
// Vim.print(job);
}
#end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment