Skip to content

Commit 21534e4

Browse files
committed
cfunction: add help for struct CFunction
This struct had a brief mention in the manual, but was missing NEWs and info about usage fix #27490
1 parent 917ae8b commit 21534e4

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

NEWS.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -994,8 +994,11 @@ Deprecated or removed
994994

995995
* `Base.SparseArrays.SpDiagIterator` has been removed ([#23261]).
996996

997-
* The tuple-of-types form of `cfunction`, `cfunction(f, returntype, (types...))`, has been deprecated
998-
in favor of the tuple-type form `cfunction(f, returntype, Tuple{types...})` ([#23066]).
997+
* The function `cfunction`, has been deprecated in favor of a macro form `@cfunction`.
998+
Most existing uses can be upgraded simply by adding a `@`.
999+
The new syntax now additionally supports allocating closures at runtime,
1000+
for dealing with C APIs that don't provide a separate `void* env`-type callback
1001+
argument. ([#26486])
9991002

10001003
* `diagm(v::AbstractVector, k::Integer=0)` has been deprecated in favor of
10011004
`diagm(k => v)` ([#24047]).

base/c.jl

+14-3
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,22 @@ respectively.
1616
"""
1717
cglobal
1818

19-
struct CFunction
19+
"""
20+
CFunction struct
21+
22+
Garbage-collection handle for the return value from `@cfunction`
23+
when the first argument is annotated with '\$'.
24+
Like all `cfunction` handles, it should be passed to `ccall` as a `Ptr{Cvoid}`,
25+
and will be converted automatically at the call site to the appropriate type.
26+
27+
See [`@cfunction`](@ref).
28+
"""
29+
struct CFunction <: Ref{Cvoid}
2030
ptr::Ptr{Cvoid}
2131
f::Any
2232
_1::Ptr{Cvoid}
2333
_2::Ptr{Cvoid}
24-
let construtor = false end
34+
let constructor = false end
2535
end
2636
unsafe_convert(::Type{Ptr{Cvoid}}, cf::CFunction) = cf.ptr
2737

@@ -31,11 +41,12 @@ unsafe_convert(::Type{Ptr{Cvoid}}, cf::CFunction) = cf.ptr
3141
3242
Generate a C-callable function pointer from the Julia function `closure`
3343
for the given type signature.
44+
To pass the return value to a `ccall`, use the argument type `Ptr{Cvoid}` in the signature.
3445
3546
Note that the argument type tuple must be a literal tuple, and not a tuple-valued variable or expression
3647
(although it can include a splat expression). And that these arguments will be evaluated in global scope
3748
during compile-time (not deferred until runtime).
38-
Adding a `\$` in front of the function argument changes this to instead create a runtime closure
49+
Adding a '\$' in front of the function argument changes this to instead create a runtime closure
3950
over the local variable `callable`.
4051
4152
See [manual section on ccall and cfunction usage](@ref Calling-C-and-Fortran-Code).

base/compiler/validation.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const VALID_EXPR_HEADS = IdDict{Any,Any}(
2222
:meta => 0:typemax(Int),
2323
:global => 1:1,
2424
:foreigncall => 3:typemax(Int),
25-
:cfunction => 6:6,
25+
:cfunction => 5:5,
2626
:isdefined => 1:1,
2727
:simdloop => 0:0,
2828
:gc_preserve_begin => 0:typemax(Int),

doc/src/base/c.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
ccall
55
Core.Intrinsics.cglobal
66
Base.@cfunction
7+
Base.CFunction
78
Base.unsafe_convert
89
Base.cconvert
910
Base.unsafe_load

0 commit comments

Comments
 (0)