From ff69efea52d88903b657e2e9ff09221a90ceedc9 Mon Sep 17 00:00:00 2001 From: Ali Chraghi Date: Sat, 25 Jun 2022 17:13:05 +0430 Subject: [PATCH] mach: check application signature --- src/platform/common.zig | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/platform/common.zig b/src/platform/common.zig index 059b9744..3b3892f3 100644 --- a/src/platform/common.zig +++ b/src/platform/common.zig @@ -1,6 +1,27 @@ +const Engine = @import("../Engine.zig"); + pub fn checkApplication(comptime App: type) void { - // TODO: check signature - if (!@hasDecl(App, "init")) @compileError("App must export 'pub fn init(app: *App, engine: *mach.Engine) !void'"); - if (!@hasDecl(App, "deinit")) @compileError("App must export 'pub fn deinit(app: *App, engine: *mach.Engine) void'"); - if (!@hasDecl(App, "update")) @compileError("App must export 'pub fn update(app: *App, engine: *mach.Engine) !void'"); + if (@hasDecl(App, "init")) { + const InitFn = @TypeOf(@field(App, "init")); + if (InitFn != fn (app: *App, engine: *Engine) @typeInfo(@typeInfo(InitFn).Fn.return_type.?).ErrorUnion.error_set!void) + @compileError("expected 'pub fn init(app: *App, engine: *mach.Engine) !void' found '" ++ @typeName(InitFn) ++ "'"); + } else { + @compileError("App must export 'pub fn init(app: *App, engine: *mach.Engine) !void'"); + } + + if (@hasDecl(App, "update")) { + const UpdateFn = @TypeOf(@field(App, "update")); + if (UpdateFn != fn (app: *App, engine: *Engine) @typeInfo(@typeInfo(UpdateFn).Fn.return_type.?).ErrorUnion.error_set!void) + @compileError("expected 'pub fn update(app: *App, engine: *mach.Engine) !void' found '" ++ @typeName(UpdateFn) ++ "'"); + } else { + @compileError("App must export 'pub fn update(app: *App, engine: *mach.Engine) !void'"); + } + + if (@hasDecl(App, "deinit")) { + const DeinitFn = @TypeOf(@field(App, "deinit")); + if (DeinitFn != fn (app: *App, engine: *Engine) void) + @compileError("expected 'pub fn deinit(app: *App, engine: *mach.Engine) void' found '" ++ @typeName(DeinitFn) ++ "'"); + } else { + @compileError("App must export 'pub fn deinit(app: *App, engine: *mach.Engine) void'"); + } }