-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for the GNUstep Objective-C runtime #27
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,31 @@ | ||
language: rust | ||
rust: | ||
- stable | ||
- beta | ||
- nightly | ||
sudo: false | ||
install: | ||
- if [ "$TRAVIS_OS_NAME" = "linux" ]; then export FEATURE="gnustep"; export CC="clang"; export CXX="clang++"; else export FEATURE=""; fi | ||
- if [[ "$FEATURE" == *"gnustep"* ]]; then git clone https:/gnustep/libobjc2.git; fi | ||
- if [[ "$FEATURE" == *"gnustep"* ]]; then mkdir libobjc2/build; pushd libobjc2/build; fi | ||
- if [[ "$FEATURE" == *"gnustep"* ]]; then cmake -DCMAKE_INSTALL_PREFIX:PATH=$HOME/libobjc2_staging ../; fi | ||
- if [[ "$FEATURE" == *"gnustep"* ]]; then make install; fi | ||
- if [[ "$FEATURE" == *"gnustep"* ]]; then export CPATH=$HOME/libobjc2_staging/include:$CPATH; export LIBRARY_PATH=$HOME/libobjc2_staging/lib:$LIBRARY_PATH; LD_LIBRARY_PATH=$HOME/libobjc2_staging/lib:$LD_LIBRARY_PATH; fi | ||
- if [[ "$FEATURE" == *"gnustep"* ]]; then popd; fi | ||
- if [ -n "$FEATURE" ]; then export FEATURES="--features $FEATURE"; else export FEATURES=""; fi; | ||
script: | ||
- cargo build $FEATURES | ||
- cargo test $FEATURES | ||
- cargo doc $FEATURES | ||
env: | ||
notifications: | ||
email: | ||
on_success: never | ||
os: | ||
- linux | ||
- osx | ||
addons: | ||
apt: | ||
packages: | ||
- clang-3.7 | ||
- cmake |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
use std::any::Any; | ||
use std::mem; | ||
|
||
use runtime::{Class, Object, Sel, Super, self}; | ||
|
||
/// Types that may be sent Objective-C messages. | ||
|
@@ -31,7 +30,7 @@ fn msg_send_fn<R: Any>() -> unsafe extern fn(*mut Object, Sel, ...) -> R { | |
} | ||
} | ||
|
||
#[cfg(target_arch = "x86")] | ||
#[cfg(all(target_arch = "x86", not(feature = "gnustep")))] | ||
fn msg_send_super_fn<R: Any>() -> unsafe extern fn(*mut Object, Sel, ...) -> R { | ||
let size = mem::size_of::<R>(); | ||
if size == 0 || size == 1 || size == 2 || size == 4 || size == 8 { | ||
|
@@ -55,7 +54,7 @@ fn msg_send_fn<R>() -> unsafe extern fn(*mut Object, Sel, ...) -> R { | |
} | ||
} | ||
|
||
#[cfg(target_arch = "x86_64")] | ||
#[cfg(all(target_arch = "x86_64", not(feature = "gnustep")))] | ||
fn msg_send_super_fn<R>() -> unsafe extern fn(*const Super, Sel, ...) -> R { | ||
if mem::size_of::<R>() <= 16 { | ||
unsafe { mem::transmute(runtime::objc_msgSendSuper) } | ||
|
@@ -83,7 +82,7 @@ fn msg_send_fn<R: Any>() -> unsafe extern fn(*mut Object, Sel, ...) -> R { | |
} | ||
} | ||
|
||
#[cfg(target_arch = "arm")] | ||
#[cfg(all(target_arch = "arm", not(feature = "gnustep")))] | ||
fn msg_send_super_fn<R: Any>() -> unsafe extern fn(*mut Object, Sel, ...) -> R { | ||
use std::any::TypeId; | ||
|
||
|
@@ -98,15 +97,15 @@ fn msg_send_super_fn<R: Any>() -> unsafe extern fn(*mut Object, Sel, ...) -> R { | |
} | ||
} | ||
|
||
#[cfg(target_arch = "aarch64")] | ||
#[cfg(all(target_arch = "aarch64", not(feature = "gnustep")))] | ||
fn msg_send_fn<R>() -> unsafe extern fn(*mut Object, Sel, ...) -> R { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None of the other There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We don't have an implementation for aarch64, so we don't want to define it here. Also, a mips implementation is still missing because I didn't have the time to figure out the calling conventions (specifically when to use |
||
// stret is not even available in arm64. | ||
// https://twitter.com/gparker/status/378079715824660480 | ||
|
||
unsafe { mem::transmute(runtime::objc_msgSend) } | ||
} | ||
|
||
#[cfg(target_arch = "aarch64")] | ||
#[cfg(all(target_arch = "aarch64", not(feature ="gnustep")))] | ||
fn msg_send_super_fn<R>() -> unsafe extern fn(*const Super, Sel, ...) -> R { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aarch64 was changed to arm here, was that intentional? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, this should still be aarch64. |
||
unsafe { mem::transmute(runtime::objc_msgSendSuper) } | ||
} | ||
|
@@ -134,6 +133,10 @@ pub trait MessageArguments { | |
macro_rules! message_args_impl { | ||
($($a:ident : $t:ident),*) => ( | ||
impl<$($t),*> MessageArguments for ($($t,)*) { | ||
#[cfg(any(not(feature="gnustep"), | ||
any(target_arch = "arm", | ||
target_arch = "x86", | ||
target_arch = "x86_64")))] | ||
unsafe fn send<T, R>(self, obj: *mut T, sel: Sel) -> R | ||
where T: Message, R: Any { | ||
let msg_send_fn = msg_send_fn::<R>(); | ||
|
@@ -145,6 +148,25 @@ macro_rules! message_args_impl { | |
}) | ||
} | ||
|
||
#[cfg(all(feature="gnustep", | ||
not(any(target_arch = "arm", | ||
target_arch = "x86", | ||
target_arch = "x86_64"))))] | ||
unsafe fn send<T, R>(self, obj: *mut T, sel: Sel) -> R | ||
where T: Message, R: Any { | ||
let mut receiver = obj as *mut Object; | ||
let nil: *mut Object = ::std::ptr::null_mut(); | ||
let ref slot = *runtime::objc_msg_lookup_sender(&mut receiver as *mut *mut Object, sel, nil); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why can't this use Otherwise the only reason to use the slot lookup seems to be so that you can keep the slot and call the method repeatedly, but since we get the slot anew each time that doesn't seem like it'd be a benefit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we can implement without slots (just using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can only use You're right that the slot mechanism is immensely useful for caching IMPs, but I don't see how to build a platform-independent abstraction over this. The reason is that caching IMPs is generally unsafe on the Apple runtime. Things like isa-swizzling, |
||
let imp_fn = slot.method; | ||
let imp_fn: unsafe extern fn(*mut Object, Sel $(, $t)*) -> R = | ||
mem::transmute(imp_fn); | ||
let ($($a,)*) = self; | ||
objc_try!({ | ||
imp_fn(receiver as *mut Object, sel $(, $a)*) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does calling this function on a slot handle unrecognized selectors properly? The reason this crate is using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it handles unrecognised selectors. The runtime has a hook |
||
}) | ||
} | ||
|
||
#[cfg(not(feature="gnustep"))] | ||
unsafe fn send_super<T, R>(self, obj: *mut T, superclass: &Class, sel: Sel) -> R | ||
where T: Message, R: Any { | ||
let msg_send_fn = msg_send_super_fn::<R>(); | ||
|
@@ -156,6 +178,20 @@ macro_rules! message_args_impl { | |
msg_send_fn(&sup, sel $(, $a)*) | ||
}) | ||
} | ||
|
||
#[cfg(feature="gnustep")] | ||
unsafe fn send_super<T, R>(self, obj: *mut T, superclass: &Class, sel: Sel) -> R | ||
where T: Message, R: Any { | ||
let sup = Super { receiver: obj as *mut Object, superclass: superclass }; | ||
let ref slot = *runtime::objc_slot_lookup_super(&sup, sel); | ||
let imp_fn = slot.method; | ||
let imp_fn: unsafe extern fn(*mut Object, Sel $(, $t)*) -> R = | ||
mem::transmute(imp_fn); | ||
let ($($a,)*) = self; | ||
objc_try!({ | ||
imp_fn(obj as *mut Object, sel $(, $a)*) | ||
}) | ||
} | ||
} | ||
); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "test_ns_object" | ||
version = "0.0.1" | ||
authors = ["Niels Grewe"] | ||
|
||
description = "Mock implementation of NSObject for tests" | ||
repository = "http:/SSheldon/rust-objc" | ||
license = "MIT" | ||
|
||
build = "build.rs" | ||
|
||
[features] | ||
gnustep = [ "gcc" ] | ||
|
||
[lib] | ||
name = "test_ns_object" | ||
path = "lib.rs" | ||
|
||
[build-dependencies.gcc] | ||
gcc = "0.3" | ||
optional = true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <objc/runtime.h> | ||
#include <stdint.h> | ||
/** | ||
* This is a mock implementation of NSObject, which will be linked against | ||
* the tests in order to provide a superclass for them. | ||
*/ | ||
__attribute__((objc_root_class)) | ||
@interface NSObject | ||
{ | ||
Class isa; | ||
} | ||
@end | ||
|
||
@implementation NSObject | ||
|
||
+ (id)alloc | ||
{ | ||
return class_createInstance(self, 0); | ||
} | ||
|
||
- (id)init | ||
{ | ||
return self; | ||
} | ||
|
||
- (id)self | ||
{ | ||
return self; | ||
} | ||
|
||
- (uintptr_t)hash | ||
{ | ||
return (uintptr_t)(void*)self; | ||
} | ||
|
||
- (void)dealloc | ||
{ | ||
object_dispose(self); | ||
} | ||
|
||
- (NSObject*)description | ||
{ | ||
return nil; | ||
} | ||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#[cfg(feature="gnustep")] | ||
extern crate gcc; | ||
#[cfg(feature="gnustep")] | ||
use std::path::PathBuf; | ||
|
||
|
||
#[cfg(not(feature="gnustep"))] | ||
fn compile() { | ||
} | ||
|
||
#[cfg(feature="gnustep")] | ||
fn compile() { | ||
gcc::Config::new().flag("-lobjc") | ||
.flag("-fobjc-runtime=gnustep-1.8") | ||
.flag("-fno-objc-legacy-dispatch") | ||
.file("NSObject.m") | ||
.compile("libNSObject.a"); | ||
let path = ::std::env::var_os("OUT_DIR").map(PathBuf::from).unwrap(); | ||
println!("cargo:rustc-link-search=native={}", path.display()); | ||
} | ||
fn main() { | ||
compile(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#![crate_name = "test_ns_object"] | ||
#![crate_type = "lib"] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this have to be
cfg
'd out? It didn't look like any runtime methods were removed in gnustep, so I'd expect it to still compile.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The GNUstep runtime doesn't implement sending messages to super via
objc_msgSendSuper
. The GNUstep-specific implementation for sending messages to super is at ll. 182–194.