From d5b33cc018804c761398d56bf8e722a05847fdbf Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Thu, 3 Mar 2022 00:46:48 +0100 Subject: [PATCH] cfg-guard objc_sys symbols not available in ObjFW --- objc-sys/src/class.rs | 29 +++++++++++++++++++++++++---- objc-sys/src/exception.rs | 9 ++++++++- objc-sys/src/message.rs | 4 ++-- objc-sys/src/method.rs | 10 ++++++++-- objc-sys/src/object.rs | 20 +++++++++++++++++--- objc-sys/src/property.rs | 6 +++++- objc-sys/src/protocol.rs | 36 +++++++++++++++++++++++++----------- objc-sys/src/rc.rs | 18 ++++++++++++------ objc-sys/src/selector.rs | 7 +++++-- objc-sys/src/various.rs | 24 +++++++++++++++++++----- 10 files changed, 126 insertions(+), 37 deletions(-) diff --git a/objc-sys/src/class.rs b/objc-sys/src/class.rs index 74396969a..5de95ed60 100644 --- a/objc-sys/src/class.rs +++ b/objc-sys/src/class.rs @@ -1,9 +1,8 @@ use std::os::raw::{c_char, c_int, c_uint}; -use crate::{ - objc_ivar, objc_method, objc_object, objc_property, objc_property_attribute_t, objc_protocol, - objc_selector, OpaqueData, BOOL, IMP, -}; +#[cfg(not(objfw))] +use crate::{objc_ivar, objc_method, objc_object, objc_property, objc_property_attribute_t}; +use crate::{objc_protocol, objc_selector, OpaqueData, BOOL, IMP}; /// An opaque type that represents an Objective-C class. #[repr(C)] @@ -14,6 +13,7 @@ pub struct objc_class { _p: OpaqueData, } +#[cfg(not(objfw))] /// This is `c_char` in GNUStep's libobjc2 and `uint8_t` in Apple's objc4. /// /// The pointer represents opaque data, and is definitely not just an integer, @@ -27,6 +27,7 @@ extern_c! { pub fn objc_getClass(name: *const c_char) -> *const objc_class; pub fn objc_getRequiredClass(name: *const c_char) -> *const objc_class; pub fn objc_lookUpClass(name: *const c_char) -> *const objc_class; + #[cfg(not(objfw))] pub fn objc_getMetaClass(name: *const c_char) -> *const objc_class; pub fn objc_copyClassList(out_len: *mut c_uint) -> *mut *const objc_class; pub fn objc_getClassList(buffer: *mut *const objc_class, buffer_len: c_int) -> c_int; @@ -42,9 +43,11 @@ extern_c! { name: *const c_char, extra_bytes: usize, ) -> *mut objc_class; + #[cfg(not(objfw))] pub fn objc_disposeClassPair(cls: *mut objc_class); pub fn objc_registerClassPair(cls: *mut objc_class); + #[cfg(not(objfw))] pub fn class_addIvar( cls: *mut objc_class, name: *const c_char, @@ -58,53 +61,68 @@ extern_c! { imp: IMP, types: *const c_char, ) -> BOOL; + #[cfg(not(objfw))] pub fn class_addProperty( cls: *mut objc_class, name: *const c_char, attributes: *const objc_property_attribute_t, attributes_count: c_uint, ) -> BOOL; + #[cfg(not(objfw))] pub fn class_addProtocol(cls: *mut objc_class, protocol: *const objc_protocol) -> BOOL; pub fn class_conformsToProtocol(cls: *const objc_class, protocol: *const objc_protocol) -> BOOL; + + #[cfg(not(objfw))] // Available in newer versions pub fn class_copyIvarList( cls: *const objc_class, out_len: *mut c_uint, ) -> *mut *const objc_ivar; + #[cfg(not(objfw))] // Available in newer versions pub fn class_copyMethodList( cls: *const objc_class, out_len: *mut c_uint, ) -> *mut *const objc_method; + #[cfg(not(objfw))] // Available in newer versions pub fn class_copyPropertyList( cls: *const objc_class, out_len: *mut c_uint, ) -> *mut *const objc_property; + #[cfg(not(objfw))] pub fn class_copyProtocolList( cls: *const objc_class, out_len: *mut c_uint, ) -> *mut *const objc_protocol; + #[cfg(not(objfw))] pub fn class_createInstance(cls: *const objc_class, extra_bytes: usize) -> *mut objc_object; + #[cfg(not(objfw))] pub fn class_getClassMethod( cls: *const objc_class, name: *const objc_selector, ) -> *const objc_method; + #[cfg(not(objfw))] pub fn class_getClassVariable(cls: *const objc_class, name: *const c_char) -> *const objc_ivar; #[cfg(apple)] pub fn class_getImageName(cls: *const objc_class) -> *const c_char; + #[cfg(not(objfw))] // Available in newer versions pub fn class_getInstanceMethod( cls: *const objc_class, name: *const objc_selector, ) -> *const objc_method; pub fn class_getInstanceSize(cls: *const objc_class) -> usize; + #[cfg(not(objfw))] pub fn class_getInstanceVariable( cls: *const objc_class, name: *const c_char, ) -> *const objc_ivar; + #[cfg(not(objfw))] pub fn class_getIvarLayout(cls: *const objc_class) -> *const ivar_layout_type; pub fn class_getName(cls: *const objc_class) -> *const c_char; + #[cfg(not(objfw))] pub fn class_getProperty(cls: *const objc_class, name: *const c_char) -> *const objc_property; pub fn class_getSuperclass(cls: *const objc_class) -> *const objc_class; + #[cfg(not(objfw))] pub fn class_getVersion(cls: *const objc_class) -> c_int; #[cfg(apple)] pub fn class_getWeakIvarLayout(cls: *const objc_class) -> *const ivar_layout_type; @@ -115,6 +133,7 @@ extern_c! { imp: IMP, types: *const c_char, ) -> IMP; + #[cfg(not(objfw))] pub fn class_replaceProperty( cls: *mut objc_class, name: *const c_char, @@ -122,7 +141,9 @@ extern_c! { attributes_len: c_uint, ); pub fn class_respondsToSelector(cls: *const objc_class, sel: *const objc_selector) -> BOOL; + #[cfg(not(objfw))] pub fn class_setIvarLayout(cls: *mut objc_class, layout: *const ivar_layout_type); + #[cfg(not(objfw))] pub fn class_setVersion(cls: *mut objc_class, version: c_int); #[cfg(apple)] pub fn class_setWeakIvarLayout(cls: *mut objc_class, layout: *const ivar_layout_type); diff --git a/objc-sys/src/exception.rs b/objc-sys/src/exception.rs index 815153680..c5be06838 100644 --- a/objc-sys/src/exception.rs +++ b/objc-sys/src/exception.rs @@ -2,6 +2,7 @@ //! Apple: `objc-exception.h` //! GNUStep: `eh_personality.c`, which is a bit brittle to rely on, but I //! think it's fine... +#[cfg(not(objfw))] use core::ffi::c_void; #[cfg(apple)] use std::os::raw::c_int; @@ -24,6 +25,10 @@ pub type objc_exception_preprocessor = #[cfg(apple)] pub type objc_uncaught_exception_handler = unsafe extern "C" fn(exception: *mut objc_object); +#[cfg(objfw)] +pub type objc_uncaught_exception_handler = + Option; + /// Only available on macOS. /// /// Remember that this is non-null! @@ -32,7 +37,9 @@ pub type objc_exception_handler = unsafe extern "C" fn(unused: *mut objc_object, context: *mut c_void); extern_c! { + #[cfg(not(objfw))] pub fn objc_begin_catch(exc_buf: *mut c_void) -> *mut objc_object; + #[cfg(not(objfw))] pub fn objc_end_catch(); /// See [`objc-exception.h`]. /// @@ -49,7 +56,7 @@ extern_c! { pub fn objc_setExceptionPreprocessor( f: objc_exception_preprocessor, ) -> objc_exception_preprocessor; - #[cfg(apple)] + #[cfg(any(apple, objfw))] pub fn objc_setUncaughtExceptionHandler( f: objc_uncaught_exception_handler, ) -> objc_uncaught_exception_handler; diff --git a/objc-sys/src/message.rs b/objc-sys/src/message.rs index fc26843be..5c9144416 100644 --- a/objc-sys/src/message.rs +++ b/objc-sys/src/message.rs @@ -68,8 +68,8 @@ extern_c! { /// Not available on `target_arch = "aarch64"` #[cfg(all(apple, not(target_arch = "aarch64")))] pub fn _objc_msgForward_stret(); - /// Not available on `target_arch = "aarch64"` - #[cfg(not(target_arch = "aarch64"))] + /// Not available on `target_arch = "aarch64"`, always available on ObjFW + #[cfg(any(objfw, not(target_arch = "aarch64")))] pub fn class_getMethodImplementation_stret(); // __x86_64__ and __i386__ diff --git a/objc-sys/src/method.rs b/objc-sys/src/method.rs index f29ced2ac..50704073b 100644 --- a/objc-sys/src/method.rs +++ b/objc-sys/src/method.rs @@ -1,6 +1,10 @@ -use std::os::raw::{c_char, c_uint}; +use std::os::raw::c_char; +#[cfg(objfw)] +use std::os::raw::c_uint; -use crate::{objc_selector, OpaqueData, IMP}; +#[cfg(objfw)] +use crate::IMP; +use crate::{objc_selector, OpaqueData}; /// A type that represents a method in a class definition. #[repr(C)] @@ -20,6 +24,8 @@ pub struct objc_method_description { } extern_c! { + #![cfg(objfw)] + pub fn method_copyArgumentType(method: *const objc_method, index: c_uint) -> *mut c_char; pub fn method_copyReturnType(method: *const objc_method) -> *mut c_char; pub fn method_exchangeImplementations(method1: *mut objc_method, method2: *mut objc_method); diff --git a/objc-sys/src/object.rs b/objc-sys/src/object.rs index 58950a0f0..1c852dd4f 100644 --- a/objc-sys/src/object.rs +++ b/objc-sys/src/object.rs @@ -1,7 +1,10 @@ +#[cfg(not(objfw))] use core::ffi::c_void; use std::os::raw::c_char; -use crate::{objc_class, objc_ivar, OpaqueData}; +#[cfg(not(objfw))] +use crate::objc_ivar; +use crate::{objc_class, OpaqueData}; /// An opaque type that represents an object / an instance of a class. #[repr(C)] @@ -15,23 +18,31 @@ pub struct objc_object { extern_c! { pub fn object_getClass(obj: *const objc_object) -> *const objc_class; pub fn object_getClassName(obj: *const objc_object) -> *const c_char; + pub fn object_setClass(obj: *mut objc_object, cls: *const objc_class) -> *const objc_class; + + #[cfg(not(objfw))] pub fn object_getIndexedIvars(obj: *const objc_object) -> *const c_void; + #[cfg(not(objfw))] pub fn object_getIvar(obj: *const objc_object, ivar: *const objc_ivar) -> *const objc_object; - - pub fn object_setClass(obj: *mut objc_object, cls: *const objc_class) -> *const objc_class; + #[cfg(not(objfw))] pub fn object_setIvar(obj: *mut objc_object, ivar: *const objc_ivar, value: *mut objc_object); #[deprecated = "Not needed since ARC"] #[cfg(apple)] pub fn object_copy(obj: *const objc_object, size: usize) -> *mut objc_object; + #[deprecated = "Not needed since ARC"] + #[cfg(not(objfw))] pub fn object_dispose(obj: *mut objc_object) -> *mut objc_object; + #[deprecated = "Not needed since ARC"] + #[cfg(not(objfw))] pub fn object_setInstanceVariable( obj: *mut objc_object, name: *const c_char, value: *mut c_void, ) -> *const objc_ivar; + // Available in macOS 10.12 // #[deprecated = "Not needed since ARC"] // #[cfg(apple)] @@ -40,12 +51,15 @@ extern_c! { // name: *const c_char, // value: *mut c_void, // ) -> *const objc_ivar; + #[deprecated = "Not needed since ARC"] + #[cfg(not(objfw))] pub fn object_getInstanceVariable( obj: *const objc_object, name: *const c_char, out_value: *mut *const c_void, ) -> *const objc_ivar; + #[deprecated = "Not needed since ARC"] #[cfg(apple)] pub fn objc_getFutureClass(name: *const c_char) -> *const objc_class; diff --git a/objc-sys/src/property.rs b/objc-sys/src/property.rs index 57fd62771..6887fa3c4 100644 --- a/objc-sys/src/property.rs +++ b/objc-sys/src/property.rs @@ -1,4 +1,6 @@ -use std::os::raw::{c_char, c_uint}; +use std::os::raw::c_char; +#[cfg(not(objfw))] +use std::os::raw::c_uint; use crate::OpaqueData; @@ -22,6 +24,8 @@ pub struct objc_property_attribute_t { } extern_c! { + #![cfg(not(objfw))] + pub fn property_copyAttributeList( property: *const objc_property, out_len: *mut c_uint, diff --git a/objc-sys/src/protocol.rs b/objc-sys/src/protocol.rs index 064037545..b8684ba0d 100644 --- a/objc-sys/src/protocol.rs +++ b/objc-sys/src/protocol.rs @@ -1,9 +1,10 @@ -use std::os::raw::{c_char, c_uint}; +use std::os::raw::c_char; +#[cfg(not(objfw))] +use std::os::raw::c_uint; -use crate::{ - objc_method_description, objc_property, objc_property_attribute_t, objc_selector, OpaqueData, - BOOL, -}; +#[cfg(not(objfw))] +use crate::{objc_method_description, objc_property, objc_property_attribute_t, objc_selector}; +use crate::{OpaqueData, BOOL}; /// Opaque type for Objective-C protocols. /// @@ -19,12 +20,24 @@ pub struct objc_protocol { } extern_c! { + #[cfg(not(objfw))] pub fn objc_getProtocol(name: *const c_char) -> *const objc_protocol; + #[cfg(not(objfw))] pub fn objc_copyProtocolList(out_len: *mut c_uint) -> *mut *const objc_protocol; + #[cfg(not(objfw))] pub fn objc_allocateProtocol(name: *const c_char) -> *mut objc_protocol; + #[cfg(not(objfw))] pub fn objc_registerProtocol(proto: *mut objc_protocol); + pub fn protocol_conformsToProtocol( + proto: *const objc_protocol, + other: *const objc_protocol, + ) -> BOOL; + pub fn protocol_isEqual(proto: *const objc_protocol, other: *const objc_protocol) -> BOOL; + pub fn protocol_getName(proto: *const objc_protocol) -> *const c_char; + + #[cfg(not(objfw))] pub fn protocol_addMethodDescription( proto: *mut objc_protocol, name: *const objc_selector, @@ -32,6 +45,7 @@ extern_c! { is_required_method: BOOL, is_instance_method: BOOL, ); + #[cfg(not(objfw))] pub fn protocol_addProperty( proto: *mut objc_protocol, name: *const c_char, @@ -40,39 +54,39 @@ extern_c! { is_required_property: BOOL, is_instance_property: BOOL, ); + #[cfg(not(objfw))] pub fn protocol_addProtocol(proto: *mut objc_protocol, addition: *const objc_protocol); - pub fn protocol_conformsToProtocol( - proto: *const objc_protocol, - other: *const objc_protocol, - ) -> BOOL; + #[cfg(not(objfw))] pub fn protocol_copyMethodDescriptionList( proto: *const objc_protocol, is_required_method: BOOL, is_instance_method: BOOL, out_len: *mut c_uint, ) -> *mut objc_method_description; + #[cfg(not(objfw))] pub fn protocol_copyPropertyList( proto: *const objc_protocol, out_len: *mut c_uint, ) -> *mut *const objc_property; + #[cfg(not(objfw))] pub fn protocol_copyProtocolList( proto: *const objc_protocol, out_len: *mut c_uint, ) -> *mut *const objc_protocol; + #[cfg(not(objfw))] pub fn protocol_getMethodDescription( proto: *const objc_protocol, sel: *const objc_selector, is_required_method: BOOL, is_instance_method: BOOL, ) -> objc_method_description; - pub fn protocol_getName(proto: *const objc_protocol) -> *const c_char; + #[cfg(not(objfw))] pub fn protocol_getProperty( proto: *const objc_protocol, name: *const c_char, is_required_property: BOOL, is_instance_property: BOOL, ) -> *const objc_property; - pub fn protocol_isEqual(proto: *const objc_protocol, other: *const objc_protocol) -> BOOL; // #[cfg(macos >= 10.12)] // protocol_copyPropertyList2 diff --git a/objc-sys/src/rc.rs b/objc-sys/src/rc.rs index 62638245c..4b421c1cf 100644 --- a/objc-sys/src/rc.rs +++ b/objc-sys/src/rc.rs @@ -1,11 +1,13 @@ //! ARC functions. //! -//! All available since macOS `10.7`. +//! Is available in Clang's [documentation][ARC] so these are safe to rely on. //! -//! Defined in `objc-internal.h`, but is available in Clang's -//! [documentation][ARC] so these are safe to rely on. +//! All available since macOS `10.7`. //! -//! On GNUStep these are defined in `objc-arc.h`. +//! Defined in: +//! - Apple: `objc-internal.h` +//! - GNUStep: `objc-arc.h` +//! - ObjFW: `runtime/arc.m` //! //! [ARC]: https://clang.llvm.org/docs/AutomaticReferenceCounting.html#runtime-support> use core::ffi::c_void; @@ -13,11 +15,15 @@ use core::ffi::c_void; use crate::objc_object; extern_c! { - // Autorelease + // Autoreleasepool + // ObjFW: Defined in `autorelease.h` - pub fn objc_autorelease(value: *mut objc_object) -> *mut objc_object; pub fn objc_autoreleasePoolPop(pool: *mut c_void); pub fn objc_autoreleasePoolPush() -> *mut c_void; + + // Autorelease + + pub fn objc_autorelease(value: *mut objc_object) -> *mut objc_object; pub fn objc_autoreleaseReturnValue(value: *mut objc_object) -> *mut objc_object; // Weak pointers diff --git a/objc-sys/src/selector.rs b/objc-sys/src/selector.rs index d9caec111..35d968229 100644 --- a/objc-sys/src/selector.rs +++ b/objc-sys/src/selector.rs @@ -13,9 +13,12 @@ pub struct objc_selector { extern_c! { pub fn sel_getName(sel: *const objc_selector) -> *const c_char; - pub fn sel_getUid(name: *const c_char) -> *const objc_selector; pub fn sel_isEqual(lhs: *const objc_selector, rhs: *const objc_selector) -> BOOL; + pub fn sel_registerName(name: *const c_char) -> *const objc_selector; + + #[cfg(not(objfw))] + pub fn sel_getUid(name: *const c_char) -> *const objc_selector; + #[cfg(apple)] pub fn sel_isMapped(sel: *const objc_selector) -> BOOL; - pub fn sel_registerName(name: *const c_char) -> *const objc_selector; } diff --git a/objc-sys/src/various.rs b/objc-sys/src/various.rs index 0d8b2e4f8..956920dbd 100644 --- a/objc-sys/src/various.rs +++ b/objc-sys/src/various.rs @@ -1,9 +1,13 @@ use core::ffi::c_void; +#[cfg(not(objfw))] +use std::os::raw::c_char; +use std::os::raw::c_int; #[cfg(apple)] use std::os::raw::c_uint; -use std::os::raw::{c_char, c_int}; -use crate::{objc_AssociationPolicy, objc_object, OpaqueData, BOOL}; +#[cfg(not(objfw))] +use crate::{objc_AssociationPolicy, BOOL}; +use crate::{objc_object, OpaqueData}; /// An opaque type that represents an instance variable. #[repr(C)] @@ -32,12 +36,18 @@ pub type objc_hook_lazyClassNamer = unsafe extern "C" fn(cls: *const crate::objc_class) -> *const c_char; extern_c! { + #[cfg(not(objfw))] pub fn imp_getBlock(imp: IMP) -> *mut objc_object; + #[cfg(not(objfw))] pub fn imp_implementationWithBlock(block: *mut objc_object) -> IMP; + #[cfg(not(objfw))] pub fn imp_removeBlock(imp: IMP) -> BOOL; + #[cfg(not(objfw))] pub fn ivar_getName(ivar: *const objc_ivar) -> *const c_char; + #[cfg(not(objfw))] pub fn ivar_getOffset(ivar: *const objc_ivar) -> isize; + #[cfg(not(objfw))] pub fn ivar_getTypeEncoding(ivar: *const objc_ivar) -> *const c_char; #[cfg(apple)] @@ -49,30 +59,34 @@ extern_c! { pub fn objc_copyImageNames(out_len: *mut c_uint) -> *mut *const c_char; // Instead of being able to change this, it's a weak symbol on GNUStep. - #[cfg(apple)] + #[cfg(any(apple, objfw))] pub fn objc_enumerationMutation(obj: *mut objc_object); - #[cfg(apple)] + #[cfg(any(apple, objfw))] pub fn objc_setEnumerationMutationHandler( handler: Option, ); + #[cfg(not(objfw))] pub fn objc_getAssociatedObject( object: *const objc_object, key: *const c_void, ) -> *const objc_object; + #[cfg(not(objfw))] pub fn objc_setAssociatedObject( object: *mut objc_object, key: *const c_void, value: *mut objc_object, policy: objc_AssociationPolicy, ); + #[cfg(not(objfw))] pub fn objc_removeAssociatedObjects(object: *mut objc_object); - #[cfg(apple)] + #[cfg(any(apple, objfw))] pub fn objc_setForwardHandler(fwd: *mut c_void, fwd_stret: *mut c_void); // These two are defined in: // - Apple: objc-sync.h // - GNUStep: dtable.h / associate.m + // - ObjFW: ObjFW-RT.h pub fn objc_sync_enter(obj: *mut objc_object) -> c_int; pub fn objc_sync_exit(obj: *mut objc_object) -> c_int;