DIP87: Enhanced Foreign-Language Binding

Jacob Carlborg via Digitalmars-d digitalmars-d at puremagic.com
Sat Jan 23 04:00:05 PST 2016


On 2016-01-23 02:31, Anon wrote:

> Ideally, by whether the `extern()` forms a block or it is attached
> directly to the symbol. I understand that wouldn't work with existing
> implementation in the compiler, but hopefully it wouldn't be too
> difficult to do. But I know nothing of compiler internals so am probably
> wrong.

That will change the behavior compared to how all other attributes work. 
There's no difference for these syntaxes:

extern(C) int a;
extern(C) { int a; }
extern(C): int a;

Or:

public int a;
public { int a; }
public: int a;

> I'll have to look into this more. My cursory reading told me of
> "instance" methods and "class" methods, that get mangled as
> "_i_<Something>_<method>..." and "_c_<Something>_<method>..."
> respectively. Is this what you are talking about?

No. In Objective-C there's a protocol (interface) called "NSObject" and 
a class called "NSObject". It needs to be possible to do this:

extern(Objective-C)
class NSObject {} // standard mangling, NSObject

extern(Objective-C) pragma(mangle, "NSObject")
interface NSObjectProtocol {} // custom mangling, NSObject

The mangling for a method is more like L_OBJC_SELECTOR_REFERENCES_0. 
Where 0 is an increasing number.

If you have access to a Mac you can try compiling this [1] example and 
list all symbols with the "nm" command.

> Sure, but when using ObjC code from D:
>
> extern(Objective-C) class Something {
> void moveTo(float x, float y) @selector("x:y:");
> }
>
> // Elsewhere
> something.moveTo(1,2);
>
> I don't see how the selector is doing anything more than mangling here.
> Even if you have multiple methods in D that mangle to the method name in
> ObjC (minus parameters), that selection is done by D, which then mangles
> the name according to the selector, right? If not, do you have an
> example handy to illustrate?

No. The above call to "moveTo" is lowered to something like this:

objc_msgSend(something, "x:y:", 1, 2);

The Objective-C runtime will find the correct method implementation to 
call based on the selector.

[1] http://dlang.org/spec/objc_interface.html#usage-example

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list