From 72ea652dca598bfd093db8bd60656a37c1e6df06 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Mon, 4 Jul 2022 21:16:16 -0700 Subject: [PATCH] mach: require a 'pub const App' to be exposed This requires apps expose a `pub const App` from their `main.zig`, effectively adopting the high-level/low-level application API outlined in hexops/mach#349 If `pub const App` does not exist, a clear compiler error is produced: ``` ./src/platform/common.zig:5:9: error: expected e.g. `pub const App = mach.App(modules, init)' (App definition missing in your main Zig file) @compileError("expected e.g. `pub const App = mach.App(modules, init)' (App definition missing in your main Zig file)"); ^ ./src/platform/native.zig:13:28: note: called from here common.checkApplication(app_pkg); ^ ./src/platform/native.zig:12:10: note: called from here comptime { ^ ./src/platform/native.zig:15:20: error: container '.app' has no member called 'App' const App = app_pkg.App; ^ ``` Signed-off-by: Stephen Gutekanst --- src/platform/common.zig | 7 ++++++- src/platform/native.zig | 13 +++++++------ src/platform/wasm.zig | 13 +++++++------ 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/platform/common.zig b/src/platform/common.zig index 1fba01c0..116b4ff8 100644 --- a/src/platform/common.zig +++ b/src/platform/common.zig @@ -1,6 +1,11 @@ const Core = @import("../Core.zig"); -pub fn checkApplication(comptime App: type) void { +pub fn checkApplication(comptime app_pkg: type) void { + if (!@hasDecl(app_pkg, "App")) { + @compileError("expected e.g. `pub const App = mach.App(modules, init)' (App definition missing in your main Zig file)"); + } + const App = app_pkg.App; + if (@hasDecl(App, "init")) { const InitFn = @TypeOf(@field(App, "init")); if (InitFn != fn (app: *App, core: *Core) @typeInfo(@typeInfo(InitFn).Fn.return_type.?).ErrorUnion.error_set!void) diff --git a/src/platform/native.zig b/src/platform/native.zig index c91049ea..736aff02 100644 --- a/src/platform/native.zig +++ b/src/platform/native.zig @@ -1,13 +1,19 @@ const std = @import("std"); const glfw = @import("glfw"); const gpu = @import("gpu"); -const App = @import("app"); +const app_pkg = @import("app"); const Core = @import("../Core.zig"); const structs = @import("../structs.zig"); const enums = @import("../enums.zig"); const util = @import("util.zig"); const c = @import("c.zig").c; +const common = @import("common.zig"); +comptime { + common.checkApplication(app_pkg); +} +const App = app_pkg.App; + pub const scope_levels = if (@hasDecl(App, "scope_levels")) App.scope_levels else [0]std.log.ScopeLevel{}; pub const log_level = if (@hasDecl(App, "log_level")) App.log_level else std.log.default_level; @@ -581,11 +587,6 @@ pub const Platform = struct { pub const BackingTimer = std.time.Timer; -const common = @import("common.zig"); -comptime { - common.checkApplication(App); -} - pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); diff --git a/src/platform/wasm.zig b/src/platform/wasm.zig index afe58636..5cf74c54 100644 --- a/src/platform/wasm.zig +++ b/src/platform/wasm.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const App = @import("app"); +const app_pkg = @import("app"); const Core = @import("../Core.zig"); const structs = @import("../structs.zig"); const enums = @import("../enums.zig"); @@ -29,6 +29,12 @@ const js = struct { extern fn machPanic(str: [*]const u8, len: u32) void; }; +const common = @import("common.zig"); +comptime { + common.checkApplication(app_pkg); +} +const App = app_pkg.App; + pub const CanvasId = u32; pub const Platform = struct { @@ -273,11 +279,6 @@ pub const BackingTimer = struct { } }; -const common = @import("common.zig"); -comptime { - common.checkApplication(App); -} - var app: App = undefined; var core: Core = undefined;