1 ! Copyright (C) 2006, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.strings arrays assocs
4 combinators compiler kernel math namespaces make parser
5 prettyprint prettyprint.sections quotations sequences strings
6 words cocoa.runtime io macros memoize debugger
7 io.encodings.ascii effects compiler.generator libc libc.private ;
10 : make-sender ( method function -- quot )
11 [ over first , f , , second , \ alien-invoke , ] [ ] make ;
13 : sender-stub-name ( method function -- string )
14 [ % "_" % unparse % ] "" make ;
16 : sender-stub ( method function -- word )
17 [ sender-stub-name f <word> dup ] 2keep
18 over first large-struct? [ "_stret" append ] when
21 SYMBOL: message-senders
22 SYMBOL: super-message-senders
24 message-senders global [ H{ } assoc-like ] change-at
25 super-message-senders global [ H{ } assoc-like ] change-at
27 : cache-stub ( method function hash -- )
29 over get [ 2drop ] [ over >r sender-stub r> set ] if
32 : cache-stubs ( method -- )
34 "objc_msgSendSuper" super-message-senders get cache-stub
35 "objc_msgSend" message-senders get cache-stub ;
37 : <super> ( receiver -- super )
38 "objc-super" <c-object> [
39 >r dup object_getClass class_getSuperclass r>
42 [ set-objc-super-receiver ] keep ;
44 TUPLE: selector name object ;
46 MEMO: <selector> ( name -- sel ) f \ selector boa ;
48 : selector ( selector -- alien )
49 dup object>> expired? [
50 dup name>> sel_registerName
51 [ >>object drop ] keep
58 objc-methods global [ H{ } assoc-like ] change-at
60 : lookup-method ( selector -- method )
61 dup objc-methods get at
62 [ ] [ "No such method: " prepend throw ] ?if ;
64 : make-dip ( quot n -- quot' )
66 \ >r <repetition> >quotation -rot
67 \ r> <repetition> >quotation 3append ;
69 MEMO: make-prepare-send ( selector method super? -- quot )
72 swap <selector> , \ selector ,
74 swap second length 2 - make-dip ;
76 MACRO: (send) ( selector super? -- quot )
77 >r dup lookup-method r>
78 [ make-prepare-send ] 2keep
79 super-message-senders message-senders ? get at
80 [ slip execute ] 2curry ;
82 : send ( receiver args... selector -- return... ) f (send) ; inline
84 \ send soft "break-after" set-word-prop
86 : super-send ( receiver args... selector -- return... ) t (send) ; inline
88 \ super-send soft "break-after" set-word-prop
90 ! Runtime introspection
91 : (objc-class) ( string word -- class )
93 [ ] [ "No such class: " prepend throw ] ?if ; inline
95 : objc-class ( string -- class )
96 \ objc_getClass (objc-class) ;
98 : objc-protocol ( string -- class )
99 \ objc_getProtocol (objc-class) ;
101 : objc-meta-class ( string -- class )
102 \ objc_getMetaClass (objc-class) ;
104 SYMBOL: objc>alien-types
118 { "?" "unknown_type" }
123 "ptrdiff_t" heap-size {
137 assoc-union objc>alien-types set-global
139 ! The transpose of the above map
140 SYMBOL: alien>objc-types
142 objc>alien-types get [ swap ] assoc-map
144 "ptrdiff_t" heap-size {
146 { "NSPoint" "{_NSPoint=ff}" }
147 { "NSRect" "{_NSRect={_NSPoint=ff}{_NSSize=ff}}" }
148 { "NSSize" "{_NSSize=ff}" }
149 { "NSRange" "{_NSRange=II}" }
155 { "NSPoint" "{CGPoint=dd}" }
156 { "NSRect" "{CGRect={CGPoint=dd}{CGSize=dd}}" }
157 { "NSSize" "{CGSize=dd}" }
158 { "NSRange" "{_NSRange=QQ}" }
164 assoc-union alien>objc-types set-global
166 : objc-struct-type ( i string -- ctype )
167 2dup CHAR: = -rot index-from swap subseq
168 dup c-types get key? [
169 "Warning: no such C type: " write dup print
173 : (parse-objc-type) ( i string -- ctype )
174 2dup nth >r >r 1+ r> r> {
175 { [ dup "rnNoORV" member? ] [ drop (parse-objc-type) ] }
176 { [ dup CHAR: ^ = ] [ 3drop "void*" ] }
177 { [ dup CHAR: { = ] [ drop objc-struct-type ] }
178 { [ dup CHAR: [ = ] [ 3drop "void*" ] }
179 [ 2nip 1string objc>alien-types get at ]
182 : parse-objc-type ( string -- ctype ) 0 swap (parse-objc-type) ;
184 : method-arg-type ( method i -- type )
185 method_copyArgumentType
186 [ ascii alien>string parse-objc-type ] keep
189 : method-arg-types ( method -- args )
190 dup method_getNumberOfArguments
191 [ method-arg-type ] with map ;
193 : method-return-type ( method -- ctype )
194 method_copyReturnType
195 [ ascii alien>string parse-objc-type ] keep
198 : register-objc-method ( method -- )
199 dup method-return-type over method-arg-types 2array
201 swap method_getName sel_getName
202 objc-methods get set-at ;
204 : (register-objc-methods) ( methods count -- methods )
205 over [ void*-nth register-objc-method ] curry each ;
207 : register-objc-methods ( class -- )
208 0 <uint> [ class_copyMethodList ] keep *uint
209 (register-objc-methods) (free) ;
211 : class-exists? ( string -- class ) objc_getClass >boolean ;
213 : unless-defined ( class quot -- )
214 >r class-exists? r> unless ; inline
216 : define-objc-class-word ( name quot -- )
218 over , , \ unless-defined , dup , \ objc-class ,
219 ] [ ] make >r "cocoa.classes" create r>
220 (( -- class )) define-declared ;
222 : import-objc-class ( name quot -- )
224 dupd define-objc-class-word
227 objc-class register-objc-methods
228 objc-meta-class register-objc-methods
231 : root-class ( class -- root )
232 dup class_getSuperclass [ root-class ] [ ] ?if ;