diff --git a/BlockHook/BlockHook.m b/BlockHook/BlockHook.m index a0598e3..0800e5c 100644 --- a/BlockHook/BlockHook.m +++ b/BlockHook/BlockHook.m @@ -192,7 +192,9 @@ static void BHFFIClosureFunc(ffi_cif *cif, void *ret, void **args, void *userdat { BHToken *token = (__bridge BHToken *)(userdata); if (token.hasStret) { - token.retValue = args[0]; + // The first arg contains address of a pointer of returned struct. + token.retValue = *((void **)args[0]); + // Other args move backwards. token.args = args + 1; } else { diff --git a/BlockHookSample iOS/ViewController.m b/BlockHookSample iOS/ViewController.m index a6ebe5c..4ec93dd 100644 --- a/BlockHookSample iOS/ViewController.m +++ b/BlockHookSample iOS/ViewController.m @@ -16,10 +16,4 @@ - (void)viewDidLoad { // Do any additional setup after loading the view, typically from a nib. } -- (void)didReceiveMemoryWarning { - [super didReceiveMemoryWarning]; - // Dispose of any resources that can be recreated. -} - - @end diff --git a/BlockHookSample iOSTests/BlockHookSample_iOSTests.m b/BlockHookSample iOSTests/BlockHookSample_iOSTests.m index 3a994e0..e2557a3 100644 --- a/BlockHookSample iOSTests/BlockHookSample_iOSTests.m +++ b/BlockHookSample iOSTests/BlockHookSample_iOSTests.m @@ -62,7 +62,7 @@ struct TestStruct (^StructReturnBlock)(int) = ^(int x) [StructReturnBlock block_hookWithMode:BlockHookModeInstead usingBlock:^(BHToken *token, int x){ [token invokeOriginalBlock]; - (**(struct TestStruct **)(token.retValue)).a = 100; + (*(struct TestStruct *)(token.retValue)).a = 100; NSAssert(x == 8, @"Wrong arg!"); }]; diff --git a/BlockHookSample macOS/ViewController.m b/BlockHookSample macOS/ViewController.m index e1f4d5a..948367c 100644 --- a/BlockHookSample macOS/ViewController.m +++ b/BlockHookSample macOS/ViewController.m @@ -9,43 +9,10 @@ #import "ViewController.h" #import -struct Block_descriptor { - void *reserved; - uintptr_t size; -}; - -struct Block_layout { - void *isa; - int32_t flags; // contains ref count - int32_t reserved; - void *invoke; - struct Block_descriptor *descriptor; -}; @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; - struct Block_layout (^testblock3)(int) = ^(int a) - { - NSLog(@"This is a Global block for stret"); - return (struct Block_layout){0,1,2,0,0}; - }; - [testblock3 block_hookWithMode:BlockHookModeInstead usingBlock:^(BHToken *token, int a){ - [token invokeOriginalBlock]; - struct Block_layout lala = **(struct Block_layout **)(token.retValue); - NSLog(@"lala flag:%d", lala.reserved); - NSLog(@"arg a:%d", a); - }]; - struct Block_layout result = testblock3(100); - result.flags; -} - - -- (void)setRepresentedObject:(id)representedObject { - [super setRepresentedObject:representedObject]; - - // Update the view, if already loaded. } - @end diff --git a/BlockHookSample macOSTests/BlockHookSample_macOSTests.m b/BlockHookSample macOSTests/BlockHookSample_macOSTests.m index 7a22b33..9c3fc7c 100644 --- a/BlockHookSample macOSTests/BlockHookSample_macOSTests.m +++ b/BlockHookSample macOSTests/BlockHookSample_macOSTests.m @@ -67,7 +67,7 @@ struct TestStruct (^StructReturnBlock)(int) = ^(int x) [StructReturnBlock block_hookWithMode:BlockHookModeInstead usingBlock:^(BHToken *token, int x){ [token invokeOriginalBlock]; - (**(struct TestStruct **)(token.retValue)).a = 100; + (*(struct TestStruct *)(token.retValue)).a = 100; NSAssert(x == 8, @"Wrong arg!"); }];