Thread overview
D-Cocoa Port
Apr 13, 2012
Rizo Isrof
Apr 13, 2012
Jacob Carlborg
Apr 18, 2012
Rizo Isrof
Apr 18, 2012
Jacob Carlborg
April 13, 2012
Hi,

I am planning to use D for creating native applications on Mac OS X. For that, of course, D must interact with the Cocoa API. I have no knowledge of how this bindings could be done. I've already looked at Cocado[1] and do know the Michel Fortin's D-ObjC bridge[2], and would like to be enlightened about some details:
 * What is their (or any other possible) approach of porting Cocoa to D? -- here I essentially ask for technical reading sources (books/articles/references, etc);
 * Is it possible to establish an ABI compatibility with ObjC directly or through the C ABI? -- There's a question on _stackoverflow_[3] where the answers describe what could be achieved with Objective-C's Runtime Reference[4];
 * Is it feasible to make this kind of stuff work nicely and get the level of performance and stability like, e.g., with Qt framework on Mac OS X?

I have no experience at all in this field so forgive me any technical faults. Any help would be appreciable.

Thanks in advance for your responses.

[1]: http://sourceforge.net/projects/cocodao/
[2]: http://michelf.com/projects/d-objc/
[3]: http://stackoverflow.com/questions/5901508/calling-cocoa-apis-from-c
[4]: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html

 - Rizo
April 13, 2012
On 2012-04-13 15:34, Rizo Isrof wrote:
> Hi,
>
> I am planning to use D for creating native applications on Mac OS X. For
> that, of course, D must interact with the Cocoa API. I have no knowledge
> of how this bindings could be done. I've already looked at Cocado[1] and
> do know the Michel Fortin's D-ObjC bridge[2], and would like to be
> enlightened about some details:
> * What is their (or any other possible) approach of porting Cocoa to D?
> -- here I essentially ask for technical reading sources
> (books/articles/references, etc);

There are three ways (two deepening on how you look at it).

1. Use the Objective-C runtime manually, which is implement with regular C functions.

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html

2. Use a bridge that handles everything nicely. The bridge uses the same implementation as in the first way. The bridge also performs type conversions automatically and other things to ease the development.

http://www.dsource.org/projects/dstep
http://michelf.com/projects/d-objc-bridge/

3. Implement direct support in the language to be compatible with Objective-C. This is what Michel Fortin did here:

http://michelf.com/projects/d-objc/
(Note, this is not a bridge)

He forked DMD and added support for extern(Objective-C) which will output code similar to what GCC/Clang would do. This is a more direct way to interface with Objective-C and won't add any more overhead than using Objective-C directly would. I guess this would mean the ABI's are compatible.

> * Is it possible to establish an ABI compatibility with ObjC directly or
> through the C ABI? -- There's a question on _stackoverflow_[3] where the
> answers describe what could be achieved with Objective-C's Runtime
> Reference[4];

Both are possible, see above.

> * Is it feasible to make this kind of stuff work nicely and get the
> level of performance and stability like, e.g., with Qt framework on Mac
> OS X?

I think so if you use Michel's fork of DMD.

> I have no experience at all in this field so forgive me any technical
> faults. Any help would be appreciable.
>
> Thanks in advance for your responses.

I think the best way is to use the third one mentioned in the list above. Note that this is based on an older version of DMD. The project is not finished yet and is currently on old. Although an alpha version has been released.

The problem with a bridge is the it cause way to much bloat. A Hello World application written using a bridge generated a 60MB executable.

Using the Objective-C runtime manually is just too verbose and tedious. If you are interested in this you can have a look at my documentation of how my Objective-c/D bridge works and is implemented:

http://www.dsource.org/projects/dstep/wiki/ObjcBridge/BridgeInternals

> [1]: http://sourceforge.net/projects/cocodao/
> [2]: http://michelf.com/projects/d-objc/

Note, this is not a bridge. This is third one mentioned in the list above.

> [3]: http://stackoverflow.com/questions/5901508/calling-cocoa-apis-from-c
> [4]:
> http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html
>
>
> - Rizo


