]> gitweb.factorcode.org Git - factor.git/blob - basis/cocoa/messages/messages.factor
7342451c386bb693806d177b414236b9619b49b3
[factor.git] / basis / cocoa / messages / messages.factor
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 ;
9 IN: cocoa.messages
10
11 : make-sender ( method function -- quot )
12     [ over first , f , , second , \ alien-invoke , ] [ ] make ;
13
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 ;
18
19 SYMBOL: message-senders
20 SYMBOL: super-message-senders
21
22 message-senders [ H{ } clone ] initialize
23 super-message-senders [ H{ } clone ] initialize
24
25 : cache-stub ( method assoc function -- )
26     '[ _ sender-stub ] cache drop ;
27
28 : cache-stubs ( method -- )
29     [ super-message-senders get "objc_msgSendSuper" cache-stub ]
30     [ message-senders get "objc_msgSend" cache-stub ]
31     bi ;
32
33 : <super> ( receiver -- super )
34     [ ] [ object_getClass class_getSuperclass ] bi
35     objc-super <struct-boa> ;
36
37 TUPLE: selector name object ;
38
39 MEMO: <selector> ( name -- sel ) f \ selector boa ;
40
41 : selector ( selector -- alien )
42     dup object>> expired? [
43         dup name>> sel_registerName
44         [ >>object drop ] keep
45     ] [
46         object>>
47     ] if ;
48
49 SYMBOL: objc-methods
50
51 objc-methods [ H{ } clone ] initialize
52
53 : lookup-method ( selector -- method )
54     dup objc-methods get at
55     [ ] [ "No such method: " prepend throw ] ?if ;
56
57 MEMO: make-prepare-send ( selector method super? -- quot )
58     [
59         [ \ <super> , ] when
60         swap <selector> , \ selector ,
61     ] [ ] make
62     swap second length 2 - '[ _ _ ndip ] ;
63
64 MACRO: (send) ( selector super? -- quot )
65     [ dup lookup-method ] dip
66     [ make-prepare-send ] 2keep
67     super-message-senders message-senders ? get at
68     1quotation append ;
69
70 : send ( receiver args... selector -- return... ) f (send) ; inline
71
72 : super-send ( receiver args... selector -- return... ) t (send) ; inline
73
74 ! Runtime introspection
75 SYMBOL: class-init-hooks
76
77 class-init-hooks [ H{ } clone ] initialize
78
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
84         ] if
85     ] if ; inline
86
87 : objc-class ( string -- class )
88     \ objc_getClass (objc-class) ;
89
90 : objc-protocol ( string -- class )
91     \ objc_getProtocol (objc-class) ;
92
93 : objc-meta-class ( string -- class )
94     \ objc_getMetaClass (objc-class) ;
95
96 SYMBOL: objc>alien-types
97
98 H{
99     { "c" "char" }
100     { "i" "int" }
101     { "s" "short" }
102     { "C" "uchar" }
103     { "I" "uint" }
104     { "S" "ushort" }
105     { "f" "float" }
106     { "d" "double" }
107     { "B" "bool" }
108     { "v" "void" }
109     { "*" "char*" }
110     { "?" "unknown_type" }
111     { "@" "id" }
112     { "#" "Class" }
113     { ":" "SEL" }
114 }
115 "ptrdiff_t" heap-size {
116     { 4 [ H{
117         { "l" "long" }
118         { "q" "longlong" }
119         { "L" "ulong" }
120         { "Q" "ulonglong" }
121     } ] }
122     { 8 [ H{
123         { "l" "long32" }
124         { "q" "long" }
125         { "L" "ulong32" }
126         { "Q" "ulong" }
127     } ] }
128 } case
129 assoc-union objc>alien-types set-global
130
131 ! The transpose of the above map
132 SYMBOL: alien>objc-types
133
134 objc>alien-types get [ swap ] assoc-map
135 ! A hack...
136 "ptrdiff_t" heap-size {
137     { 4 [ H{
138         { "NSPoint"    "{_NSPoint=ff}" }
139         { "NSRect"     "{_NSRect={_NSPoint=ff}{_NSSize=ff}}" }
140         { "NSSize"     "{_NSSize=ff}" }
141         { "NSRange"    "{_NSRange=II}" }
142         { "NSInteger"  "i" }
143         { "NSUInteger" "I" }
144         { "CGFloat"    "f" }
145     } ] }
146     { 8 [ H{
147         { "NSPoint"    "{CGPoint=dd}" }
148         { "NSRect"     "{CGRect={CGPoint=dd}{CGSize=dd}}" }
149         { "NSSize"     "{CGSize=dd}" }
150         { "NSRange"    "{_NSRange=QQ}" }
151         { "NSInteger"  "q" }
152         { "NSUInteger" "Q" }
153         { "CGFloat"    "d" }
154     } ] }
155 } case
156 assoc-union alien>objc-types set-global
157
158 : internal-cocoa-type? ( c-type -- ? )
159     [ "?" = ] [ first CHAR: _ = ] bi or ;
160
161 : warn-c-type ( c-type -- )
162     dup internal-cocoa-type?
163     [ drop ] [ "Warning: no such C type: " write print ] if ;
164
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 ;
168
169 ERROR: no-objc-type name ;
170
171 : decode-type ( ch -- ctype )
172     1string dup objc>alien-types get at
173     [ ] [ no-objc-type ] ?if ;
174
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*" ] }
181         [ 2nip decode-type ]
182     } cond ;
183
184 : parse-objc-type ( string -- ctype ) 0 swap (parse-objc-type) ;
185
186 : method-arg-type ( method i -- type )
187     method_copyArgumentType
188     [ utf8 alien>string parse-objc-type ] keep
189     (free) ;
190
191 : method-arg-types ( method -- args )
192     dup method_getNumberOfArguments
193     [ method-arg-type ] with map ;
194
195 : method-return-type ( method -- ctype )
196     method_copyReturnType
197     [ utf8 alien>string parse-objc-type ] keep
198     (free) ;
199
200 : register-objc-method ( method -- )
201     dup method-return-type over method-arg-types 2array
202     dup cache-stubs
203     swap method_getName sel_getName
204     objc-methods get set-at ;
205
206 : each-method-in-class ( class quot -- )
207     [ 0 <uint> [ class_copyMethodList ] keep *uint ] dip
208     over 0 = [ 3drop ] [
209         [ <direct-void*-array> ] dip
210         [ each ] [ drop (free) ] 2bi
211     ] if ; inline
212
213 : register-objc-methods ( class -- )
214     [ register-objc-method ] each-method-in-class ;
215
216 : class-exists? ( string -- class ) objc_getClass >boolean ;
217
218 : define-objc-class-word ( quot name -- )
219     [ class-init-hooks get set-at ]
220     [
221         [ "cocoa.classes" create ] [ '[ _ objc-class ] ] bi
222         (( -- class )) define-declared
223     ] bi ;
224
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 ;
229
230 : root-class ( class -- root )
231     dup class_getSuperclass [ root-class ] [ ] ?if ;