Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: add unsafe_pointer_to_any (fixes #2458) #2468

Merged
merged 1 commit into from
Mar 6, 2013

Conversation

stevengj
Copy link
Member

@stevengj stevengj commented Mar 5, 2013

This adds a function unsafe_pointer_to_any(p::Ptr) that takes a boxed jl_value_t* and converts it back to a Julia Any. It works by adding a new pointerany intrinsic.

This is useful in callback functions, because raw jl_value_t* pointers to boxed Julia types can be passed as void* to C functions by declaring the argument as Any, but there was previously no way to undo this conversion to get back the native Julia object.

In particular, it should be quite useful in implementing closures, as discussed on the mailing list.

@stevengj
Copy link
Member Author

stevengj commented Mar 5, 2013

@JeffBezanson should probably look over the src/intrinsics.cpp patch, to make sure I'm grokking the LLVM stuff.

@vtjnash
Copy link
Member

vtjnash commented Mar 5, 2013

to avoid adding new intrinsics, i was going to give this behavior to unsafe_ref (sorry, I would have responded sooner, but didn't have good internet access earlier). currently unsafe_ref(pointer(Any,uint(0))) throws a compile error only because it was explicitly excluded from things unsafe_ref was allowed to return. i expect i should be able to add that functionality pretty quickly (it's pretty much the same code as you have here, just tacked on a different function)

@stevengj
Copy link
Member Author

stevengj commented Mar 5, 2013

@vtjnash, unsafe_ref on Ptr{Any} sounds good to me. Looking forward to your patch.

@JeffBezanson
Copy link
Member

I could also just allow Any as an argument type in cfunction. Currently it only accepts concrete types, so for purposes of passing a closure you can use Function as the argument type. I did this in Tk.jl.

@stevengj
Copy link
Member Author

stevengj commented Mar 5, 2013

@JeffBezanson, allowing Any in cfunction would be great, although maybe not essential if you can use Function and other Julia leaf types. But it might still be good to have unsafe_ref(p::Ptr{Any}) or whatever, for conversions in other circumstances (e.g. pointers stored in other data structures).

@stevengj
Copy link
Member Author

stevengj commented Mar 5, 2013

@JeffBezanson, passing Function to cfunction seems to work some of the time but not all of the time, e.g.

julia> foo(f::Function) = 3
julia> cfunction(foo, Int, (Function,))
ERROR: function is not yet c-callable
 in cfunction at c.jl:30

@vtjnash vtjnash closed this in b145dbe Mar 6, 2013
@vtjnash
Copy link
Member

vtjnash commented Mar 6, 2013

note that if you do know the type, you can add it as typeassert (same as Any return type for ccall)

x = unsafe_ref( pointer(Any, ptr) )::XType

@JeffBezanson JeffBezanson reopened this Mar 6, 2013
JeffBezanson added a commit that referenced this pull request Mar 6, 2013
RFC: add unsafe_pointer_to_any (fixes #2458)
@JeffBezanson JeffBezanson merged commit ea73f25 into JuliaLang:master Mar 6, 2013
@stevengj stevengj mentioned this pull request Mar 16, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants