Skip to content

Commit

Permalink
Added a verify_message feature.
Browse files Browse the repository at this point in the history
This fixes #10.
  • Loading branch information
SSheldon committed Nov 7, 2015
1 parent c6756b5 commit 9109614
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ exclude = [".gitignore", ".travis.yml", "ios-tests/**", "xtests/**"]

[features]
exception = ["objc_exception"]
verify_message = []

[dependencies]
libc = ">= 0.1, < 0.3"
Expand Down
40 changes: 40 additions & 0 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ message_args_impl!(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J, k

#[doc(hidden)]
#[inline(always)]
#[cfg(not(feature = "verify_message"))]
pub unsafe fn send_message<T, A, R>(obj: *const T, sel: Sel, args: A)
-> Result<R, String>
where T: Message, A: MessageArguments, R: Any {
Expand All @@ -184,12 +185,51 @@ pub unsafe fn send_message<T, A, R>(obj: *const T, sel: Sel, args: A)

#[doc(hidden)]
#[inline(always)]
#[cfg(feature = "verify_message")]
pub unsafe fn send_message<T, A, R>(obj: *const T, sel: Sel, args: A)
-> Result<R, String>
where T: Message, A: MessageArguments + ::verify::EncodeArguments,
R: Any + ::Encode {
use verify::verify_message_signature;

let cls = if obj.is_null() {
return Err(format!("Messaging {:?} to nil", sel));
} else {
(*(obj as *const Object)).class()
};

verify_message_signature::<A, R>(cls, sel).map(|_| {
args.send(obj as *mut T, sel)
})
}

#[doc(hidden)]
#[inline(always)]
#[cfg(not(feature = "verify_message"))]
pub unsafe fn send_super_message<T, A, R>(obj: *const T, superclass: &Class,
sel: Sel, args: A) -> Result<R, String>
where T: Message, A: MessageArguments, R: Any {
Ok(args.send_super(obj as *mut T, superclass, sel))
}

#[doc(hidden)]
#[inline(always)]
#[cfg(feature = "verify_message")]
pub unsafe fn send_super_message<T, A, R>(obj: *const T, superclass: &Class,
sel: Sel, args: A) -> Result<R, String>
where T: Message, A: MessageArguments + ::verify::EncodeArguments,
R: Any + ::Encode {
use verify::verify_message_signature;

if obj.is_null() {
return Err(format!("Messaging {:?} to nil", sel));
}

verify_message_signature::<A, R>(superclass, sel).map(|_| {
args.send_super(obj as *mut T, superclass, sel)
})
}

#[cfg(test)]
mod tests {
use runtime::Object;
Expand Down
2 changes: 2 additions & 0 deletions src/verify.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(not(feature = "verify_message"), allow(dead_code))]

use runtime::{Class, Object, Sel};
use {Encode, Encoding};

Expand Down

0 comments on commit 9109614

Please sign in to comment.