Skip to content

Commit e4913d3

Browse files
committed
fix(interpreter): update to latest Zig
1 parent 9424612 commit e4913d3

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

examples/interpreter.zig

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ const zlua = @import("zlua");
88

99
const ReadError = error{BufferTooSmall};
1010

11-
fn readlineStdin(out_buf: []u8) anyerror!usize {
11+
fn readlineStdin(io: std.Io, out_buf: []u8) anyerror!usize {
1212
var in_buf: [4096]u8 = undefined;
13-
var stdin_file = std.fs.File.stdin().reader(&in_buf);
13+
var stdin_file = std.Io.File.stdin().reader(io, &in_buf);
1414
const stdin = &stdin_file.interface;
1515
const s = try stdin.takeDelimiterExclusive('\n');
1616
if (s.len < out_buf.len) {
@@ -20,9 +20,9 @@ fn readlineStdin(out_buf: []u8) anyerror!usize {
2020
return error.BufferTooSmall;
2121
}
2222

23-
fn flushedStdoutPrint(comptime fmt: []const u8, args: anytype) !void {
23+
fn flushedStdoutPrint(io: std.Io, comptime fmt: []const u8, args: anytype) !void {
2424
var out_buf: [4096]u8 = undefined;
25-
var w = std.fs.File.stdout().writer(&out_buf);
25+
var w = std.Io.File.stdout().writer(io, &out_buf);
2626
const stdout = &w.interface;
2727
try stdout.print(fmt, args);
2828
try stdout.flush();
@@ -33,6 +33,10 @@ pub fn main() anyerror!void {
3333
const allocator = gpa.allocator();
3434
defer _ = gpa.deinit();
3535

36+
var threaded: std.Io.Threaded = .init_single_threaded;
37+
defer threaded.deinit();
38+
const io = threaded.io();
39+
3640
// Initialize The Lua vm and get a reference to the main thread
3741
//
3842
// Passing a Zig allocator to the Lua state requires a stable pointer
@@ -43,14 +47,14 @@ pub fn main() anyerror!void {
4347
lua.openLibs();
4448

4549
while (true) {
46-
try flushedStdoutPrint("> ", .{});
50+
try flushedStdoutPrint(io, "> ", .{});
4751

4852
// Read a line of input
4953
var buffer: [256]u8 = undefined;
50-
const len = readlineStdin(buffer[0 .. buffer.len - 1]) catch |err| {
54+
const len = readlineStdin(io, buffer[0 .. buffer.len - 1]) catch |err| {
5155
switch (err) {
5256
error.BufferTooSmall => {
53-
try flushedStdoutPrint("error: line too long!\n", .{});
57+
try flushedStdoutPrint(io, "error: line too long!\n", .{});
5458
continue;
5559
},
5660
error.EndOfStream => break,
@@ -65,7 +69,7 @@ pub fn main() anyerror!void {
6569
lua.loadString(buffer[0..len :0]) catch {
6670
// If there was an error, Lua will place an error string on the top of the stack.
6771
// Here we print out the string to inform the user of the issue.
68-
try flushedStdoutPrint("{s}\n", .{lua.toString(-1) catch unreachable});
72+
try flushedStdoutPrint(io, "{s}\n", .{lua.toString(-1) catch unreachable});
6973

7074
// Remove the error from the stack and go back to the prompt
7175
lua.pop(1);
@@ -75,7 +79,7 @@ pub fn main() anyerror!void {
7579
// Execute a line of Lua code
7680
lua.protectedCall(.{}) catch {
7781
// Error handling here is the same as above.
78-
try flushedStdoutPrint("{s}\n", .{lua.toString(-1) catch unreachable});
82+
try flushedStdoutPrint(io, "{s}\n", .{lua.toString(-1) catch unreachable});
7983
lua.pop(1);
8084
};
8185
}

0 commit comments

Comments
 (0)