authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-01-20 03:22:13 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-01-20 03:22:13 -08:00
logc771db2448322a7d51ebe41cc4d48454f62c475c
treeb2a86fbd7e3f056f2cd86d68a7c578cc75f09b09
parentb1ea50c5214e9c69708e83d519b3389338e8d9cc

pass function impls a Writer to enable zero-alloc handlers


1 files changed, 9 insertions(+), 3 deletions(-)

src/lib.zig+9-3
...@@ -152,17 +152,23 @@ fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Value, d...@@ -152,17 +152,23 @@ fn do(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Value, d
152 }152 }
153 },153 },
154 .function => |v| {154 .function => |v| {
155 var arena = std.heap.ArenaAllocator.init(alloc);
156 defer arena.deinit();
157
155 if (@hasDecl(root, "pek_" ++ v.name)) {158 if (@hasDecl(root, "pek_" ++ v.name)) {
156 const func = @field(root, "pek_" ++ v.name);159 const func = @field(root, "pek_" ++ v.name);
160 var list = std.ArrayList(u8).init(arena.allocator());
161 errdefer list.deinit();
157 var args: FnArgsTuple(func) = undefined;162 var args: FnArgsTuple(func) = undefined;
158 args.@"0" = alloc;163 args.@"0" = alloc;
164 args.@"1" = list.writer();
159 inline for (v.args) |arg, i| {165 inline for (v.args) |arg, i| {
160 const field_name = comptime std.fmt.comptimePrint("{d}", .{i + 1});166 const field_name = comptime std.fmt.comptimePrint("{d}", .{i + 2});
161 @field(args, field_name) = if (comptime std.mem.eql(u8, arg[0], "this")) search(arg[1..], data) else search(arg, ctx);167 @field(args, field_name) = if (comptime std.mem.eql(u8, arg[0], "this")) search(arg[1..], data) else search(arg, ctx);
162 }168 }
163 const repvalue = astgen.Value{ .replacement = &.{"this"} };169 const repvalue = astgen.Value{ .replacement = &.{"this"} };
164 const newdata = try @call(.{}, func, args);170 try @call(.{}, func, args);
165 try do(alloc, writer, repvalue, newdata, ctx, indent, flag1);171 try do(alloc, writer, repvalue, list.toOwnedSlice(), ctx, indent, flag1);
166 return;172 return;
167 }173 }
168 @compileError("pek: unknown custom function: " ++ v.name);174 @compileError("pek: unknown custom function: " ++ v.name);