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.alien ;
11 : make-sender ( method function -- quot )
12 [ over first , f , , second , \ alien-invoke , ] [ ] make ;
14 : sender-stub ( method function -- word )
15 [ "( sender-stub )" f <word> dup ] 2dip
16 over first large-struct? [ "_stret" append ] when
17 make-sender dup infer define-declared ;
19 SYMBOL: message-senders
20 SYMBOL: super-message-senders
22 message-senders [ H{ } clone ] initialize
23 super-message-senders [ H{ } clone ] initialize
25 : cache-stub ( method assoc function -- )
26 '[ _ sender-stub ] cache drop ;
28 : cache-stubs ( method -- )
29 [ super-message-senders get "objc_msgSendSuper" cache-stub ]
30 [ message-senders get "objc_msgSend" cache-stub ]
33 : <super> ( receiver -- super )
34 [ ] [ object_getClass class_getSuperclass ] bi
35 objc-super <struct-boa> ;
37 TUPLE: selector name object ;
39 MEMO: <selector> ( name -- sel ) f \ selector boa ;
41 : selector ( selector -- alien )
42 dup object>> expired? [
43 dup name>> sel_registerName
44 [ >>object drop ] keep
51 objc-methods [ H{ } clone ] initialize
53 : lookup-method ( selector -- method )
54 dup objc-methods get at
55 [ ] [ "No such method: " prepend throw ] ?if ;
57 MEMO: make-prepare-send ( selector method super? -- quot )
60 swap <selector> , \ selector ,
62 swap second length 2 - '[ _ _ ndip ] ;
64 MACRO: (send) ( selector super? -- quot )
65 [ dup lookup-method ] dip
66 [ make-prepare-send ] 2keep
67 super-message-senders message-senders ? get at
70 : send ( receiver args... selector -- return... ) f (send) ; inline
72 : super-send ( receiver args... selector -- return... ) t (send) ; inline
74 ! Runtime introspection
75 SYMBOL: class-init-hooks
77 class-init-hooks [ H{ } clone ] initialize
79 : (objc-class) ( name word -- class )
80 2dup execute dup [ 2nip ] [
81 drop over class-init-hooks get at [ call( -- ) ] when*
82 2dup execute dup [ 2nip ] [
83 2drop "No such class: " prepend throw
87 : objc-class ( string -- class )
88 \ objc_getClass (objc-class) ;
90 : objc-protocol ( string -- class )
91 \ objc_getProtocol (objc-class) ;
93 : objc-meta-class ( string -- class )
94 \ objc_getMetaClass (objc-class) ;
96 SYMBOL: objc>alien-types
110 { "?" "unknown_type" }
115 "ptrdiff_t" heap-size {
129 assoc-union objc>alien-types set-global
131 ! The transpose of the above map
132 SYMBOL: alien>objc-types
134 objc>alien-types get [ swap ] assoc-map
136 "ptrdiff_t" heap-size {
138 { "NSPoint" "{_NSPoint=ff}" }
139 { "NSRect" "{_NSRect={_NSPoint=ff}{_NSSize=ff}}" }
140 { "NSSize" "{_NSSize=ff}" }
141 { "NSRange" "{_NSRange=II}" }
147 { "NSPoint" "{CGPoint=dd}" }
148 { "NSRect" "{CGRect={CGPoint=dd}{CGSize=dd}}" }
149 { "NSSize" "{CGSize=dd}" }
150 { "NSRange" "{_NSRange=QQ}" }
156 assoc-union alien>objc-types set-global
158 : internal-cocoa-type? ( c-type -- ? )
159 [ "?" = ] [ first CHAR: _ = ] bi or ;
161 : warn-c-type ( c-type -- )
162 dup internal-cocoa-type?
163 [ drop ] [ "Warning: no such C type: " write print ] if ;
165 : objc-struct-type ( i string -- ctype )
166 [ CHAR: = ] 2keep index-from swap subseq
167 dup c-types get key? [ warn-c-type "void*" ] unless ;
169 ERROR: no-objc-type name ;
171 : decode-type ( ch -- ctype )
172 1string dup objc>alien-types get at
173 [ ] [ no-objc-type ] ?if ;
175 : (parse-objc-type) ( i string -- ctype )
176 [ [ 1 + ] dip ] [ nth ] 2bi {
177 { [ dup "rnNoORV" member? ] [ drop (parse-objc-type) ] }
178 { [ dup CHAR: ^ = ] [ 3drop "void*" ] }
179 { [ dup CHAR: { = ] [ drop objc-struct-type ] }
180 { [ dup CHAR: [ = ] [ 3drop "void*" ] }
184 : parse-objc-type ( string -- ctype ) 0 swap (parse-objc-type) ;
186 : method-arg-type ( method i -- type )
187 method_copyArgumentType
188 [ utf8 alien>string parse-objc-type ] keep
191 : method-arg-types ( method -- args )
192 dup method_getNumberOfArguments
193 [ method-arg-type ] with map ;
195 : method-return-type ( method -- ctype )
196 method_copyReturnType
197 [ utf8 alien>string parse-objc-type ] keep
200 : register-objc-method ( method -- )
201 dup method-return-type over method-arg-types 2array
203 swap method_getName sel_getName
204 objc-methods get set-at ;
206 : each-method-in-class ( class quot -- )
207 [ 0 <uint> [ class_copyMethodList ] keep *uint ] dip
209 [ <direct-void*-array> ] dip
210 [ each ] [ drop (free) ] 2bi
213 : register-objc-methods ( class -- )
214 [ register-objc-method ] each-method-in-class ;
216 : class-exists? ( string -- class ) objc_getClass >boolean ;
218 : define-objc-class-word ( quot name -- )
219 [ class-init-hooks get set-at ]
221 [ "cocoa.classes" create ] [ '[ _ objc-class ] ] bi
222 (( -- class )) define-declared
225 : import-objc-class ( name quot -- )
226 over define-objc-class-word
227 [ objc-class register-objc-methods ]
228 [ objc-meta-class register-objc-methods ] bi ;
230 : root-class ( class -- root )
231 dup class_getSuperclass [ root-class ] [ ] ?if ;