mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-14 15:56:13 +03:00
pkg/objc: more message send stuff
This commit is contained in:
@ -26,6 +26,17 @@ test "getClass" {
|
||||
test "msgSend" {
|
||||
const testing = std.testing;
|
||||
const NSObject = Class.getClass("NSObject").?;
|
||||
|
||||
// Should work with primitives
|
||||
const id = NSObject.msgSend(c.id, objc.Sel.registerName("alloc"), .{});
|
||||
try testing.expect(id != null);
|
||||
{
|
||||
const obj: objc.Object = .{ .value = id };
|
||||
obj.msgSend(void, objc.sel("dealloc"), .{});
|
||||
}
|
||||
|
||||
// Should work with our wrappers
|
||||
const obj = NSObject.msgSend(objc.Object, objc.Sel.registerName("alloc"), .{});
|
||||
try testing.expect(obj.value != null);
|
||||
obj.msgSend(void, objc.sel("dealloc"), .{});
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
pub const c = @import("c.zig");
|
||||
pub usingnamespace @import("class.zig");
|
||||
pub usingnamespace @import("object.zig");
|
||||
pub usingnamespace @import("sel.zig");
|
||||
|
||||
test {
|
||||
|
@ -19,9 +19,26 @@ pub fn MsgSend(comptime T: type) type {
|
||||
sel: objc.Sel,
|
||||
args: anytype,
|
||||
) Return {
|
||||
// Build our function type and call it
|
||||
const Fn = MsgSendFn(Return, @TypeOf(target.value), @TypeOf(args));
|
||||
const msg_send_ptr = @ptrCast(std.meta.FnPtr(Fn), &c.objc_msgSend);
|
||||
return @call(.{}, msg_send_ptr, .{ target.value, sel } ++ args);
|
||||
const result = @call(.{}, msg_send_ptr, .{ target.value, sel } ++ args);
|
||||
|
||||
// This is a special nicety: if the return type is one of our
|
||||
// public structs then we wrap the msgSend id result with it.
|
||||
// This lets msgSend magically work with Object and so on.
|
||||
const is_pkg_struct = comptime is_pkg_struct: {
|
||||
for (@typeInfo(objc).Struct.decls) |decl| {
|
||||
if (decl.is_pub and Return == @field(objc, decl.name)) {
|
||||
break :is_pkg_struct true;
|
||||
}
|
||||
}
|
||||
|
||||
break :is_pkg_struct false;
|
||||
};
|
||||
|
||||
if (!is_pkg_struct) return result;
|
||||
return .{ .value = result };
|
||||
}
|
||||
};
|
||||
}
|
||||
|
10
pkg/objc/object.zig
Normal file
10
pkg/objc/object.zig
Normal file
@ -0,0 +1,10 @@
|
||||
const std = @import("std");
|
||||
const c = @import("c.zig");
|
||||
const objc = @import("main.zig");
|
||||
const MsgSend = @import("msg_send.zig").MsgSend;
|
||||
|
||||
pub const Object = struct {
|
||||
value: c.id,
|
||||
|
||||
pub usingnamespace MsgSend(Object);
|
||||
};
|
@ -1,6 +1,11 @@
|
||||
const std = @import("std");
|
||||
const c = @import("c.zig");
|
||||
|
||||
// Shorthand, equivalent to Sel.registerName
|
||||
pub inline fn sel(name: [:0]const u8) Sel {
|
||||
return Sel.registerName(name);
|
||||
}
|
||||
|
||||
pub const Sel = struct {
|
||||
value: c.SEL,
|
||||
|
||||
@ -20,6 +25,6 @@ pub const Sel = struct {
|
||||
|
||||
test {
|
||||
const testing = std.testing;
|
||||
const sel = Sel.registerName("yo");
|
||||
try testing.expectEqualStrings("yo", sel.getName());
|
||||
const s = Sel.registerName("yo");
|
||||
try testing.expectEqualStrings("yo", s.getName());
|
||||
}
|
||||
|
Reference in New Issue
Block a user