From da5e411d242f9b784ea584564e6e470e9ee5dc6d Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Mon, 7 Jan 2019 09:06:16 +0000 Subject: [PATCH] Add two Apple-compatible block introspection functions. --- blocks_runtime.m | 13 +++++++++++-- objc/blocks_runtime.h | 7 +++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/blocks_runtime.m b/blocks_runtime.m index bda2f00..233f3e0 100644 --- a/blocks_runtime.m +++ b/blocks_runtime.m @@ -39,12 +39,17 @@ static void *_HeapBlockByRef = (void*)1; +PUBLIC _Bool _Block_has_signature(id b) +{ + const struct Block_layout *block = (struct Block_layout*)b; + return ((NULL != block) && (block->flags & BLOCK_HAS_SIGNATURE)); +} /** * Returns the Objective-C type encoding for the block. */ -PUBLIC const char *block_getType_np(const void *b) +PUBLIC const char * _Block_signature(id b) { - const struct Block_layout *block = b; + const struct Block_layout *block = (struct Block_layout*)b; if ((NULL == block) || !(block->flags & BLOCK_HAS_SIGNATURE)) { return NULL; @@ -55,6 +60,10 @@ PUBLIC const char *block_getType_np(const void *b) } return block->descriptor->encoding; } +PUBLIC const char *block_getType_np(const void *b) +{ + return _Block_signature(b); +} static int increment24(int *ref) { diff --git a/objc/blocks_runtime.h b/objc/blocks_runtime.h index 49702f4..5dd762e 100644 --- a/objc/blocks_runtime.h +++ b/objc/blocks_runtime.h @@ -16,6 +16,13 @@ PUBLIC BLOCKS_EXPORT void *_Block_copy(const void *); PUBLIC BLOCKS_EXPORT void _Block_release(const void *); PUBLIC BLOCKS_EXPORT const char *block_getType_np(const void *b) OBJC_NONPORTABLE; +#ifdef __OBJC__ +PUBLIC BLOCKS_EXPORT _Bool _Block_has_signature(id); +PUBLIC BLOCKS_EXPORT const char * _Block_signature(id); +#else +PUBLIC BLOCKS_EXPORT _Bool _Block_has_signature(void *); +PUBLIC BLOCKS_EXPORT const char * _Block_signature(void *); +#endif #define Block_copy(x) ((__typeof(x))_Block_copy((const void *)(x))) #define Block_release(x) _Block_release((const void *)(x))