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

Implement variadic function #315

Merged
merged 2 commits into from
Jul 25, 2021
Merged

Implement variadic function #315

merged 2 commits into from
Jul 25, 2021

Conversation

melonedo
Copy link
Contributor

@melonedo melonedo commented Jul 19, 2021

I have manually tested this implementation strategy with Julia's default printf, although this can not be done automatically.

fix #146 , fix #17

@Gnimuc
Copy link
Member

Gnimuc commented Jul 19, 2021

By Julia's default printf, you mean the @printf macro in Ptrinf or the printf in Libc?

@Gnimuc
Copy link
Member

Gnimuc commented Jul 19, 2021

CImGui.jl has some use cases for this feature, you could give it a test and submit a PR there. IIRC, LibCURL.jl also has some variadic functions, it might have some test cases as well.

https://github.com/JuliaImGui/LibCImGui.jl/blob/9af63ee69dc109000dabdb30ef86073d546738c3/src/LibCImGui.jl#L39-L90

@melonedo
Copy link
Contributor Author

By Julia's default printf, you mean the @printf macro in Ptrinf or the printf in Libc?

Substitute the $libname.$funcname part for printf or scanf, then the code works.

@generated function scanf(s, va_list...)
        va_list = map(enumerate(to_c_type.(va_list))) do (ind, type)
                :(va_list[$ind]::$type)
            end
        :(@ccall(scanf(s::Ptr{Cchar}; $(va_list...))::Cint))
    end
a = Ref(0)
scanf("%d", a)
# Enter 123
println(a[]) # => 123

@melonedo
Copy link
Contributor Author

CImGui.jl has some use cases for this feature, you could give it a test and submit a PR there.

Thanks! I'll check it out.

Copy link
Member

@Gnimuc Gnimuc left a comment

Choose a reason for hiding this comment

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

I think it's succinct to give users full control of the types of varargs, just like what they need to do when using @ccall. But to_c_type also has some use cases where users might want to hack into the @generated function and insert their own implementation of to_c_type.

@generated function scanf(s, args::Vararg{Any,N}) where {N}
        varargs = (:(args[$i]::$(args[i])) for i = 1:N)
        :(@ccall(scanf(s::Ptr{Cchar}; $(varargs...))::Cint))
end

src/generator/passes.jl Outdated Show resolved Hide resolved
src/generator/codegen.jl Outdated Show resolved Hide resolved
src/generator/passes.jl Outdated Show resolved Hide resolved
docs/src/generator.md Outdated Show resolved Hide resolved
docs/src/generator.md Outdated Show resolved Hide resolved
docs/src/generator.md Outdated Show resolved Hide resolved
docs/src/generator.md Outdated Show resolved Hide resolved
docs/src/generator.md Outdated Show resolved Hide resolved
@melonedo
Copy link
Contributor Author

CImGui.jl has some use cases for this feature, you could give it a test and submit a PR there. IIRC, LibCURL.jl also has some variadic functions, it might have some test cases as well.

https://github.com/JuliaImGui/LibCImGui.jl/blob/9af63ee69dc109000dabdb30ef86073d546738c3/src/LibCImGui.jl#L39-L90

Unfortunately both CimGui.jl and LibCimGui.jl have few tests, so I forked LibCURL.jl and tested on Downloads.jl, and it looks fine.

gen/generator.toml Outdated Show resolved Hide resolved
@Gnimuc
Copy link
Member

Gnimuc commented Jul 22, 2021

This is great! I would give it a battle test if I could find some time.

@Gnimuc
Copy link
Member

Gnimuc commented Jul 25, 2021

I'm going to merge this. @melonedo could you rebase the commit history to be a little bit cleaner? For example, one commit for the implementation and another one for docs.

@melonedo
Copy link
Contributor Author

Should I force-push the combined commits here?

@Gnimuc
Copy link
Member

Gnimuc commented Jul 25, 2021

Yup.

melonedo added 2 commits July 25, 2021 12:58
* Reorganize emit! for FunctionProto to extract common parts

Wrap code in generated function

Support no `@ccall` option

Move repeated va_list transformer code to prologue

Make variadic functions and to_c_type optional

Remove default type deduction for String and Array

Disable variadic functions by default
Update documentation
@Gnimuc Gnimuc merged commit d186b2d into JuliaInterop:master Jul 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants