From b90c6894703f435ac901232e8d6d30482e4f9f23 Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Sat, 12 Apr 2025 15:20:34 -0600 Subject: [PATCH] take pointers rather than slices Modified some of the wrapper functions to take sentinel-terminated pointers rather than slices. --- src/class.zig | 20 ++++++++++---------- src/object.zig | 8 ++++---- src/property.zig | 4 ++-- src/protocol.zig | 4 ++-- src/sel.zig | 6 +++--- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/class.zig b/src/class.zig index cf3373e..7b7a3e2 100644 --- a/src/class.zig +++ b/src/class.zig @@ -15,9 +15,9 @@ pub const Class = struct { pub const msgSendSuper = msg_send.msgSendSuper; // Returns a property with a given name of a given class. - pub fn getProperty(self: Class, name: [:0]const u8) ?objc.Property { + pub fn getProperty(self: Class, name: [*:0]const u8) ?objc.Property { return objc.Property{ - .value = c.class_getProperty(self.value, name.ptr) orelse return null, + .value = c.class_getProperty(self.value, name) orelse return null, }; } @@ -56,7 +56,7 @@ pub const Class = struct { // currently only allows for overriding methods previously defined, e.g. by a superclass. // imp should be a function with C calling convention // whose first two arguments are a `c.id` and a `c.SEL`. - pub fn replaceMethod(self: Class, name: [:0]const u8, imp: anytype) void { + pub fn replaceMethod(self: Class, name: [*:0]const u8, imp: anytype) void { const fn_info = @typeInfo(@TypeOf(imp)).@"fn"; assert(std.meta.eql(fn_info.calling_convention, std.builtin.CallingConvention.c)); assert(fn_info.is_var_args == false); @@ -69,7 +69,7 @@ pub const Class = struct { /// allows adding new methods; returns true on success. // imp should be a function with C calling convention // 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) !bool { const Fn = @TypeOf(imp); const fn_info = @typeInfo(Fn).@"fn"; assert(std.meta.eql(fn_info.calling_convention, std.builtin.CallingConvention.c)); @@ -88,26 +88,26 @@ pub const Class = struct { // 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 { + pub fn addIvar(self: Class, name: [*:0]const u8) bool { // The return type is i8 when we're cross compiling, unsure why. const result = c.class_addIvar(self.value, name, @sizeOf(c.id), @alignOf(c.id), "@"); return boolResult(@TypeOf(c.class_addIvar), result); } }; -pub fn getClass(name: [:0]const u8) ?Class { - return .{ .value = c.objc_getClass(name.ptr) orelse return null }; +pub fn getClass(name: [*:0]const u8) ?Class { + return .{ .value = c.objc_getClass(name) orelse return null }; } -pub fn getMetaClass(name: [:0]const u8) ?Class { +pub fn getMetaClass(name: [*:0]const u8) ?Class { return .{ .value = c.objc_getMetaClass(name) orelse return null }; } // begin by calling this function, then call registerClassPair on the result when you are finished -pub fn allocateClassPair(superclass: ?Class, name: [:0]const u8) ?Class { +pub fn allocateClassPair(superclass: ?Class, name: [*:0]const u8) ?Class { return .{ .value = c.objc_allocateClassPair( if (superclass) |cls| cls.value else null, - name.ptr, + name, 0, ) orelse return null }; } diff --git a/src/object.zig b/src/object.zig index 6773928..9bf507d 100644 --- a/src/object.zig +++ b/src/object.zig @@ -34,7 +34,7 @@ pub const Object = struct { /// Set a property. This is a helper around getProperty and is /// strictly less performant than doing it manually. Consider doing /// this manually if performance is critical. - pub fn setProperty(self: Object, comptime n: [:0]const u8, v: anytype) void { + pub fn setProperty(self: Object, comptime n: [*:0]const u8, v: anytype) void { const Class = self.getClass().?; const setter = setter: { // See getProperty for why we do this. @@ -59,7 +59,7 @@ pub const Object = struct { /// Get a property. This is a helper around Class.getProperty and is /// strictly less performant than doing it manually. Consider doing /// this manually if performance is critical. - pub fn getProperty(self: Object, comptime T: type, comptime n: [:0]const u8) T { + pub fn getProperty(self: Object, comptime T: type, comptime n: [*:0]const u8) T { const Class = self.getClass().?; const getter = getter: { // Sometimes a property is not a property because it has been @@ -91,12 +91,12 @@ pub const Object = struct { return c.object_isClass(self.value) == 1; } - pub fn getInstanceVariable(self: Object, name: [:0]const u8) Object { + pub fn getInstanceVariable(self: Object, name: [*:0]const u8) Object { const ivar = c.object_getInstanceVariable(self.value, name, null); return fromId(c.object_getIvar(self.value, ivar)); } - pub fn setInstanceVariable(self: Object, name: [:0]const u8, val: Object) void { + pub fn setInstanceVariable(self: Object, name: [*:0]const u8, val: Object) void { const ivar = c.object_getInstanceVariable(self.value, name, null); c.object_setIvar(self.value, ivar, val.value); } diff --git a/src/property.zig b/src/property.zig index e042306..7c97d5d 100644 --- a/src/property.zig +++ b/src/property.zig @@ -11,9 +11,9 @@ pub const Property = extern struct { } /// Returns the value of a property attribute given the attribute name. - pub fn copyAttributeValue(self: Property, attr: [:0]const u8) ?[:0]u8 { + pub fn copyAttributeValue(self: Property, attr: [*:0]const u8) ?[:0]u8 { return std.mem.sliceTo( - c.property_copyAttributeValue(self.value, attr.ptr) orelse return null, + c.property_copyAttributeValue(self.value, attr) orelse return null, 0, ); } diff --git a/src/protocol.zig b/src/protocol.zig index e72e170..77fa244 100644 --- a/src/protocol.zig +++ b/src/protocol.zig @@ -19,7 +19,7 @@ pub const Protocol = extern struct { pub fn getProperty( self: Protocol, - name: [:0]const u8, + name: [*:0]const u8, is_required: bool, is_instance: bool, ) ?objc.Property { @@ -39,6 +39,6 @@ pub const Protocol = extern struct { } }; -pub fn getProtocol(name: [:0]const u8) ?Protocol { +pub fn getProtocol(name: [*:0]const u8) ?Protocol { return .{ .value = c.objc_getProtocol(name) orelse return null }; } diff --git a/src/sel.zig b/src/sel.zig index 8036961..372172a 100644 --- a/src/sel.zig +++ b/src/sel.zig @@ -2,7 +2,7 @@ const std = @import("std"); const c = @import("c.zig").c; // Shorthand, equivalent to Sel.registerName -pub inline fn sel(name: [:0]const u8) Sel { +pub inline fn sel(name: [*:0]const u8) Sel { return Sel.registerName(name); } @@ -11,9 +11,9 @@ pub const Sel = struct { /// Registers a method with the Objective-C runtime system, maps the /// method name to a selector, and returns the selector value. - pub fn registerName(name: [:0]const u8) Sel { + pub fn registerName(name: [*:0]const u8) Sel { return Sel{ - .value = c.sel_registerName(name.ptr), + .value = c.sel_registerName(name), }; }