Skip to content

Instantly share code, notes, and snippets.

@fjolnir
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fjolnir/11325074 to your computer and use it in GitHub Desktop.
Save fjolnir/11325074 to your computer and use it in GitHub Desktop.
use std::libc;
use std::cast::transmute;
type Id = libc::uintptr_t;
type Sel = libc::uintptr_t;
#[link(name = "objc")]
extern {
fn sel_registerName(sel: &str) -> Sel;
fn objc_msgSend(object: Id, sel: Sel, ...) -> libc::uintptr_t;
fn objc_lookUpClass(name: &str) -> Id;
}
trait ObjcObject {
fn send<T>(&self, sel: &str) -> T;
}
impl ObjcObject for Id {
fn send<T>(&self, sel: &str) -> T {
let sel_ = unsafe { sel_registerName(sel) };
let imp: extern "C" fn(Id, Sel, ...) -> T = unsafe { std::cast::transmute(objc_msgSend) };
imp(*self, sel_)
}
}
fn objc_class(name: &str) -> Id {
println!("Looking up {}", name);
unsafe { objc_lookUpClass(name) }
}
fn main() {
let kls = objc_class("NSObject");
let hash: uint = kls.send("hash");
let instance: Id = kls.send("alloc");
println!("NSObject={} +[NSObject hash]={} +[NSObject alloc]={}", kls, hash, instance);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment