an experimental irc client

core: dupe strings from lua for config table

rockorager.dev b6a77117 5a586e95

verified
+35 -16
+27 -7
src/irc.zig
··· 1699 1699 name: ?[]const u8 = null, 1700 1700 tls: bool = true, 1701 1701 lua_table: i32, 1702 + 1703 + /// Creates a copy of this config. Nullable strings are not copied 1704 + pub fn copy(self: Config, gpa: std.mem.Allocator) Allocator.Error!Config { 1705 + return .{ 1706 + .user = try gpa.dupe(u8, self.user), 1707 + .nick = try gpa.dupe(u8, self.nick), 1708 + .password = try gpa.dupe(u8, self.password), 1709 + .real_name = try gpa.dupe(u8, self.real_name), 1710 + .server = try gpa.dupe(u8, self.server), 1711 + .port = self.port, 1712 + .lua_table = self.lua_table, 1713 + }; 1714 + } 1715 + 1716 + pub fn deinit(self: Config, gpa: std.mem.Allocator) void { 1717 + gpa.free(self.user); 1718 + gpa.free(self.nick); 1719 + gpa.free(self.password); 1720 + gpa.free(self.real_name); 1721 + gpa.free(self.server); 1722 + if (self.network_id) |v| gpa.free(v); 1723 + if (self.network_nick) |v| gpa.free(v); 1724 + if (self.name) |v| gpa.free(v); 1725 + } 1702 1726 }; 1703 1727 1704 1728 pub const Capabilities = struct { ··· 1849 1873 thread.join(); 1850 1874 self.thread = null; 1851 1875 } 1852 - // id gets allocated in the main thread. We need to deallocate it here if 1853 - // we have one 1854 - if (self.config.network_id) |id| self.alloc.free(id); 1855 - if (self.config.name) |name| self.alloc.free(name); 1856 1876 1857 - if (self.config.network_nick) |nick| self.alloc.free(nick); 1877 + self.config.deinit(self.alloc); 1858 1878 1859 1879 for (self.channels.items) |channel| { 1860 1880 channel.deinit(self.alloc); ··· 2400 2420 } 2401 2421 } 2402 2422 2403 - var cfg = client.config; 2404 - cfg.network_id = try self.alloc.dupe(u8, id); 2423 + var cfg = try client.config.copy(self.alloc); 2424 + cfg.network_id = try self.app.alloc.dupe(u8, id); 2405 2425 2406 2426 var attr_iter = std.mem.splitScalar(u8, attr, ';'); 2407 2427 while (attr_iter.next()) |kv| {
+8 -9
src/lua.zig
··· 387 387 else => lua.raiseErrorStr("expected a boolean for field 'tls'", .{}), 388 388 }; 389 389 390 - // Ref the config table so it doesn't get garbage collected 391 - _ = lua.ref(registry_index) catch lua.raiseErrorStr("couldn't ref config table", .{}); // [] 392 - 393 390 Client.initTable(lua); // [table] 394 391 const table_ref = lua.ref(registry_index) catch { 395 392 lua.raiseErrorStr("couldn't ref client table", .{}); 396 393 }; 397 394 395 + const app = getApp(lua); 396 + const gpa = app.alloc; 397 + 398 398 const cfg: irc.Client.Config = .{ 399 - .server = server, 400 - .user = user, 401 - .nick = nick, 402 - .password = password, 403 - .real_name = real_name, 399 + .server = gpa.dupe(u8, server) catch lua.raiseErrorStr("out of memory", .{}), 400 + .user = gpa.dupe(u8, user) catch lua.raiseErrorStr("out of memory", .{}), 401 + .nick = gpa.dupe(u8, nick) catch lua.raiseErrorStr("out of memory", .{}), 402 + .password = gpa.dupe(u8, password) catch lua.raiseErrorStr("out of memory", .{}), 403 + .real_name = gpa.dupe(u8, real_name) catch lua.raiseErrorStr("out of memory", .{}), 404 404 .tls = tls, 405 405 .lua_table = table_ref, 406 406 .port = port, 407 407 }; 408 408 409 - const app = getApp(lua); 410 409 app.connect(cfg) catch { 411 410 lua.raiseErrorStr("couldn't connect", .{}); 412 411 };