digitalmars.D.learn - Object pointer from delegate
- Lutger (2/2) Aug 25 2006 Is it possible to obtain the object pointer from a delegate that is
- Kirk McDonald (30/32) Aug 25 2006 Yes, it is a hack. Here's the method I use in Pyd:
- Lutger (3/40) Aug 26 2006 Thanks! This can be useful if needed. Imho it would be nice if there was...
- BCS (18/21) Aug 28 2006 Not all delegates have a Object pointer in them.
- Don Clugston (6/42) Aug 27 2006 Yes, but it has been stated that it will change in DMD 2.0, when
- Craig Black (3/5) Aug 28 2006 I've not heard about this before. I only have a fuzzy understanding of ...
- Don Clugston (15/22) Aug 28 2006 In the current situation, delegates are structs with an objectpointer
Is it possible to obtain the object pointer from a delegate that is taken from a member function? Is it a hack?
Aug 25 2006
Lutger wrote:Is it possible to obtain the object pointer from a delegate that is taken from a member function? Is it a hack?Yes, it is a hack. Here's the method I use in Pyd: struct FakeDG { Object instance; void* fn; } template dg_union(Dg) { union dg_union { FakeDG fake_dg; Dg real_dg; } } class Foo { void foo() { } } void main() { Foo f = new Foo; auto dg = &f.foo; dg_union!(typeof(dg)) u; u.real_dg = dg; Foo g = cast(Foo)u.fake_dg.instance; assert(f is g); } This relies on the particular way that DMD represents delegates internally, which is not (yet) a part of the spec. I don't really expect this to change any time soon, but it should still be considered a hack. -- Kirk McDonald Pyd: Wrapping Python with D http://pyd.dsource.org
Aug 25 2006
Thanks! This can be useful if needed. Imho it would be nice if there was a delegate property to get the object pointer. Kirk McDonald wrote:Lutger wrote:Is it possible to obtain the object pointer from a delegate that is taken from a member function? Is it a hack?Yes, it is a hack. Here's the method I use in Pyd: struct FakeDG { Object instance; void* fn; } template dg_union(Dg) { union dg_union { FakeDG fake_dg; Dg real_dg; } } class Foo { void foo() { } } void main() { Foo f = new Foo; auto dg = &f.foo; dg_union!(typeof(dg)) u; u.real_dg = dg; Foo g = cast(Foo)u.fake_dg.instance; assert(f is g); } This relies on the particular way that DMD represents delegates internally, which is not (yet) a part of the spec. I don't really expect this to change any time soon, but it should still be considered a hack.
Aug 26 2006
Lutger wrote:Thanks! This can be useful if needed. Imho it would be nice if there was a delegate property to get the object pointer.Not all delegates have a Object pointer in them. class C { int j; int m(){return j;} } int fn(bool b) { int i; int nfn() { return i; } auto dg = b ? &nfn : &(new C).m; // context on dg might be an Object or a stack frame return dg(); }
Aug 28 2006
Kirk McDonald wrote:Lutger wrote:Yes, but it has been stated that it will change in DMD 2.0, when delegates and function pointers become the same. At which time it might not even be possible to get the object pointer from a delegate. (Since the delegate will point to a thunk, it may be possible to disassemble the thunk to recover the object pointer. Maybe).Is it possible to obtain the object pointer from a delegate that is taken from a member function? Is it a hack?Yes, it is a hack. Here's the method I use in Pyd: struct FakeDG { Object instance; void* fn; } template dg_union(Dg) { union dg_union { FakeDG fake_dg; Dg real_dg; } } class Foo { void foo() { } } void main() { Foo f = new Foo; auto dg = &f.foo; dg_union!(typeof(dg)) u; u.real_dg = dg; Foo g = cast(Foo)u.fake_dg.instance; assert(f is g); } This relies on the particular way that DMD represents delegates internally, which is not (yet) a part of the spec. I don't really expect this to change any time soon, but it should still be considered a hack.
Aug 27 2006
(Since the delegate will point to a thunk, it may be possible to disassemble the thunk to recover the object pointer. Maybe).I've not heard about this before. I only have a fuzzy understanding of what a thunk is. What is the benefit of using thunks in this context? -Craig
Aug 28 2006
Craig Black wrote:In the current situation, delegates are structs with an objectpointer and a function pointer. Calling a delegate dg() = someobj.somefunc() results in code something like: mov ebx, dg.objectpointer call dg.functionpointer --> calls somefunc. In D 2.0, delegates will just be function pointers. The equivalent code is call [dg] --> calls thunk thunk: mov ebx, someobj jmp somefunc A major advantage is that you can pass a delegate to a C function that expects a function pointer (eg, the WindowsAPI). This is how WTL works in the C++ world. The thunk code needs to be created at run time; normally, it's created on the stack.(Since the delegate will point to a thunk, it may be possible to disassemble the thunk to recover the object pointer. Maybe).I've not heard about this before. I only have a fuzzy understanding of what a thunk is. What is the benefit of using thunks in this context? -Craig
Aug 28 2006