Skip to content

Commit

Permalink
Use JuliaInterface conversion methods in LibGAP.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasguts committed Nov 13, 2018
1 parent 7fa8432 commit f073191
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
14 changes: 14 additions & 0 deletions LibGAP.jl/src/ccalls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 21 additions & 23 deletions LibGAP.jl/src/conversion.jl
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
1 change: 1 addition & 0 deletions LibGAP.jl/test/conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f073191

Please sign in to comment.