How To Call D Code from Objective C?

Rikki Cattermole via Digitalmars-d digitalmars-d at puremagic.com
Thu Dec 10 22:05:04 PST 2015


On 11/12/15 7:01 PM, Mike McKee wrote:
> Is there a way to call D code from Objective C on OSX?
>
> Here's something cool I was thinking about doing:
>
> * I've learned how to make Cocoa UI Objective C apps on OSX.
>
> * I've learned how to load the native OSX webkit widget in a Cocoa app.
> It provides a rich user interface using ordinary HTML, CSS, and
> Javascript/jQuery. It's much better than anything Qt has with its
> widgets or QML. It's much better than anything Apple allows with its
> other Aqua Cocoa widgets. I can make any kind of widget you can think
> of, colored and styled anyway I want, using webkit.
>
> * I've learned how to enable a bridge between Javascript to Objective C.
> This lets me make an Objective C function that Javascript can call. So,
> if I need my webkit app to do something powerful like show a file
> chooser dialog, I can call an object called "objc" and so something like:
>
> var sFilePath = objc.chooseFile();
>
> * Now all I want to do is to bridge Objective C to D. That way, my
> webkit pages can call Objective C, which can then call D. I can put much
> of the heavy lifting work in D for my app. If I need to call an Apple
> Foundation Class library, I can have D call back to Objective C just for
> that special thing.

D.learn is more appropriate but here in the "easy" answer (ignoring 
extern(Objective-C)).

Create a c wrapper.

Example:
https://github.com/Devisualization/window/blob/master/platforms/macosx/devisualization/window/window.d#L227
https://github.com/Devisualization/window/blob/master/cocoa_library/project/dwc-osx/dwc_cocoa.h
https://github.com/Devisualization/window/blob/master/cocoa_library/project/dwc-osx/window.m




More information about the Digitalmars-d mailing list