www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Follow-on question about delegates

reply Jerry Quinn <jlquinn optonline.net> writes:
OK, having thought a bit more about delegates, I now have another question.

The ABI shows a delegate as consisting of a context ptr and a function ptr. 
The context ptr can be a class reference, ptr to struct, ptr to closure or ptr
to stack frame.

If you're passing one of these delegates into another function, how does the
receiving function figure out what kind of context it's looking at?  Each of
these things will look different in memory.  Also, structs at least don't have
a header that can be used to disambiguate the situation.

Thanks,
Jerry
Jun 09 2009
next sibling parent grauzone <none example.net> writes:
Jerry Quinn wrote:
 OK, having thought a bit more about delegates, I now have another question.
 
 The ABI shows a delegate as consisting of a context ptr and a function ptr. 
The context ptr can be a class reference, ptr to struct, ptr to closure or ptr
to stack frame.
 
 If you're passing one of these delegates into another function, how does the
receiving function figure out what kind of context it's looking at?  Each of
these things will look different in memory.  Also, structs at least don't have
a header that can be used to disambiguate the situation.

The caller just passes the context pointer as a hidden argument when calling the function ptr and doesn't care about the rest. It's up to the code on the callee side to actually use the context pointer. But there's never an ambiguity what the callee expects as the context pointer. It's simple: if you take a delegate of an object method, the context pointer has to be a class reference. If you take a delegate of a nested function, the context pointer must point to the stack frame. And so on. If you want to say so, the code referenced by the function pointer decides how the context pointer is interpreted. Note that this can never go wrong: it's not possible to take a delegate of a class method and (somehow) to use a frame pointer as the context pointer.
 Thanks,
 Jerry
 

Jun 09 2009
prev sibling next sibling parent BCS <none anon.com> writes:
Hello Jerry,

 OK, having thought a bit more about delegates, I now have another
 question.
 
 The ABI shows a delegate as consisting of a context ptr and a function
 ptr.  The context ptr can be a class reference, ptr to struct, ptr to
 closure or ptr to stack frame.
 
 If you're passing one of these delegates into another function, how
 does the receiving function figure out what kind of context it's
 looking at?  Each of these things will look different in memory.
 Also, structs at least don't have a header that can be used to
 disambiguate the situation.
 
 Thanks,
 Jerry

ok here is a brain twister: the context can be anything, it's just a size_t full of bits. **** THE FOLLOWING IS NOT RECOMMENDED ***** http://codepad.org/8nnoIKNQ import std.stdio; struct S { int go() { U u; u.s = this; return u.a + u.b; } } union U { S* s; struct { short a; short b; } } void main() { U u; u.a= 54; u.b = 42; int delegate() dg = &u.s.go; writef("%s\n", dg()); }
Jun 09 2009
prev sibling parent Don <nospam nospam.com> writes:
Jerry Quinn wrote:
 OK, having thought a bit more about delegates, I now have another question.

These type of questions belong in digitalmars.D.learn.
Jun 09 2009