Expand description
§Raw bindings to Objective-C runtimes
These bindings contain almost no documentation, so it is highly
recommended to read Apple’s documentation about the Objective-C
runtime, Apple’s runtime reference, or to use
objc2::runtime
which provides a higher-level API.
§Runtime Support
Objective-C has a runtime, different implementations of said runtime
exist, and they act in slightly different ways. By default, Apple
platforms link to Apple’s runtime, but if you’re using another runtime you
must tell it to this library using feature flags (you might have to
disable the default apple
feature first).
One could ask, why even bother supporting other runtimes? For me, the primary reasoning iss robustness. By testing with these alternative runtimes in CI, we become by extension much more confident that our implementation doesn’t rely on brittle unspecified behaviour, and works across different macOS and iOS versions.
§Apple’s objc4
- Feature flag:
apple
.
This is used by default, and has the highest support priority (all of
objc2
will work with this runtime).
The supported runtime version (higher versions lets the compiler enable
newer optimizations, at the cost of not supporting older operating
systems) can be chosen using the standard X_DEPLOYMENT_TARGET
environment variables:
- macOS:
MACOSX_DEPLOYMENT_TARGET
, default10.12
,11.0
on Aarch64. - iOS / iPadOS:
IPHONEOS_DEPLOYMENT_TARGET
, default10.0
. - tvOS:
TVOS_DEPLOYMENT_TARGET
, default10.0
. - watchOS:
WATCHOS_DEPLOYMENT_TARGET
, default5.0
.
The default (and minimum) versions are the same as those Rust itself has.
§GNUStep’s libobjc2
- Feature flag:
gnustep-1-7
,gnustep-1-8
,gnustep-1-9
,gnustep-2-0
andgnustep-2-1
depending on the version you’re using.
§Microsoft’s WinObjC
- Feature flag:
unstable-winobjc
.
Unstable: Hasn’t been tested on Windows yet!
A fork based on GNUStep’s
libobjc2
version 1.8, with very few user-facing changes.
§ObjFW
- Feature flag:
unstable-objfw
.
Unstable: Doesn’t work yet!
TODO.
§Other runtimes
This library will probably only ever support “Modern”
Objective-C runtimes, since support for reference-counting primitives like
objc_retain
and objc_autoreleasePoolPop
is a vital requirement for
most applications.
This rules out the GCC libobjc
runtime (see
this), the mulle-objc
runtime and cocotron. (But
support for darling
may be added). More information on different
runtimes can be found in GNUStep’s Objective-C Compiler and Runtime
FAQ.
§Advanced linking configuration
This crate defines the links
key in Cargo.toml
so it’s possible to
change the linking to libobjc
, see the relevant cargo
docs.
In the future, this crate may vendor the required source code to automatically build and link to the runtimes. Choosing static vs. dynamic linking here may also become an option.
§Objective-C Compiler configuration
Objective-C compilers like clang
and gcc
requires configuring the
calling ABI to the runtime you’re using:
clang
uses the-fobjc-runtime
flag, of which there are a few different options.gcc
uses the-fgnu-runtime
or-fnext-runtime
options. Note that Modern Objective-C features are ill supported.
This is relevant if you’re building and linking to custom Objective-C
sources in a build script. To assist in compiling Objective-C sources,
this crate’s build script expose the DEP_OBJC_0_3_CC_ARGS
environment
variable to downstream build scripts.
Example usage in your build.rs
(using the cc
crate) would be as
follows:
fn main() {
let mut builder = cc::Build::new();
builder.compiler("clang");
builder.file("my_objective_c_script.m");
for flag in std::env::var("DEP_OBJC_0_3_CC_ARGS").unwrap().split(' ') {
builder.flag(flag);
}
builder.compile("libmy_objective_c_script.a");
}
§Design choices
It is recognized that the most primary consumer of this library will be
macOS and secondly iOS applications. Therefore it was chosen not to use
bindgen
1 in our build script to not add compilation cost to those
targets.
Deprecated functions are also not included for future compability, since they could be removed in any macOS release, and then our code would break. If you have a need for these, please open an issue and we can discuss it!
Some items (in particular the objc_msgSend_X
family) have cfg
s that
prevent their usage on different platforms; these are semver-stable in
the sense that they will only get less restrictive, never more.
That said, most of this is created with the help of
bindgen
’s commandline interface, so huge thanks to them! ↩
Structs§
- objc_
class - An opaque type that represents an Objective-C class.
- objc_
ivar - An opaque type that represents an instance variable.
- objc_
method - A type that represents a method in a class definition.
- objc_
method_ description - Describes an Objective-C method.
- objc_
object - An opaque type that represents an object / an instance of a class.
- objc_
property - An opaque type that describes a property in a class.
- objc_
property_ attribute_ t - Describes an Objective-C property attribute.
- objc_
protocol - Opaque type for Objective-C protocols.
- objc_
selector - An opaque type that represents a method selector.
- objc_
super - Specifies data used when sending messages to superclasses.
Constants§
- NO
- The equivalent of
false
for Objective-C’sBOOL
type. - NSInteger
Max - The maximum value for a
NSInteger
. - NSInteger
Min - The minimum value for a
NSInteger
. - NSUInteger
Max - The maximum value for a
NSUInteger
. - Nil
- A quick alias for a
null_mut
class. - OBJC_
ASSOCIATION_ ASSIGN - Specifies a weak reference to the associated object.
- OBJC_
ASSOCIATION_ COPY - Specifies that the associated object is copied.
- OBJC_
ASSOCIATION_ COPY_ NONATOMIC - Specifies that the associated object is copied.
- OBJC_
ASSOCIATION_ RETAIN - Specifies a strong reference to the associated object.
- OBJC_
ASSOCIATION_ RETAIN_ NONATOMIC - Specifies a strong reference to the associated object.
- OBJC_
SYNC_ NOT_ INITIALIZED Apple - Only relevant before macOS 10.13
- OBJC_
SYNC_ NOT_ OWNING_ THREAD_ ERROR Apple - OBJC_
SYNC_ SUCCESS Apple - OBJC_
SYNC_ TIMED_ OUT Apple - Only relevant before macOS 10.13
- YES
- The equivalent of
true
for Objective-C’sBOOL
type. - nil
- A quick alias for a
null_mut
object / instance.
Functions§
- _objc_
msgForward ⚠Apple - _objc_
msgForward_ ⚠stret Apple and non-AArch64 - class_
addIvar ⚠Non- unstable-objfw
- class_
addMethod ⚠ - class_
addProperty ⚠Non- unstable-objfw
- class_
addProtocol ⚠Non- unstable-objfw
- class_
conforms ⚠ToProtocol - class_
copy ⚠Ivar List Non- unstable-objfw
- The return value is deallocated with
free
. - class_
copy ⚠Method List Non- unstable-objfw
- The returned array is deallocated with
free
. - class_
copy ⚠Property List Non- unstable-objfw
- The returned array is deallocated with
free
. - class_
copy ⚠Protocol List Non- unstable-objfw
- The returned array is deallocated with
free
. - class_
create ⚠Instance Non- unstable-objfw
- class_
getClass ⚠Method Non- unstable-objfw
- class_
getClass ⚠Variable Non- unstable-objfw
- class_
getImage ⚠Name Apple - class_
getInstance ⚠Method Non- unstable-objfw
- class_
getInstance ⚠Size - class_
getInstance ⚠Variable Non- unstable-objfw
- class_
getIvar ⚠Layout Non- unstable-objfw
- class_
getMethod ⚠Implementation - class_
getMethod ⚠Implementation_ stret unstable-objfw
or non-AArch64 - class_
getName ⚠ - class_
getProperty ⚠Non- unstable-objfw
- class_
getSuperclass ⚠ - class_
getVersion ⚠Non- unstable-objfw
- class_
getWeak ⚠Ivar Layout Apple - class_
isMeta ⚠Class - class_
replace ⚠Method - class_
replace ⚠Property Non- unstable-objfw
- class_
responds ⚠ToSelector - class_
setIvar ⚠Layout Non- unstable-objfw
- class_
setVersion ⚠Non- unstable-objfw
- class_
setWeak ⚠Ivar Layout Apple - free⚠
- The Objective-C runtime has several methods, usually with “
copy
” in their name, whose return value is allocated with C’smalloc
and deallocated with C’sfree
method. - imp_
getBlock ⚠Non- unstable-objfw
- imp_
implementation ⚠With Block Non- unstable-objfw
- imp_
remove ⚠Block Non- unstable-objfw
- ivar_
getName ⚠Non- unstable-objfw
- ivar_
getOffset ⚠Non- unstable-objfw
- ivar_
getType ⚠Encoding Non- unstable-objfw
- method_
copy ⚠Argument Type Non- unstable-objfw
- The return value is deallocated with
free
. - method_
copy ⚠Return Type Non- unstable-objfw
- The return value is deallocated with
free
. - method_
exchange ⚠Implementations Non- unstable-objfw
- method_
getArgument ⚠Type Non- unstable-objfw
- method_
getDescription ⚠Apple - method_
getImplementation ⚠Non- unstable-objfw
- method_
getName ⚠Non- unstable-objfw
- method_
getNumber ⚠OfArguments Non- unstable-objfw
- method_
getReturn ⚠Type Non- unstable-objfw
- method_
getType ⚠Encoding Non- unstable-objfw
- method_
invoke ⚠Apple - method_
invoke_ ⚠stret Apple and non-AArch64 - method_
setImplementation ⚠Non- unstable-objfw
- objc_
addException ⚠Handler Apple and macOS and non-x86 - objc_
alloc ⚠Apple and not (macOS and x86) - objc_
alloc ⚠With Zone Apple and not (macOS and x86) - objc_
allocate ⚠Class Pair - objc_
allocate ⚠Protocol Non- unstable-objfw
- objc_
autorelease ⚠ - objc_
autorelease ⚠Pool Pop Non- unstable-objfw
- objc_
autorelease ⚠Pool Push Non- unstable-objfw
- objc_
autorelease ⚠Return Value - objc_
begin_ ⚠catch gnustep-1-7
, or Apple and not (macOS and x86) - objc_
construct ⚠Instance Deprecated Apple - objc_
copy ⚠Class List - The returned array is deallocated with
free
. - objc_
copy ⚠Class Names ForImage Apple - objc_
copy ⚠Image Names Apple - The returned array is deallocated with
free
. - objc_
copy ⚠Protocol List Non- unstable-objfw
- The returned array is deallocated with
free
. - objc_
copy ⚠Weak - objc_
destroy ⚠Weak - objc_
destruct ⚠Instance Deprecated Apple - objc_
dispose ⚠Class Pair Non- unstable-objfw
- objc_
duplicate ⚠Class Apple - objc_
end_ ⚠catch gnustep-1-7
, or Apple and not (macOS and x86) - objc_
enumeration ⚠Mutation Apple or unstable-objfw
- objc_
exception_ ⚠rethrow Apple and non- gnustep-1-7
and not (macOS and x86) - objc_
exception_ ⚠throw - See
objc-exception.h
. - objc_
exception_ ⚠try_ enter Apple and macOS and x86 - objc_
exception_ ⚠try_ exit Apple and macOS and x86 - objc_
getAssociated ⚠Object Non- unstable-objfw
- objc_
getClass ⚠ - objc_
getClass ⚠List - objc_
getFuture ⚠Class Deprecated Apple - objc_
getMeta ⚠Class Non- unstable-objfw
- objc_
getProtocol ⚠Non- unstable-objfw
- objc_
getRequired ⚠Class - objc_
init ⚠Weak - objc_
load ⚠Weak - objc_
load ⚠Weak Retained - objc_
look ⚠UpClass - objc_
move ⚠Weak - objc_
msgSend ⚠Non- unstable-objfw
- objc_
msgSend ⚠Super Apple - objc_
msgSend ⚠Super_ stret Apple and non-AArch64 - objc_
msgSend_ ⚠fp2ret Apple and x86-64 - objc_
msgSend_ ⚠fpret Non- unstable-objfw
and (x86-64 or x86) - objc_
msgSend_ ⚠stret Non- unstable-objfw
and non-AArch64 - objc_
msg_ ⚠lookup gnustep-1-7
orunstable-objfw
- objc_
msg_ ⚠lookup_ stret unstable-objfw
- objc_
msg_ ⚠lookup_ super gnustep-1-7
orunstable-objfw
- objc_
msg_ ⚠lookup_ super_ stret unstable-objfw
- objc_
register ⚠Class Pair - objc_
register ⚠Protocol Non- unstable-objfw
- objc_
release ⚠ - objc_
remove ⚠Associated Objects Non- unstable-objfw
- objc_
remove ⚠Exception Handler Apple and macOS and non-x86 - objc_
retain ⚠ - objc_
retain ⚠Autorelease - objc_
retain ⚠Autorelease Return Value - objc_
retain ⚠Autoreleased Return Value - objc_
retain ⚠Block - objc_
setAssociated ⚠Object Non- unstable-objfw
- objc_
setEnumeration ⚠Mutation Handler Apple or unstable-objfw
- objc_
setException ⚠Matcher Apple and not (macOS and x86) - objc_
setException ⚠Preprocessor Apple and not (macOS and x86) - objc_
setForward ⚠Handler Apple or unstable-objfw
- objc_
setUncaught ⚠Exception Handler Apple and not (macOS and x86), or unstable-objfw
- objc_
store ⚠Strong - objc_
store ⚠Weak - objc_
sync_ ⚠enter - objc_
sync_ ⚠exit - objc_
terminate ⚠Apple and not (macOS and x86) - object_
copy ⚠Deprecated Apple - object_
dispose ⚠Deprecated Non- unstable-objfw
- object_
getClass ⚠ - object_
getClass ⚠Name - object_
getIndexed ⚠Ivars Non- unstable-objfw
- object_
getInstance ⚠Variable Deprecated Non- unstable-objfw
- object_
getIvar ⚠Non- unstable-objfw
- object_
isClass ⚠Apple - object_
setClass ⚠ - object_
setInstance ⚠Variable Deprecated Non- unstable-objfw
- object_
setIvar ⚠Non- unstable-objfw
- property_
copy ⚠Attribute List Non- unstable-objfw
- The returned array is deallocated with
free
. - property_
copy ⚠Attribute Value Non- unstable-objfw
- property_
getAttributes ⚠Non- unstable-objfw
- property_
getName ⚠Non- unstable-objfw
- protocol_
addMethod ⚠Description Non- unstable-objfw
- protocol_
addProperty ⚠Non- unstable-objfw
- protocol_
addProtocol ⚠Non- unstable-objfw
- protocol_
conforms ⚠ToProtocol - protocol_
copy ⚠Method Description List Non- unstable-objfw
- The returned array is deallocated with
free
. - protocol_
copy ⚠Property List Non- unstable-objfw
- The returned array is deallocated with
free
. - protocol_
copy ⚠Protocol List Non- unstable-objfw
- The returned array is deallocated with
free
. - protocol_
getMethod ⚠Description Non- unstable-objfw
- protocol_
getName ⚠ - protocol_
getProperty ⚠Non- unstable-objfw
- protocol_
isEqual ⚠ - sel_
getName ⚠ - sel_
getUid ⚠Non- unstable-objfw
- sel_
isEqual ⚠ - sel_
isMapped ⚠Apple - sel_
register ⚠Name
Type Aliases§
- BOOL
- The Objective-C
BOOL
type. - IMP
- A nullable pointer to the start of a method implementation.
- NSInteger
- A signed integer value type.
- NSUInteger
- Describes an unsigned integer.
- SEL
- An immutable pointer to a selector.
- id
- A mutable pointer to an object / instance.
- objc_
Association Policy - Policies related to associative references.
- objc_
exception_ handler Apple and macOS and non-x86 - Remember that this is non-null!
- objc_
exception_ matcher Apple and not (macOS and x86) - Remember that this is non-null!
- objc_
exception_ preprocessor Apple and not (macOS and x86) - Remember that this is non-null!
- objc_
uncaught_ exception_ handler Apple and not (macOS and x86) - Remember that this is non-null!