Merge pull request #11 from mitchellh/push-nssvmnpztqqs
remove usingnamespace
This commit is contained in:
commit
9b8ba849b0
9 changed files with 53 additions and 31 deletions
|
@ -1,4 +1,4 @@
|
|||
pub usingnamespace @cImport({
|
||||
pub const c = @cImport({
|
||||
@cInclude("objc/runtime.h");
|
||||
@cInclude("objc/message.h");
|
||||
});
|
||||
|
|
|
@ -1,12 +1,18 @@
|
|||
const std = @import("std");
|
||||
const assert = std.debug.assert;
|
||||
const c = @import("c.zig");
|
||||
const cpkg = @import("c.zig");
|
||||
const c = cpkg.c;
|
||||
const boolResult = cpkg.boolResult;
|
||||
const objc = @import("main.zig");
|
||||
const MsgSend = @import("msg_send.zig").MsgSend;
|
||||
|
||||
pub const Class = struct {
|
||||
value: c.Class,
|
||||
pub usingnamespace MsgSend(Class);
|
||||
|
||||
// Implement msgSend
|
||||
const msg_send = MsgSend(Class);
|
||||
pub const msgSend = msg_send.msgSend;
|
||||
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 {
|
||||
|
@ -72,7 +78,7 @@ pub const Class = struct {
|
|||
assert(fn_info.params[0].type == c.id);
|
||||
assert(fn_info.params[1].type == c.SEL);
|
||||
const encoding = comptime objc.comptimeEncode(Fn);
|
||||
return c.boolResult(@TypeOf(c.class_addMethod), c.class_addMethod(
|
||||
return boolResult(@TypeOf(c.class_addMethod), c.class_addMethod(
|
||||
self.value,
|
||||
objc.sel(name).value,
|
||||
@ptrCast(&imp),
|
||||
|
@ -85,7 +91,7 @@ pub const Class = struct {
|
|||
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 c.boolResult(@TypeOf(c.class_addIvar), result);
|
||||
return boolResult(@TypeOf(c.class_addIvar), result);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
const std = @import("std");
|
||||
const objc = @import("main.zig");
|
||||
const c = @import("c.zig");
|
||||
const c = @import("c.zig").c;
|
||||
const assert = std.debug.assert;
|
||||
const testing = std.testing;
|
||||
|
||||
|
|
38
src/main.zig
38
src/main.zig
|
@ -1,15 +1,33 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub const c = @import("c.zig");
|
||||
pub usingnamespace @import("autorelease.zig");
|
||||
pub usingnamespace @import("block.zig");
|
||||
pub usingnamespace @import("class.zig");
|
||||
pub usingnamespace @import("encoding.zig");
|
||||
pub usingnamespace @import("iterator.zig");
|
||||
pub usingnamespace @import("object.zig");
|
||||
pub usingnamespace @import("property.zig");
|
||||
pub usingnamespace @import("protocol.zig");
|
||||
pub usingnamespace @import("sel.zig");
|
||||
const autorelease = @import("autorelease.zig");
|
||||
const block = @import("block.zig");
|
||||
const class = @import("class.zig");
|
||||
const encoding = @import("encoding.zig");
|
||||
const iterator = @import("iterator.zig");
|
||||
const object = @import("object.zig");
|
||||
const property = @import("property.zig");
|
||||
const protocol = @import("protocol.zig");
|
||||
const selpkg = @import("sel.zig");
|
||||
|
||||
pub const c = @import("c.zig").c;
|
||||
pub const AutoreleasePool = autorelease.AutoreleasePool;
|
||||
pub const Block = block.Block;
|
||||
pub const Class = class.Class;
|
||||
pub const getClass = class.getClass;
|
||||
pub const getMetaClass = class.getMetaClass;
|
||||
pub const allocateClassPair = class.allocateClassPair;
|
||||
pub const registerClassPair = class.registerClassPair;
|
||||
pub const disposeClassPair = class.disposeClassPair;
|
||||
pub const Encoding = encoding.Encoding;
|
||||
pub const comptimeEncode = encoding.comptimeEncode;
|
||||
pub const Iterator = iterator.Iterator;
|
||||
pub const Object = object.Object;
|
||||
pub const Property = property.Property;
|
||||
pub const Protocol = protocol.Protocol;
|
||||
pub const getProtocol = protocol.getProtocol;
|
||||
pub const sel = selpkg.sel;
|
||||
pub const Sel = selpkg.Sel;
|
||||
|
||||
/// This just calls the C allocator free. Some things need to be freed
|
||||
/// and this is how they can be freed for objc.
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
const std = @import("std");
|
||||
const builtin = @import("builtin");
|
||||
const assert = std.debug.assert;
|
||||
const c = @import("c.zig");
|
||||
const c = @import("c.zig").c;
|
||||
const objc = @import("main.zig");
|
||||
|
||||
/// Returns a struct that implements the msgSend function for type T.
|
||||
/// This is meant to be used with `usingnamespace` to add dispatch
|
||||
/// capability to a type that supports it.
|
||||
pub fn MsgSend(comptime T: type) type {
|
||||
// 1. T should be a struct
|
||||
// 2. T should have a field "value" that can be an "id" (same size)
|
||||
|
@ -216,15 +214,12 @@ fn MsgSendFn(
|
|||
}
|
||||
|
||||
test {
|
||||
// https://github.com/ziglang/zig/issues/12360
|
||||
if (true) return error.SkipZigTest;
|
||||
|
||||
const testing = std.testing;
|
||||
try testing.expectEqual(fn (
|
||||
u8,
|
||||
objc.Sel,
|
||||
) callconv(.C) u64, MsgSendFn(u64, u8, @TypeOf(.{})));
|
||||
try testing.expectEqual(fn (u8, objc.Sel, u16, u32) callconv(.C) u64, MsgSendFn(u64, u8, @TypeOf(.{
|
||||
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(.{
|
||||
@as(u16, 0),
|
||||
@as(u32, 0),
|
||||
})));
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const std = @import("std");
|
||||
const c = @import("c.zig");
|
||||
const c = @import("c.zig").c;
|
||||
const objc = @import("main.zig");
|
||||
const MsgSend = @import("msg_send.zig").MsgSend;
|
||||
const Iterator = @import("iterator.zig").Iterator;
|
||||
|
@ -8,7 +8,10 @@ const Iterator = @import("iterator.zig").Iterator;
|
|||
pub const Object = struct {
|
||||
value: c.id,
|
||||
|
||||
pub usingnamespace MsgSend(Object);
|
||||
// Implement msgSend
|
||||
const msg_send = MsgSend(Object);
|
||||
pub const msgSend = msg_send.msgSend;
|
||||
pub const msgSendSuper = msg_send.msgSendSuper;
|
||||
|
||||
/// Convert a raw "id" into an Object. id must fit the size of the
|
||||
/// normal C "id" type (i.e. a `usize`).
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const std = @import("std");
|
||||
const c = @import("c.zig");
|
||||
const c = @import("c.zig").c;
|
||||
const objc = @import("main.zig");
|
||||
|
||||
pub const Property = extern struct {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const std = @import("std");
|
||||
const c = @import("c.zig");
|
||||
const c = @import("c.zig").c;
|
||||
const objc = @import("main.zig");
|
||||
|
||||
pub const Protocol = extern struct {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const std = @import("std");
|
||||
const c = @import("c.zig");
|
||||
const c = @import("c.zig").c;
|
||||
|
||||
// Shorthand, equivalent to Sel.registerName
|
||||
pub inline fn sel(name: [:0]const u8) Sel {
|
||||
|
|
Loading…
Add table
Reference in a new issue