1 ! Copyright (C) 2006, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.strings arrays assocs
4 classes.struct continuations combinators compiler compiler.alien
5 stack-checker kernel math namespaces make quotations sequences
6 strings words cocoa.runtime io macros memoize io.encodings.utf8
7 effects libc libc.private lexer init core-foundation fry
8 generalizations specialized-arrays ;
11 SPECIALIZED-ARRAY: void*
13 : make-sender ( method function -- quot )
14 [ over first , f , , second , \ alien-invoke , ] [ ] make ;
16 : sender-stub ( method function -- word )
17 [ "( sender-stub )" f <word> dup ] 2dip
18 over first large-struct? [ "_stret" append ] when
19 make-sender dup infer define-declared ;
21 SYMBOL: message-senders
22 SYMBOL: super-message-senders
24 message-senders [ H{ } clone ] initialize
25 super-message-senders [ H{ } clone ] initialize
27 : cache-stub ( method assoc function -- )
28 '[ _ sender-stub ] cache drop ;
30 : cache-stubs ( method -- )
31 [ super-message-senders get "objc_msgSendSuper" cache-stub ]
32 [ message-senders get "objc_msgSend" cache-stub ]
35 : <super> ( receiver -- super )
36 [ ] [ object_getClass class_getSuperclass ] bi
37 objc-super <struct-boa> ;
39 TUPLE: selector name object ;
41 MEMO: <selector> ( name -- sel ) f \ selector boa ;
43 : selector ( selector -- alien )
44 dup object>> expired? [
45 dup name>> sel_registerName
46 [ >>object drop ] keep
53 objc-methods [ H{ } clone ] initialize
55 : lookup-method ( selector -- method )
56 dup objc-methods get at
57 [ ] [ "No such method: " prepend throw ] ?if ;
59 MEMO: make-prepare-send ( selector method super? -- quot )
62 swap <selector> , \ selector ,
64 swap second length 2 - '[ _ _ ndip ] ;
66 MACRO: (send) ( selector super? -- quot )
67 [ dup lookup-method ] dip
68 [ make-prepare-send ] 2keep
69 super-message-senders message-senders ? get at
72 : send ( receiver args... selector -- return... ) f (send) ; inline
74 : super-send ( receiver args... selector -- return... ) t (send) ; inline
76 ! Runtime introspection
77 SYMBOL: class-init-hooks
79 class-init-hooks [ H{ } clone ] initialize
81 : (objc-class) ( name word -- class )
82 2dup execute dup [ 2nip ] [
83 drop over class-init-hooks get at [ call( -- ) ] when*
84 2dup execute dup [ 2nip ] [
85 2drop "No such class: " prepend throw
89 : objc-class ( string -- class )
90 \ objc_getClass (objc-class) ;
92 : objc-protocol ( string -- class )
93 \ objc_getProtocol (objc-class) ;
95 : objc-meta-class ( string -- class )
96 \ objc_getMetaClass (objc-class) ;
98 SYMBOL: objc>alien-types
112 { "?" "unknown_type" }
117 "ptrdiff_t" heap-size {
131 assoc-union objc>alien-types set-global
133 ! The transpose of the above map
134 SYMBOL: alien>objc-types
136 objc>alien-types get [ swap ] assoc-map
138 "ptrdiff_t" heap-size {
140 { "NSPoint" "{_NSPoint=ff}" }
141 { "NSRect" "{_NSRect={_NSPoint=ff}{_NSSize=ff}}" }
142 { "NSSize" "{_NSSize=ff}" }
143 { "NSRange" "{_NSRange=II}" }
149 { "NSPoint" "{CGPoint=dd}" }
150 { "NSRect" "{CGRect={CGPoint=dd}{CGSize=dd}}" }
151 { "NSSize" "{CGSize=dd}" }
152 { "NSRange" "{_NSRange=QQ}" }
158 assoc-union alien>objc-types set-global
160 : internal-cocoa-type? ( c-type -- ? )
161 [ "?" = ] [ first CHAR: _ = ] bi or ;
163 : warn-c-type ( c-type -- )
164 dup internal-cocoa-type?
165 [ drop ] [ "Warning: no such C type: " write print ] if ;
167 : objc-struct-type ( i string -- ctype )
168 [ CHAR: = ] 2keep index-from swap subseq
169 dup c-types get key? [ warn-c-type "void*" ] unless ;
171 ERROR: no-objc-type name ;
173 : decode-type ( ch -- ctype )
174 1string dup objc>alien-types get at
175 [ ] [ no-objc-type ] ?if ;
177 : (parse-objc-type) ( i string -- ctype )
178 [ [ 1 + ] dip ] [ nth ] 2bi {
179 { [ dup "rnNoORV" member? ] [ drop (parse-objc-type) ] }
180 { [ dup CHAR: ^ = ] [ 3drop "void*" ] }
181 { [ dup CHAR: { = ] [ drop objc-struct-type ] }
182 { [ dup CHAR: [ = ] [ 3drop "void*" ] }
186 : parse-objc-type ( string -- ctype ) 0 swap (parse-objc-type) ;
188 : method-arg-type ( method i -- type )
189 method_copyArgumentType
190 [ utf8 alien>string parse-objc-type ] keep
193 : method-arg-types ( method -- args )
194 dup method_getNumberOfArguments
195 [ method-arg-type ] with map ;
197 : method-return-type ( method -- ctype )
198 method_copyReturnType
199 [ utf8 alien>string parse-objc-type ] keep
202 : register-objc-method ( method -- )
203 dup method-return-type over method-arg-types 2array
205 swap method_getName sel_getName
206 objc-methods get set-at ;
208 : each-method-in-class ( class quot -- )
209 [ 0 <uint> [ class_copyMethodList ] keep *uint ] dip
211 [ <direct-void*-array> ] dip
212 [ each ] [ drop (free) ] 2bi
215 : register-objc-methods ( class -- )
216 [ register-objc-method ] each-method-in-class ;
218 : class-exists? ( string -- class ) objc_getClass >boolean ;
220 : define-objc-class-word ( quot name -- )
221 [ class-init-hooks get set-at ]
223 [ "cocoa.classes" create ] [ '[ _ objc-class ] ] bi
224 (( -- class )) define-declared
227 : import-objc-class ( name quot -- )
228 over define-objc-class-word
229 [ objc-class register-objc-methods ]
230 [ objc-meta-class register-objc-methods ] bi ;
232 : root-class ( class -- root )
233 dup class_getSuperclass [ root-class ] [ ] ?if ;