1 ! Copyright (C) 2006, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.data alien.strings
4 arrays assocs classes.struct continuations combinators compiler
5 core-graphics.types stack-checker kernel math namespaces make
6 quotations sequences strings words cocoa.runtime cocoa.types io
7 macros memoize io.encodings.utf8 effects layouts libc lexer init
8 core-foundation fry generalizations specialized-arrays ;
9 QUALIFIED-WITH: alien.c-types c
12 SPECIALIZED-ARRAY: void*
14 : make-sender ( method function -- quot )
15 [ over first , f , , second , \ alien-invoke , ] [ ] make ;
17 : sender-stub ( method function -- word )
18 [ "( sender-stub )" f <word> dup ] 2dip
19 over first large-struct? [ "_stret" append ] when
20 make-sender dup infer define-declared ;
22 SYMBOL: message-senders
23 SYMBOL: super-message-senders
25 message-senders [ H{ } clone ] initialize
26 super-message-senders [ H{ } clone ] initialize
28 : cache-stub ( method assoc function -- )
29 '[ _ sender-stub ] cache drop ;
31 : cache-stubs ( method -- )
32 [ super-message-senders get "objc_msgSendSuper" cache-stub ]
33 [ message-senders get "objc_msgSend" cache-stub ]
36 : <super> ( receiver -- super )
37 [ ] [ object_getClass class_getSuperclass ] bi
38 objc-super <struct-boa> ;
40 TUPLE: selector name object ;
42 MEMO: <selector> ( name -- sel ) f \ selector boa ;
44 : selector ( selector -- alien )
45 dup object>> expired? [
46 dup name>> sel_registerName
47 [ >>object drop ] keep
54 objc-methods [ H{ } clone ] initialize
56 : lookup-method ( selector -- method )
57 dup objc-methods get at
58 [ ] [ "No such method: " prepend throw ] ?if ;
60 MEMO: make-prepare-send ( selector method super? -- quot )
63 swap <selector> , \ selector ,
65 swap second length 2 - '[ _ _ ndip ] ;
67 MACRO: (send) ( selector super? -- quot )
68 [ dup lookup-method ] dip
69 [ make-prepare-send ] 2keep
70 super-message-senders message-senders ? get at
73 : send ( receiver args... selector -- return... ) f (send) ; inline
75 : super-send ( receiver args... selector -- return... ) t (send) ; inline
77 ! Runtime introspection
78 SYMBOL: class-init-hooks
80 class-init-hooks [ H{ } clone ] initialize
82 : (objc-class) ( name word -- class )
83 2dup execute dup [ 2nip ] [
84 drop over class-init-hooks get at [ call( -- ) ] when*
85 2dup execute dup [ 2nip ] [
86 2drop "No such class: " prepend throw
90 : objc-class ( string -- class )
91 \ objc_getClass (objc-class) ;
93 : objc-protocol ( string -- class )
94 \ objc_getProtocol (objc-class) ;
96 : objc-meta-class ( string -- class )
97 \ objc_getMetaClass (objc-class) ;
99 SYMBOL: objc>alien-types
132 assoc-union objc>alien-types set-global
134 SYMBOL: objc>struct-types
137 { "_NSPoint" NSPoint }
138 { "NSPoint" NSPoint }
139 { "CGPoint" NSPoint }
146 { "_NSRange" NSRange }
147 { "NSRange" NSRange }
148 } objc>struct-types set-global
150 ! The transpose of the above map
151 SYMBOL: alien>objc-types
153 objc>alien-types get [ swap ] assoc-map
157 { NSPoint "{_NSPoint=ff}" }
158 { NSRect "{_NSRect={_NSPoint=ff}{_NSSize=ff}}" }
159 { NSSize "{_NSSize=ff}" }
160 { NSRange "{_NSRange=II}" }
166 { NSPoint "{CGPoint=dd}" }
167 { NSRect "{CGRect={CGPoint=dd}{CGSize=dd}}" }
168 { NSSize "{CGSize=dd}" }
169 { NSRange "{_NSRange=QQ}" }
175 assoc-union alien>objc-types set-global
177 : objc-struct-type ( i string -- ctype )
178 [ CHAR: = ] 2keep index-from swap subseq
179 objc>struct-types get at* [ drop void* ] unless ;
181 ERROR: no-objc-type name ;
183 : decode-type ( ch -- ctype )
184 1string dup objc>alien-types get at
185 [ ] [ no-objc-type ] ?if ;
187 : (parse-objc-type) ( i string -- ctype )
188 [ [ 1 + ] dip ] [ nth ] 2bi {
189 { [ dup "rnNoORV" member? ] [ drop (parse-objc-type) ] }
190 { [ dup CHAR: ^ = ] [ 3drop void* ] }
191 { [ dup CHAR: { = ] [ drop objc-struct-type ] }
192 { [ dup CHAR: [ = ] [ 3drop void* ] }
196 : parse-objc-type ( string -- ctype ) 0 swap (parse-objc-type) ;
198 : method-arg-type ( method i -- type )
199 method_copyArgumentType
200 [ utf8 alien>string parse-objc-type ] keep
203 : method-arg-types ( method -- args )
204 dup method_getNumberOfArguments iota
205 [ method-arg-type ] with map ;
207 : method-return-type ( method -- ctype )
208 method_copyReturnType
209 [ utf8 alien>string parse-objc-type ] keep
212 : register-objc-method ( method -- )
213 dup method-return-type over method-arg-types 2array
215 swap method_getName sel_getName
216 objc-methods get set-at ;
218 : each-method-in-class ( class quot -- )
219 [ { uint } [ class_copyMethodList ] with-out-parameters ] dip
221 [ <direct-void*-array> ] dip
222 [ each ] [ drop (free) ] 2bi
225 : register-objc-methods ( class -- )
226 [ register-objc-method ] each-method-in-class ;
228 : class-exists? ( string -- class ) objc_getClass >boolean ;
230 : define-objc-class-word ( quot name -- )
231 [ class-init-hooks get set-at ]
233 [ "cocoa.classes" create ] [ '[ _ objc-class ] ] bi
234 (( -- class )) define-declared
237 : import-objc-class ( name quot -- )
238 2dup swap define-objc-class-word
239 over class-exists? [ drop ] [ call( -- ) ] if
241 [ objc_getClass register-objc-methods ]
242 [ objc_getMetaClass register-objc-methods ] bi
245 : root-class ( class -- root )
246 dup class_getSuperclass [ root-class ] [ ] ?if ;