Skip to content

Commit

Permalink
improved blocks and generics support
Browse files Browse the repository at this point in the history
  • Loading branch information
pdoane committed Sep 20, 2023
1 parent fbd5d65 commit 720676d
Show file tree
Hide file tree
Showing 4 changed files with 443 additions and 400 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ See https://machengine.org/pkg/mach-objc

Use `generate.sh` to regenerate the source files. Only `metal/mtl.zig` can be regenerated at this point, but other frameworks may be added later.

Methods using generic types (e.g. `ns.Array`) or inline block types are not being translated correctly and are currently commented out by hand. This will be updated soon in mach-objc-generator.

## Issues

Issues are tracked in the [main Mach repository](https://github.com/hexops/mach/issues?q=is%3Aissue+is%3Aopen+label%3Aobjc).
Expand Down
58 changes: 1 addition & 57 deletions mtl_manual.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,77 +9,22 @@ pub const dispatch_data_t = *opaque {};
pub const dispatch_queue_t = *opaque {};
pub const IOSurfaceRef = *opaque {};

// ------------------------------------------------------------------------------------------------
// Blocks

// TODO - can we use definition in ns
extern const _NSConcreteStackBlock: *anyopaque;
extern fn _Block_copy(block: *const anyopaque) callconv(.C) *anyopaque;
extern fn _Block_release(block: *const anyopaque) callconv(.C) void;

const BlockDescriptor = extern struct {
reserved: c_ulong,
size: c_ulong,
};

const BlockLiteral = extern struct {
isa: *anyopaque,
flags: c_int,
reserved: c_int,
invoke: *const fn () callconv(.C) void,
descriptor: *const BlockDescriptor,
};

const block_descriptor = BlockDescriptor{ .reserved = 0, .size = @sizeOf(BlockLiteral) };

// TODO - variable capture
pub fn Block(comptime FuncType: type) type {
return opaque {
const Self = @This();

pub fn init(comptime invoke: FuncType) *Self {
const block = BlockLiteral{
.isa = _NSConcreteStackBlock,
.flags = 0,
.reserved = 0,
.invoke = @as(*const fn () callconv(.C) void, @ptrCast(invoke)),
.descriptor = &block_descriptor,
};
return _Block_copy(&block);
}
};
}

// ------------------------------------------------------------------------------------------------
// Types

// MTLCommandBuffer.hpp
pub const CommandBufferHandler = *Block(*const fn (*anyopaque, *CommandBuffer) callconv(.C) void);

// MTLCounters.hpp
pub const CommonCounter = *ns.String;
pub const CommonCounterSet = *ns.String;

// MTLDevice.hpp
pub const DeviceNotificationName = *ns.String;
pub const DeviceNotificationHandlerBlock = *Block(fn (*anyopaque, *Device, DeviceNotificationName) callconv(.C) void);
pub const NewLibraryCompletionHandler = *Block(fn (*anyopaque, *Library, *ns.Error) callconv(.C) void);
pub const NewRenderPipelineStateCompletionHandler = *Block(fn (*anyopaque, *RenderPipelineState, *ns.Error) callconv(.C) void);
pub const NewRenderPipelineStateWithReflectionCompletionHandler = *Block(fn (*anyopaque, *RenderPipelineState, *RenderPipelineReflection, *ns.Error) callconv(.C) void);
pub const NewComputePipelineStateCompletionHandler = *Block(fn (*anyopaque, *ComputePipelineState, *ns.Error) callconv(.C) void);
pub const NewComputePipelineStateWithReflectionCompletionHandler = *Block(fn (*anyopaque, *ComputePipelineState, *ComputePipelineReflection, *ns.Error) callconv(.C) void);
pub const AutoreleasedComputePipelineReflection = *ComputePipelineReflection;
pub const AutoreleasedRenderPipelineReflection = *RenderPipelineReflection;
pub const Timestamp = u64;

// MTLIOCommandBuffer.hpp
pub const IOCommandBufferHandler = *Block(fn (*anyopaque, *IOCommandBuffer) callconv(.C) void);

// MTLDrawable.hpp
pub const DrawablePresentedHandler = *Block(fn (*anyopaque, *Drawable) callconv(.C) void);

// MTLEvent.hpp
pub const SharedEventNotificationBlock = *Block(fn (*anyopaque, *SharedEvent, u64) callconv(.C) void);

// MTLLibrary.hpp
pub const AutoreleasedArgument = *Argument;

Expand Down Expand Up @@ -291,5 +236,4 @@ pub const SizeAndAlign = extern struct {
size: ns.UInteger,
@"align": ns.UInteger,
};

// ------------------------------------------------------------------------------------------------
21 changes: 21 additions & 0 deletions src/foundation/ns.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@ pub const TimeInterval = f64;
pub const UInteger = usize;
pub const unichar = u16;

// ------------------------------------------------------------------------------------------------
// Blocks

extern const _NSConcreteStackBlock: *anyopaque;

pub const BlockDescriptor = extern struct {
reserved: c_ulong,
size: c_ulong,
};

pub fn BlockLiteral(comptime Context: type) type {
return extern struct {
isa: *anyopaque,
flags: c_int,
reserved: c_int,
invoke: *const fn () callconv(.C) void,
descriptor: *const BlockDescriptor,
context: Context,
};
}

// ------------------------------------------------------------------------------------------------
// Enumerations

Expand Down
Loading

0 comments on commit 720676d

Please sign in to comment.