One can now create (at runtime) objective-c classes and add methods

to them to get them to do things.  See lab/objc-create-class.ss for
a cute example.
This commit is contained in:
Abdulaziz Ghuloum
2008-09-29 01:40:58 -04:00
parent 0a7a3a8266
commit cd4e12be68
4 changed files with 394 additions and 117 deletions
+26 -2
View File
@@ -2,6 +2,11 @@
(import (ikarus) (objc) (Cocoa) (Cocoa helpers))
(define-class NSTableView)
(define-class NSTableColumn)
(define-class NSColor)
(define-class NSButton)
(define-class NSImageView)
(define pool [$ [$ NSAutoreleasePool alloc] init])
@@ -55,14 +60,33 @@
(define win
[$ [$ NSWindow alloc]
initWithContentRect: '#(#(50 50) #(600 400))
initWithContentRect: '#(#(400 500) #(400 500))
styleMask: style
backing: backing
defer: #f])
[$ win setTitle: (nsstring "Hello Ikarus")]
;[$ win setAlphaValue: 3/4] ; cute
#; ; button test
(let ([btn [$ [$ NSButton alloc] init]])
[$ btn setTitle: (nsstring "Quit")]
[$ btn setTarget: NSApp]
[$ btn setAction: (get-selector "terminate:")]
[$ win setContentView: btn])
#;
(let ([table [$ [$ NSTableView alloc] init]])
[$ table setBackgroundColor: [$ NSColor blueColor]]
(printf "~s\n" [$ table headerView])
(let ([col [$ [$ NSTableColumn alloc] init]])
[$ table addTableColumn: col]
[$ [$ col headerCell] setStringValue: (nsstring "header")])
[$ win setContentView: table]
[$ win setDelegate: table])
[$ win makeKeyAndOrderFront: win]
;[$ win setAlphaValue: 0.5] ; cute
[$ NSApp run]
[$ pool release]
+51
View File
@@ -0,0 +1,51 @@
#!/usr/bin/env ikarus --r6rs-script
(import (ikarus) (objc))
(define-framework Cocoa)
;(load-shared-object "IKFoo.dylib")
(define who (car (command-line)))
(define (println x) (printf "~a\n" x))
(define (print-classes)
(for-each println
(list-sort string<?
(map class-name (get-class-list)))))
(define (parents x)
(let ([p (class-parent x)])
(if p (cons p (parents p)) '())))
(define (ivar-info x)
`(ivar name: ,(ivar-name x)
type: ,(ivar-type x)
offset: ,(ivar-offset x)))
(define (print-class-methods x)
(define (public? x)
(and (> (string-length x) 0)
(not (char=? (string-ref x 0) #\_))))
(let ([x (or (get-class x) (error who "cannot find class" x))])
(printf "instance size = ~s\n" (class-instance-size x))
(printf "parents = ~s\n"
(map (lambda (x)
(cons (class-name x)
(class-instance-size x)))
(parents x)))
(printf "ivars=~s\n" (map ivar-info (class-ivars x)))
#;
(for-each println
(list-sort string<?
(filter public?
(map method-name (class-methods x)))))))
(apply
(case-lambda
[() (print-classes)]
[(x) (print-class-methods x)]
[args
(error who "supply either 0 or 1 arguments")])
(cdr (command-line)))
+17
View File
@@ -0,0 +1,17 @@
#!/usr/bin/env ikarus --r6rs-script
(import (ikarus) (objc))
(define-framework Cocoa)
(define-class NSObject)
(define IKFact
(create-class "IKFact" NSObject '() #f))
(class-add-class-method IKFact 'fact: 'int '(class selector int)
(trace-lambda fact (self sel n)
(if (zero? n)
1
(* n [$ self fact: (sub1 n)]))))
(printf "(fact 5) = ~s\n" [$ IKFact fact: 5])