1 | A Cocoa Bridge for OpenMCL |
---|
2 | |
---|
3 | Randall D. Beer |
---|
4 | beer@eecs.cwru.edu |
---|
5 | http://vorlon.cwru.edu/~beer |
---|
6 | |
---|
7 | |
---|
8 | INTRODUCTION |
---|
9 | |
---|
10 | The purpose of CocoaBridge is to make Cocoa as easy as possible to use |
---|
11 | from OpenMCL, in order to support GUI application and development |
---|
12 | environment activities. It builds on the capabilities provided in the |
---|
13 | APPLE-OBJC example. The eventual goal is complete integration of |
---|
14 | Cocoa into CLOS. The current release provides Lisp-like syntax and |
---|
15 | naming conventions for ObjC object creation and message sending, with |
---|
16 | automatic type processing and compile-time checking of message |
---|
17 | sends. It also provides some convenience facilities for working with |
---|
18 | Cocoa. |
---|
19 | |
---|
20 | A small sample Cocoa program can be invoked by evaluating (REQUIRE |
---|
21 | 'TINY) and then (CCL::TINY-SETUP). This program provides a simple example |
---|
22 | of using several of the bridge's capabilities |
---|
23 | |
---|
24 | |
---|
25 | BASICS |
---|
26 | |
---|
27 | The main things you need to know are: |
---|
28 | |
---|
29 | 1) You create and initialize ObjC objects using |
---|
30 | MAKE-OBJC-INSTANCE. This should be replaced by MAKE-INSTANCE as CLOS |
---|
31 | integration improves |
---|
32 | |
---|
33 | Example: |
---|
34 | [[NSNumber alloc] initWithFloat: 2.7] in ObjC becomes |
---|
35 | (MAKE-OBJC-INSTANCE 'NS-NUMBER :INIT-WITH-FLOAT 2.7) in Lisp |
---|
36 | |
---|
37 | Note that class names and init keywords are translated from ObjC to Lisp in |
---|
38 | pretty much the obvious way |
---|
39 | |
---|
40 | 2) You send messages to ObjC objects using SEND |
---|
41 | |
---|
42 | Examples: |
---|
43 | [w alphaValue] becomes (SEND W 'ALPHA-VALUE) |
---|
44 | [w setAlphaValue: 0.5] becomes (SEND W :SET-ALPHA-VALUE 0.5) |
---|
45 | [v mouse: p inRect: r] becomes (SEND V :MOUSE P :IN-RECT R) |
---|
46 | |
---|
47 | Note that message keywords are translated to Lisp in pretty much the obvious |
---|
48 | way. From within a method, you can also use SEND-SUPER. |
---|
49 | |
---|
50 | |
---|
51 | 3) The @CLASS macro from APPLE-OBJC is currently used to refer to named ObjC |
---|
52 | classes, which can also be sent messages via SEND. This should be replaced by |
---|
53 | FIND-CLASS as CLOS integration improves. |
---|
54 | |
---|
55 | Example: |
---|
56 | [NSColor whiteColor] becomes (SEND (@CLASS NS-COLOR) 'WHITE-COLOR) |
---|
57 | |
---|
58 | |
---|
59 | 4) New ObjC classes and methods are currently defined using DEF-OBJC-CLASS and |
---|
60 | DEFINE-OBJC-METHOD from APPLE-OBJC. This should be replaced by DEFCLASS and |
---|
61 | DEFMETHOD as CLOS integration improves. |
---|
62 | |
---|
63 | |
---|
64 | NAME TRANSLATION |
---|
65 | |
---|
66 | There are a standard set of naming conventions for Cocoa classes, |
---|
67 | messages, etc. As long as these are followed, the bridge is fairly |
---|
68 | good at automaticallly translating between ObjC and Lisp names. |
---|
69 | |
---|
70 | Examples: |
---|
71 | "NSURLHandleClient" <==> NS-URL-HANDLE-CLIENT |
---|
72 | "NSOpenGLView" <==> NS-OPENGL-VIEW |
---|
73 | "nextEventMatchingMask:untilDate:inMode:dequeue:" <==> |
---|
74 | (:NEXT-EVENT-MATCHING-MASK :UNTIL-DATE :IN-MODE :DEQUEUE) |
---|
75 | |
---|
76 | To see how a given ObjC or Lisp name will be translated by the bridge, you can |
---|
77 | use the following functions: |
---|
78 | |
---|
79 | OBJC-TO-LISP-CLASSNAME string |
---|
80 | LISP-TO-OBJC-CLASSNAME symbol |
---|
81 | OBJC-TO-LISP-MESSAGE string |
---|
82 | LISP-TO-OBJC-MESSAGE keyword-list |
---|
83 | OBJC-TO-LISP-INIT string |
---|
84 | LISP-TO-OBJC-INIT keyword-list |
---|
85 | |
---|
86 | Of course, there will always be exceptions to any naming convention. |
---|
87 | Please let me know if you come across any name translation problems |
---|
88 | that seem to be bugs. Otherwise, the bridge provides two ways of |
---|
89 | dealing with exceptions: |
---|
90 | |
---|
91 | 1) You can pass a string as the class name of MAKE-OBJC-INSTANCE and |
---|
92 | as the message to SEND. These strings will be directly interpreted as |
---|
93 | ObjC names, with no translation. This is useful for a one-time |
---|
94 | exception. |
---|
95 | |
---|
96 | Examples: |
---|
97 | (MAKE-OBJC-INSTANCE "WiErDclass") |
---|
98 | (SEND o "WiErDmEsSaGe:WithARG:" x y) |
---|
99 | |
---|
100 | 2) You can define a special translation rule for your exception. This is useful |
---|
101 | for an exceptional name that you need to use throughout your code. |
---|
102 | |
---|
103 | Examples: |
---|
104 | (DEFINE-CLASSNAME-TRANSLATION "WiErDclass" WEIRD-CLASS) |
---|
105 | (DEFINE-MESSAGE-TRANSLATION "WiErDmEsSaGe:WithARG:" (:WEIRD-MESSAGE :WITH-ARG)) |
---|
106 | (DEFINE-INIT-TRANSLATION "WiErDiNiT:WITHOPTION:" (:WEIRD-INIT :OPTION) |
---|
107 | |
---|
108 | The normal rule in ObjC names is that each word begins with a capital letter |
---|
109 | (except possibly the first). Using this rule literally, "NSWindow" would be |
---|
110 | translated as N-S-WINDOW, which seems wrong. "NS" is a special word in ObjC |
---|
111 | that should not be broken at each capital letter. Likewise "URL", "PDF", |
---|
112 | "OpenGL", etc. Most common special words used in Cocoa are already defined in |
---|
113 | the bridge, but you can define new ones as follows: (DEFINE-SPECIAL-OBJC-WORD |
---|
114 | "QuickDraw") |
---|
115 | |
---|
116 | Note that message keywords in a SEND such as (SEND V :MOUSE P :IN-RECT R) may |
---|
117 | look like Lisp keyword args, but they really aren't. All keywords must be |
---|
118 | present and the order is significant. Neither (:IN-RECT :MOUSE) nor (:MOUSE) |
---|
119 | translate to "mouse:inRect:" |
---|
120 | |
---|
121 | Note that an "init" prefix is optional in the initializer keywords, so |
---|
122 | (MAKE-OBJC-INSTANCE 'NS-NUMBER :INIT-WITH-FLOAT 2.7) can also be expressed as |
---|
123 | (MAKE-OBJC-INSTANCE 'NS-NUMBER :WITH-FLOAT 2.7) |
---|
124 | |
---|
125 | |
---|
126 | STRETS |
---|
127 | |
---|
128 | Some Cocoa methods return small structures (such as those used to represent |
---|
129 | points, rects, sizes and ranges). Although this is normally hidden by the ObjC |
---|
130 | compiler, such messages are sent in a special way, with the storage for the |
---|
131 | STructure RETurn (STRET) passed as an extra argument. This STRET and special |
---|
132 | SEND must normally be made explicit in Lisp. Thus |
---|
133 | |
---|
134 | NSRect r = [v1 bounds]; |
---|
135 | [v2 setBounds r]; |
---|
136 | |
---|
137 | in ObjC becomes |
---|
138 | |
---|
139 | (RLET ((R :<NSR>ect)) |
---|
140 | (SEND/STRET R V1 'BOUNDS) |
---|
141 | (SEND V2 :SET-BOUNDS R)) |
---|
142 | |
---|
143 | In order to make STRETs easier to use, the bridge provides two conveniences: |
---|
144 | |
---|
145 | 1) The SLET and SLET* macros may be used to define local variables that are |
---|
146 | initialized to STRETs using a normal SEND syntax. Thus, the following is |
---|
147 | equivalent to the above RLET: |
---|
148 | |
---|
149 | (SLET ((R (SEND V 'BOUNDS))) |
---|
150 | (SEND V2 :SET-BOUNDS R)) |
---|
151 | |
---|
152 | 2) The arguments to a SEND are evaluated inside an implicit SLET, so instead of |
---|
153 | the above, one could in fact just write: |
---|
154 | |
---|
155 | (SEND V1 :SET-BOUNDS (SEND V2 'BOUNDS)) |
---|
156 | |
---|
157 | There are also several psuedo-functions provided for convenience by the ObjC |
---|
158 | compiler. The following are currently supported by the bridge: NS-MAKE-POINT, |
---|
159 | NS-MAKE-RANGE, NS-MAKE-RECT, and NS-MAKE-SIZE. These can be used within a SLET |
---|
160 | initform or within a message send: |
---|
161 | |
---|
162 | (SLET ((P (NS-MAKE-POINT 100.0 200.0))) |
---|
163 | (SEND W :SET-FRAME-ORIGIN P)) |
---|
164 | |
---|
165 | or |
---|
166 | |
---|
167 | (SEND W :SET-ORIGIN (NS-MAKE-POINT 100.0 200.0)) |
---|
168 | |
---|
169 | However, since these aren't real functions, a call like the following won't |
---|
170 | work: |
---|
171 | |
---|
172 | (SETQ P (NS-MAKE-POINT 100.0 200.0)) |
---|
173 | |
---|
174 | The following convenience macros are also provided: NS-MAX-RANGE, NS-MIN-X, |
---|
175 | NS-MIN-Y, NS-MAX-X, NS-MAX-Y, NS-MID-X, NS-MID-Y, NS-HEIGHT, and NS-WIDTH. |
---|
176 | |
---|
177 | Note that there is also a SEND-SUPER/STRET for use within methods. |
---|
178 | |
---|
179 | |
---|
180 | OPTIMIZATION |
---|
181 | |
---|
182 | The bridge works fairly hard to optimize message sends under two conditions. In |
---|
183 | both of these cases, a message send should be nearly as efficient as in ObjC: |
---|
184 | |
---|
185 | 1) When both the message and the receiver's class are known at compile-time. In |
---|
186 | general, the only way the receiver's class is known is if you declare it, which |
---|
187 | you can do either via a DECLARE or THE form. For example: |
---|
188 | |
---|
189 | (SEND (THE NS-WINDOW W) 'CENTER) |
---|
190 | |
---|
191 | Note that there is no way in ObjC to name the class of a class. Thus |
---|
192 | the bridge provides a @METACLASS declaration. The type of an instance |
---|
193 | of "NSColor" is NS-COLOR. The type of the *class* "NSColor" is |
---|
194 | (@METACLASS NS-COLOR): |
---|
195 | |
---|
196 | (LET ((C (@CLASS NS-COLOR))) |
---|
197 | (DECLARE ((@METACLASS NS-COLOR) C)) |
---|
198 | (SEND C 'WHITE-COLOR)) |
---|
199 | |
---|
200 | 2) When only the message is known at compile-time, but its type |
---|
201 | signature is unique. Of the over 6000 messages currently provided by |
---|
202 | Cocoa, only about 50 of them have nonunique type signatures. An |
---|
203 | example of a message whose type signature is not unique is SET. It |
---|
204 | returns VOID for NSColor, but ID for NSSet. In order to optimize |
---|
205 | sends of messages with nonunique type signatures, the class of the |
---|
206 | receiver must be declared at compile-time. |
---|
207 | |
---|
208 | If the type signature is nonunique or the message is unknown at compile-time, |
---|
209 | then a slower runtime call must be used. |
---|
210 | |
---|
211 | The ability of the bridge to optimize most constant message sends even |
---|
212 | when the receiver's class is unknown crucially depends on a type |
---|
213 | signature table that the bridge maintains. When the bridge is first |
---|
214 | loaded, it initializes this table by scanning all methods of all ObjC |
---|
215 | classes defined in the environment. If new methods are later defined, |
---|
216 | this table must be updated. After a major change (such as loading a |
---|
217 | new framework with many classes), you should evaluate |
---|
218 | (UPDATE-TYPE-SIGNATURES) to rebuild the type signature table. |
---|
219 | |
---|
220 | Because SEND, SEND-SUPER, SEND/STRET and SEND-SUPER/STRET are macros, |
---|
221 | they cannot be FUNCALLed, APPLYed or passed as functional arguments. |
---|
222 | The functions %SEND and %SEND/STRET are provided for this |
---|
223 | purpose. There are also %SEND-SUPER and %SEND-SUPER/STRET functions |
---|
224 | for use within methods. However, these functions should be used only |
---|
225 | when necessary since they perform general (nonoptimized) message |
---|
226 | sends. |
---|
227 | |
---|
228 | |
---|
229 | VARIABLE ARITY MESSAGES |
---|
230 | |
---|
231 | There are a few messages in Cocoa that take variable numbers of arguments. |
---|
232 | Perhaps the most common examples involve formatted strings: |
---|
233 | |
---|
234 | [NSClass stringWithFormat: "%f %f" x y] |
---|
235 | |
---|
236 | In the bridge, this would be written as follows: |
---|
237 | |
---|
238 | (SEND (@CLASS NS-STRING) |
---|
239 | :STRING-WITH-FORMAT #@"%f %f" |
---|
240 | (:DOUBLE-FLOAT X :DOUBLE-FLOAT Y)) |
---|
241 | |
---|
242 | Note that the types of the variable arguments must be given, since the compiler |
---|
243 | has no way of knowing these types in general. |
---|
244 | |
---|
245 | Variable arity messages can also be sent with the %SEND function: |
---|
246 | |
---|
247 | (%SEND (@CLASS NS-STRING) |
---|
248 | :STRING-WITH-FORMAT #@"%f %f" |
---|
249 | (LIST :DOUBLE-FLOAT X :DOUBLE-FLOAT Y)) |
---|
250 | |
---|
251 | Because the ObjC runtime system does not provide any information on |
---|
252 | which messages are variable arity, they must be explicitly defined. |
---|
253 | The standard variable arity messages in Cocoa are predefined. If you |
---|
254 | need to define a new variable arity message, use |
---|
255 | (DEFINE-VARIABLE-ARITY-MESSAGE "myVariableArityMessage:") |
---|
256 | |
---|
257 | |
---|
258 | TYPE COERCION |
---|
259 | |
---|
260 | OpenMCL's FFI handles many common conversions between Lisp and foreign data, |
---|
261 | such as unboxing floating-point args and boxing floating-point results. The |
---|
262 | bridge adds a few more automatic conversions: |
---|
263 | |
---|
264 | 1) NIL is equivalent to (%NULL-PTR) for any message argument that requires a |
---|
265 | pointer |
---|
266 | |
---|
267 | 2) T/NIL are equivalent to #$YES/#$NO for any boolean argument |
---|
268 | |
---|
269 | 3) A #$YES/#$NO returned by any method that returns BOOL will be automatically |
---|
270 | converted to T/NIL |
---|
271 | |
---|
272 | To make this last conversion work, the bridge has to engage in a bit |
---|
273 | of hackery. The bridge uses ObjC run-time type info. Unfortunately, |
---|
274 | BOOL is typed as CHAR by ObjC. Thus, a method that returns CHAR might |
---|
275 | actually return only BOOL, or it might return any CHAR. The bridge |
---|
276 | currently assumes that any method that returns CHAR actually returns |
---|
277 | BOOL. But it provides a facility for defining exceptions to this |
---|
278 | assumption: (DEFINE-RETURNS-BOOLEAN-EXCEPTION "charValue"). |
---|
279 | Eventually, the best way to handle issues like this is probably to get |
---|
280 | our method type info directly from the header files rather than using |
---|
281 | ObjC's runtime type system. |
---|
282 | |
---|
283 | Note that no automatic conversion is currently performed between Lisp |
---|
284 | strings and NSStrings. However, APPLE-OBJ provides a convenient |
---|
285 | syntax for creating constant NSStrings: (SEND W :SET-TITLE #@"My |
---|
286 | Window"), as well as facilities for converting between Lisp strings |
---|
287 | and NSStrings. Note that #@"Hello" is a full ObjC object, so messages |
---|
288 | can be sent to it: (SEND #@"Hello" 'LENGTH) |
---|
289 | |
---|