ObjcRuntimeExtras.h [plain text]
#import <memory>
#import <objc/Protocol.h>
#import <objc/runtime.h>
#import <wtf/HashSet.h>
#import <wtf/SystemFree.h>
#import <wtf/Vector.h>
#import <wtf/text/CString.h>
template<typename T, typename U>
inline std::unique_ptr<T, WTF::SystemFree<T>> adoptSystem(U value)
{
return std::unique_ptr<T, WTF::SystemFree<T>>(value);
}
inline bool protocolImplementsProtocol(Protocol *candidate, Protocol *target)
{
unsigned protocolProtocolsCount;
auto protocolProtocols = adoptSystem<__unsafe_unretained Protocol*[]>(protocol_copyProtocolList(candidate, &protocolProtocolsCount));
for (unsigned i = 0; i < protocolProtocolsCount; ++i) {
if (protocol_isEqual(protocolProtocols[i], target))
return true;
}
return false;
}
inline void forEachProtocolImplementingProtocol(Class cls, Protocol *target, void (^callback)(Protocol *, bool& stop))
{
ASSERT(cls);
ASSERT(target);
Vector<Protocol*> worklist;
HashSet<void*> visited;
{
unsigned protocolsCount;
auto protocols = adoptSystem<__unsafe_unretained Protocol*[]>(class_copyProtocolList(cls, &protocolsCount));
worklist.append(protocols.get(), protocolsCount);
}
bool stop = false;
while (!worklist.isEmpty()) {
Protocol *protocol = worklist.last();
worklist.removeLast();
if (!visited.add((__bridge void*)protocol).isNewEntry)
continue;
if (protocolImplementsProtocol(protocol, target)) {
callback(protocol, stop);
if (stop)
break;
}
{
unsigned protocolsCount;
auto protocols = adoptSystem<__unsafe_unretained Protocol*[]>(protocol_copyProtocolList(protocol, &protocolsCount));
worklist.append(protocols.get(), protocolsCount);
}
}
}
inline void forEachMethodInClass(Class cls, void (^callback)(Method))
{
unsigned count;
auto methods = adoptSystem<Method[]>(class_copyMethodList(cls, &count));
for (unsigned i = 0; i < count; ++i)
callback(methods[i]);
}
inline void forEachMethodInProtocol(Protocol *protocol, BOOL isRequiredMethod, BOOL isInstanceMethod, void (^callback)(SEL, const char*))
{
unsigned count;
auto methods = adoptSystem<objc_method_description[]>(protocol_copyMethodDescriptionList(protocol, isRequiredMethod, isInstanceMethod, &count));
for (unsigned i = 0; i < count; ++i)
callback(methods[i].name, methods[i].types);
}
inline void forEachPropertyInProtocol(Protocol *protocol, void (^callback)(objc_property_t))
{
unsigned count;
auto properties = adoptSystem<objc_property_t[]>(protocol_copyPropertyList(protocol, &count));
for (unsigned i = 0; i < count; ++i)
callback(properties[i]);
}
template<char open, char close>
void skipPair(const char*& position)
{
size_t count = 1;
do {
char c = *position++;
if (!c)
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Malformed type encoding" userInfo:nil];
if (c == open)
++count;
else if (c == close)
--count;
} while (count);
}
class StringRange {
WTF_MAKE_NONCOPYABLE(StringRange);
public:
StringRange(const char* begin, const char* end)
: m_string(begin, end - begin)
{ }
operator const char*() const { return m_string.data(); }
const char* get() const { return m_string.data(); }
private:
CString m_string;
};
class StructBuffer {
WTF_MAKE_NONCOPYABLE(StructBuffer);
public:
StructBuffer(const char* encodedType)
{
NSUInteger size, alignment;
NSGetSizeAndAlignment(encodedType, &size, &alignment);
m_buffer = fastAlignedMalloc(alignment, size);
}
~StructBuffer() { fastAlignedFree(m_buffer); }
operator void*() const { return m_buffer; }
private:
void* m_buffer;
};
template<typename DelegateType>
typename DelegateType::ResultType parseObjCType(const char*& position)
{
ASSERT(*position);
switch (*position++) {
case 'c':
return DelegateType::template typeInteger<char>();
case 'i':
return DelegateType::template typeInteger<int>();
case 's':
return DelegateType::template typeInteger<short>();
case 'l':
return DelegateType::template typeInteger<long>();
case 'q':
return DelegateType::template typeDouble<long long>();
case 'C':
return DelegateType::template typeInteger<unsigned char>();
case 'I':
return DelegateType::template typeInteger<unsigned>();
case 'S':
return DelegateType::template typeInteger<unsigned short>();
case 'L':
return DelegateType::template typeInteger<unsigned long>();
case 'Q':
return DelegateType::template typeDouble<unsigned long long>();
case 'f':
return DelegateType::template typeDouble<float>();
case 'd':
return DelegateType::template typeDouble<double>();
case 'B':
return DelegateType::typeBool();
case 'v':
return DelegateType::typeVoid();
case '@': { if (position[0] == '?' && position[1] == '<') {
position += 2;
const char* begin = position;
skipPair<'<','>'>(position);
return DelegateType::typeBlock(begin, position - 1);
}
if (*position == '"') {
const char* begin = position + 1;
const char* protocolPosition = strchr(begin, '<');
const char* endOfType = strchr(begin, '"');
position = endOfType + 1;
if (!protocolPosition || protocolPosition > endOfType)
return DelegateType::typeOfClass(begin, endOfType);
if (begin == protocolPosition)
return DelegateType::typeId();
return DelegateType::typeOfClass(begin, protocolPosition);
}
return DelegateType::typeId();
}
case '{': { const char* begin = position - 1;
skipPair<'{','}'>(position);
return DelegateType::typeStruct(begin, position);
}
case '*': case '[': case '(': case 'b': case '^': case '?': case '#': case ':': default:
return nil;
}
}
extern "C" {
const char *_protocol_getMethodTypeEncoding(Protocol *, SEL, BOOL isRequiredMethod, BOOL isInstanceMethod);
id objc_initWeak(id *, id);
void objc_destroyWeak(id *);
bool _Block_has_signature(void *);
const char * _Block_signature(void *);
}