Convert a delegate to a function (i.e. make a thunk)

Alex Rønne Petersen xtzgzorex at gmail.com
Wed Apr 25 15:25:09 PDT 2012


On 26-04-2012 00:06, David Nadlinger wrote:
> 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

libffi can do it. There's a reason I wanted std.ffi. ;) (Unfortunately 
interest in such a module seemed low earlier.)

-- 
- Alex


More information about the Digitalmars-d mailing list