actually setup the remote_lua protocol on the server

This commit is contained in:
Squibid 2025-11-28 18:16:33 -05:00
parent ffbe496599
commit 7ed5c4840d
Signed by: squibid
GPG key ID: BECE5684D3C4005D
3 changed files with 36 additions and 15 deletions

View file

@ -3,15 +3,16 @@ const RemoteLua = @This();
const std = @import("std");
const wayland = @import("wayland");
const Utils = @import("utils.zig");
const Lua = @import("lua/lua.zig");
const wl = wayland.server.wl;
const mez = wayland.server.zmez;
const gpa = std.heap.c_allocator;
const server = &@import("main.zig").server;
const Lua = &@import("main.zig").lua;
id: usize,
remote_lua_v1: *mez.RemoteLuaV1,
L: Lua,
pub fn sendNewLogEntry(str: [*:0]const u8) void {
for (server.remote_lua_clients.items) |c| {
@ -24,32 +25,49 @@ pub fn create(client: *wl.Client, version: u32, id: u32) !void {
const node = try gpa.create(RemoteLua);
errdefer gpa.destroy(node);
node = .{
node.* = .{
.remote_lua_v1 = remote_lua_v1,
.id = server.remote_lua_clients.items.len,
.L = undefined,
};
server.remote_lua_clients.append(gpa, node);
try node.L.init();
errdefer node.L.deinit();
try server.remote_lua_clients.append(gpa, node);
remote_lua_v1.setHandler(*RemoteLua, handleRequest, handleDestroy, &node);
remote_lua_v1.setHandler(*RemoteLua, handleRequest, handleDestroy, node);
}
fn handleRequest(
remote_lua_v1: *mez.RemoteLuaV1,
request: mez.RemoteLuaV1.Request,
_: *RemoteLua,
remote: *RemoteLua,
) void {
switch (request) {
.destroy => remote_lua_v1.destroy(),
.push_lua => |req| {
Lua.state.loadString(req.lua_chunk) catch {
const errTxt: []const u8 = Lua.state.toString(-1) catch unreachable;
try sendNewLogEntry("repl: " ++ errTxt);
};
const chunk = std.mem.sliceTo(req.lua_chunk, 0);
remote.L.state.loadString(chunk) catch catchLuaFail(remote);
remote.L.state.protectedCall(.{}) catch catchLuaFail(remote);
},
}
}
fn catchLuaFail(remote: *RemoteLua) void {
const err_txt: []const u8 = remote.L.state.toString(-1) catch unreachable;
const txt = std.mem.concat(gpa, u8, &[_][]const u8{ "repl: ", err_txt }) catch Utils.oomPanic();
defer gpa.free(txt);
// must add the sentinel back to pass the data over the wire
const w_sentinel = gpa.allocSentinel(u8, txt.len, 0) catch Utils.oomPanic();
defer gpa.free(w_sentinel);
std.mem.copyForwards(u8, w_sentinel, txt[0..txt.len]);
sendNewLogEntry(w_sentinel);
return;
}
fn handleDestroy(_: *mez.RemoteLuaV1, remote_lua: *RemoteLua) void {
server.remote_lua_clients.swapRemove(remote_lua.id);
_ = server.remote_lua_clients.swapRemove(remote_lua.id);
remote_lua.L.deinit();
gpa.destroy(remote_lua);
}