Skip to content

Commit a275174

Browse files
committed
Make objc_sys::IMP nullable
Probably all of the underlying APIs can handle NULL values, so not having this possibility is restricting. And most of them can sometimes _return_ a NULL value, so not handling that is unsound!
1 parent 98ae6d3 commit a275174

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

objc/src/declare.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,12 @@ impl ClassDecl {
182182
);
183183

184184
let types = method_type_encoding(&F::Ret::ENCODING, encs);
185-
let success =
186-
runtime::class_addMethod(self.cls as _, sel.as_ptr() as _, func.imp(), types.as_ptr());
185+
let success = runtime::class_addMethod(
186+
self.cls as _,
187+
sel.as_ptr() as _,
188+
Some(func.imp()),
189+
types.as_ptr(),
190+
);
187191
assert!(success != NO, "Failed to add method {:?}", sel);
188192
}
189193

@@ -213,8 +217,12 @@ impl ClassDecl {
213217

214218
let types = method_type_encoding(&F::Ret::ENCODING, encs);
215219
let metaclass = (*self.cls).metaclass() as *const _ as *mut _;
216-
let success =
217-
runtime::class_addMethod(metaclass, sel.as_ptr() as _, func.imp(), types.as_ptr());
220+
let success = runtime::class_addMethod(
221+
metaclass,
222+
sel.as_ptr() as _,
223+
Some(func.imp()),
224+
types.as_ptr(),
225+
);
218226
assert!(success != NO, "Failed to add class method {:?}", sel);
219227
}
220228

objc/src/message/gnustep.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ where
1616

1717
let receiver = obj as *mut T as *mut Object;
1818
let msg_send_fn = objc_msg_lookup(receiver as *mut _, sel.as_ptr() as *const _);
19-
objc_try!({ A::invoke(msg_send_fn, receiver, sel, args) })
19+
objc_try!({ A::invoke(msg_send_fn.expect("Null IMP"), receiver, sel, args) })
2020
}
2121

2222
pub unsafe fn send_super_unverified<T, A, R>(
@@ -36,5 +36,5 @@ where
3636
super_class: superclass as *const Class as *const _,
3737
};
3838
let msg_send_fn = objc_msg_lookup_super(&sup, sel.as_ptr() as *const _);
39-
objc_try!({ A::invoke(msg_send_fn, receiver, sel, args) })
39+
objc_try!({ A::invoke(msg_send_fn.expect("Null IMP"), receiver, sel, args) })
4040
}

objc/src/runtime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub struct Protocol(objc_sys::Protocol);
6060
pub struct Object(objc_sys::objc_object);
6161

6262
/// A pointer to the start of a method implementation.
63-
pub type Imp = objc_sys::IMP;
63+
pub type Imp = unsafe extern "C" fn();
6464

6565
impl Sel {
6666
/// Registers a method with the Objective-C runtime system,
@@ -189,7 +189,7 @@ impl Method {
189189

190190
/// Returns the implementation of self.
191191
pub fn implementation(&self) -> Imp {
192-
unsafe { method_getImplementation(self.as_ptr()) }
192+
unsafe { method_getImplementation(self.as_ptr()).expect("Null IMP") }
193193
}
194194
}
195195

objc_sys/src/class.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ extern "C" {
113113
name: *const objc_selector,
114114
imp: IMP,
115115
types: *const c_char,
116-
) -> Option<IMP>;
116+
) -> IMP;
117117
pub fn class_replaceProperty(
118118
cls: *mut objc_class,
119119
name: *const c_char,

objc_sys/src/various.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ pub struct objc_ivar {
1010
_p: OpaqueData,
1111
}
1212

13-
/// A pointer to the start of a method implementation.
13+
/// A nullable pointer to the start of a method implementation.
1414
///
15-
/// This must be non-null. Use `Option<IMP>` when nullability is desired.
16-
pub type IMP = unsafe extern "C" fn();
15+
/// Not all APIs are guaranteed to take NULL values; read the docs!
16+
pub type IMP = Option<unsafe extern "C" fn()>;
1717

1818
/// Not available on macOS x86.
1919
///

0 commit comments

Comments
 (0)