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
8 parser lexer init core-foundation ;
11 : make-sender ( method function -- quot )
12 [ over first , f , , second , \ alien-invoke , ] [ ] make ;
14 : sender-stub-name ( method function -- string )
15 [ % "_" % unparse % ] "" make ;
17 : sender-stub ( method function -- word )
18 [ sender-stub-name f <word> dup ] 2keep
19 over first large-struct? [ "_stret" append ] when
22 SYMBOL: message-senders
23 SYMBOL: super-message-senders
25 message-senders global [ H{ } assoc-like ] change-at
26 super-message-senders global [ H{ } assoc-like ] change-at
28 : cache-stub ( method function hash -- )
30 over get [ 2drop ] [ over >r sender-stub r> set ] if
33 : cache-stubs ( method -- )
35 "objc_msgSendSuper" super-message-senders get cache-stub
36 "objc_msgSend" message-senders get cache-stub ;
38 : <super> ( receiver -- super )
39 "objc-super" <c-object> [
40 >r dup object_getClass class_getSuperclass r>
43 [ set-objc-super-receiver ] keep ;
45 TUPLE: selector name object ;
47 MEMO: <selector> ( name -- sel ) f \ selector boa ;
49 : selector ( selector -- alien )
50 dup object>> expired? [
51 dup name>> sel_registerName
52 [ >>object drop ] keep
59 objc-methods global [ H{ } assoc-like ] change-at
61 : lookup-method ( selector -- method )
62 dup objc-methods get at
63 [ ] [ "No such method: " prepend throw ] ?if ;
65 : make-dip ( quot n -- quot' )
67 \ >r <repetition> >quotation -rot
68 \ r> <repetition> >quotation 3append ;
70 MEMO: make-prepare-send ( selector method super? -- quot )
73 swap <selector> , \ selector ,
75 swap second length 2 - make-dip ;
77 MACRO: (send) ( selector super? -- quot )
78 >r dup lookup-method r>
79 [ make-prepare-send ] 2keep
80 super-message-senders message-senders ? get at
81 [ slip execute ] 2curry ;
83 : send ( receiver args... selector -- return... ) f (send) ; inline
85 \ send soft "break-after" set-word-prop
87 : super-send ( receiver args... selector -- return... ) t (send) ; inline
89 \ super-send soft "break-after" set-word-prop
91 ! Runtime introspection
92 : (objc-class) ( string word -- class )
94 [ ] [ "No such class: " prepend throw ] ?if ; inline
96 : objc-class ( string -- class )
97 \ objc_getClass (objc-class) ;
99 : objc-protocol ( string -- class )
100 \ objc_getProtocol (objc-class) ;
102 : objc-meta-class ( string -- class )
103 \ objc_getMetaClass (objc-class) ;
105 SYMBOL: objc>alien-types
126 } objc>alien-types set-global
128 ! The transpose of the above map
129 SYMBOL: alien>objc-types
131 objc>alien-types get [ swap ] assoc-map
133 "ptrdiff_t" heap-size {
135 { "NSPoint" "{_NSPoint=ff}" }
136 { "NSRect" "{_NSRect=ffff}" }
137 { "NSSize" "{_NSSize=ff}" }
138 { "NSRange" "{_NSRange=II}" }
141 { "NSPoint" "{_NSPoint=dd}" }
142 { "NSRect" "{_NSRect=dddd}" }
143 { "NSSize" "{_NSSize=dd}" }
144 { "NSRange" "{_NSRange=QQ}" }
147 assoc-union alien>objc-types set-global
149 : objc-struct-type ( i string -- ctype )
150 2dup CHAR: = -rot index-from swap subseq
151 dup c-types get key? [
152 "Warning: no such C type: " write dup print
156 : (parse-objc-type) ( i string -- ctype )
157 2dup nth >r >r 1+ r> r> {
158 { [ dup "rnNoORV" member? ] [ drop (parse-objc-type) ] }
159 { [ dup CHAR: ^ = ] [ 3drop "void*" ] }
160 { [ dup CHAR: { = ] [ drop objc-struct-type ] }
161 { [ dup CHAR: [ = ] [ 3drop "void*" ] }
162 [ 2nip 1string objc>alien-types get at ]
165 : parse-objc-type ( string -- ctype ) 0 swap (parse-objc-type) ;
167 : method-arg-type ( method i -- type )
168 method_copyArgumentType
169 [ ascii alien>string parse-objc-type ] keep
172 : method-arg-types ( method -- args )
173 dup method_getNumberOfArguments
174 [ method-arg-type ] with map ;
176 : method-return-type ( method -- ctype )
177 method_copyReturnType
178 [ ascii alien>string parse-objc-type ] keep
181 : register-objc-method ( method -- )
182 dup method-return-type over method-arg-types 2array
184 swap method_getName sel_getName
185 objc-methods get set-at ;
187 : (register-objc-methods) ( methods count -- methods )
188 over [ void*-nth register-objc-method ] curry each ;
190 : register-objc-methods ( class -- )
191 0 <uint> [ class_copyMethodList ] keep *uint
192 (register-objc-methods) (free) ;
194 : class-exists? ( string -- class ) objc_getClass >boolean ;
196 : unless-defined ( class quot -- )
197 >r class-exists? r> unless ; inline
199 : define-objc-class-word ( name quot -- )
201 over , , \ unless-defined , dup , \ objc-class ,
202 ] [ ] make >r "cocoa.classes" create r>
203 (( -- class )) define-declared ;
205 : import-objc-class ( name quot -- )
207 dupd define-objc-class-word
210 objc-class register-objc-methods
211 objc-meta-class register-objc-methods
214 : root-class ( class -- root )
215 dup class_getSuperclass [ root-class ] [ ] ?if ;