JuliaInterop / Cxx.jl

The Julia C++ Interface
Other
757 stars 108 forks source link

Calling Julia Base.error() from C++ #433

Closed canaknesil closed 4 years ago

canaknesil commented 5 years ago

I am trying to call Julia Base.error method from C++. The example in README.md works fine:

julia> cxx"""
       void foo() {
         $:(println("Message.")::Nothing);
       }
       """
true

julia> @cxx foo()
Message.

But, when I replace println with error, I get this error:

julia> cxx"""
       void julia_error() {
           $:(error("This is the error.")::Nothing);
       }
       """
ERROR: Inferred Union or abstract type Union{} for return value of lambda
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] cpptype(::Cxx.CxxCore.ClangCompiler, ::Type{getfield(Main, Symbol("##5#6"))}) at /home/eakncan/.julia/packages/Cxx/vxYtJ/src/typetranslation.jl:302
 [3] ssv(::Cxx.CxxCore.ClangCompiler, ::Any, ::Cxx.CxxCore.CppPtr{Cxx.CxxCore.CxxQualType{Cxx.CxxCore.CppBaseType{Symbol("clang::DeclContext")},(false, false, false)},(false, false, false)}, ::Int64, ::Base.GenericIOBuffer{Array{UInt8,1}}, ::Dict{Nothing,Nothing}) at /home/eakncan/.julia/packages/Cxx/vxYtJ/src/cxxstr.jl:56
 [4] ssv(::Cxx.CxxCore.ClangCompiler, ::Any, ::Cxx.CxxCore.CppPtr{Cxx.CxxCore.CxxQualType{Cxx.CxxCore.CppBaseType{Symbol("clang::DeclContext")},(false, false, false)},(false, false, false)}, ::Int64, ::Base.GenericIOBuffer{Array{UInt8,1}}) at /home/eakncan/.julia/packages/Cxx/vxYtJ/src/cxxstr.jl:40
 [5] top-level scope at /home/eakncan/.julia/packages/Cxx/vxYtJ/src/cxxstr.jl:641
Gnimuc commented 5 years ago

Not sure why but directly ccall "jl_error" works:

julia> cxx"""
              void my_julia_error() {
                  $:(ccall("jl_error", Cvoid, (Cstring,), "This is the error."));
              }
              """
true

julia> @cxx my_julia_error()
ERROR: This is the error.
Stacktrace:
 [1] (::getfield(Main, Symbol("##7#8")))() at ./none:0
 [2] macro expansion at /Users/gnimuc/.julia/dev/Cxx/src/codegen.jl:841 [inlined]
 [3] cppcall(::Cxx.CxxCore.CxxInstance{1}, ::Cxx.CxxCore.CppNNS{Tuple{:my_julia_error}}) at /Users/gnimuc/.julia/dev/Cxx/src/codegen.jl:841
 [4] top-level scope at none:0
canaknesil commented 5 years ago

This worked for me. Thank you.