Skip to content

Commit 8d49116

Browse files
authored
Merge pull request #19 from madsmtm/sys-crates
Add `objc_sys` crate
2 parents d48dc10 + a97a2ea commit 8d49116

36 files changed

+1495
-271
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ members = [
77
"objc_foundation",
88
"objc_foundation_derive",
99
"objc_id",
10+
"objc_sys",
1011
"objc_test_utils",
1112
]
1213
exclude = ["objc/tests-ios"]

objc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ unstable_autoreleasesafe = []
2828

2929
[dependencies]
3030
malloc_buf = "1.0"
31+
objc_sys = { path = "../objc_sys" }
3132
objc-encode = { path = "../objc_encode", version = "1.0" }
3233
objc_exception = { path = "../objc_exception", version = "0.1", optional = true }

objc/src/cache.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ impl CachedSel {
2626
// It should be fine to use `Relaxed` ordering here because `sel_registerName` is
2727
// thread-safe.
2828
if ptr.is_null() {
29-
let sel = runtime::sel_registerName(name.as_ptr() as *const _);
30-
self.ptr.store(sel.as_ptr() as *mut _, Ordering::Relaxed);
31-
sel
29+
let ptr = runtime::sel_registerName(name.as_ptr() as *const _);
30+
self.ptr.store(ptr as *mut _, Ordering::Relaxed);
31+
Sel::from_ptr(ptr as *const _)
3232
} else {
3333
Sel::from_ptr(ptr)
3434
}
@@ -56,7 +56,7 @@ impl CachedClass {
5656
// `Relaxed` should be fine since `objc_getClass` is thread-safe.
5757
let ptr = self.ptr.load(Ordering::Relaxed);
5858
if ptr.is_null() {
59-
let cls = runtime::objc_getClass(name.as_ptr() as *const _);
59+
let cls = runtime::objc_getClass(name.as_ptr() as *const _) as *const Class;
6060
self.ptr.store(cls as *mut _, Ordering::Relaxed);
6161
cls.as_ref()
6262
} else {

objc/src/declare.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ pub struct ClassDecl {
118118
impl ClassDecl {
119119
fn with_superclass(name: &str, superclass: Option<&Class>) -> Option<ClassDecl> {
120120
let name = CString::new(name).unwrap();
121-
let super_ptr = superclass.map_or(ptr::null(), |c| c);
121+
let super_ptr = superclass.map_or(ptr::null(), |c| c) as _;
122122
let cls = unsafe { runtime::objc_allocateClassPair(super_ptr, name.as_ptr(), 0) };
123123
if cls.is_null() {
124124
None
125125
} else {
126-
Some(ClassDecl { cls })
126+
Some(ClassDecl { cls: cls as _ })
127127
}
128128
}
129129

@@ -182,7 +182,12 @@ impl ClassDecl {
182182
);
183183

184184
let types = method_type_encoding(&F::Ret::ENCODING, encs);
185-
let success = runtime::class_addMethod(self.cls, sel, func.imp(), types.as_ptr());
185+
let success = runtime::class_addMethod(
186+
self.cls as _,
187+
sel.as_ptr() as _,
188+
Some(func.imp()),
189+
types.as_ptr(),
190+
);
186191
assert!(success != NO, "Failed to add method {:?}", sel);
187192
}
188193

@@ -212,7 +217,12 @@ impl ClassDecl {
212217

213218
let types = method_type_encoding(&F::Ret::ENCODING, encs);
214219
let metaclass = (*self.cls).metaclass() as *const _ as *mut _;
215-
let success = runtime::class_addMethod(metaclass, sel, func.imp(), types.as_ptr());
220+
let success = runtime::class_addMethod(
221+
metaclass,
222+
sel.as_ptr() as _,
223+
Some(func.imp()),
224+
types.as_ptr(),
225+
);
216226
assert!(success != NO, "Failed to add class method {:?}", sel);
217227
}
218228

@@ -227,7 +237,13 @@ impl ClassDecl {
227237
let size = mem::size_of::<T>();
228238
let align = log2_align_of::<T>();
229239
let success = unsafe {
230-
runtime::class_addIvar(self.cls, c_name.as_ptr(), size, align, encoding.as_ptr())
240+
runtime::class_addIvar(
241+
self.cls as _,
242+
c_name.as_ptr(),
243+
size,
244+
align,
245+
encoding.as_ptr(),
246+
)
231247
};
232248
assert!(success != NO, "Failed to add ivar {}", name);
233249
}
@@ -238,7 +254,7 @@ impl ClassDecl {
238254
///
239255
/// If the protocol wasn't successfully added.
240256
pub fn add_protocol(&mut self, proto: &Protocol) {
241-
let success = unsafe { runtime::class_addProtocol(self.cls, proto) };
257+
let success = unsafe { runtime::class_addProtocol(self.cls as _, proto.as_ptr()) };
242258
assert!(success != NO, "Failed to add protocol {:?}", proto);
243259
}
244260

@@ -247,7 +263,7 @@ impl ClassDecl {
247263
pub fn register(self) -> &'static Class {
248264
unsafe {
249265
let cls = self.cls;
250-
runtime::objc_registerClassPair(cls);
266+
runtime::objc_registerClassPair(cls as _);
251267
// Forget self otherwise the class will be disposed in drop
252268
mem::forget(self);
253269
&*cls
@@ -258,7 +274,7 @@ impl ClassDecl {
258274
impl Drop for ClassDecl {
259275
fn drop(&mut self) {
260276
unsafe {
261-
runtime::objc_disposeClassPair(self.cls);
277+
runtime::objc_disposeClassPair(self.cls as _);
262278
}
263279
}
264280
}
@@ -275,7 +291,7 @@ impl ProtocolDecl {
275291
/// Returns [`None`] if the protocol couldn't be allocated.
276292
pub fn new(name: &str) -> Option<ProtocolDecl> {
277293
let c_name = CString::new(name).unwrap();
278-
let proto = unsafe { runtime::objc_allocateProtocol(c_name.as_ptr()) };
294+
let proto = unsafe { runtime::objc_allocateProtocol(c_name.as_ptr()) } as *mut Protocol;
279295
if proto.is_null() {
280296
None
281297
} else {
@@ -303,8 +319,8 @@ impl ProtocolDecl {
303319
let types = method_type_encoding(&Ret::ENCODING, encs);
304320
unsafe {
305321
runtime::protocol_addMethodDescription(
306-
self.proto,
307-
sel,
322+
self.proto as _,
323+
sel.as_ptr() as _,
308324
types.as_ptr(),
309325
is_required as BOOL,
310326
is_instance_method as BOOL,
@@ -333,15 +349,15 @@ impl ProtocolDecl {
333349
/// Adds a requirement on another protocol.
334350
pub fn add_protocol(&mut self, proto: &Protocol) {
335351
unsafe {
336-
runtime::protocol_addProtocol(self.proto, proto);
352+
runtime::protocol_addProtocol(self.proto as _, proto.as_ptr());
337353
}
338354
}
339355

340356
/// Registers the [`ProtocolDecl`], consuming it and returning a reference
341357
/// to the newly registered [`Protocol`].
342358
pub fn register(self) -> &'static Protocol {
343359
unsafe {
344-
runtime::objc_registerProtocol(self.proto);
360+
runtime::objc_registerProtocol(self.proto as _);
345361
&*self.proto
346362
}
347363
}

objc/src/message/apple/arm.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
use core::mem;
2+
use objc_sys::{objc_msgSend, objc_msgSendSuper, objc_msgSendSuper_stret, objc_msgSend_stret};
23

34
use super::MsgSendFn;
45
use crate::runtime::Imp;
56
use crate::{Encode, Encoding};
67

7-
// TODO: C-unwind
8-
extern "C" {
9-
fn objc_msgSend();
10-
fn objc_msgSend_stret();
11-
12-
fn objc_msgSendSuper();
13-
fn objc_msgSendSuper_stret();
14-
}
15-
168
/// Double-word sized fundamental data types don't use stret, but any
179
/// composite type larger than 4 bytes does.
1810
///

objc/src/message/apple/arm64.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1+
use objc_sys::{objc_msgSend, objc_msgSendSuper};
2+
13
use super::MsgSendFn;
24
use crate::runtime::Imp;
35
use crate::Encode;
46

5-
// TODO: C-unwind
6-
extern "C" {
7-
fn objc_msgSend();
8-
9-
fn objc_msgSendSuper();
10-
}
11-
127
/// `objc_msgSend_stret` is not even available in arm64.
138
///
149
/// <https://twitter.com/gparker/status/378079715824660480>

objc/src/message/apple/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use super::{Encode, Message, MessageArguments, MessageError, Super};
1+
use objc_sys::objc_super;
2+
3+
use super::{Encode, Message, MessageArguments, MessageError};
24
use crate::runtime::{Class, Imp, Object, Sel};
35

46
#[cfg(target_arch = "x86")]
@@ -43,11 +45,11 @@ where
4345
A: MessageArguments,
4446
R: Encode,
4547
{
46-
let sup = Super {
47-
receiver: obj as *mut T as *mut Object,
48-
superclass,
48+
let sup = objc_super {
49+
receiver: obj as *mut T as *mut Object as *mut _,
50+
super_class: superclass as *const Class as *const _,
4951
};
50-
let receiver = &sup as *const Super as *mut Object;
52+
let receiver = &sup as *const objc_super as *mut Object;
5153
let msg_send_fn = R::MSG_SEND_SUPER;
5254
objc_try!({ A::invoke(msg_send_fn, receiver, sel, args) })
5355
}

objc/src/message/apple/x86.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
use core::mem;
2+
use objc_sys::{
3+
objc_msgSend, objc_msgSendSuper, objc_msgSendSuper_stret, objc_msgSend_fpret,
4+
objc_msgSend_stret,
5+
};
26

37
use super::MsgSendFn;
48
use crate::runtime::Imp;
59
use crate::{Encode, Encoding};
610

7-
// TODO: C-unwind
8-
extern "C" {
9-
fn objc_msgSend();
10-
fn objc_msgSend_fpret();
11-
fn objc_msgSend_stret();
12-
13-
fn objc_msgSendSuper();
14-
fn objc_msgSendSuper_stret();
15-
}
16-
1711
/// Structures 1 or 2 bytes in size are placed in EAX.
1812
/// Structures 4 or 8 bytes in size are placed in: EAX and EDX.
1913
/// Structures of other sizes are placed at the address supplied by the caller.

objc/src/message/apple/x86_64.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
use core::mem;
2+
use objc_sys::{objc_msgSend, objc_msgSendSuper, objc_msgSendSuper_stret, objc_msgSend_stret};
23

34
use super::MsgSendFn;
45
use crate::runtime::Imp;
56
use crate::Encode;
67

7-
// TODO: C-unwind
8-
extern "C" {
9-
fn objc_msgSend();
10-
fn objc_msgSend_stret();
11-
12-
fn objc_msgSendSuper();
13-
fn objc_msgSendSuper_stret();
14-
}
15-
168
/// If the size of an object is larger than two eightbytes, it has class
179
/// MEMORY. If the type has class MEMORY, then the caller provides space for
1810
/// the return value and passes the address of this storage.
1911
///
2012
/// <http://people.freebsd.org/~obrien/amd64-elf-abi.pdf>
2113
impl<T: Encode> MsgSendFn for T {
14+
// TODO: Should we use objc_msgSend_fpret and objc_msgSend_fp2ret ?
2215
const MSG_SEND: Imp = {
2316
if mem::size_of::<T>() <= 16 {
2417
objc_msgSend

objc/src/message/gnustep.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
use core::mem;
2+
use objc_sys::{objc_msg_lookup, objc_msg_lookup_super, objc_super};
23

3-
use super::{Encode, Message, MessageArguments, MessageError, Super};
4-
use crate::runtime::{Class, Imp, Object, Sel};
5-
6-
extern "C" {
7-
fn objc_msg_lookup(receiver: *mut Object, op: Sel) -> Imp;
8-
fn objc_msg_lookup_super(sup: *const Super, sel: Sel) -> Imp;
9-
}
4+
use super::{Encode, Message, MessageArguments, MessageError};
5+
use crate::runtime::{Class, Object, Sel};
106

117
pub unsafe fn send_unverified<T, A, R>(obj: *const T, sel: Sel, args: A) -> Result<R, MessageError>
128
where
@@ -19,8 +15,8 @@ where
1915
}
2016

2117
let receiver = obj as *mut T as *mut Object;
22-
let msg_send_fn = objc_msg_lookup(receiver, sel);
23-
objc_try!({ A::invoke(msg_send_fn, receiver, sel, args) })
18+
let msg_send_fn = objc_msg_lookup(receiver as *mut _, sel.as_ptr() as *const _);
19+
objc_try!({ A::invoke(msg_send_fn.expect("Null IMP"), receiver, sel, args) })
2420
}
2521

2622
pub unsafe fn send_super_unverified<T, A, R>(
@@ -35,10 +31,10 @@ where
3531
R: Encode,
3632
{
3733
let receiver = obj as *mut T as *mut Object;
38-
let sup = Super {
39-
receiver: receiver,
40-
superclass: superclass,
34+
let sup = objc_super {
35+
receiver: receiver as *mut _,
36+
super_class: superclass as *const Class as *const _,
4137
};
42-
let msg_send_fn = objc_msg_lookup_super(&sup, sel);
43-
objc_try!({ A::invoke(msg_send_fn, receiver, sel, args) })
38+
let msg_send_fn = objc_msg_lookup_super(&sup, sel.as_ptr() as *const _);
39+
objc_try!({ A::invoke(msg_send_fn.expect("Null IMP"), receiver, sel, args) })
4440
}

0 commit comments

Comments
 (0)