Hi guys,
Just wanted to chime in to say, NO!!! I never really did get blocks working (except maybe one time when it was firing and then crashing the engine). Nor did I ever really fully understand them.
So a little background: a Block (denoted by the ^ symbol) is something that Apple added to Objective-C in Snow Leopard (macOS 10.6+) but from what I understand it isn't a concept that is unique to ObjC, it can be used with C-Foundation APIs (CoreMIDI) or C++ too and has been implemented in different ways in other languages as well. From what I understand, it's an encapsulated block of code, either separately from or inline within a larger chunk of code, similar to an OOP object but it can also be inline, and it can include local-scoped variables/data and can passed or substituted for a parameter variable. It seems that a bunch of Apple's API's want to use them for processing of callbacks. The actual structure of these ^blocks from a compiler's view looks like this:
Code: Select all
struct Block_literal_1 {
void *isa; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock --- so a Pointer to your Handler goes here
int flags; // -- flags bitfield as an integer
int reserved; // another integer, probably ok to fill with null
void (*invoke)(void *, ...);
// -- another structure that describes the block:
struct Block_descriptor_1 {
unsigned long int reserved; // NULL long integer
unsigned long int size; // sizeof(struct Block_literal_1) -- the size of the first block as a long integer
// optional helper functions
void (*copy_helper)(void *dst, void *src); // IFF (1<<25)
void (*dispose_helper)(void *src); // IFF (1<<25)
// required ABI.2010.3.16
const char *signature; // IFF (1<<30)
} *descriptor;
// imported variables
};
Further Reading:
https://clang.llvm.org/docs/Block-ABI-Apple.html
A while back Ali put together some Hacky code to wrap LiveCode Builder handlers into a Block Structure that could be passed to these Apple APIs that require them. If I remember correctly Monte then refined it a bit into somewhat of a block-handling library. This all was posted in LiveCode's chatroom on GitHub's
Gitter.im site, you'd probably have to scroll for about a half a mile to see all the relevant comments and code fragments. I tried to collect it all into some collection of code for trying to decipher better at a later date. So far that date has not arrived. I've been hoping they'd formally add these ^block wrapping handlers to the Objective C LCB module at some point, and then hopefully I wouldn't have to try to grok it anymore.
What I've been trying to find with the work I was doing with Apple's C (CF CoreFoundation) based CoreMIDI API is early versions of certain functions prior to when ^Blocks were introduced to the language to see what the equivalencies might be. It seems that ^Blocks were a popular choice to replace Delegates.