-
-
Save danielo515/38c30b46b70d94d2d22060dc8429c9bb to your computer and use it in GitHub Desktop.
Lua test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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