Convert a delegate to a function (i.e. make a thunk)
David Nadlinger
see at klickverbot.at
Wed Apr 25 15:06:54 PDT 2012
On Wednesday, 25 April 2012 at 21:27:30 UTC, Jonathan M Davis
wrote:
> But you can't convert a delegate to a function without passing
> the delegate to
> the function one way or another, because the function doesn't
> have a context
> (not being a delegate).
That's the whole point of the question - you _can_ pull this off
if you create a small code fragment somewhere in memory which
calls the delegate with that specific context argument, and then
set your function pointer to its address (similar to thunks in
most vtable implementations or SEH, if you are familiar with
these). Now, the only problem is that executing data is not quite
easy, mostly because of security mechanisms (W^X, NX and friends).
As for the original question, I didn't need the feature so far
(many C libraries provide a void* bit of data to use at the
client's discretion), but if you need inspiration on how to
implement it, I'd look at projects like Mono and FFI libraries
which likely need to provide similar functionality.
Would probably be a good addition to Phobos.
David
More information about the Digitalmars-d
mailing list