|
| 1 | +use core::mem::{size_of, ManuallyDrop}; |
| 2 | +use std::os::raw::c_int; |
| 3 | + |
| 4 | +use objc2::rc::{autoreleasepool, AutoreleasePool, Id, Owned}; |
| 5 | +use objc2::runtime::{Bool, Class, Object, Protocol}; |
| 6 | +#[cfg(feature = "malloc")] |
| 7 | +use objc2::sel; |
| 8 | +use objc2::{class, msg_send, msg_send_bool}; |
| 9 | +use objc2_foundation::NSObject; |
| 10 | + |
| 11 | +#[repr(C)] |
| 12 | +struct MyTestObject { |
| 13 | + inner: NSObject, |
| 14 | +} |
| 15 | + |
| 16 | +unsafe impl ::objc2::Message for MyTestObject {} |
| 17 | + |
| 18 | +unsafe impl ::objc2::RefEncode for MyTestObject { |
| 19 | + const ENCODING_REF: ::objc2::Encoding<'static> = ::objc2::Encoding::Object; |
| 20 | +} |
| 21 | + |
| 22 | +impl MyTestObject { |
| 23 | + fn class() -> &'static Class { |
| 24 | + class!(MyTestObject) |
| 25 | + } |
| 26 | + |
| 27 | + fn new() -> Id<Self, Owned> { |
| 28 | + let cls = Self::class(); |
| 29 | + unsafe { Id::new(msg_send![cls, new]).unwrap() } |
| 30 | + } |
| 31 | + |
| 32 | + fn new_autoreleased<'p>(pool: &'p AutoreleasePool) -> &'p Self { |
| 33 | + let cls = Self::class(); |
| 34 | + let ptr: *const Self = unsafe { msg_send![cls, getAutoreleasedInstance] }; |
| 35 | + unsafe { pool.ptr_as_ref(ptr) } |
| 36 | + } |
| 37 | + |
| 38 | + fn add_numbers(a: c_int, b: c_int) -> c_int { |
| 39 | + let cls = Self::class(); |
| 40 | + unsafe { msg_send![cls, add: a, and: b] } |
| 41 | + } |
| 42 | + |
| 43 | + fn var1(&self) -> c_int { |
| 44 | + unsafe { msg_send![self, var1] } |
| 45 | + } |
| 46 | + |
| 47 | + fn var1_ivar(&self) -> &c_int { |
| 48 | + unsafe { self.inner.ivar("var1") } |
| 49 | + } |
| 50 | + |
| 51 | + fn var1_ivar_mut(&mut self) -> &mut c_int { |
| 52 | + unsafe { self.inner.ivar_mut("var1") } |
| 53 | + } |
| 54 | + |
| 55 | + fn add_to_ivar1(&mut self, number: c_int) { |
| 56 | + unsafe { msg_send![self, addToVar1: number] } |
| 57 | + } |
| 58 | + |
| 59 | + fn var2(&self) -> bool { |
| 60 | + unsafe { msg_send_bool![self, var2] } |
| 61 | + } |
| 62 | + |
| 63 | + fn var2_ivar(&self) -> &Bool { |
| 64 | + unsafe { self.inner.ivar("var2") } |
| 65 | + } |
| 66 | + |
| 67 | + fn var2_ivar_mut(&mut self) -> &mut Bool { |
| 68 | + unsafe { self.inner.ivar_mut("var2") } |
| 69 | + } |
| 70 | + |
| 71 | + fn var3(&self) -> *mut Object { |
| 72 | + unsafe { msg_send![self, var3] } |
| 73 | + } |
| 74 | + |
| 75 | + fn set_var3(&mut self, obj: *mut Object) { |
| 76 | + unsafe { msg_send![self, setVar3: obj] } |
| 77 | + } |
| 78 | + |
| 79 | + fn var3_ivar(&self) -> &*mut Object { |
| 80 | + unsafe { self.inner.ivar("var3") } |
| 81 | + } |
| 82 | + |
| 83 | + fn var3_ivar_mut(&mut self) -> &mut *mut Object { |
| 84 | + unsafe { self.inner.ivar_mut("var3") } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +#[cfg(feature = "malloc")] |
| 89 | +macro_rules! assert_in { |
| 90 | + ($item:expr, $lst:expr) => {{ |
| 91 | + let mut found = false; |
| 92 | + for &x in $lst.iter() { |
| 93 | + if x == $item { |
| 94 | + found = true; |
| 95 | + } |
| 96 | + } |
| 97 | + assert!( |
| 98 | + found, |
| 99 | + "Did not find {} in {}", |
| 100 | + stringify!($item), |
| 101 | + stringify!($lst), |
| 102 | + ); |
| 103 | + }}; |
| 104 | +} |
| 105 | + |
| 106 | +#[test] |
| 107 | +fn test_class() { |
| 108 | + let cls = MyTestObject::class(); |
| 109 | + assert_eq!(MyTestObject::add_numbers(-3, 15), 12); |
| 110 | + |
| 111 | + #[cfg(feature = "malloc")] |
| 112 | + { |
| 113 | + let classes = Class::classes(); |
| 114 | + assert_eq!(classes.len(), Class::classes_count()); |
| 115 | + assert_in!(cls, classes); |
| 116 | + } |
| 117 | + |
| 118 | + // Test objc2::runtime functionality |
| 119 | + assert_eq!(Class::get("MyTestObject"), Some(cls)); |
| 120 | + assert_ne!(cls, class!(NSObject)); |
| 121 | + assert_eq!(cls.name(), "MyTestObject"); |
| 122 | + assert_eq!(cls.superclass(), Some(class!(NSObject))); |
| 123 | + assert_eq!(cls.metaclass().name(), "MyTestObject"); |
| 124 | + assert_ne!(cls.metaclass(), cls); |
| 125 | + assert_eq!(cls.instance_size(), { |
| 126 | + #[repr(C)] |
| 127 | + struct MyTestObjectLayout { |
| 128 | + isa: *const Class, |
| 129 | + var1: c_int, |
| 130 | + var2: Bool, |
| 131 | + var3: *mut NSObject, |
| 132 | + } |
| 133 | + size_of::<MyTestObjectLayout>() |
| 134 | + }); |
| 135 | + |
| 136 | + let protocol = Protocol::get("NSObject").unwrap(); |
| 137 | + assert!(cls.conforms_to(protocol)); |
| 138 | + assert!(!cls.conforms_to(Protocol::get("NSCopying").unwrap())); |
| 139 | + #[cfg(feature = "malloc")] |
| 140 | + assert_in!(protocol, cls.adopted_protocols()); |
| 141 | + |
| 142 | + #[cfg(feature = "malloc")] |
| 143 | + { |
| 144 | + let method = cls.instance_method(sel!(addToVar1:)).unwrap(); |
| 145 | + assert_in!(method, cls.instance_methods()); |
| 146 | + |
| 147 | + let ivar = cls.instance_variable("var1").unwrap(); |
| 148 | + assert_in!(ivar, cls.instance_variables()); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +#[test] |
| 153 | +fn test_object() { |
| 154 | + autoreleasepool(|pool| { |
| 155 | + let _obj = MyTestObject::new_autoreleased(pool); |
| 156 | + }); |
| 157 | + |
| 158 | + let mut obj = MyTestObject::new(); |
| 159 | + assert_eq!((*obj.inner).class(), MyTestObject::class()); |
| 160 | + |
| 161 | + assert_eq!(obj.var1(), 42); |
| 162 | + assert_eq!(*obj.var1_ivar(), 42); |
| 163 | + |
| 164 | + obj.add_to_ivar1(3); |
| 165 | + assert_eq!(obj.var1(), 45); |
| 166 | + assert_eq!(*obj.var1_ivar(), 45); |
| 167 | + |
| 168 | + *obj.var1_ivar_mut() = 100; |
| 169 | + assert_eq!(obj.var1(), 100); |
| 170 | + assert_eq!(*obj.var1_ivar(), 100); |
| 171 | + |
| 172 | + assert!(obj.var2()); |
| 173 | + assert!(obj.var2_ivar().is_true()); |
| 174 | + |
| 175 | + *obj.var2_ivar_mut() = Bool::NO; |
| 176 | + assert!(!obj.var2()); |
| 177 | + assert!(obj.var2_ivar().is_false()); |
| 178 | + |
| 179 | + assert!(obj.var3().is_null()); |
| 180 | + assert!(obj.var3_ivar().is_null()); |
| 181 | + |
| 182 | + let obj2 = ManuallyDrop::new(NSObject::new()).as_ptr().cast::<Object>(); |
| 183 | + obj.set_var3(obj2); |
| 184 | + assert_eq!(obj.var3(), obj2); |
| 185 | + assert_eq!(*obj.var3_ivar(), obj2); |
| 186 | + |
| 187 | + let obj3 = ManuallyDrop::new(NSObject::new()).as_ptr().cast::<Object>(); |
| 188 | + *obj.var3_ivar_mut() = obj3; |
| 189 | + assert_ne!(obj.var3(), obj2); |
| 190 | + assert_ne!(*obj.var3_ivar(), obj2); |
| 191 | + assert_eq!(obj.var3(), obj3); |
| 192 | + assert_eq!(*obj.var3_ivar(), obj3); |
| 193 | +} |
0 commit comments