Skip to content

Commit

Permalink
cfg-guard objc_sys symbols not available in ObjFW
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Mar 2, 2022
1 parent aac75da commit d5b33cc
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 37 deletions.
29 changes: 25 additions & 4 deletions objc-sys/src/class.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -115,14 +133,17 @@ extern_c! {
imp: IMP,
types: *const c_char,
) -> IMP;
#[cfg(not(objfw))]
pub fn class_replaceProperty(
cls: *mut objc_class,
name: *const c_char,
attributes: *const objc_property_attribute_t,
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);
Expand Down
9 changes: 8 additions & 1 deletion objc-sys/src/exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<unsafe extern "C" fn(exception: *mut objc_object)>;

/// Only available on macOS.
///
/// Remember that this is non-null!
Expand All @@ -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`].
///
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions objc-sys/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down
10 changes: 8 additions & 2 deletions objc-sys/src/method.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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);
Expand Down
20 changes: 17 additions & 3 deletions objc-sys/src/object.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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)]
Expand All @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion objc-sys/src/property.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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,
Expand Down
36 changes: 25 additions & 11 deletions objc-sys/src/protocol.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand All @@ -19,19 +20,32 @@ 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,
types: *const c_char,
is_required_method: BOOL,
is_instance_method: BOOL,
);
#[cfg(not(objfw))]
pub fn protocol_addProperty(
proto: *mut objc_protocol,
name: *const c_char,
Expand All @@ -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
Expand Down
Loading

0 comments on commit d5b33cc

Please sign in to comment.