started to standardize and comment other apis

This commit is contained in:
Harrison DiAmbrosio 2025-11-27 00:10:25 -05:00
parent 3a0a29b55d
commit a764be6af6
3 changed files with 42 additions and 103 deletions

View file

@ -1,10 +1,10 @@
const Hook = @This();
const std = @import("std");
const zlua = @import("zlua");
const THook = @import("../types/hook.zig");
const zlua = @import("zlua");
const Utils = @import("../utils.zig");
const gpa = std.heap.c_allocator;
const server = &@import("../main.zig").server;
@ -12,14 +12,9 @@ const server = &@import("../main.zig").server;
pub fn add(L: *zlua.Lua) i32 {
L.checkType(2, .table);
var hook: *THook = gpa.create(THook) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
hook.events = std.ArrayList([]const u8).initCapacity(gpa, 1) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
errdefer Utils.oomPanic();
var hook: *THook = try gpa.create(THook);
hook.events = try std.ArrayList([]const u8).initCapacity(gpa, 1);
// We support both a string and a table of strings as the first value of
// add. Regardless of which type is passed in we create an arraylist of
@ -28,47 +23,28 @@ pub fn add(L: *zlua.Lua) i32 {
L.pushNil();
while (L.next(1)) {
if (L.isString(-1)) {
const s = L.toString(-1) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
hook.events.append(gpa, s) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
const s = L.checkString(-1);
try hook.events.append(gpa, s);
}
L.pop(1);
}
} else if (L.isString(1)) {
const s = L.toString(1) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
hook.events.append(gpa, s) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
const s = L.checkString(1);
try hook.events.append(gpa, s);
}
_ = L.pushString("callback");
_ = L.getTable(2);
if (L.isFunction(-1)) {
hook.options.lua_cb_ref_idx = L.ref(zlua.registry_index) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
L.raiseErrorStr("Lua error check your config", .{}); // TODO: Give more descriptive error
};
}
server.hooks.append(gpa, hook) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
try server.hooks.append(gpa, hook);
for (hook.events.items) |value| {
server.events.put(value, hook) catch {
L.raiseErrorStr("Lua error check your config", .{});
return 0;
};
try server.events.put(value, hook);
}
return 0;