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

Using fieldnames (and similar) for wrapped C unions #426

Open
JoshuaLampert opened this issue Mar 25, 2023 · 2 comments
Open

Using fieldnames (and similar) for wrapped C unions #426

JoshuaLampert opened this issue Mar 25, 2023 · 2 comments
Labels
enhancement good first issue Good for newcomers help wanted Extra attention is needed

Comments

@JoshuaLampert
Copy link
Contributor

Currently, C unions and structs containing a union are wrapped as a julia struct having one field data with overwritten Base.getproperty and Base.setproperty! functions, e.g.

union testunion
{
    int y;
};

struct teststruct
{
  int x;
  union testunion p;
};

is wrapped as

using CEnum

struct testunion
    data::NTuple{4, UInt8}
end

function Base.getproperty(x::Ptr{testunion}, f::Symbol)
    f === :y && return Ptr{Cint}(x + 0)
    return getfield(x, f)
end

function Base.getproperty(x::testunion, f::Symbol)
    r = Ref{testunion}(x)
    ptr = Base.unsafe_convert(Ptr{testunion}, r)
    fptr = getproperty(ptr, f)
    GC.@preserve r unsafe_load(fptr)
end

function Base.setproperty!(x::Ptr{testunion}, f::Symbol, v)
    unsafe_store!(getproperty(x, f), v)
end

struct teststruct
    data::NTuple{8, UInt8}
end

function Base.getproperty(x::Ptr{teststruct}, f::Symbol)
    f === :x && return Ptr{Cint}(x + 0)
    f === :p && return Ptr{testunion}(x + 4)
    return getfield(x, f)
end

function Base.getproperty(x::teststruct, f::Symbol)
    r = Ref{teststruct}(x)
    ptr = Base.unsafe_convert(Ptr{teststruct}, r)
    fptr = getproperty(ptr, f)
    GC.@preserve r unsafe_load(fptr)
end

function Base.setproperty!(x::Ptr{teststruct}, f::Symbol, v)
    unsafe_store!(getproperty(x, f), v)
end

This doesn' t allow to properly use fieldnames or propertynames (and similar functions) as they return (:data,) (or similar) and not the actual fields you can access by getproperty. The problem came up in trixi-framework/P4est.jl#72.

cc @ranocha, @sloede

@Gnimuc
Copy link
Member

Gnimuc commented Mar 26, 2023

dup of #316?

@thomvet
Copy link

thomvet commented Dec 10, 2023

It isn't really the same issue? In #316 the autocomplete doesn't work for the Ptr{T} type, but it would work for struct T directly. In this issue, there is only the ":data" field in the struct. Way to the solution is probably quite different?

@Gnimuc Gnimuc added enhancement good first issue Good for newcomers help wanted Extra attention is needed labels Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants