[Prev][Next][Index][Thread]

Re: Objective-C from Dylan



On Wednesday, December 13, 2000, at 01:15 AM, Andreas Bogk wrote:

> Rob Myers <robm@h2g2.com> writes: 
>  
> > Here's a toy program to call Objective-C from Gwydion Dylan code on MacOS X, written 
> quickly last night. 
>  
> Interesting. Can you do the opposite? Receiving an Objective-C message 
> in Dylan code? I understand that it is required to inherit from an 
> application object in Cocoa. 

It's possible in principle:
- Objective-C code compiles down to C functions and data structures.
- The Objective-C runtime is implemented as C functions and data types.
- The Objective-C messaging system is basically a series of hashtables of function pointers.
- d2c and Fun-Dev can generate C callbacks to Dylan code which can be passed as function pointers.
Therefore as long as we generate Objective-C method names/signatures correctly for hashing, we can add Dylan callbacks as function pointers in the Objective-C messaging tables using knowledge of runtime internals for a given Objective-C implementation. This will allow the Objective-C runtime to call Dylan methods, passing Objective-C messages into Dylan. 

The bit I don't know about is how to access Dylan data structures from Objective-C and vice versa. Given NeXT/Apple's proxy and message forwarding systems we may be able to get away with pretending that Dylan methods are Objective-C methods on a trivial subclass of an AppKit class that add no data members.

The funkiest thing would be to declare an obj-c-call Dylan macro that used square brackets and keywords for Objective-C calls, allowing embedded Objective-C syntax in dylan:
	Objective-C[call klin: 8, blim:[object retain]]
but that's getting a little silly. :-)

Thinking about it, a simple interface between Next/Apple Objective-C and Dylan could just use an Objective-C wrapper function to call a Dylan callback as a dispatch point. The wrapper would be declared in .i and .m files as pure Objective-C, and would dispatch messages to a Dylan callback. The project's Makefile then needs to enable Objective-C and include the AppKit for this to work. The wrapper would declare a proxy object for the Dylan code (for example for the Application proxy), and implement the forwardInvocation: message.  forwardInvocation: would forward messages into the Dylan runtime along with the NSInvocation object provided by the Objective-C runtime. The NSInvocation object encapsulates the original call and its parameters, allowing the Dylan code to access the call parameters, the sender, this and super, using wrapped Objective-C calls.

See "Inside Cocoa: Object-Oriented Programming and the Objective-C Language" p. 140 on Forwarding.

- Rob.