May 04
On Saturday, 1 May 2021 at 16:06:05 UTC, Steven Schveighoffer wrote:
> An interface cast involves a thunk (constant pointer adjustment) to get to the interface/object

Yes, but it isn't a https://en.wikipedia.org/wiki/Thunk ?


May 04
On Tuesday, 4 May 2021 at 10:21:42 UTC, Ola Fosheim Grøstad wrote:
> On Saturday, 1 May 2021 at 16:06:05 UTC, Steven Schveighoffer wrote:
>> An interface cast involves a thunk (constant pointer adjustment) to get to the interface/object
>
> Yes, but it isn't a https://en.wikipedia.org/wiki/Thunk ?

The article literally gives this exact use-case as an example:

https://en.wikipedia.org/wiki/Thunk#Object-oriented_programming
May 04

On Tuesday, 4 May 2021 at 01:20:15 UTC, Q. Schroll wrote:

>

On Saturday, 1 May 2021 at 06:17:36 UTC, Mike Parker wrote:

>

No. An interface is like a pointer to a pointer.

Can you elaborate on this one? I don't really get it. Is an object handle also like a pointer to a pointer? (I feel like I can learn something here.)

So I have no deep knowledge of the implementation details. I only know that a class reference is a pointer under the hood, so it can be cast to void* and back, and that an interface reference has an extra level of indirection, so it can go to void** and back.

May 04
On 5/4/21 9:21 AM, Paul Backus wrote:
> On Tuesday, 4 May 2021 at 10:21:42 UTC, Ola Fosheim Grøstad wrote:
>> On Saturday, 1 May 2021 at 16:06:05 UTC, Steven Schveighoffer wrote:
>>> An interface cast involves a thunk (constant pointer adjustment) to get to the interface/object
>>
>> Yes, but it isn't a https://en.wikipedia.org/wiki/Thunk ?
> 
> The article literally gives this exact use-case as an example:
> 
> https://en.wikipedia.org/wiki/Thunk#Object-oriented_programming

Yeah, I wasn't aware of the more general usage, I thought it was always a pointer adjustment. But I also am not steeped in the terminology, just parroting what I've heard.

-Steve
May 04
On 5/4/21 6:03 AM, Ola Fosheim Grøstad wrote:
> On Tuesday, 4 May 2021 at 01:20:15 UTC, Q. Schroll wrote:
>> On Saturday, 1 May 2021 at 06:17:36 UTC, Mike Parker wrote:
>>> On Saturday, 1 May 2021 at 04:55:10 UTC, frame wrote:
>>>> I always thought as long as an object implements an interface, it should be able to cast it from a void* if it really points to a supporting object.
>>>
>>> No. An interface is like a pointer to a pointer.
>>
>> Can you elaborate on this one? I don't really get it. Is an object handle also like a pointer to a pointer? (I feel like I can learn something here.)
> 
> Off the top of my head the object layout is something like this:
> {
> pointer to vtable0 for the class;
> monitor mutex stuff;
> pointer to interface 1 vtable1;
> pointer to interface 2 vtable2;
> ...
> pointer to interface N vtableN;
> object data 1;
> object data 2;
> ...
> }
> 
> 
> A pointer to interface 1 is a pointer to the pointer to vtable1 in the object above.
> 

Yes, this is exactly how it is. See the ABI document:

https://dlang.org/spec/abi.html#classes

So a class reference is *also* a pointer to a pointer (to the vtable), and a interface reference is the same. However, the slight difference is, the pointer to the interface doesn't allow useful mechanisms until you apply the offset (necessitating a double indirection), whereas a class pointer (combined with compile-time type knowledge) the compiler can access member fields of the class.

-Steve
May 04
On Tuesday, 4 May 2021 at 13:58:59 UTC, Steven Schveighoffer wrote:
> Yeah, I wasn't aware of the more general usage, I thought it was always a pointer adjustment. But I also am not steeped in the terminology, just parroting what I've heard.

My understanding is that a thunk is the code object that fetches the value for you, in this case there might not be a thunk involved as the interface code can just apply a fixed offset?

May 04
On Tuesday, 4 May 2021 at 14:18:30 UTC, Ola Fosheim Grøstad wrote:
> On Tuesday, 4 May 2021 at 13:58:59 UTC, Steven Schveighoffer wrote:
>> Yeah, I wasn't aware of the more general usage, I thought it was always a pointer adjustment. But I also am not steeped in the terminology, just parroting what I've heard.
>
> My understanding is that a thunk is the code object that fetches the value for you, in this case there might not be a thunk involved as the interface code can just apply a fixed offset?

I guess in D terms it can be best explained as a compiler internal "delegate". Let's take the example of getting the this-pointer:

Say, you want to write generic code gen, then you can let that code gen take a "delegate" that computes a this-pointer rather than writing many different code gens for different layouts. Then you trust the optimizer to get rid of it, or keep it if need be. That delegate would be a thunk. It may or may not exist at run-time based on the optimizer/language semantics.

You could also do something similar with an offset or switch statement. The point is, the code gen have no idea of how to obtain the this-pointer, the thunk does. It is a separate external code piece that obtains the value of the this-pointer.
Next ›   Last »
1 2