Skip to content

Commit

Permalink
Merge pull request #76 from krrutkow/master
Browse files Browse the repository at this point in the history
Fix bug with statically-sized arrays as function argument
  • Loading branch information
krrutkow authored Jul 19, 2021
2 parents 6eb4875 + 7fa4a84 commit b53abfe
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "CBinding"
uuid = "d43a6710-96b8-4a2d-833c-c424785e5374"
authors = ["Keith Rutkowski <[email protected]>"]
version = "1.0.4"
version = "1.0.5"

[deps]
Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@ Therefore, attaching other methods to a bound C function is not possible.
It is also sometimes necessary to use the `c"..."` mangled names directly in Julia (for instance in the REPL help mode).
Until consistent, universal support for the string macro is available, the mangled names can be used directly as `var"c\"...\""`, like `help?> var"c\"struct Y\""`.

When a C function has a statically-sized array as an argument in its signature, the semantics of C is to treat the argument as a pointer instead.
Therefore, the statically-sized array in the binding signature will be lowered to a pointer for the underlying `ccall`.
A user can then pass any argument (array, pointer, etc.) that is compatible with a pointer argument type (not the anticipated statically-sized array type).


# Some helpful tips

Expand Down
1 change: 1 addition & 0 deletions src/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Base.size(::Type{CA}) where {T, N, CA<:Carrays{T, N}} = (N,)
Base.length(::Type{CA}) where {T, N, CA<:Carrays{T, N}} = N
Base.eltype(::Type{CA}) where {T, N, CA<:Carrays{T, N}} = T
Base.convert(::Type{CA}, t::Tuple) where {CA<:Carray} = CA(t...)
Base.convert(::Type{CA}, v::Vector) where {CA<:Carray} = CA(v...)

Base.convert(::Type{CA}, str::String) where {T<:Union{Int8, UInt8, Cconst{Int8}, Cconst{UInt8}}, CA<:Carray{T}} = CA(str...)
Base.convert(::Type{String}, ca::Carray{T}) where {T<:Union{Int8, UInt8, Cconst{Int8}, Cconst{UInt8}}} = String(map(t -> reinterpret(UInt8, convert(unqualifiedtype(T), t)), collect(ca)))
Expand Down
6 changes: 5 additions & 1 deletion src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ cconvert_default(::Type{T}) where {E, T<:AbstractArray{E}} = Cptr{E}
$(func <: Cptr ? :(Core.Intrinsics.bitcast(Ptr{Cvoid}, func)) : lib isa Symbol ? :(($(QuoteNode(name)), $(String(lib)))) : QuoteNode(name)),
$((conv isa Symbol ? (conv,) : ())...),
$(retT),
($(((argT <: Cvariadic ? :(Ptr{Cvoid}...) : argT) for argT in argTs)...),),
($(((
argT <: Cvariadic ? :(Ptr{Cvoid}...) :
argT <: Carray ? Cptr{eltype(argT)} : # statically sized arrays (even when typedef-ed) are passed by pointer
argT
) for argT in argTs)...),),
$((:(args[$(i)]) for i in eachindex(args))...)
))
end
Expand Down

2 comments on commit b53abfe

@krrutkow
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/41173

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.5 -m "<description of version>" b53abfe9771388b26752c529ddbb61a9a62c826f
git push origin v1.0.5

Please sign in to comment.