|
| 1 | +use core::cell::RefCell; |
| 2 | +use core::ops::{Deref, DerefMut}; |
| 3 | +use std::sync::Once; |
| 4 | + |
| 5 | +use super::{Id, Owned}; |
| 6 | +use crate::declare::ClassBuilder; |
| 7 | +use crate::runtime::{Bool, Class, Object, Sel}; |
| 8 | +use crate::{msg_send, msg_send_bool}; |
| 9 | +use crate::{Encoding, Message, RefEncode}; |
| 10 | + |
| 11 | +#[derive(Debug, Clone, Default, PartialEq)] |
| 12 | +pub(crate) struct ThreadTestData { |
| 13 | + pub(crate) alloc: usize, |
| 14 | + pub(crate) dealloc: usize, |
| 15 | + pub(crate) init: usize, |
| 16 | + pub(crate) retain: usize, |
| 17 | + pub(crate) release: usize, |
| 18 | + pub(crate) autorelease: usize, |
| 19 | + pub(crate) try_retain: usize, |
| 20 | + pub(crate) try_retain_fail: usize, |
| 21 | +} |
| 22 | + |
| 23 | +impl ThreadTestData { |
| 24 | + /// Get the amount of method calls performed on the current thread. |
| 25 | + pub(crate) fn current() -> ThreadTestData { |
| 26 | + TEST_DATA.with(|data| data.borrow().clone()) |
| 27 | + } |
| 28 | + |
| 29 | + #[track_caller] |
| 30 | + pub(crate) fn assert_current(&self) { |
| 31 | + let current = Self::current(); |
| 32 | + assert_eq!(¤t, self); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +std::thread_local! { |
| 37 | + pub(crate) static TEST_DATA: RefCell<ThreadTestData> = RefCell::new(Default::default()); |
| 38 | +} |
| 39 | + |
| 40 | +/// A helper object that counts how many times various reference-counting |
| 41 | +/// primitives are called. |
| 42 | +#[repr(C)] |
| 43 | +pub(crate) struct RcTestObject { |
| 44 | + inner: Object, |
| 45 | +} |
| 46 | + |
| 47 | +unsafe impl RefEncode for RcTestObject { |
| 48 | + const ENCODING_REF: Encoding<'static> = Object::ENCODING_REF; |
| 49 | +} |
| 50 | + |
| 51 | +unsafe impl Message for RcTestObject {} |
| 52 | + |
| 53 | +unsafe impl Send for RcTestObject {} |
| 54 | +unsafe impl Sync for RcTestObject {} |
| 55 | + |
| 56 | +impl Deref for RcTestObject { |
| 57 | + type Target = Object; |
| 58 | + fn deref(&self) -> &Self::Target { |
| 59 | + &self.inner |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl DerefMut for RcTestObject { |
| 64 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 65 | + &mut self.inner |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl RcTestObject { |
| 70 | + fn class() -> &'static Class { |
| 71 | + static REGISTER_CLASS: Once = Once::new(); |
| 72 | + |
| 73 | + REGISTER_CLASS.call_once(|| { |
| 74 | + extern "C" fn alloc(cls: &Class, _cmd: Sel) -> *mut RcTestObject { |
| 75 | + TEST_DATA.with(|data| data.borrow_mut().alloc += 1); |
| 76 | + let superclass = class!(NSObject).metaclass(); |
| 77 | + unsafe { msg_send![super(cls, superclass), alloc] } |
| 78 | + } |
| 79 | + extern "C" fn init(this: &mut RcTestObject, _cmd: Sel) -> *mut RcTestObject { |
| 80 | + TEST_DATA.with(|data| data.borrow_mut().init += 1); |
| 81 | + unsafe { msg_send![super(this, class!(NSObject)), init] } |
| 82 | + } |
| 83 | + extern "C" fn retain(this: &RcTestObject, _cmd: Sel) -> *mut RcTestObject { |
| 84 | + TEST_DATA.with(|data| data.borrow_mut().retain += 1); |
| 85 | + unsafe { msg_send![super(this, class!(NSObject)), retain] } |
| 86 | + } |
| 87 | + extern "C" fn release(this: &RcTestObject, _cmd: Sel) { |
| 88 | + TEST_DATA.with(|data| data.borrow_mut().release += 1); |
| 89 | + unsafe { msg_send![super(this, class!(NSObject)), release] } |
| 90 | + } |
| 91 | + extern "C" fn autorelease(this: &RcTestObject, _cmd: Sel) -> *mut RcTestObject { |
| 92 | + TEST_DATA.with(|data| data.borrow_mut().autorelease += 1); |
| 93 | + unsafe { msg_send![super(this, class!(NSObject)), autorelease] } |
| 94 | + } |
| 95 | + unsafe extern "C" fn dealloc(_this: *mut RcTestObject, _cmd: Sel) { |
| 96 | + TEST_DATA.with(|data| data.borrow_mut().dealloc += 1); |
| 97 | + // Don't call superclass |
| 98 | + } |
| 99 | + unsafe extern "C" fn try_retain(this: &RcTestObject, _cmd: Sel) -> Bool { |
| 100 | + TEST_DATA.with(|data| data.borrow_mut().try_retain += 1); |
| 101 | + let res = unsafe { msg_send_bool![super(this, class!(NSObject)), _tryRetain] }; |
| 102 | + if !res { |
| 103 | + TEST_DATA.with(|data| data.borrow_mut().try_retain -= 1); |
| 104 | + TEST_DATA.with(|data| data.borrow_mut().try_retain_fail += 1); |
| 105 | + } |
| 106 | + Bool::from(res) |
| 107 | + } |
| 108 | + |
| 109 | + let mut builder = ClassBuilder::new("RcTestObject", class!(NSObject)).unwrap(); |
| 110 | + unsafe { |
| 111 | + builder.add_class_method( |
| 112 | + sel!(alloc), |
| 113 | + alloc as extern "C" fn(&Class, Sel) -> *mut RcTestObject, |
| 114 | + ); |
| 115 | + builder.add_method( |
| 116 | + sel!(init), |
| 117 | + init as extern "C" fn(&mut RcTestObject, Sel) -> _, |
| 118 | + ); |
| 119 | + builder.add_method( |
| 120 | + sel!(retain), |
| 121 | + retain as extern "C" fn(&RcTestObject, Sel) -> _, |
| 122 | + ); |
| 123 | + builder.add_method( |
| 124 | + sel!(_tryRetain), |
| 125 | + try_retain as unsafe extern "C" fn(&RcTestObject, Sel) -> Bool, |
| 126 | + ); |
| 127 | + builder.add_method(sel!(release), release as extern "C" fn(&RcTestObject, Sel)); |
| 128 | + builder.add_method( |
| 129 | + sel!(autorelease), |
| 130 | + autorelease as extern "C" fn(&RcTestObject, Sel) -> _, |
| 131 | + ); |
| 132 | + builder.add_method( |
| 133 | + sel!(dealloc), |
| 134 | + dealloc as unsafe extern "C" fn(*mut RcTestObject, Sel), |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + builder.register(); |
| 139 | + }); |
| 140 | + |
| 141 | + class!(RcTestObject) |
| 142 | + } |
| 143 | + |
| 144 | + pub(crate) fn new() -> Id<Self, Owned> { |
| 145 | + unsafe { Id::new(msg_send![Self::class(), new]) }.unwrap() |
| 146 | + } |
| 147 | +} |
0 commit comments