@@ -11,32 +11,41 @@ ccall_test_func(x) = ccall((:testUcharX, "./libccalltest"), Int32, (UInt8,), x %
11
11
12
12
# Test for proper round-trip of Ref{T} type
13
13
ccall_echo_func {T,U} (x, :: Type{T} , :: Type{U} ) = ccall ((:test_echo_p , " ./libccalltest" ), T, (U,), x)
14
+ # Make sure object x is still valid (rooted as argument)
15
+ # when loading the pointer. This works as long as we still keep the argument
16
+ # rooted but might fail if we are smarter about eliminating dead root.
17
+ ccall_echo_load {T,U} (x, :: Type{T} , :: Type{U} ) =
18
+ unsafe_load (ccall_echo_func (x, T, U))
19
+ ccall_echo_objref {T,U} (x, :: Type{T} , :: Type{U} ) =
20
+ unsafe_pointer_to_objref (ccall_echo_func (x, Ptr{T}, U))
14
21
type IntLike
15
22
x:: Int
16
23
end
17
- @test unsafe_load ( ccall_echo_func ( 132 , Ptr{Int}, Ref{Int}) ) === 132
18
- @test unsafe_load ( ccall_echo_func ( Ref (921 ), Ptr{Int}, Ref{Int}) ) === 921
19
- @test unsafe_load ( ccall_echo_func ( IntLike (993 ), Ptr{Int}, Ref{IntLike}) ) === 993
20
- @test unsafe_load ( ccall_echo_func ( IntLike (881 ), Ptr{IntLike}, Ref{IntLike}) ). x === 881
24
+ @test ccall_echo_load ( 132 , Ptr{Int}, Ref{Int}) === 132
25
+ @test ccall_echo_load ( Ref (921 ), Ptr{Int}, Ref{Int}) === 921
26
+ @test ccall_echo_load ( IntLike (993 ), Ptr{Int}, Ref{IntLike}) === 993
27
+ @test ccall_echo_load ( IntLike (881 ), Ptr{IntLike}, Ref{IntLike}). x === 881
21
28
@test ccall_echo_func (532 , Int, Int) === 532
22
29
if WORD_SIZE == 64
23
30
# this test is valid only for x86_64 and win64
24
31
@test ccall_echo_func (164 , IntLike, Int). x === 164
25
32
end
26
33
@test ccall_echo_func (IntLike (828 ), Int, IntLike) === 828
27
34
@test ccall_echo_func (913 , Any, Any) === 913
28
- @test unsafe_pointer_to_objref ( ccall_echo_func ( 553 , Ptr{Any}, Any) ) === 553
35
+ @test ccall_echo_objref ( 553 , Ptr{Any}, Any) === 553
29
36
@test ccall_echo_func (124 , Ref{Int}, Any) === 124
30
- @test unsafe_load ( ccall_echo_func ( 422 , Ptr{Any}, Ref{Any}) ) === 422
31
- @test unsafe_load ( ccall_echo_func ( [383 ], Ptr{Int}, Ref{Int}) ) === 383
32
- @test unsafe_load ( ccall_echo_func ( Ref ([144 ,172 ],2 ), Ptr{Int}, Ref{Int}) ) === 172
33
- # @test unsafe_load(ccall_echo_func( Ref([8],1,1), Ptr{Int}, Ref{Int}) ) === 8
37
+ @test ccall_echo_load ( 422 , Ptr{Any}, Ref{Any}) === 422
38
+ @test ccall_echo_load ( [383 ], Ptr{Int}, Ref{Int}) === 383
39
+ @test ccall_echo_load ( Ref ([144 ,172 ],2 ), Ptr{Int}, Ref{Int}) === 172
40
+ # @test ccall_echo_load( Ref([8],1,1), Ptr{Int}, Ref{Int}) === 8
34
41
35
42
# Tests for passing and returning structs
36
43
ci = 20 + 51im
37
44
b = ccall ((:ctest , " ./libccalltest" ), Complex{Int}, (Complex{Int},), ci)
38
45
@test b == ci + 1 - 2im
39
- b = unsafe_load (ccall ((:cptest , " ./libccalltest" ), Ptr{Complex{Int}}, (Ptr{Complex{Int}},), [ci]))
46
+ ci_ary = [ci] # Make sure the array is alive during unsafe_load
47
+ b = unsafe_load (ccall ((:cptest , " ./libccalltest" ), Ptr{Complex{Int}},
48
+ (Ptr{Complex{Int}},), ci_ary))
40
49
@test b == ci + 1 - 2im
41
50
@test ci == 20 + 51im
42
51
@@ -47,14 +56,16 @@ Libc.free(convert(Ptr{Void},b))
47
56
cf64 = 2.84 + 5.2im
48
57
b = ccall ((:cgtest , " ./libccalltest" ), Complex128, (Complex128,), cf64)
49
58
@test b == cf64 + 1 - 2im
50
- b = unsafe_load (ccall ((:cgptest , " ./libccalltest" ), Ptr{Complex128}, (Ptr{Complex128},), [cf64]))
59
+ cf64_ary = [cf64] # Make sure the array is alive during unsafe_load
60
+ b = unsafe_load (ccall ((:cgptest , " ./libccalltest" ), Ptr{Complex128}, (Ptr{Complex128},), cf64_ary))
51
61
@test b == cf64 + 1 - 2im
52
62
@test cf64 == 2.84 + 5.2im
53
63
54
64
cf32 = 3.34f0 + 53.2f0im
55
65
b = ccall ((:cftest , " ./libccalltest" ), Complex64, (Complex64,), cf32)
56
66
@test b == cf32 + 1 - 2im
57
- b = unsafe_load (ccall ((:cfptest , " ./libccalltest" ), Ptr{Complex64}, (Ptr{Complex64},), [cf32]))
67
+ cf32_ary = [cf32] # Make sure the array is alive during unsafe_load
68
+ b = unsafe_load (ccall ((:cfptest , " ./libccalltest" ), Ptr{Complex64}, (Ptr{Complex64},), cf32_ary))
58
69
@test b == cf32 + 1 - 2im
59
70
@test cf32 == 3.34f0 + 53.2f0im
60
71
0 commit comments