Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ pub fn build(b: *std.Build) !void {

const tests = b.addTest(.{
.name = "objc-test",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
}),
});
tests.linkSystemLibrary("objc");
tests.linkFramework("Foundation");
Expand Down Expand Up @@ -66,7 +68,7 @@ pub fn addAppleSDK(b: *std.Build, m: *std.Build.Module) !void {
if (!gop.found_existing) {
gop.value_ptr.* = std.zig.system.darwin.getSdk(
b.allocator,
m.resolved_target.?.result,
&m.resolved_target.?.result,
);
}

Expand Down
1 change: 1 addition & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.name = .zig_objc,
.version = "0.0.0",
.fingerprint = 0x8a91772ba7d2bf22,
.minimum_zig_version = "0.15.1",
.paths = .{
"src/",
"build.zig",
Expand Down
55 changes: 35 additions & 20 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
description = "Objective-C runtime bindings for Zig";

inputs = {
nixpkgs.url = "github:nixos/nixpkgs/release-24.05";
nixpkgs.url = "https://channels.nixos.org/nixos-25.05/nixexprs.tar.xz";
flake-utils.url = "github:numtide/flake-utils";
zig.url = "github:mitchellh/zig-overlay";

Expand Down Expand Up @@ -35,7 +35,7 @@
in rec {
devShells.default = pkgs.mkShell {
nativeBuildInputs = with pkgs; [
zigpkgs."0.14.0"
zigpkgs."0.15.1"
];
};

Expand Down
22 changes: 11 additions & 11 deletions src/block.zig
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub fn Block(
_Block_release(@ptrCast(@alignCast(ctx)));
}

fn descCopyHelper(src: *anyopaque, dst: *anyopaque) callconv(.C) void {
fn descCopyHelper(src: *anyopaque, dst: *anyopaque) callconv(.c) void {
const real_src: *Context = @ptrCast(@alignCast(src));
const real_dst: *Context = @ptrCast(@alignCast(dst));
inline for (captures_info.fields) |field| {
Expand All @@ -134,7 +134,7 @@ pub fn Block(
}
}

fn descDisposeHelper(src: *anyopaque) callconv(.C) void {
fn descDisposeHelper(src: *anyopaque) callconv(.c) void {
const real_src: *Context = @ptrCast(@alignCast(src));
inline for (captures_info.fields) |field| {
if (field.type == objc.c.id) {
Expand All @@ -158,7 +158,7 @@ pub fn Block(

return @Type(.{
.@"fn" = .{
.calling_convention = .C,
.calling_convention = .c,
.is_generic = false,
.is_var_args = false,
.return_type = Return,
Expand Down Expand Up @@ -249,16 +249,16 @@ const BlockFieldFlags = enum(c_int) {
byref_caller = 128, // BLOCK_BYREF_CALLER
};

extern "C" fn _Block_copy(src: *const anyopaque) callconv(.c) ?*anyopaque;
extern "C" fn _Block_release(src: *const anyopaque) callconv(.c) void;
extern "C" fn _Block_object_assign(dst: *anyopaque, src: *const anyopaque, flag: BlockFieldFlags) void;
extern "C" fn _Block_object_dispose(src: *const anyopaque, flag: BlockFieldFlags) void;
extern "c" fn _Block_copy(src: *const anyopaque) callconv(.c) ?*anyopaque;
extern "c" fn _Block_release(src: *const anyopaque) callconv(.c) void;
extern "c" fn _Block_object_assign(dst: *anyopaque, src: *const anyopaque, flag: BlockFieldFlags) void;
extern "c" fn _Block_object_dispose(src: *const anyopaque, flag: BlockFieldFlags) void;

const Descriptor = extern struct {
reserved: c_ulong = 0,
size: c_ulong,
copy_helper: *const fn (dst: *anyopaque, src: *anyopaque) callconv(.C) void,
dispose_helper: *const fn (src: *anyopaque) callconv(.C) void,
copy_helper: *const fn (dst: *anyopaque, src: *anyopaque) callconv(.c) void,
dispose_helper: *const fn (src: *anyopaque) callconv(.c) void,
signature: ?[*:0]const u8,
};

Expand Down Expand Up @@ -287,7 +287,7 @@ test "Block" {
};

var block: AddBlock.Context = AddBlock.init(captures, (struct {
fn addFn(block: *const AddBlock.Context) callconv(.C) i32 {
fn addFn(block: *const AddBlock.Context) callconv(.c) i32 {
return block.x + block.y;
}
}).addFn);
Expand Down Expand Up @@ -317,7 +317,7 @@ test "Block copy objc id" {
var block = TestBlock.init(.{
.id = obj.value,
}, (struct {
fn addFn(block: *const TestBlock.Context) callconv(.C) i32 {
fn addFn(block: *const TestBlock.Context) callconv(.c) i32 {
_ = block;
return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/class.zig
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ test "allocatecClassPair and replaceMethod" {
const NSObject = getClass("NSObject").?;
var my_object = allocateClassPair(NSObject, "my_object").?;
my_object.replaceMethod("hash", struct {
fn inner(target: c.id, sel: c.SEL) callconv(.C) u64 {
fn inner(target: c.id, sel: c.SEL) callconv(.c) u64 {
_ = sel;
_ = target;
return 69;
Expand Down Expand Up @@ -208,7 +208,7 @@ test "addMethod" {
const My_Class = allocateClassPair(objc.getClass("NSObject").?, "my_class").?;
defer registerClassPair(My_Class);
std.debug.assert(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;
_ = target;
return a + b;
Expand Down
30 changes: 14 additions & 16 deletions src/encoding.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ fn comptimeN(comptime T: type) usize {
const encoding = objc.Encoding.init(T);

// Figure out how much space we need
var counting = std.io.countingWriter(std.io.null_writer);
try std.fmt.format(counting.writer(), "{}", .{encoding});
return counting.bytes_written;
var stream: std.io.Writer.Discarding = .init(&.{});
stream.writer.print("{f}", .{encoding}) catch unreachable;
return stream.count;
}
}

Expand All @@ -23,8 +23,8 @@ pub fn comptimeEncode(comptime T: type) [comptimeN(T):0]u8 {

// Build our final signature
var buf: [comptimeN(T) + 1]u8 = undefined;
var fbs = std.io.fixedBufferStream(buf[0 .. buf.len - 1]);
try std.fmt.format(fbs.writer(), "{}", .{encoding});
var fbs: std.io.Writer = .fixed(buf[0 .. buf.len - 1]);
fbs.print("{f}", .{encoding}) catch unreachable;
buf[buf.len - 1] = 0;

return buf[0 .. buf.len - 1 :0].*;
Expand Down Expand Up @@ -107,9 +107,7 @@ pub const Encoding = union(enum) {

pub fn format(
comptime self: Encoding,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
writer: *std.io.Writer,
) !void {
switch (self) {
.char => try writer.writeAll("c"),
Expand All @@ -133,7 +131,7 @@ pub const Encoding = union(enum) {
.array => |a| {
try writer.print("[{}", .{a.len});
const encode_type = init(a.arr_type);
try encode_type.format(fmt, options, writer);
try encode_type.format(writer);
try writer.writeAll("]");
},
.structure => |s| {
Expand All @@ -152,7 +150,7 @@ pub const Encoding = union(enum) {
try writer.writeAll("=");
inline for (struct_info.@"struct".fields) |field| {
const field_encode = init(field.type);
try field_encode.format(fmt, options, writer);
try field_encode.format(writer);
}
}

Expand All @@ -174,7 +172,7 @@ pub const Encoding = union(enum) {
try writer.writeAll("=");
inline for (union_info.@"union".fields) |field| {
const field_encode = init(field.type);
try field_encode.format(fmt, options, writer);
try field_encode.format(writer);
}
}

Expand Down Expand Up @@ -209,7 +207,7 @@ pub const Encoding = union(enum) {
}

// call this format function again, this time with the child type encoding
try encoding.format(fmt, options, writer);
try encoding.format(writer);
},
else => @compileError("Pointer size not supported for encoding"),
}
Expand All @@ -219,10 +217,10 @@ pub const Encoding = union(enum) {

// Return type is first in a method encoding
const ret_type_enc = init(fn_info.return_type.?);
try ret_type_enc.format(fmt, options, writer);
try ret_type_enc.format(writer);
inline for (fn_info.params) |param| {
const param_enc = init(param.type.?);
try param_enc.format(fmt, options, writer);
try param_enc.format(writer);
}
},
.unknown => {},
Expand All @@ -249,7 +247,7 @@ fn indirectionCountAndType(comptime T: type) struct {
fn encodingMatchesType(comptime T: type, expected_encoding: []const u8) !void {
var buf: [200]u8 = undefined;
const enc = Encoding.init(T);
const enc_string = try std.fmt.bufPrint(&buf, "{s}", .{enc});
const enc_string = try std.fmt.bufPrint(&buf, "{f}", .{enc});
try testing.expectEqualStrings(expected_encoding, enc_string);
}

Expand Down Expand Up @@ -400,7 +398,7 @@ test "**Union to Encoding.union encoding" {

test "Fn to Encoding.function encoding" {
const test_fn = struct {
fn add(_: c.id, _: c.SEL, _: i8) callconv(.C) void {}
fn add(_: c.id, _: c.SEL, _: i8) callconv(.c) void {}
};

try encodingMatchesType(@TypeOf(test_fn.add), "v@:c");
Expand Down
10 changes: 5 additions & 5 deletions src/msg_send.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn MsgSend(comptime T: type) type {
fn msgSendPtr(
comptime Return: type,
comptime super: bool,
) *const fn () callconv(.C) void {
) *const fn () callconv(.c) void {
// See objc/message.h. The high-level is that depending on the
// target architecture and return type, we must use a different
// objc_msgSend function.
Expand Down Expand Up @@ -204,7 +204,7 @@ fn MsgSendFn(

return @Type(.{
.@"fn" = .{
.calling_convention = .C,
.calling_convention = .c,
.is_generic = false,
.is_var_args = false,
.return_type = Return,
Expand All @@ -218,8 +218,8 @@ test {
try testing.expectEqual(fn (
c.id,
c.SEL,
) callconv(.C) u64, MsgSendFn(u64, c.id, @TypeOf(.{})));
try testing.expectEqual(fn (c.id, c.SEL, u16, u32) callconv(.C) u64, MsgSendFn(u64, c.id, @TypeOf(.{
) callconv(.c) u64, MsgSendFn(u64, c.id, @TypeOf(.{})));
try testing.expectEqual(fn (c.id, c.SEL, u16, u32) callconv(.c) u64, MsgSendFn(u64, c.id, @TypeOf(.{
@as(u16, 0),
@as(u32, 0),
})));
Expand All @@ -229,7 +229,7 @@ test "subClass" {
const Subclass = objc.allocateClassPair(objc.getClass("NSObject").?, "subclass").?;
defer objc.disposeClassPair(Subclass);
const str = struct {
fn inner(target: objc.c.id, sel: objc.c.SEL) callconv(.C) objc.c.id {
fn inner(target: objc.c.id, sel: objc.c.SEL) callconv(.c) objc.c.id {
_ = sel;
const self = objc.Object.fromId(target);
self.msgSendSuper(objc.getClass("NSObject").?, void, "init", .{});
Expand Down