Skip to content

Commit

Permalink
从方法中传入的全局和栈block copy到堆
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverFruity committed May 31, 2021
1 parent aecf944 commit 0a6dbab
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion OCRunner.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "OCRunner"
s.version = "1.0.17"
s.version = "1.0.18"
s.summary = "OCRunner"
s.description = <<-DESC
Execute Objective-C code Dynamically. iOS hotfix SDK.
Expand Down
16 changes: 12 additions & 4 deletions OCRunner/ORCoreImp/ORCoreImp.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@ void methodIMP(ffi_cif *cfi,void *ret,void **args, void*userdata){
for (NSUInteger i = 2; i < sig.numberOfArguments; i++) {
MFValue *argValue = [[MFValue alloc] initTypeEncode:[sig getArgumentTypeAtIndex:i] pointer:args[i]];
//针对系统传入的block,检查一次签名,如果没有,将在结构体中添加签名信息.
if (argValue.isObject && argValue.isBlockValue && argValue.objectValue != nil && NSBlockHasSignature(argValue.objectValue) == NO) {
ORTypeVarPair *blockdecl = methodImp.declare.parameterTypes[i - 2];
if ([blockdecl.var isKindOfClass:[ORFuncVariable class]]) {
NSBlockSetSignature(argValue.objectValue, blockdecl.blockSignature);
if (argValue.isObject && argValue.isBlockValue && argValue.objectValue != nil) {
struct MFSimulateBlock *bb = (void *)argValue->realBaseValue.pointerValue;
// 针对传入的block,如果为全局block或栈block,使用copy转换为堆block
if (bb->isa == &_NSConcreteGlobalBlock || bb->isa == &_NSConcreteStackBlock){
id copied = (__bridge id)Block_copy(argValue->realBaseValue.pointerValue);
argValue.pointer = &copied;
}
if (NSBlockHasSignature(argValue.objectValue) == NO) {
ORTypeVarPair *blockdecl = methodImp.declare.parameterTypes[i - 2];
if ([blockdecl.var isKindOfClass:[ORFuncVariable class]]) {
NSBlockSetSignature(argValue.objectValue, blockdecl.blockSignature);
}
}
}
[argValues addObject:argValue];
Expand Down
11 changes: 2 additions & 9 deletions OCRunner/RunEnv/MFValue.m
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,8 @@ - (void)setModifier:(DeclarationModifier)modifier{
// 引用计数-1
_strongObjectValue = nil;
}else if (modifier & (DeclarationModifierNone | DeclarationModifierStrong)){
do {
// 针对传入的全局block和栈block,不增加引用计数
if (self.isBlockValue && realBaseValue.pointerValue){
struct MFSimulateBlock *bb = (void *)realBaseValue.pointerValue;
if (bb->isa == &_NSConcreteGlobalBlock || bb->isa == &_NSConcreteStackBlock) break;
}
// 引用计数+1
_strongObjectValue = (__bridge id)(realBaseValue.pointerValue);
} while (0);
// 引用计数+1
_strongObjectValue = (__bridge id)(realBaseValue.pointerValue);
}
}
_modifier = modifier;
Expand Down
2 changes: 1 addition & 1 deletion OCRunnerArm64.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "OCRunnerArm64"
s.version = "1.0.17"
s.version = "1.0.18"
s.summary = "OCRunnerArm64"
s.description = <<-DESC
Only Support Arm64, Execute Objective-C code Dynamically. iOS hotfix SDK.
Expand Down
5 changes: 4 additions & 1 deletion OCRunnerDemo/OCRunnerDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ - (void)sendStackBlock{
NSLog(@"%@",weakSelf);
NSLog(@"%@",str);
}];
[self receiveStackBlock:^(NSString *str) {
NSLog(@"global block %@",str);
}];
}
- (void)receiveStackBlock:(void (^)(NSString *str))block{
if (block) {
if (block) {
block(@"123");
}
}
Expand Down

0 comments on commit 0a6dbab

Please sign in to comment.