-- 
/Jacob Carlborg
April 18, 2012
On Friday, 13 April 2012 at 14:28:05 UTC, Jacob Carlborg wrote:
> On 2012-04-13 15:34, Rizo Isrof wrote:
>> Hi,
>>
>> I am planning to use D for creating native applications on Mac OS X. For
>> that, of course, D must interact with the Cocoa API. I have no knowledge
>> of how this bindings could be done. I've already looked at Cocado[1] and
>> do know the Michel Fortin's D-ObjC bridge[2], and would like to be
>> enlightened about some details:
>> * What is their (or any other possible) approach of porting Cocoa to D?
>> -- here I essentially ask for technical reading sources
>> (books/articles/references, etc);
>
> There are three ways (two deepening on how you look at it).
>
> 1. Use the Objective-C runtime manually, which is implement with regular C functions.
>
> https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html
>
> 2. Use a bridge that handles everything nicely. The bridge uses the same implementation as in the first way. The bridge also performs type conversions automatically and other things to ease the development.
>
> http://www.dsource.org/projects/dstep
> http://michelf.com/projects/d-objc-bridge/
>
> 3. Implement direct support in the language to be compatible with Objective-C. This is what Michel Fortin did here:
>
> http://michelf.com/projects/d-objc/
> (Note, this is not a bridge)
>
> He forked DMD and added support for extern(Objective-C) which will output code similar to what GCC/Clang would do. This is a more direct way to interface with Objective-C and won't add any more overhead than using Objective-C directly would. I guess this would mean the ABI's are compatible.
>
>> * Is it possible to establish an ABI compatibility with ObjC directly or
>> through the C ABI? -- There's a question on _stackoverflow_[3] where the
>> answers describe what could be achieved with Objective-C's Runtime
>> Reference[4];
>
> Both are possible, see above.
>
>> * Is it feasible to make this kind of stuff work nicely and get the
>> level of performance and stability like, e.g., with Qt framework on Mac
>> OS X?
>
> I think so if you use Michel's fork of DMD.
>
>> I have no experience at all in this field so forgive me any technical
>> faults. Any help would be appreciable.
>>
>> Thanks in advance for your responses.
>
> I think the best way is to use the third one mentioned in the list above. Note that this is based on an older version of DMD. The project is not finished yet and is currently on old. Although an alpha version has been released.
>
> The problem with a bridge is the it cause way to much bloat. A Hello World application written using a bridge generated a 60MB executable.
>
> Using the Objective-C runtime manually is just too verbose and tedious. If you are interested in this you can have a look at my documentation of how my Objective-c/D bridge works and is implemented:
>
> http://www.dsource.org/projects/dstep/wiki/ObjcBridge/BridgeInternals
>
>> [1]: http://sourceforge.net/projects/cocodao/
>> [2]: http://michelf.com/projects/d-objc/
>
> Note, this is not a bridge. This is third one mentioned in the list above.
>
>> [3]: http://stackoverflow.com/questions/5901508/calling-cocoa-apis-from-c
>> [4]:
>> http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html
>>
>>
>> - Rizo

Thank you for such a detailed overview of the existing approaches - it helped me a lot to understand the theory. As you suggested, I will probably use a modified version of the compiler for tests. But in order to fully understand the implementation details I will try to write my own  extension of D for LDC with `extern(Objective-C)` support.

Thanks again for you answer.

- Rizo

April 18, 2012
On 2012-04-18 16:39, Rizo Isrof wrote:

> Thank you for such a detailed overview of the existing approaches - it
> helped me a lot to understand the theory. As you suggested, I will
> probably use a modified version of the compiler for tests. But in order
> to fully understand the implementation details I will try to write my
> own extension of D for LDC with `extern(Objective-C)` support.
>
> Thanks again for you answer.

DMD and LDC uses the same frontend so you could study the DMD fork if you want to. The glue code (connecting the frontend and backend) would be unique to each compiler.

How about contributing to the DMD fork instead and try to finish it?

-- 
/Jacob Carlborg