]> gitweb.factorcode.org Git - factor.git/blob - basis/cocoa/messages/messages.factor
Delete empty unit tests files, remove 1- and 1+, reorder IN: lines in a lot of places...
[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 continuations combinators compiler compiler.alien stack-checker kernel
5 math namespaces make quotations sequences strings words
6 cocoa.runtime io macros memoize io.encodings.utf8 effects libc
7 libc.private lexer init core-foundation fry generalizations
8 specialized-arrays.direct.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     "objc-super" <c-object> [
35         [ dup object_getClass class_getSuperclass ] dip
36         set-objc-super-class
37     ] keep
38     [ set-objc-super-receiver ] keep ;
39
40 TUPLE: selector name object ;
41
42 MEMO: <selector> ( name -- sel ) f \ selector boa ;
43
44 : selector ( selector -- alien )
45     dup object>> expired? [
46         dup name>> sel_registerName
47         [ >>object drop ] keep
48     ] [
49         object>>
50     ] if ;
51
52 SYMBOL: objc-methods
53
54 objc-methods [ H{ } clone ] initialize
55
56 : lookup-method ( selector -- method )
57     dup objc-methods get at
58     [ ] [ "No such method: " prepend throw ] ?if ;
59
60 MEMO: make-prepare-send ( selector method super? -- quot )
61     [
62         [ \ <super> , ] when
63         swap <selector> , \ selector ,
64     ] [ ] make
65     swap second length 2 - '[ _ _ ndip ] ;
66
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     1quotation append ;
72
73 : send ( receiver args... selector -- return... ) f (send) ; inline
74
75 : super-send ( receiver args... selector -- return... ) t (send) ; inline
76
77 ! Runtime introspection
78 SYMBOL: class-init-hooks
79
80 class-init-hooks [ H{ } clone ] initialize
81
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
87         ] if
88     ] if ; inline
89
90 : objc-class ( string -- class )
91     \ objc_getClass (objc-class) ;
92
93 : objc-protocol ( string -- class )
94     \ objc_getProtocol (objc-class) ;
95
96 : objc-meta-class ( string -- class )
97     \ objc_getMetaClass (objc-class) ;
98
99 SYMBOL: objc>alien-types
100
101 H{
102     { "c" "char" }
103     { "i" "int" }
104     { "s" "short" }
105     { "C" "uchar" }
106     { "I" "uint" }
107     { "S" "ushort" }
108     { "f" "float" }
109     { "d" "double" }
110     { "B" "bool" }
111     { "v" "void" }
112     { "*" "char*" }
113     { "?" "unknown_type" }
114     { "@" "id" }
115     { "#" "Class" }
116     { ":" "SEL" }
117 }
118 "ptrdiff_t" heap-size {
119     { 4 [ H{
120         { "l" "long" }
121         { "q" "longlong" }
122         { "L" "ulong" }
123         { "Q" "ulonglong" }
124     } ] }
125     { 8 [ H{
126         { "l" "long32" }
127         { "q" "long" }
128         { "L" "ulong32" }
129         { "Q" "ulong" }
130     } ] }
131 } case
132 assoc-union objc>alien-types set-global
133
134 ! The transpose of the above map
135 SYMBOL: alien>objc-types
136
137 objc>alien-types get [ swap ] assoc-map
138 ! A hack...
139 "ptrdiff_t" heap-size {
140     { 4 [ H{
141         { "NSPoint"    "{_NSPoint=ff}" }
142         { "NSRect"     "{_NSRect={_NSPoint=ff}{_NSSize=ff}}" }
143         { "NSSize"     "{_NSSize=ff}" }
144         { "NSRange"    "{_NSRange=II}" }
145         { "NSInteger"  "i" }
146         { "NSUInteger" "I" }
147         { "CGFloat"    "f" }
148     } ] }
149     { 8 [ H{
150         { "NSPoint"    "{CGPoint=dd}" }
151         { "NSRect"     "{CGRect={CGPoint=dd}{CGSize=dd}}" }
152         { "NSSize"     "{CGSize=dd}" }
153         { "NSRange"    "{_NSRange=QQ}" }
154         { "NSInteger"  "q" }
155         { "NSUInteger" "Q" }
156         { "CGFloat"    "d" }
157     } ] }
158 } case
159 assoc-union alien>objc-types set-global
160
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
165         drop "void*"
166     ] unless ;
167
168 ERROR: no-objc-type name ;
169
170 : decode-type ( ch -- ctype )
171     1string dup objc>alien-types get at
172     [ ] [ no-objc-type ] ?if ;
173
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*" ] }
180         [ 2nip decode-type ]
181     } cond ;
182
183 : parse-objc-type ( string -- ctype ) 0 swap (parse-objc-type) ;
184
185 : method-arg-type ( method i -- type )
186     method_copyArgumentType
187     [ utf8 alien>string parse-objc-type ] keep
188     (free) ;
189
190 : method-arg-types ( method -- args )
191     dup method_getNumberOfArguments
192     [ method-arg-type ] with map ;
193
194 : method-return-type ( method -- ctype )
195     method_copyReturnType
196     [ utf8 alien>string parse-objc-type ] keep
197     (free) ;
198
199 : register-objc-method ( method -- )
200     dup method-return-type over method-arg-types 2array
201     dup cache-stubs
202     swap method_getName sel_getName
203     objc-methods get set-at ;
204
205 : each-method-in-class ( class quot -- )
206     [ 0 <uint> [ class_copyMethodList ] keep *uint ] dip
207     over 0 = [ 3drop ] [
208         [ <direct-void*-array> ] dip
209         [ each ] [ drop (free) ] 2bi
210     ] if ; inline
211
212 : register-objc-methods ( class -- )
213     [ register-objc-method ] each-method-in-class ;
214
215 : class-exists? ( string -- class ) objc_getClass >boolean ;
216
217 : define-objc-class-word ( quot name -- )
218     [ class-init-hooks get set-at ]
219     [
220         [ "cocoa.classes" create ] [ '[ _ objc-class ] ] bi
221         (( -- class )) define-declared
222     ] bi ;
223
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 ;
228
229 : root-class ( class -- root )
230     dup class_getSuperclass [ root-class ] [ ] ?if ;