Skip to content

Commit

Permalink
Fail-Fast on Redundant Callback Invokes
Browse files Browse the repository at this point in the history
Reviewed By: javache

Differential Revision: D4295268

fbshipit-source-id: 1258ffbc02bcf7d7199348c7df8fcd744bb9963f
  • Loading branch information
theoy authored and Facebook Github Bot committed Dec 8, 2016
1 parent 9dcea13 commit 4a6f2ec
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions React/Base/RCTModuleMethod.m
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ static RCTNullability RCTParseNullabilityPostfix(const char **input)
return RCTNullabilityUnspecified;
}

// returns YES if execution is safe to proceed (enqueue callback invocation), NO if callback has already been invoked
static BOOL RCTCheckCallbackMultipleInvocations(BOOL *didInvoke) {
if (*didInvoke) {
RCTFatal(RCTErrorWithMessage(@"Illegal callback invocation from native module. This callback type only permits a single invocation from native code."));
return NO;
} else {
*didInvoke = YES;
return YES;
}
}

SEL RCTParseMethodSignature(NSString *, NSArray<RCTMethodArgument *> **);
SEL RCTParseMethodSignature(NSString *methodSignature, NSArray<RCTMethodArgument *> **arguments)
{
Expand Down Expand Up @@ -205,8 +216,11 @@ - (void)processMethodSignature
return NO;
}

__block BOOL didInvoke = NO;
RCT_BLOCK_ARGUMENT(^(NSArray *args) {
[bridge enqueueCallback:json args:args];
if (RCTCheckCallbackMultipleInvocations(&didInvoke)) {
[bridge enqueueCallback:json args:args];
}
});
)
};
Expand Down Expand Up @@ -302,8 +316,11 @@ - (void)processMethodSignature
return NO;
}

__block BOOL didInvoke = NO;
RCT_BLOCK_ARGUMENT(^(NSError *error) {
[bridge enqueueCallback:json args:@[RCTJSErrorFromNSError(error)]];
if (RCTCheckCallbackMultipleInvocations(&didInvoke)) {
[bridge enqueueCallback:json args:@[RCTJSErrorFromNSError(error)]];
}
});
)
} else if ([typeName isEqualToString:@"RCTPromiseResolveBlock"]) {
Expand All @@ -316,8 +333,11 @@ - (void)processMethodSignature
return NO;
}

__block BOOL didInvoke = NO;
RCT_BLOCK_ARGUMENT(^(id result) {
[bridge enqueueCallback:json args:result ? @[result] : @[]];
if (RCTCheckCallbackMultipleInvocations(&didInvoke)) {
[bridge enqueueCallback:json args:result ? @[result] : @[]];
}
});
)
} else if ([typeName isEqualToString:@"RCTPromiseRejectBlock"]) {
Expand All @@ -330,9 +350,12 @@ - (void)processMethodSignature
return NO;
}

__block BOOL didInvoke = NO;
RCT_BLOCK_ARGUMENT(^(NSString *code, NSString *message, NSError *error) {
NSDictionary *errorJSON = RCTJSErrorFromCodeMessageAndNSError(code, message, error);
[bridge enqueueCallback:json args:@[errorJSON]];
if (RCTCheckCallbackMultipleInvocations(&didInvoke)) {
NSDictionary *errorJSON = RCTJSErrorFromCodeMessageAndNSError(code, message, error);
[bridge enqueueCallback:json args:@[errorJSON]];
}
});
)
} else {
Expand Down

0 comments on commit 4a6f2ec

Please sign in to comment.