mach: improve compatibility with self-hosted compiler
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
e92572b776
commit
72b081c97a
3 changed files with 47 additions and 19 deletions
|
|
@ -8,24 +8,39 @@ pub fn checkApplication(comptime app_pkg: type) void {
|
||||||
|
|
||||||
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 (@import("builtin").zig_backend == .stage1) {
|
||||||
@compileError("expected 'pub fn init(app: *App, core: *mach.Core) !void' found '" ++ @typeName(InitFn) ++ "'");
|
if (InitFn != fn (app: *App, core: *Core) @typeInfo(@typeInfo(InitFn).Fn.return_type.?).ErrorUnion.error_set!void)
|
||||||
|
@compileError("expected 'pub fn init(app: *App, core: *mach.Core) !void' found '" ++ @typeName(InitFn) ++ "'");
|
||||||
|
} else {
|
||||||
|
if (InitFn != *const fn (app: *App, core: *Core) @typeInfo(@typeInfo(InitFn).Fn.return_type.?).ErrorUnion.error_set!void)
|
||||||
|
@compileError("expected 'pub fn init(app: *App, core: *mach.Core) !void' found '" ++ @typeName(InitFn) ++ "'");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
@compileError("App must export 'pub fn init(app: *App, core: *mach.Core) !void'");
|
@compileError("App must export 'pub fn init(app: *App, core: *mach.Core) !void'");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (@hasDecl(App, "update")) {
|
if (@hasDecl(App, "update")) {
|
||||||
const UpdateFn = @TypeOf(@field(App, "update"));
|
const UpdateFn = @TypeOf(@field(App, "update"));
|
||||||
if (UpdateFn != fn (app: *App, core: *Core) @typeInfo(@typeInfo(UpdateFn).Fn.return_type.?).ErrorUnion.error_set!void)
|
if (@import("builtin").zig_backend == .stage1) {
|
||||||
@compileError("expected 'pub fn update(app: *App, core: *mach.Core) !void' found '" ++ @typeName(UpdateFn) ++ "'");
|
if (UpdateFn != fn (app: *App, core: *Core) @typeInfo(@typeInfo(UpdateFn).Fn.return_type.?).ErrorUnion.error_set!void)
|
||||||
|
@compileError("expected 'pub fn update(app: *App, core: *mach.Core) !void' found '" ++ @typeName(UpdateFn) ++ "'");
|
||||||
|
} else {
|
||||||
|
if (UpdateFn != *const fn (app: *App, core: *Core) @typeInfo(@typeInfo(UpdateFn).Fn.return_type.?).ErrorUnion.error_set!void)
|
||||||
|
@compileError("expected 'pub fn update(app: *App, core: *mach.Core) !void' found '" ++ @typeName(UpdateFn) ++ "'");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
@compileError("App must export 'pub fn update(app: *App, core: *mach.Core) !void'");
|
@compileError("App must export 'pub fn update(app: *App, core: *mach.Core) !void'");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (@hasDecl(App, "deinit")) {
|
if (@hasDecl(App, "deinit")) {
|
||||||
const DeinitFn = @TypeOf(@field(App, "deinit"));
|
const DeinitFn = @TypeOf(@field(App, "deinit"));
|
||||||
if (DeinitFn != fn (app: *App, core: *Core) void)
|
if (@import("builtin").zig_backend == .stage1) {
|
||||||
@compileError("expected 'pub fn deinit(app: *App, core: *mach.Core) void' found '" ++ @typeName(DeinitFn) ++ "'");
|
if (DeinitFn != fn (app: *App, core: *Core) void)
|
||||||
|
@compileError("expected 'pub fn deinit(app: *App, core: *mach.Core) void' found '" ++ @typeName(DeinitFn) ++ "'");
|
||||||
|
} else {
|
||||||
|
if (DeinitFn != *const fn (app: *App, core: *Core) void)
|
||||||
|
@compileError("expected 'pub fn deinit(app: *App, core: *mach.Core) void' found '" ++ @typeName(DeinitFn) ++ "'");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
@compileError("App must export 'pub fn deinit(app: *App, core: *mach.Core) void'");
|
@compileError("App must export 'pub fn deinit(app: *App, core: *mach.Core) void'");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -632,7 +632,10 @@ pub fn coreDeinit(core: *Core, allocator: std.mem.Allocator) void {
|
||||||
allocator.destroy(core);
|
allocator.destroy(core);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const CoreResizeCallback = fn (*Core, u32, u32) callconv(.C) void;
|
pub const CoreResizeCallback = if (@import("builtin").zig_backend == .stage1)
|
||||||
|
fn (*Core, u32, u32) callconv(.C) void
|
||||||
|
else
|
||||||
|
*const fn (*Core, u32, u32) callconv(.C) void;
|
||||||
|
|
||||||
pub fn coreUpdate(core: *Core, resize: ?CoreResizeCallback) !void {
|
pub fn coreUpdate(core: *Core, resize: ?CoreResizeCallback) !void {
|
||||||
if (core.internal.wait_event_timeout > 0.0) {
|
if (core.internal.wait_event_timeout > 0.0) {
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,7 @@ const std = @import("std");
|
||||||
|
|
||||||
const glfw = @import("glfw");
|
const glfw = @import("glfw");
|
||||||
const gpu = @import("gpu");
|
const gpu = @import("gpu");
|
||||||
const objc = @cImport({
|
const objc = @import("objc_message.zig");
|
||||||
@cInclude("objc/message.h");
|
|
||||||
});
|
|
||||||
|
|
||||||
pub inline fn printUnhandledErrorCallback(_: void, typ: gpu.ErrorType, message: [*:0]const u8) void {
|
pub inline fn printUnhandledErrorCallback(_: void, typ: gpu.ErrorType, message: [*:0]const u8) void {
|
||||||
switch (typ) {
|
switch (typ) {
|
||||||
|
|
@ -166,21 +164,33 @@ pub const AutoReleasePool = if (!@import("builtin").target.isDarwin()) opaque {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Borrowed from https://github.com/hazeycode/zig-objcrt
|
// Borrowed from https://github.com/hazeycode/zig-objcrt
|
||||||
fn msgSend(obj: anytype, sel_name: [:0]const u8, args: anytype, comptime ReturnType: type) ReturnType {
|
pub fn msgSend(obj: anytype, sel_name: [:0]const u8, args: anytype, comptime ReturnType: type) ReturnType {
|
||||||
const args_meta = @typeInfo(@TypeOf(args)).Struct.fields;
|
const args_meta = @typeInfo(@TypeOf(args)).Struct.fields;
|
||||||
|
|
||||||
const FnType = switch (args_meta.len) {
|
const FnType = if (@import("builtin").zig_backend == .stage1)
|
||||||
0 => fn (@TypeOf(obj), objc.SEL) callconv(.C) ReturnType,
|
switch (args_meta.len) {
|
||||||
1 => fn (@TypeOf(obj), objc.SEL, args_meta[0].field_type) callconv(.C) ReturnType,
|
0 => fn (@TypeOf(obj), objc.SEL) callconv(.C) ReturnType,
|
||||||
2 => fn (@TypeOf(obj), objc.SEL, args_meta[0].field_type, args_meta[1].field_type) callconv(.C) ReturnType,
|
1 => fn (@TypeOf(obj), objc.SEL, args_meta[0].field_type) callconv(.C) ReturnType,
|
||||||
3 => fn (@TypeOf(obj), objc.SEL, args_meta[0].field_type, args_meta[1].field_type, args_meta[2].field_type) callconv(.C) ReturnType,
|
2 => fn (@TypeOf(obj), objc.SEL, args_meta[0].field_type, args_meta[1].field_type) callconv(.C) ReturnType,
|
||||||
4 => fn (@TypeOf(obj), objc.SEL, args_meta[0].field_type, args_meta[1].field_type, args_meta[2].field_type, args_meta[3].field_type) callconv(.C) ReturnType,
|
3 => fn (@TypeOf(obj), objc.SEL, args_meta[0].field_type, args_meta[1].field_type, args_meta[2].field_type) callconv(.C) ReturnType,
|
||||||
|
4 => fn (@TypeOf(obj), objc.SEL, args_meta[0].field_type, args_meta[1].field_type, args_meta[2].field_type, args_meta[3].field_type) callconv(.C) ReturnType,
|
||||||
|
else => @compileError("Unsupported number of args"),
|
||||||
|
}
|
||||||
|
else switch (args_meta.len) {
|
||||||
|
0 => *const fn (@TypeOf(obj), objc.SEL) callconv(.C) ReturnType,
|
||||||
|
1 => *const fn (@TypeOf(obj), objc.SEL, args_meta[0].field_type) callconv(.C) ReturnType,
|
||||||
|
2 => *const fn (@TypeOf(obj), objc.SEL, args_meta[0].field_type, args_meta[1].field_type) callconv(.C) ReturnType,
|
||||||
|
3 => *const fn (@TypeOf(obj), objc.SEL, args_meta[0].field_type, args_meta[1].field_type, args_meta[2].field_type) callconv(.C) ReturnType,
|
||||||
|
4 => *const fn (@TypeOf(obj), objc.SEL, args_meta[0].field_type, args_meta[1].field_type, args_meta[2].field_type, args_meta[3].field_type) callconv(.C) ReturnType,
|
||||||
else => @compileError("Unsupported number of args"),
|
else => @compileError("Unsupported number of args"),
|
||||||
};
|
};
|
||||||
|
|
||||||
// NOTE: func is a var because making it const causes a compile error which I believe is a compiler bug
|
// NOTE: func is a var because making it const causes a compile error which I believe is a compiler bug
|
||||||
var func = @ptrCast(FnType, objc.objc_msgSend);
|
var func = if (@import("builtin").zig_backend == .stage1)
|
||||||
const sel = objc.sel_getUid(sel_name);
|
@ptrCast(FnType, objc.objc_msgSend)
|
||||||
|
else
|
||||||
|
@ptrCast(FnType, &objc.objc_msgSend);
|
||||||
|
const sel = objc.sel_getUid(@ptrCast([*c]const u8, sel_name));
|
||||||
|
|
||||||
return @call(.{}, func, .{ obj, sel } ++ args);
|
return @call(.{}, func, .{ obj, sel } ++ args);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue