ghostty/pkg/objc/class.zig
2022-10-25 20:55:41 -07:00

43 lines
1.2 KiB
Zig

const std = @import("std");
const c = @import("c.zig");
const objc = @import("main.zig");
const MsgSend = @import("msg_send.zig").MsgSend;
pub const Class = struct {
value: c.Class,
pub usingnamespace MsgSend(Class);
/// Returns the class definition of a specified class.
pub fn getClass(name: [:0]const u8) ?Class {
return Class{
.value = c.objc_getClass(name.ptr) orelse return null,
};
}
};
test "getClass" {
const testing = std.testing;
const NSObject = Class.getClass("NSObject");
try testing.expect(NSObject != null);
try testing.expect(Class.getClass("NoWay") == null);
}
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"), .{});
}