[swift-users] Function to unsafe pointer and back
jordan_rose at apple.com
Fri Sep 2 11:49:27 CDT 2016
> On Sep 1, 2016, at 13:48, Andrew Trick via swift-users <swift-users at swift.org> wrote:
>> On Sep 1, 2016, at 12:37 PM, Lou Zell via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>> As to your real question, what’s your high-level goal? Swift doesn’t really do pointers to functions  but it does provide lots of other excellent ‘treat code as data’ features. If you can explain more about your goal, perhaps we can direct you to a better path.
>> Curiosity got the better of me on this one - there's no higher level goal other than improved understanding. I was playing around with function currying in the repl and that's what lead me to those experiments.
>> The article, and the preceding one in the series, has plenty for me to work with. Thank you!
> That’s an awesome article, but I don’t think you need to understand any of it! I’m not an expert in this either, but here’s what I can see from your code...
> It looks like you’re trying to capture the address of a function that was JIT’d for the purpose of evaluating a statement in the repl. That address might not be valid in the next statement.
> Also, in the first case the UnsafePointer points to local memory that holds the function value. In the second case, you’re trying to load a function value from the address of the function body. So a level of indirection is missing.
> You can’t take an address to a function in Swift. You can assign a variable to the function, as you did in the first case:
> var x = doNothing
> And view the address of that variable as an argument, only for the duration of the call, as you did in the first case:
I feel like there’s still one last piece missing here, which is that in Swft a "function-typed value” is not the same as a “function pointer” in C. Because a function value might have come from a closure, it might include captured state…so the representation of a closure is not the same as a representation of a plain function.
Because of that, the address you’re seeing isn’t the address of the function in memory; it’s the address of the temporary allocation used to represent “this global function and no associated state”. That’s why you can’t just form a pointer with that bit-pattern and expect it to work.
If you really need something compatible with a C function, you can use the type '@convention(c) () -> Void’ (or whatever). Note the lack of UnsafePointer here—the reference-ness is baked in in Swift. Even then, though, you can’t rely on the function having the same address every time you run the program (at least on macOS), because of address space randomization—the offset at which your code is loaded will be different on each run. There shouldn’t be any need to do this anyway.
(One place we are still weak is in converting between C function references and UnsafeRawPointer—there’s no dedicated API to do this. I’m not sure we have a recommendation in the rare cases when this is necessary. Andy?)
Hope you’ve gotten some useful information between the three of us. :-)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-users