From 337ec0a630ee757247e2794313b5fae105b43294 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 21 Oct 2023 10:24:50 -0700 Subject: [PATCH] handle i8/bool result in ci --- src/c.zig | 11 +++++++++++ src/class.zig | 11 +++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/c.zig b/src/c.zig index 622ea4b..6c3b6f0 100644 --- a/src/c.zig +++ b/src/c.zig @@ -2,3 +2,14 @@ pub usingnamespace @cImport({ @cInclude("objc/runtime.h"); @cInclude("objc/message.h"); }); + +/// This is a funky helper to help with the fact that some macOS +/// SDKs have an i8 return value for bools and some have stdbool. +pub fn boolResult(comptime Fn: type, result: anytype) bool { + const fn_info = @typeInfo(Fn).Fn; + return switch (fn_info.return_type.?) { + bool => result, + i8 => result == 1, + else => @compileError("unhandled class_addIvar return type"), + }; +} diff --git a/src/class.zig b/src/class.zig index 722071b..f63264a 100644 --- a/src/class.zig +++ b/src/class.zig @@ -72,25 +72,20 @@ pub const Class = struct { assert(fn_info.params[0].type == c.id); assert(fn_info.params[1].type == c.SEL); const encoding = comptime objc.comptimeEncode(Fn); - return c.class_addMethod( + return c.boolResult(@TypeOf(c.class_addMethod), c.class_addMethod( self.value, objc.sel(name).value, @ptrCast(&imp), encoding.ptr, - ); + )); } // only call this function between allocateClassPair and registerClassPair // this adds an Ivar of type `id`. pub fn addIvar(self: Class, name: [:0]const u8) bool { // The return type is i8 when we're cross compiling, unsure why. - const fn_info = @typeInfo(@TypeOf(c.class_addIvar)).Fn; const result = c.class_addIvar(self.value, name, @sizeOf(c.id), @alignOf(c.id), "@"); - return switch (fn_info.return_type.?) { - bool => result, - i8 => result == 1, - else => @compileError("unhandled class_addIvar return type"), - }; + return c.boolResult(@TypeOf(c.class_addIvar), result); } };