Skip to content

fix(objc-call): Don't crash when calling an optional protocol method #993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/NativeScript/Metadata/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ enum MetaFlags {
FunctionReturnsUnmanaged = 3,
FunctionIsVariadic = 5,
FunctionOwnsReturnedCocoaObject = 4,
MemberIsOptional = 0, // Mustn't equal any Method or Property flag since it can be applicable to both
MethodIsInitializer = 1,
MethodIsVariadic = 2,
MethodIsNullTerminatedVariadic = 3,
MethodOwnsReturnedCocoaObject = 4,
MethodHasErrorOutParameter = 5,
PropertyHasGetter = 2,
PropertyHasSetter = 3
PropertyHasSetter = 3,

};

/// This enum describes the possible ObjectiveC entity types.
Expand Down Expand Up @@ -581,6 +583,9 @@ struct VarMeta : Meta {
};

struct MemberMeta : Meta {
bool isOptional() const {
return this->flag(MetaFlags::MemberIsOptional);
}
};

struct MethodMeta : MemberMeta {
Expand Down
2 changes: 2 additions & 0 deletions src/NativeScript/ObjC/ObjCMethodCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class ObjCMethodCall : public FFICall {

bool _hasErrorOutParameter;

bool _isOptional;

bool _isInitializer;

SEL _selector;
Expand Down
8 changes: 7 additions & 1 deletion src/NativeScript/ObjC/ObjCMethodCall.mm
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

Base::initializeFFI(vm, { &preInvocation, &postInvocation }, returnTypeCell, parameterTypesCells, 2);
this->_retainsReturnedCocoaObjects = metadata->ownsReturnedCocoaObject();
this->_isOptional = metadata->isOptional();
this->_isInitializer = metadata->isInitializer();
this->_hasErrorOutParameter = metadata->hasErrorOutParameter();

Expand Down Expand Up @@ -130,7 +131,12 @@ static bool isJavaScriptDerived(JSC::JSValue value) {
bool isInstance = !class_isMetaClass(object_getClass(target));
NSLog(@"> %@[%@ %@]", isInstance ? @"-" : @"+", NSStringFromClass(object_getClass(target)), NSStringFromSelector(call->_selector));
#endif
invocation.setArgument(0, target);
if (call->_isOptional && ![target respondsToSelector:call->_selector]) {
// Unimplemented optional method: Silently perform a dummy call to nil object
invocation.setArgument(0, nil);
} else {
invocation.setArgument(0, target);
}
invocation.setArgument(1, call->_selector);
invocation.function = call->_msgSend;
}
Expand Down