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 <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-07-04 21:16:16 -07:00 committed by Stephen Gutekanst
parent 1b97c9f1e5
commit 72ea652dca
3 changed files with 20 additions and 13 deletions

View file

@ -1,6 +1,11 @@
const Core = @import("../Core.zig"); 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")) { if (@hasDecl(App, "init")) {
const InitFn = @TypeOf(@field(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) if (InitFn != fn (app: *App, core: *Core) @typeInfo(@typeInfo(InitFn).Fn.return_type.?).ErrorUnion.error_set!void)

View file

@ -1,13 +1,19 @@
const std = @import("std"); const std = @import("std");
const glfw = @import("glfw"); const glfw = @import("glfw");
const gpu = @import("gpu"); const gpu = @import("gpu");
const App = @import("app"); const app_pkg = @import("app");
const Core = @import("../Core.zig"); const Core = @import("../Core.zig");
const structs = @import("../structs.zig"); const structs = @import("../structs.zig");
const enums = @import("../enums.zig"); const enums = @import("../enums.zig");
const util = @import("util.zig"); const util = @import("util.zig");
const c = @import("c.zig").c; 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 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; 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; pub const BackingTimer = std.time.Timer;
const common = @import("common.zig");
comptime {
common.checkApplication(App);
}
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit(); defer _ = gpa.deinit();

View file

@ -1,5 +1,5 @@
const std = @import("std"); const std = @import("std");
const App = @import("app"); const app_pkg = @import("app");
const Core = @import("../Core.zig"); const Core = @import("../Core.zig");
const structs = @import("../structs.zig"); const structs = @import("../structs.zig");
const enums = @import("../enums.zig"); const enums = @import("../enums.zig");
@ -29,6 +29,12 @@ const js = struct {
extern fn machPanic(str: [*]const u8, len: u32) void; 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 CanvasId = u32;
pub const Platform = struct { 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 app: App = undefined;
var core: Core = undefined; var core: Core = undefined;