From 83805e20bec4f1102c752ef60b380507e931ccb7 Mon Sep 17 00:00:00 2001 From: Thom Dickson Date: Fri, 23 Aug 2024 22:58:39 -0400 Subject: [PATCH 1/2] add type introspection for router handle_func --- src/router.zig | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/router.zig b/src/router.zig index c9ca7d5..40ab1d3 100644 --- a/src/router.zig +++ b/src/router.zig @@ -74,6 +74,41 @@ pub fn handle_func_unbound(self: *Self, path: []const u8, h: zap.HttpRequestFn) pub fn handle_func(self: *Self, path: []const u8, instance: *anyopaque, handler: anytype) !void { // TODO: assert type of instance has handler + // Introspection checks on handler type + comptime { + const hand_info = @typeInfo(@TypeOf(handler)); + + // Need to check: + // 1) handler is function pointer + const f = blk: { + if (hand_info == .Pointer) { + const inner = @typeInfo(hand_info.Pointer.child); + if (inner == .Fn) { + break :blk inner.Fn; + } + } + @compileError("Expected handler to be a function pointer. Found " ++ + @typeName(@TypeOf(handler))); + }; + + // 2) snd arg is zap.Request + if (f.params.len != 2) { + @compileError("Expected handler to have two paramters"); + } + const arg_type = f.params[1].type.?; + if (arg_type != zap.Request) { + @compileError("Expected handler's second argument to be of type zap.Request. Found " ++ + @typeName(arg_type)); + } + + // 3) handler returns void + const ret_info = @typeInfo(f.return_type.?); + if (ret_info != .Void) { + @compileError("Expected handler's return type to be void. Found " ++ + @typeName(f.return_type.?)); + } + } + if (path.len == 0) { return RouterError.EmptyPath; } From a56781df246f6ed9bbfc6adb41e95b7100772185 Mon Sep 17 00:00:00 2001 From: vctrmn Date: Mon, 3 Mar 2025 17:10:43 +0100 Subject: [PATCH 2/2] Fix deprecated warning for non-prototype function declaration in websocket.c --- .gitignore | 1 + facil.io/lib/facil/http/websockets.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6d2d37c..5ac9561 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ scratch .vs/ **/*.perflog wrk/*.png +.zig-cache/ \ No newline at end of file diff --git a/facil.io/lib/facil/http/websockets.c b/facil.io/lib/facil/http/websockets.c index 87353ef..1e882da 100644 --- a/facil.io/lib/facil/http/websockets.c +++ b/facil.io/lib/facil/http/websockets.c @@ -96,7 +96,7 @@ void free_ws_buffer(ws_s *owner, struct buffer_s buff) { Create/Destroy the websocket object (prototypes) */ -static ws_s *new_websocket(); +static ws_s *new_websocket(intptr_t uuid); static void destroy_ws(ws_s *ws); /*******************************************************************************