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 continuations combinators compiler compiler.alien stack-checker kernel
5 math namespaces make parser quotations sequences strings words
6 cocoa.runtime io macros memoize io.encodings.utf8 effects libc
7 libc.private parser lexer init core-foundation fry generalizations
8 specialized-arrays.direct.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 "objc-super" <c-object> [
35 [ dup object_getClass class_getSuperclass ] dip
38 [ set-objc-super-receiver ] keep ;
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
71 '[ _ call _ execute ] ;
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
113 { "?" "unknown_type" }
118 "ptrdiff_t" heap-size {
132 assoc-union objc>alien-types set-global
134 ! The transpose of the above map
135 SYMBOL: alien>objc-types
137 objc>alien-types get [ swap ] assoc-map
139 "ptrdiff_t" heap-size {
141 { "NSPoint" "{_NSPoint=ff}" }
142 { "NSRect" "{_NSRect={_NSPoint=ff}{_NSSize=ff}}" }
143 { "NSSize" "{_NSSize=ff}" }
144 { "NSRange" "{_NSRange=II}" }
150 { "NSPoint" "{CGPoint=dd}" }
151 { "NSRect" "{CGRect={CGPoint=dd}{CGSize=dd}}" }
152 { "NSSize" "{CGSize=dd}" }
153 { "NSRange" "{_NSRange=QQ}" }
159 assoc-union alien>objc-types set-global
161 : objc-struct-type ( i string -- ctype )
162 [ CHAR: = ] 2keep index-from swap subseq
163 dup c-types get key? [
164 "Warning: no such C type: " write dup print
168 ERROR: no-objc-type name ;
170 : decode-type ( ch -- ctype )
171 1string dup objc>alien-types get at
172 [ ] [ no-objc-type ] ?if ;
174 : (parse-objc-type) ( i string -- ctype )
175 [ [ 1+ ] dip ] [ nth ] 2bi {
176 { [ dup "rnNoORV" member? ] [ drop (parse-objc-type) ] }
177 { [ dup CHAR: ^ = ] [ 3drop "void*" ] }
178 { [ dup CHAR: { = ] [ drop objc-struct-type ] }
179 { [ dup CHAR: [ = ] [ 3drop "void*" ] }
183 : parse-objc-type ( string -- ctype ) 0 swap (parse-objc-type) ;
185 : method-arg-type ( method i -- type )
186 method_copyArgumentType
187 [ utf8 alien>string parse-objc-type ] keep
190 : method-arg-types ( method -- args )
191 dup method_getNumberOfArguments
192 [ method-arg-type ] with map ;
194 : method-return-type ( method -- ctype )
195 method_copyReturnType
196 [ utf8 alien>string parse-objc-type ] keep
199 : register-objc-method ( method -- )
200 dup method-return-type over method-arg-types 2array
202 swap method_getName sel_getName
203 objc-methods get set-at ;
205 : each-method-in-class ( class quot -- )
206 [ 0 <uint> [ class_copyMethodList ] keep *uint ] dip
208 [ <direct-void*-array> ] dip
209 [ each ] [ drop (free) ] 2bi
212 : register-objc-methods ( class -- )
213 [ register-objc-method ] each-method-in-class ;
215 : class-exists? ( string -- class ) objc_getClass >boolean ;
217 : define-objc-class-word ( quot name -- )
218 [ class-init-hooks get set-at ]
220 [ "cocoa.classes" create ] [ '[ _ objc-class ] ] bi
221 (( -- class )) define-declared
224 : import-objc-class ( name quot -- )
225 over define-objc-class-word
226 [ objc-class register-objc-methods ]
227 [ objc-meta-class register-objc-methods ] bi ;
229 : root-class ( class -- root )
230 dup class_getSuperclass [ root-class ] [ ] ?if ;