fix!: avoid "comptime var" error by returning array instead of slice
this changes the semantics of `comptimeEncode`; users will need to, e.g. use ```zig const encoding = comptimeEncode(T); passToObjc(&encoding) ``` to pass the encoding by pointer rather than by value.
This commit is contained in:
parent
77bb670be5
commit
8113ef70e0
5 changed files with 23 additions and 11 deletions
|
@ -5,7 +5,11 @@ pub fn build(b: *std.Build) void {
|
||||||
const target = b.standardTargetOptions(.{});
|
const target = b.standardTargetOptions(.{});
|
||||||
const add_paths = b.option(bool, "add-paths", "add macos SDK paths from dependency") orelse false;
|
const add_paths = b.option(bool, "add-paths", "add macos SDK paths from dependency") orelse false;
|
||||||
|
|
||||||
const objc = b.addModule("objc", .{ .root_source_file = .{ .path = "src/main.zig" } });
|
const objc = b.addModule("objc", .{
|
||||||
|
.root_source_file = .{ .path = "src/main.zig" },
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
if (add_paths) @import("macos_sdk").addPathsModule(objc);
|
if (add_paths) @import("macos_sdk").addPathsModule(objc);
|
||||||
objc.linkSystemLibrary("objc", .{});
|
objc.linkSystemLibrary("objc", .{});
|
||||||
objc.linkFramework("Foundation", .{});
|
objc.linkFramework("Foundation", .{});
|
||||||
|
|
|
@ -10,8 +10,8 @@
|
||||||
},
|
},
|
||||||
.dependencies = .{
|
.dependencies = .{
|
||||||
.macos_sdk = .{
|
.macos_sdk = .{
|
||||||
.url = "https://github.com/mitchellh/zig-build-macos-sdk/archive/4186e9fd445d12041651abe59ea5f396499b0844.tar.gz",
|
.url = "https://github.com/mitchellh/zig-build-macos-sdk/archive/ee70f27c08680307fa35ada92e6b2c36e0ff84c6.tar.gz",
|
||||||
.hash = "1220bc2612b57b0cfaaecbcac38e3144e5a9362ff668d71eb8334e895047bdbb7148",
|
.hash = "1220b415f529f1c04ed876c2b481e9f8119d353d4e3d4d7c8607ee302d2142e13eca",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,7 @@ pub fn Block(
|
||||||
.size = @sizeOf(Context),
|
.size = @sizeOf(Context),
|
||||||
.copy_helper = &descCopyHelper,
|
.copy_helper = &descCopyHelper,
|
||||||
.dispose_helper = &descDisposeHelper,
|
.dispose_helper = &descDisposeHelper,
|
||||||
.signature = objc.comptimeEncode(InvokeFn).ptr,
|
.signature = &objc.comptimeEncode(InvokeFn),
|
||||||
};
|
};
|
||||||
|
|
||||||
/// This is the function type that is called back.
|
/// This is the function type that is called back.
|
||||||
|
|
|
@ -76,7 +76,7 @@ pub const Class = struct {
|
||||||
self.value,
|
self.value,
|
||||||
objc.sel(name).value,
|
objc.sel(name).value,
|
||||||
@ptrCast(&imp),
|
@ptrCast(&imp),
|
||||||
encoding.ptr,
|
&encoding,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,22 +4,30 @@ const c = @import("c.zig");
|
||||||
const assert = std.debug.assert;
|
const assert = std.debug.assert;
|
||||||
const testing = std.testing;
|
const testing = std.testing;
|
||||||
|
|
||||||
/// Encode a type into a comptime string.
|
/// how much space do we need to encode this type?
|
||||||
pub fn comptimeEncode(comptime T: type) [:0]const u8 {
|
fn comptimeN(comptime T: type) usize {
|
||||||
comptime {
|
comptime {
|
||||||
const encoding = objc.Encoding.init(T);
|
const encoding = objc.Encoding.init(T);
|
||||||
|
|
||||||
// Figure out how much space we need
|
// Figure out how much space we need
|
||||||
var counting = std.io.countingWriter(std.io.null_writer);
|
var counting = std.io.countingWriter(std.io.null_writer);
|
||||||
try std.fmt.format(counting.writer(), "{}", .{encoding});
|
try std.fmt.format(counting.writer(), "{}", .{encoding});
|
||||||
|
return counting.bytes_written;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Encode a type into a comptime string.
|
||||||
|
pub fn comptimeEncode(comptime T: type) [comptimeN(T):0]u8 {
|
||||||
|
comptime {
|
||||||
|
const encoding = objc.Encoding.init(T);
|
||||||
|
|
||||||
// Build our final signature
|
// Build our final signature
|
||||||
var buf: [counting.bytes_written + 1]u8 = undefined;
|
var buf: [comptimeN(T) + 1]u8 = undefined;
|
||||||
var fbs = std.io.fixedBufferStream(buf[0..counting.bytes_written]);
|
var fbs = std.io.fixedBufferStream(buf[0 .. buf.len - 1]);
|
||||||
try std.fmt.format(fbs.writer(), "{}", .{encoding});
|
try std.fmt.format(fbs.writer(), "{}", .{encoding});
|
||||||
buf[counting.bytes_written] = 0;
|
buf[buf.len - 1] = 0;
|
||||||
|
|
||||||
return buf[0..counting.bytes_written :0];
|
return buf[0 .. buf.len - 1 :0].*;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue