← Back to context

Comment by tom_

17 hours ago

Nested functions may require a context pointer of some kind, which the caller can't supply. One way of doing this: create a thunk on the stack that provides the context pointer, and use that address as the pointer to the nested function.

But now the stack needs to be executable.

Doesn't x86 actually support nested call pointers using enter to natively support this in Pascal?

  • It’s not a question of ISA support. If you call via a function pointer, how do you supply the pointer to the data? (It has to either be in a separate place from the function code, or the same place. One requires an ABI change, the other an executable, writable section of memory.)

    • > If you call via a function pointer, how do you supply the pointer to the data?

      D has the notion of a "delegate", which is a (function pointer) and (context pointer) pair. This is incredibly useful, because delegates can:

      1. call nested functions that need a pointer to the stack frame of the nestee function

      2. call member functions that need `this` pointer

      3. call lambdas

      4. call COM member functions

      The neato thing about this is the ABI for delegates is all the same, so a function that gets a delegate parameter will work with any of 1..4. It's one of the most used features of D.

      6 replies →