1
|
1 /**
|
|
2 * Copyright: Copyright (c) 2009 Jacob Carlborg.
|
|
3 * Authors: Jacob Carlborg
|
|
4 * Version: Initial created: Feb 4, 2009
|
|
5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
|
|
6 */
|
|
7 module dstep.objc.bridge.Bridge;
|
|
8
|
|
9 import mambo.io;
|
|
10
|
|
11 version (Tango)
|
|
12 {
|
|
13 import tango.core.Memory;
|
|
14 import tango.core.Traits : ParameterTupleOf, ReturnTypeOf;
|
|
15
|
|
16 alias GC.addRoot addRoot;
|
|
17 }
|
|
18
|
|
19 else
|
|
20 {
|
|
21 import std.gc : addRoot;
|
|
22 import std.traits : ParameterTypeTuple, ReturnType;
|
|
23
|
|
24 alias ReturnType ReturnTypeOf;
|
|
25 alias ParameterTypeTuple ParameterTupleOf;
|
|
26 }
|
|
27
|
|
28 import dstep.internal.String;
|
|
29 import dstep.internal.Tuple;
|
|
30 import dstep.internal.Version;
|
|
31 import dstep.objc.bridge.Capsule;
|
|
32 import dstep.objc.bridge.ClassInitializer;
|
|
33 import dstep.objc.bridge.TypeEncoding;
|
|
34 import dstep.objc.bridge.Wrapper;
|
|
35 import dstep.objc.message;
|
|
36 import dstep.objc.objc;
|
|
37 import dstep.objc.runtime;
|
|
38
|
|
39 /**
|
|
40 * Builds a string representing a selector out of the given function
|
|
41 *
|
|
42 * It will build the string like this:
|
|
43 *
|
|
44 * ---
|
|
45 * foo (int x, int y); // foo:y:
|
|
46 * bar (); // bar
|
|
47 * fooBar (int x); // fooBar:
|
|
48 * ---
|
|
49 *
|
|
50 * Params:
|
|
51 * func = the function alias to build the selector of
|
|
52 *
|
|
53 * Returns: a string representing the selector
|
|
54 */
|
|
55 template selector (alias func)
|
|
56 {
|
|
57 const selector = buildSelector!(func);
|
|
58 }
|
|
59
|
|
60 template ObjcWrap ()
|
|
61 {
|
|
62 mixin ObjcWrap!(this.stringof);
|
|
63 }
|
|
64
|
|
65 template ObjcWrap (string name)
|
|
66 {
|
|
67 import dstep.objc.bridge.ClassInitializer : ObjcSubclassInitializer, subclassInit;
|
|
68 import dstep.objc.objc : Class, id, IMP, SEL;
|
|
69
|
|
70 static private Class objcClass_;
|
|
71 static private Class objcSuperClass_;
|
|
72
|
|
73 this (id object)
|
|
74 {
|
|
75 super(object);
|
|
76 }
|
|
77
|
|
78 static typeof(this) alloc ()
|
|
79 {
|
|
80 return invokeObjcSelfClass!(typeof(this), "alloc");
|
|
81 }
|
|
82
|
|
83 typeof(this) init ()
|
|
84 {
|
|
85 id ret = invokeObjcSelf!(id, "init");
|
|
86
|
|
87 if (!ret)
|
|
88 return null;
|
|
89
|
|
90 if (ret is objcObject)
|
|
91 {
|
|
92 dObject = this;
|
|
93 return this;
|
|
94 }
|
|
95
|
|
96 auto object = new typeof(this) (ret);
|
|
97 object.dObject = this;
|
|
98
|
|
99 return object;
|
|
100 }
|
|
101
|
|
102 mixin ObjcSubclassInitializer!("D_" ~ name, name);
|
|
103 }
|
|
104
|
|
105 struct ObjcMethodDeclaration (alias imp, R, string name, ARGS...)
|
|
106 {
|
|
107 alias imp methodImp;
|
|
108 alias R returnType;
|
|
109 const string methodName = name;
|
|
110 alias ARGS argsType;
|
|
111 }
|
|
112
|
|
113 template ObjcBindMethod (alias method)
|
|
114 {
|
|
115 import dstep.objc.bridge.TypeEncoding : buildSelector, encode;
|
|
116
|
|
117 mixin ObjcBindMethod!(method, buildSelector!(method));
|
|
118 }
|
|
119
|
|
120 template ObjcBindMethod (alias method, string selector)
|
|
121 {
|
|
122 mixin ObjcBindMethod!(method, ReturnTypeOf!(method), selector, ParameterTupleOf!(method));
|
|
123 }
|
|
124
|
|
125 template ObjcBindMethod (alias method, R, string selector, ARGS...)
|
|
126 {
|
|
127 import dstep.objc.bridge.Capsule : decapsule, encapsule, isCapsule, needsEncapsulation;
|
|
128 import dstep.internal.Tuple;
|
|
129
|
|
130 private ObjcMethodDeclaration!(method, R, selector, ARGS) objcMethodDeclaration;
|
|
131
|
|
132 private R delegate (ARGS) resolveVirtualCall ()
|
|
133 {
|
|
134 return null;
|
|
135 }
|
|
136
|
|
137 static if (needsEncapsulation!(R))
|
|
138 {
|
|
139 private alias ReplaceAllClasses!(id, ARGS) ObjcArgs;
|
|
140
|
|
141 private static id forwardVirtualCall (id self, SEL cmd, ObjcArgs objcArgs)
|
|
142 in
|
|
143 {
|
|
144 assert(isCapsule(self));
|
|
145 }
|
|
146 body
|
|
147 {
|
|
148 R delegate (ARGS) dg;
|
|
149 dg.funcptr = &method;
|
|
150 dg.ptr = Bridge.getDObject(self);
|
|
151
|
|
152 ARGS args;
|
|
153
|
|
154 foreach (i, a ; objcArgs)
|
|
155 {
|
|
156 alias typeof(args[i]) ArgType;
|
|
157
|
|
158 args[i] = decapsule!(ArgType)(a);
|
|
159 }
|
|
160
|
|
161 auto result = dg(args);
|
|
162
|
|
163 return encapsule!(R)(result);
|
|
164 }
|
|
165 }
|
|
166
|
|
167 else
|
|
168 {
|
|
169 private alias ReplaceAllClasses!(id, ARGS) ObjcArgs;
|
|
170
|
|
171 private static R forwardVirtualCall (id self, SEL cmd, ObjcArgs objcArgs)
|
|
172 in
|
|
173 {
|
|
174 assert(isCapsule(self));
|
|
175 }
|
|
176 body
|
|
177 {
|
|
178 R delegate (ARGS) dg;
|
|
179 dg.funcptr = &method;
|
|
180 dg.ptr = Bridge.getDObject(self);
|
|
181
|
|
182 ARGS args;
|
|
183
|
|
184 foreach (i, a ; objcArgs)
|
|
185 {
|
|
186 alias typeof(args[i]) ArgType;
|
|
187
|
|
188 args[i] = decapsule!(ArgType)(a);
|
|
189 }
|
|
190
|
|
191 return dg(args);
|
|
192 }
|
|
193 }
|
|
194 }
|
|
195
|
|
196 template ObjcBindClassMethod (alias method, string selector)
|
|
197 {
|
|
198 mixin ObjcBindClassMethod!(method, ReturnTypeOf!(method), selector, ParameterTupleOf!(method));
|
|
199 }
|
|
200
|
|
201 template ObjcBindClassMethod (alias method, R, string selector, ARGS...)
|
|
202 {
|
|
203 private ObjcMethodDeclaration!(method, R, selector, ARGS) objcClassMethodDeclaration;
|
|
204
|
|
205 static if (needsEncapsulation!(R))
|
|
206 {
|
|
207 private alias ReplaceAllClasses!(id, ARGS) ObjcArgs;
|
|
208
|
|
209 private static id forwardStaticCall (id self, SEL cmd, ObjcArgs objcArgs)
|
|
210 in
|
|
211 {
|
|
212 assert(isCapsule(self));
|
|
213 }
|
|
214 body
|
|
215 {
|
|
216 R function (ARGS) dg = &method;
|
|
217
|
|
218 ARGS args;
|
|
219
|
|
220 foreach (i, a ; objcArgs)
|
|
221 {
|
|
222 alias typeof(args[i]) ArgType;
|
|
223
|
|
224 args[i] = decapsule!(ArgType)(a);
|
|
225 }
|
|
226
|
|
227 auto result = dg(args);
|
|
228
|
|
229 return encapsule!(R)(result);
|
|
230 }
|
|
231 }
|
|
232
|
|
233 else
|
|
234 {
|
|
235 private alias ReplaceAllClasses!(id, ARGS) ObjcArgs;
|
|
236
|
|
237 private static R forwardStaticCall (id self, SEL cmd, ObjcArgs objcArgs)
|
|
238 in
|
|
239 {
|
|
240 assert(isCapsule(self));
|
|
241 }
|
|
242 body
|
|
243 {
|
|
244 R function (ARGS) dg = &method;
|
|
245
|
|
246 ARGS args;
|
|
247
|
|
248 foreach (i, a ; objcArgs)
|
|
249 {
|
|
250 alias typeof(args[i]) ArgType;
|
|
251
|
|
252 args[i] = decapsule!(ArgType)(a);
|
|
253 }
|
|
254
|
|
255 return dg(args);
|
|
256 }
|
|
257 }
|
|
258 }
|
|
259
|
|
260 struct Bridge
|
|
261 {
|
|
262 static:
|
|
263
|
|
264 const objcMethodDeclarationVar = "objcMethodDeclaration";
|
|
265 const objcClassMethodDeclarationVar = "objcClassMethodDeclaration";
|
|
266 const dObjectVar = "dObject";
|
|
267 alias void* DObjectType;
|
|
268 private ClassInfo[string] registeredClasses;
|
|
269
|
|
270 void registerClass (string className, ClassInfo classInfo)
|
|
271 {
|
|
272 if (className.length > 2 && className[0 .. 2] == "D_")
|
|
273 registeredClasses[className[0 .. 2]] = classInfo;
|
|
274
|
|
275 else
|
|
276 registeredClasses[className] = classInfo;
|
|
277 }
|
|
278
|
|
279 Object createRegisteredObject (T) (string className)
|
|
280 {
|
|
281 return null;
|
|
282 }
|
|
283
|
|
284 DObjectType getDObject (id self)
|
|
285 {
|
|
286 DObjectType dObject;
|
|
287
|
|
288 self.getInstanceVariable!(DObjectType, dObjectVar)(dObject);
|
|
289
|
|
290 return dObject;
|
|
291 }
|
|
292
|
|
293 id setDObject (Object dObject, id objcObject)
|
|
294 {
|
|
295 auto o = cast(DObjectType) dObject;
|
|
296 addRoot(o);
|
|
297
|
|
298 objcObject.setInstanceVariable!(DObjectType, Bridge.dObjectVar)(o);
|
|
299
|
|
300 return objcObject;
|
|
301 }
|
|
302
|
|
303 R invokeObjcMethod (R, string selector, ARGS...) (id self, ARGS args)
|
|
304 {
|
|
305 static if (!is(R : void))
|
|
306 R result;
|
|
307
|
|
308 SEL sel = sel.registerName!(selector);
|
|
309
|
|
310 alias ReplaceAllClasses!(id, ARGS) ObjcArgs;
|
|
311 ObjcArgs objcArgs;
|
|
312
|
|
313 foreach (i, a ; args)
|
|
314 {
|
|
315 alias typeof(a) ArgType;
|
|
316
|
|
317 objcArgs[i] = encapsule!(ArgType)(a);
|
|
318 }
|
|
319
|
|
320 static if (is(R == struct))
|
|
321 {
|
|
322 self.msgSend_stret(result, sel, objcArgs);
|
|
323 return result;
|
|
324 }
|
|
325
|
|
326 else static if (is(R == float) || is(R == double) || is(R == real))
|
|
327 {
|
|
328 version (X86)
|
|
329 {
|
|
330 static if (is(R == float))
|
|
331 return self.msgSend_fpret!(R)(sel, objcArgs);
|
|
332
|
|
333 else
|
|
334 return self.msgSend_fpret(sel, objcArgs);
|
|
335 }
|
|
336
|
|
337 else version (X86_64)
|
|
338 {
|
|
339 static if (is(R == real))
|
|
340 return self.msgSend_fpret(sel, objcArgs);
|
|
341
|
|
342 else
|
|
343 return self.msgSend!(R)(sel, objcArgs);
|
|
344 }
|
|
345
|
|
346 else
|
|
347 return self.msgSend!(R)(sel, objcArgs);
|
|
348 }
|
|
349
|
|
350 else static if (is(R : ObjcWrapper))
|
|
351 {
|
|
352 id r = self.msgSend(sel, objcArgs);
|
|
353
|
|
354 if (!r)
|
|
355 return null;
|
|
356
|
|
357 if (isCapsule(r))
|
|
358 {
|
|
359 result = decapsule!(R)(r);
|
|
360
|
|
361 if (result)
|
|
362 return result;
|
|
363 }
|
|
364
|
|
365 return new R(r);
|
|
366 }
|
|
367
|
|
368 else static if (is(R : Object))
|
|
369 {
|
|
370 id r = self.msgSend(sel, objcArgs);
|
|
371
|
|
372 if (!r)
|
|
373 return null;
|
|
374
|
|
375 return decapsule!(R)(r);
|
|
376 }
|
|
377
|
|
378 else
|
|
379 return self.msgSend!(R, ObjcArgs)(sel, objcArgs);
|
|
380 }
|
|
381
|
|
382 R invokeObjcClassMethod (R = id, string selector, ARGS...) (Class cls, ARGS args)
|
|
383 {
|
|
384 return invokeObjcMethod!(R, selector, ARGS)(cast(id) cls, args);
|
|
385 }
|
|
386
|
|
387 R invokeObjcSuperMethod (R = id, string selector, ARGS...) (objc_super* self, ARGS args)
|
|
388 {
|
|
389 R result;
|
|
390 SEL sel = sel.registerName!(selector);
|
|
391
|
|
392 alias ReplaceAllClasses!(id, ARGS) ObjcArgs;
|
|
393 ObjcArgs objcArgs;
|
|
394
|
|
395 foreach (i, a ; args)
|
|
396 {
|
|
397 alias typeof(a) ArgType;
|
|
398
|
|
399 objcArgs[i] = encapsule!(ArgType)(a);
|
|
400 }
|
|
401
|
|
402 static if (is(R == struct))
|
|
403 {
|
|
404 self.msgSendSuper_stret(result, sel, objcArgs);
|
|
405 }
|
|
406
|
|
407 else static if (is(R : ObjcWrapper))
|
|
408 {
|
|
409 id r = self.msgSendSuper(sel, objcArgs);
|
|
410
|
|
411 if (isCapsule(r))
|
|
412 {
|
|
413 result = decapsule!(R)(r);
|
|
414
|
|
415 if (result)
|
|
416 return result;
|
|
417 }
|
|
418
|
|
419 return r ? new R(r) : null;
|
|
420 }
|
|
421
|
|
422 else static if (is(R : Object))
|
|
423 {
|
|
424 id r = self.msgSendSuper(sel, objcArgs);
|
|
425 return decapsule!(R)(r);
|
|
426 }
|
|
427
|
|
428 else
|
|
429 return self.msgSend!(R)(sel, objcArgs);
|
|
430 }
|
|
431 } |