From c1aa849cf5b5c9b667fbb9aafdfd4314eedd6bb4 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Mon, 9 Apr 2012 18:18:20 -0700 Subject: [PATCH] Add the ability to create classes and implement methods --- base.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/base.rs b/base.rs index 8ad520f..4d30981 100644 --- a/base.rs +++ b/base.rs @@ -1,13 +1,23 @@ use std; type id = libc::intptr_t; +type Class = libc::intptr_t; +type IMP = *u8; type SEL = libc::intptr_t; const nil : id = 0 as id; native mod objc { + fn class_addMethod(cls : Class, + name : SEL, + imp : IMP, + types : *libc::c_char) -> bool; + fn objc_allocateClassPair(superclass : Class, + name : *libc::c_char, + extraBytes : libc::size_t) -> Class; fn objc_getClass(name : *libc::c_char) -> id; fn objc_msgSend(theReceiver : id, theSelector : SEL) -> id; + fn objc_registerClassPair(cls : Class); fn sel_registerName(name : *libc::c_char) -> SEL; } @@ -26,3 +36,33 @@ fn test_nsapp() { io::println(#fmt("nsapp: %d", (nsapp as int))); } +#[test] +fn test_custom_obj() { + crust fn MyObject_doSomething(this : id, _sel : SEL) -> id { + io::println("doSomething"); + ret this; + } + + let NSObject = str::as_c_str("NSObject") { |s| + objc::objc_getClass(s) + }; + let MyObject = str::as_c_str("MyObject") { |s| + objc::objc_allocateClassPair(NSObject, s, 0 as libc::size_t) + }; + let doSomething = str::as_c_str("doSomething") { |s| + objc::sel_registerName(s) + }; + let _ = str::as_c_str("@@:") { |types| + objc::class_addMethod(MyObject, doSomething, MyObject_doSomething, + types) + }; + objc::objc_registerClassPair(MyObject); + + let alloc = str::as_c_str("alloc") { |s| objc::sel_registerName(s) }; + let init = str::as_c_str("init") { |s| objc::sel_registerName(s) }; + + let mut obj = objc::objc_msgSend(MyObject, alloc); + obj = objc::objc_msgSend(obj, init); + objc::objc_msgSend(obj, doSomething); +} +