fix repl evaluations

This commit is contained in:
Squibid 2025-12-10 14:12:14 -05:00
parent 60a8466fe8
commit 64713dc0c3
Signed by: squibid
GPG key ID: BECE5684D3C4005D

View file

@ -49,24 +49,39 @@ remote_lua_v1: *mez.RemoteLuaV1,
request: mez.RemoteLuaV1.Request, request: mez.RemoteLuaV1.Request,
remote: *RemoteLua, remote: *RemoteLua,
) void { ) void {
const L = remote.L;
switch (request) { switch (request) {
.destroy => remote_lua_v1.destroy(), .destroy => remote_lua_v1.destroy(),
.push_lua => |req| { .push_lua => |req| {
const chunk = std.mem.sliceTo(req.lua_chunk, 0); const chunk: [:0]const u8 = std.mem.sliceTo(req.lua_chunk, 0);
// TODO: this could be a lot smarter, we don't want to add return to a
// statement which already has return infront of it. const str = std.mem.concatWithSentinel(gpa, u8, &[_][]const u8{
const str = std.mem.concatWithSentinel(gpa, u8, &[_][]const u8{ "return ", chunk }, 0) catch return catchLuaFail(remote); "return ",
chunk,
";",
}, 0) catch return catchLuaFail(remote);
defer gpa.free(str); defer gpa.free(str);
remote.L.loadString(str) catch catchLuaFail(remote); zlua.Lua.loadBuffer(L, str, "=repl", zlua.Mode.text) catch {
remote.L.protectedCall(.{ L.pop(L.getTop());
.results = zlua.mult_return, L.loadString(chunk) catch {
}) catch catchLuaFail(remote); catchLuaFail(remote);
L.pop(-1);
};
return;
};
L.protectedCall(.{ .results = zlua.mult_return, }) catch {
catchLuaFail(remote);
L.pop(1);
};
var i: i32 = 1; var i: i32 = 1;
while (i < remote.L.getTop() + 1) : (i += 1) { const nresults = L.getTop();
sendNewLogEntry(remote.L.toString(-1) catch return catchLuaFail(remote)); while (i <= nresults) : (i += 1) {
remote.L.pop(-1); // TODO: support lua5.1 and luajit?
sendNewLogEntry(L.toStringEx(i));
L.pop(-1);
} }
}, },
} }
@ -83,10 +98,6 @@ fn handleDestroy(_: *mez.RemoteLuaV1, remote_lua: *RemoteLua) void {
} }
fn catchLuaFail(remote: *RemoteLua) void { fn catchLuaFail(remote: *RemoteLua) void {
const err_txt: []const u8 = remote.L.toString(-1) catch "zig error"; const err: [:0]const u8 = remote.L.toStringEx(-1);
const txt = std.mem.concatWithSentinel(gpa, u8, &[_][]const u8{ "repl: ", err_txt }, 0) catch Utils.oomPanic(); sendNewLogEntry(std.mem.sliceTo(err, 0));
defer gpa.free(txt);
sendNewLogEntry(txt);
return;
} }