I’m designing a VM that allows you to define your own print and printErr functions that we’ll be used to print to stdout and stderr, while provided a default implementation. It is meant to be used when embedded or in test mode for example.
The Config and default implementation are:
pub const Config = struct {
printFn: *const fn ([]const u8) void = defaultPrint,
...
};
fn defaultPrint(text: []const u8) void {
errdefer @panic("failed to write to stdout");
var buf: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&buf);
const stdout = &stdout_writer.interface;
try stdout.print("{s}\n", .{text});
try stdout.flush();
}
When executing the print op code, I pop the last value on stack, print it to a buffer and then send it to the print function:
.print => {
var wa = std.io.Writer.Allocating.init(self.allocator);
var writer = &wa.writer;
self.stack.pop().print(writer);
self.state.config.printFn(writer.buffered());
},
This all process seems really not performant as a lot of stuff is done over and over but I’m not 100% used to new Io stuff.
What’s an elegant way to deal with that?