handle i8/bool result in ci

This commit is contained in:
Mitchell Hashimoto 2023-10-21 10:24:50 -07:00
parent 87aae5f5ad
commit 337ec0a630
No known key found for this signature in database
GPG key ID: 523D5DC389D273BC
2 changed files with 14 additions and 8 deletions

View file

@ -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"),
};
}

View file

@ -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);
}
};