Skip to content

Commit

Permalink
Fix return struct bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
yulingtianxia committed Apr 27, 2019
1 parent 876f92e commit 04525fc
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 42 deletions.
4 changes: 3 additions & 1 deletion BlockHook/BlockHook.m
Expand Up @@ -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 {
Expand Down
6 changes: 0 additions & 6 deletions BlockHookSample iOS/ViewController.m
Expand Up @@ -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
2 changes: 1 addition & 1 deletion BlockHookSample iOSTests/BlockHookSample_iOSTests.m
Expand Up @@ -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!");
}];

Expand Down
33 changes: 0 additions & 33 deletions BlockHookSample macOS/ViewController.m
Expand Up @@ -9,43 +9,10 @@
#import "ViewController.h"
#import <BlockHook/BlockHook.h>

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
2 changes: 1 addition & 1 deletion BlockHookSample macOSTests/BlockHookSample_macOSTests.m
Expand Up @@ -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!");
}];

Expand Down

0 comments on commit 04525fc

Please sign in to comment.