From abb0d1e931737217d40a23179e1d761ec75ac5f0 Mon Sep 17 00:00:00 2001 From: Niels Grewe Date: Sun, 6 Sep 2015 20:13:05 +0200 Subject: [PATCH 1/4] Fix returning argument types from methods. --- encoding2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/encoding2.c b/encoding2.c index 439c5ac..f4c7b25 100644 --- a/encoding2.c +++ b/encoding2.c @@ -433,7 +433,7 @@ unsigned method_get_number_of_arguments(struct objc_method *method) char* method_copyArgumentType(Method method, unsigned int index) { if (NULL == method) { return NULL; } - const char *types = findParameterStart(method->types, index); + const char *types = findParameterStart(method->types, index + 1); if (NULL == types) { return NULL; From 44627f69e70ba088913fc7bd1a57d3f56881cb8f Mon Sep 17 00:00:00 2001 From: Niels Grewe Date: Sun, 6 Sep 2015 21:39:24 +0200 Subject: [PATCH 2/4] Also fix method_getArgumentType(). Add test case. --- Test/MethodArguments.m | 91 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 Test/MethodArguments.m diff --git a/Test/MethodArguments.m b/Test/MethodArguments.m new file mode 100644 index 0000000..30dac38 --- /dev/null +++ b/Test/MethodArguments.m @@ -0,0 +1,91 @@ +#import "Test.h" +#include +#include +#include +#include + +#ifdef __has_attribute +#if __has_attribute(objc_root_class) +__attribute__((objc_root_class)) +#endif +#endif +@interface Foo +-(id)bar; +-(void)setBar:(id)b; +@end +@implementation Foo +- (id)bar +{ + return nil; +} + +- (void)setBar: (id)b +{ + return; +} +@end + +int main(void) +{ + Class foo = objc_getClass("Foo"); + Method barMethod = class_getInstanceMethod(foo, @selector(bar)); + Method setBarMethod = class_getInstanceMethod(foo,@selector(setBar:)); + char arg[16]; + + memset(&arg[0], '\0', 16 * sizeof(char)); + method_getReturnType(barMethod, &arg[0], 16); + assert(0 == strcmp(&arg[0],"@")); + + char* expected[3] = {"@", ":", "" }; + for (int i = 0; i < 3; i++) + { + memset(&arg[0], '\0', 16 * sizeof(char)); + method_getArgumentType(barMethod, i, &arg[0], 16); + assert(0 == strcmp(&arg[0],expected[i])); + } + + + memset(&arg[0], '\0', 16 * sizeof(char)); + method_getReturnType(setBarMethod, &arg[0], 16); + assert(0 == strcmp(&arg[0],"v")); + + expected[2] = "@"; + + for (int i = 0; i < 3; i++) + { + memset(&arg[0], '\0', 16 * sizeof(char)); + method_getArgumentType(setBarMethod, i, &arg[0], 16); + assert(0 == strcmp(&arg[0],expected[i])); + } + + char *arg_copied = method_copyReturnType(barMethod); + assert(0 == strcmp(arg_copied,"@")); + free(arg_copied); + arg_copied = NULL; + + for (int i = 0; i < 2; i++) + { + arg_copied = method_copyArgumentType(barMethod, i); + assert(0 == strcmp(arg_copied,expected[i])); + free(arg_copied); + } + + arg_copied = method_copyArgumentType(barMethod, 2); + assert(NULL == arg_copied); + + + + arg_copied = method_copyReturnType(setBarMethod); + assert(0 == strcmp(arg_copied,"v")); + free(arg_copied); + + for (int i = 0; i < 3; i++) + { + arg_copied = method_copyArgumentType(setBarMethod, i); + assert(0 == strcmp(arg_copied,expected[i])); + free(arg_copied); + } + + + return 0; +} From cbacd876328529f4aae9378fea4d072947bd15f8 Mon Sep 17 00:00:00 2001 From: Niels Grewe Date: Sun, 6 Sep 2015 22:20:08 +0200 Subject: [PATCH 3/4] Add missing changes --- Test/CMakeLists.txt | 1 + encoding2.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Test/CMakeLists.txt b/Test/CMakeLists.txt index 8e253ae..caafefe 100644 --- a/Test/CMakeLists.txt +++ b/Test/CMakeLists.txt @@ -26,6 +26,7 @@ set(TESTS WeakReferences_arc.m objc_msgSend.m msgInterpose.m + MethodArguments.m ) # Function for adding a test. This takes the name of the test and the list of diff --git a/encoding2.c b/encoding2.c index f4c7b25..1bcdb89 100644 --- a/encoding2.c +++ b/encoding2.c @@ -393,7 +393,7 @@ void method_getArgumentType(Method method, size_t dst_len) { if (NULL == method) { return; } - const char *types = findParameterStart(method->types, index); + const char *types = findParameterStart(method->types, index + 1); if (NULL == types) { strncpy(dst, "", dst_len); From dbf392ce99aab2650de9fbe389b81d61a1d5bb72 Mon Sep 17 00:00:00 2001 From: Niels Grewe Date: Mon, 7 Sep 2015 12:49:58 +0200 Subject: [PATCH 4/4] Move indexing fix into findParameterStart() --- encoding2.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/encoding2.c b/encoding2.c index 1bcdb89..5e73245 100644 --- a/encoding2.c +++ b/encoding2.c @@ -59,7 +59,9 @@ static char* copyTypeEncoding(const char *types) static const char * findParameterStart(const char *types, unsigned int index) { - for (unsigned int i=0 ; itypes, index + 1); + const char *types = findParameterStart(method->types, index); if (NULL == types) { strncpy(dst, "", dst_len); @@ -433,7 +435,7 @@ unsigned method_get_number_of_arguments(struct objc_method *method) char* method_copyArgumentType(Method method, unsigned int index) { if (NULL == method) { return NULL; } - const char *types = findParameterStart(method->types, index + 1); + const char *types = findParameterStart(method->types, index); if (NULL == types) { return NULL;