From f07319170bb7bfc637cc54f905ba8b57d371a8f5 Mon Sep 17 00:00:00 2001 From: Sebastian Gutsche Date: Thu, 8 Nov 2018 16:42:57 +0100 Subject: [PATCH] Use JuliaInterface conversion methods in LibGAP.jl --- LibGAP.jl/src/ccalls.jl | 14 ++++++++++++ LibGAP.jl/src/conversion.jl | 44 +++++++++++++++++------------------- LibGAP.jl/test/conversion.jl | 1 + 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/LibGAP.jl/src/ccalls.jl b/LibGAP.jl/src/ccalls.jl index b081fe8c..e2a609d2 100644 --- a/LibGAP.jl/src/ccalls.jl +++ b/LibGAP.jl/src/ccalls.jl @@ -85,6 +85,20 @@ function propertynames(funcobj::GlobalsType,private) return tuple(list_converted...) end +function to_gap(obj::Any) + ret_ptr = ccall(:_ConvertedFromJulia_internal,Ptr{Cvoid},(Any,),obj) + ret_val = GET_FROM_GAP(ret_ptr) + return ret_val +end + + +function from_gap(obj::MPtr,::Any) + ret_val = ccall(:_ConvertedToJulia_internal,Any,(MPtr,),obj) + return ret_val +end + + + # For backwards compatibility # TODO: remove this again GAPFuncs = Globals diff --git a/LibGAP.jl/src/conversion.jl b/LibGAP.jl/src/conversion.jl index 7be67843..b9396d73 100644 --- a/LibGAP.jl/src/conversion.jl +++ b/LibGAP.jl/src/conversion.jl @@ -1,32 +1,30 @@ -## Conversion to GAP -to_gap(v :: Int64) = v -to_gap(v :: MPtr) = v -to_gap(v :: GapFFE) = v -to_gap(v :: String) = MakeString(v) -to_gap(v :: Symbol) = to_gap(string(v)) -to_gap(v :: Bool) = v +to_gap(x::Int64) = x +to_gap(x::GapFFE) = x +to_gap(x::MPtr) = x -function to_gap(v :: Array{T, 1} where T) :: MPtr - l = NewPlist(length(v)) - for i in 1:length(v) - l[i] = to_gap(v[i]) +function to_gap(obj::Array{T,1}) where T + len = length(obj) + ret_val = NewPlist(len) + for i in 1:len + ret_val[i] = to_gap(obj[i]) end - return l + return ret_val end -## Conversion from GAP -## -## FIXME: these are not very user friendly (user has to know type of the GAP -## object in advanced), and some may even crash. It is not even clear that -## here is the right place to perform most of these conversions, maybe they -## should be done on the GAP and/or in the JuliaInterface GAP kernel -## extension instead. We need to work this out... +from_gap(obj::Int64) = obj +from_gap(obj::Int64,::Any) = obj +from_gap(obj::MPtr) = from_gap(obj,Any) +from_gap(obj::MPtr,::Type{Symbol}) = Symbol(from_gap(obj)) - -from_gap(obj , ::Any) = obj -from_gap(obj :: MPtr, ::Type{AbstractString}) = CSTR_STRING(obj) -from_gap(obj :: MPtr, ::Type{Symbol}) = Symbol(from_gap(obj,AbstractString)) +function from_gap( obj :: MPtr, ::Type{Array{Any,1}} ) + len_list = length(obj) + new_array = Array{Any,1}( undef, len_list) + for i in 1:len_list + new_array[ i ] = obj[i] + end + return new_array +end function from_gap( obj :: MPtr, ::Type{Array{T,1}} ) where T len_list = length(obj) diff --git a/LibGAP.jl/test/conversion.jl b/LibGAP.jl/test/conversion.jl index 33490c08..02378ea4 100644 --- a/LibGAP.jl/test/conversion.jl +++ b/LibGAP.jl/test/conversion.jl @@ -19,6 +19,7 @@ end @testset "conversion from GAP" begin @test GAP.from_gap(1,Any) == 1 + @test GAP.from_gap(1) == 1 x = GAP.to_gap("foo") @test GAP.to_gap(x) == x