handle i8/bool result in ci
This commit is contained in:
parent
87aae5f5ad
commit
337ec0a630
2 changed files with 14 additions and 8 deletions
11
src/c.zig
11
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"),
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue