fix: convert addMethod
bool to zig error
This commit is contained in:
parent
bca05e03d3
commit
3607c36967
1 changed files with 8 additions and 4 deletions
|
@ -60,10 +60,12 @@ pub const Class = struct {
|
||||||
_ = c.class_replaceMethod(self.value, objc.sel(name).value, @ptrCast(&imp), null);
|
_ = c.class_replaceMethod(self.value, objc.sel(name).value, @ptrCast(&imp), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const AddMethodError = error{AddMethodFailed} || std.mem.Allocator.Error;
|
||||||
|
|
||||||
/// allows adding new methods; returns true on success.
|
/// allows adding new methods; returns true on success.
|
||||||
// imp should be a function with C calling convention
|
// imp should be a function with C calling convention
|
||||||
// whose first two arguments are a `c.id` and a `c.SEL`.
|
// whose first two arguments are a `c.id` and a `c.SEL`.
|
||||||
pub fn addMethod(self: Class, name: [:0]const u8, imp: anytype) !bool {
|
pub fn addMethod(self: Class, name: [:0]const u8, imp: anytype) AddMethodError!void {
|
||||||
const fn_info = @typeInfo(@TypeOf(imp)).Fn;
|
const fn_info = @typeInfo(@TypeOf(imp)).Fn;
|
||||||
assert(fn_info.calling_convention == .C);
|
assert(fn_info.calling_convention == .C);
|
||||||
assert(fn_info.is_var_args == false);
|
assert(fn_info.is_var_args == false);
|
||||||
|
@ -72,7 +74,9 @@ pub const Class = struct {
|
||||||
assert(fn_info.params[1].type == c.SEL);
|
assert(fn_info.params[1].type == c.SEL);
|
||||||
const str = try objc.createFnSignature(fn_info.return_type.?, fn_info.params);
|
const str = try objc.createFnSignature(fn_info.return_type.?, fn_info.params);
|
||||||
defer std.heap.raw_c_allocator.free(str);
|
defer std.heap.raw_c_allocator.free(str);
|
||||||
return c.class_addMethod(self.value, objc.sel(name).value, @ptrCast(&imp), str.ptr);
|
if (!c.class_addMethod(self.value, objc.sel(name).value, @ptrCast(&imp), str.ptr)) {
|
||||||
|
return error.AddMethodFailed;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// only call this function between allocateClassPair and registerClassPair
|
// only call this function between allocateClassPair and registerClassPair
|
||||||
|
@ -201,13 +205,13 @@ test "addMethod" {
|
||||||
const My_Class = setup: {
|
const My_Class = setup: {
|
||||||
const My_Class = allocateClassPair(objc.getClass("NSObject").?, "my_class").?;
|
const My_Class = allocateClassPair(objc.getClass("NSObject").?, "my_class").?;
|
||||||
defer registerClassPair(My_Class);
|
defer registerClassPair(My_Class);
|
||||||
std.debug.assert(try My_Class.addMethod("my_addition", struct {
|
try My_Class.addMethod("my_addition", struct {
|
||||||
fn imp(target: objc.c.id, sel: objc.c.SEL, a: i32, b: i32) callconv(.C) i32 {
|
fn imp(target: objc.c.id, sel: objc.c.SEL, a: i32, b: i32) callconv(.C) i32 {
|
||||||
_ = sel;
|
_ = sel;
|
||||||
_ = target;
|
_ = target;
|
||||||
return a + b;
|
return a + b;
|
||||||
}
|
}
|
||||||
}.imp));
|
}.imp);
|
||||||
break :setup My_Class;
|
break :setup My_Class;
|
||||||
};
|
};
|
||||||
const result = My_Class.msgSend(objc.Object, "alloc", .{})
|
const result = My_Class.msgSend(objc.Object, "alloc", .{})
|
||||||
|
|
Loading…
Add table
Reference in a new issue