forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccall.jl
1968 lines (1716 loc) · 65.4 KB
/
ccall.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is a part of Julia. License is MIT: https://julialang.org/license
import Base.copy, Base.==
using Random
using InteractiveUtils: code_llvm
import Libdl
# for cfunction_closure
include("testenv.jl")
# for cfunction error
isdefined(Main, :MacroCalls) || @eval Main include("testhelpers/MacroCalls.jl")
using Main.MacroCalls
const libccalltest = "libccalltest"
const verbose = false
ccall((:set_verbose, libccalltest), Cvoid, (Int32,), verbose)
@eval function cvarargs()
strp = Ref{Ptr{Cchar}}(0)
fmt = "%3.1f"
len = ccall(:asprintf, Cint, (Ptr{Ptr{Cchar}}, Cstring, Cfloat...), strp, fmt, 0.1)
str = unsafe_string(strp[], len)
Libc.free(strp[])
return str
end
@test cvarargs() == "0.1"
# test multiple-type vararg handling (there's no syntax for this currently)
@eval function foreign_varargs()
strp = Ref{Ptr{Cchar}}(0)
fmt = "hi+%hhd-%hhd-%hhd-%hhd-%hhd-%hhd-%hhd-%hhd-%hhd-%hhd-%hhd-%hhd-%hhd-%hhd-%hhd-%.1f-%.1f-%.1f-%.1f-%.1f-%.1f-%.1f-%.1f-%.1f\n"
len = $(Expr(:foreigncall, :(:asprintf), Cint,
Core.svec(Ptr{Ptr{Cchar}}, Cstring,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
Cfloat, Cfloat, Cfloat, Cfloat, Cfloat, Cfloat, Cfloat, Cfloat, Cfloat),
2, :(:cdecl),
:(Base.unsafe_convert(Ptr{Ptr{Cchar}}, strp)), :(Base.unsafe_convert(Cstring, fmt)),
0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
Cfloat(1.1), Cfloat(2.2), Cfloat(3.3), Cfloat(4.4), Cfloat(5.5), Cfloat(6.6), Cfloat(7.7), Cfloat(8.8), Cfloat(9.9),
:strp, :fmt))
str = unsafe_string(strp[], len)
Libc.free(strp[])
return str
end
@test foreign_varargs() == "hi+1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-1.1-2.2-3.3-4.4-5.5-6.6-7.7-8.8-9.9\n"
# Test for proper argument register truncation
ccall_test_func(x) = ccall((:testUcharX, libccalltest), Int32, (UInt8,), x % UInt8)
@test ccall_test_func(3) == 1
@test ccall_test_func(259) == 1
# Test for proper round-trip of Ref{T} type
function gen_ccall_echo(x, T, U, ret=nothing)
# Construct a noninline function to do all the work, this is necessary
# to make sure object x is still valid (rooted as argument)
# when loading the pointer.
# This works as long as we still keep the argument
# rooted but might fail if we are smarter about eliminating dead root.
# `eval` in global scope to make sure the function is not a closure
func_ex = :(ccall((:test_echo_p, libccalltest), $T, ($U,), x))
# It is not allowed to allocate after the ccall returns
# and before calling `ret`.
if ret !== nothing
func_ex = :($ret($func_ex))
end
@gensym func_name
@eval @noinline $func_name(x) = $func_ex
:($func_name($(esc(x))))
end
macro ccall_echo_func(x, T, U)
gen_ccall_echo(x, T, U)
end
macro ccall_echo_load(x, T, U)
gen_ccall_echo(x, T, U, :unsafe_load)
end
macro ccall_echo_objref(x, T, U)
gen_ccall_echo(x, :(Ptr{$T}), U, :unsafe_pointer_to_objref)
end
mutable struct IntLike
x::Int
end
@test @ccall_echo_load(132, Ptr{Int}, Ref{Int}) === 132
@test @ccall_echo_load(Ref(921), Ptr{Int}, Ref{Int}) === 921
@test @ccall_echo_load(IntLike(993), Ptr{Int}, Ref{IntLike}) === 993
@test @ccall_echo_load(IntLike(881), Ptr{IntLike}, Ref{IntLike}).x === 881
@test @ccall_echo_func(532, Int, Int) === 532
if Sys.WORD_SIZE == 64
# this test is valid only for x86_64 and win64
@test @ccall_echo_func(164, IntLike, Int).x === 164
end
@test @ccall_echo_func(IntLike(828), Int, IntLike) === 828
@test @ccall_echo_func(913, Any, Any) === 913
@test @ccall_echo_objref(553, Ptr{Any}, Any) === 553
@test @ccall_echo_func(124, Ref{Int}, Any) === 124
@test @ccall_echo_load(422, Ptr{Any}, Ref{Any}) === 422
@test @ccall_echo_load([383], Ptr{Int}, Ref{Int}) === 383
@test @ccall_echo_load(Ref([144,172],2), Ptr{Int}, Ref{Int}) === 172
# @test @ccall_echo_load(Ref([8],1,1), Ptr{Int}, Ref{Int}) === 8
## Tests for passing and returning structs
let a, ci_ary, x
a = 20 + 51im
x = ccall((:ctest, libccalltest), Complex{Int}, (Complex{Int},), a)
@test x == a + 1 - 2im
ci_ary = [a] # Make sure the array is alive during unsafe_load
x = unsafe_load(ccall((:cptest, libccalltest), Ptr{Complex{Int}},
(Ptr{Complex{Int}},), ci_ary))
@test x == a + 1 - 2im
@test a == 20 + 51im
x = ccall((:cptest_static, libccalltest), Ptr{Complex{Int}}, (Ref{Complex{Int}},), a)
@test unsafe_load(x) == a
Libc.free(convert(Ptr{Cvoid}, x))
end
let a, b, x
a = 2.84 + 5.2im
x = ccall((:cgtest, libccalltest), ComplexF64, (ComplexF64,), a)
@test x == a + 1 - 2im
b = [a] # Make sure the array is alive during unsafe_load
x = unsafe_load(ccall((:cgptest, libccalltest), Ptr{ComplexF64}, (Ptr{ComplexF64},), b))
@test x == a + 1 - 2im
@test a == 2.84 + 5.2im
end
let a, b, x
a = 3.34f0 + 53.2f0im
x = ccall((:cftest, libccalltest), ComplexF32, (ComplexF32,), a)
@test x == a + 1 - 2im
b = [a] # Make sure the array is alive during unsafe_load
x = unsafe_load(ccall((:cfptest, libccalltest), Ptr{ComplexF32}, (Ptr{ComplexF32},), b))
@test x == a + 1 - 2im
@test a == 3.34f0 + 53.2f0im
end
## Tests for native Julia data types
let a
a = 2.84 + 5.2im
@test_throws MethodError ccall((:cptest, libccalltest), Ptr{Complex{Int}}, (Ptr{Complex{Int}},), a)
end
## Tests for various sized data types (ByVal)
mutable struct Struct1
x::Float32
y::Float64
end
struct Struct1I
x::Float32
y::Float64
end
copy(a::Struct1) = Struct1(a.x, a.y)
copy(a::Struct1I) = a
function test_struct1(::Type{Struct}) where {Struct}
a = Struct(352.39422f23, 19.287577)
b = Float32(123.456)
a2 = copy(a)
if Struct === Struct1
x = ccall((:test_1, libccalltest), Struct1, (Struct1, Float32), a2, b)
else
x = ccall((:test_1, libccalltest), Struct1I, (Struct1I, Float32), a2, b)
end
@test a2.x == a.x && a2.y == a.y
@test !(a2 === x)
@test x.x ≈ a.x + 1*b
@test x.y ≈ a.y - 2*b
end
test_struct1(Struct1)
test_struct1(Struct1I)
let a, b, x
a = Struct1(352.39422f23, 19.287577)
b = Float32(123.456)
a2 = copy(a)
x = ccall((:test_1long_a, libccalltest), Struct1, (Int, Int, Int, Struct1, Float32), 2, 3, 4, a2, b)
@test a2.x == a.x && a2.y == a.y
@test !(a2 === x)
@test x.x ≈ a.x + b + 9
@test x.y ≈ a.y - 2*b
x = ccall((:test_1long_b, libccalltest), Struct1, (Int, Float64, Int, Struct1, Float32), 2, 3, 4, a2, b)
@test a2.x == a.x && a2.y == a.y
@test !(a2 === x)
@test x.x ≈ a.x + b + 9
@test x.y ≈ a.y - 2*b
x = ccall((:test_1long_c, libccalltest), Struct1, (Int, Float64, Int, Int, Struct1, Float32), 2, 3, 4, 5, a2, b)
@test a2.x == a.x && a2.y == a.y
@test !(a2 === x)
@test x.x ≈ a.x + b + 14
@test x.y ≈ a.y - 2*b
end
let a, b, x, y
a = Complex{Int32}(Int32(10),Int32(31))
b = Int32(42)
x = ccall((:test_2a, libccalltest), Complex{Int32}, (Complex{Int32}, Int32), a, b)
y = ccall((:test_2b, libccalltest), Complex{Int32}, (Complex{Int32},Int32), a, b)
@test a == Complex{Int32}(Int32(10),Int32(31))
@test x == y
@test x == a + b*1 - b*2im
end
let a, b, x, y, z
a = Complex{Int64}(Int64(20),Int64(51))
b = Int64(42)
x = ccall((:test_3a, libccalltest), Complex{Int64}, (Complex{Int64}, Int64), a, b)
y = ccall((:test_3b, libccalltest), Complex{Int64}, (Complex{Int64}, Int64), a, b)
z = ccall((:test_128, libccalltest), Complex{Int64}, (Complex{Int64}, Int64), a, b)
@test a == Complex{Int64}(Int64(20),Int64(51))
@test x == y
@test x == a + b*1 - b*2im
@test z == a + 1*b
end
mutable struct Struct4
x::Int32
y::Int32
z::Int32
end
struct Struct4I
x::Int32
y::Int32
z::Int32
end
function test_struct4(::Type{Struct}) where {Struct}
a = Struct(-512275808,882558299,-2133022131)
b = Int32(42)
if Struct === Struct4
x = ccall((:test_4, libccalltest), Struct4, (Struct4, Int32), a, b)
else
x = ccall((:test_4, libccalltest), Struct4I, (Struct4I, Int32), a, b)
end
@test x.x == a.x+b*1
@test x.y == a.y-b*2
@test x.z == a.z+b*3
end
test_struct4(Struct4)
test_struct4(Struct4I)
mutable struct Struct5
x::Int32
y::Int32
z::Int32
a::Int32
end
struct Struct5I
x::Int32
y::Int32
z::Int32
a::Int32
end
function test_struct5(::Type{Struct}) where {Struct}
a = Struct(1771319039, 406394736, -1269509787, -745020976)
b = Int32(42)
if Struct === Struct5
x = ccall((:test_5, libccalltest), Struct5, (Struct5, Int32), a, b)
else
x = ccall((:test_5, libccalltest), Struct5I, (Struct5I, Int32), a, b)
end
@test x.x == a.x+b*1
@test x.y == a.y-b*2
@test x.z == a.z+b*3
@test x.a == a.a-b*4
end
test_struct5(Struct5)
test_struct5(Struct5I)
mutable struct Struct6
x::Int64
y::Int64
z::Int64
end
struct Struct6I
x::Int64
y::Int64
z::Int64
end
function test_struct6(::Type{Struct}) where {Struct}
a = Struct(-654017936452753226, -5573248801240918230, -983717165097205098)
b = Int64(42)
if Struct === Struct6
x = ccall((:test_6, libccalltest), Struct6, (Struct6, Int64), a, b)
else
x = ccall((:test_6, libccalltest), Struct6I, (Struct6I, Int64), a, b)
end
@test x.x == a.x+b*1
@test x.y == a.y-b*2
@test x.z == a.z+b*3
end
test_struct6(Struct6)
test_struct6(Struct6I)
mutable struct Struct7
x::Int64
y::Cchar
end
struct Struct7I
x::Int64
y::Cchar
end
function test_struct7(::Type{Struct}) where {Struct}
a = Struct(-384082741977533896, 'h')
b = Int8(42)
if Struct === Struct7
x = ccall((:test_7, libccalltest), Struct7, (Struct7, Int8), a, b)
else
x = ccall((:test_7, libccalltest), Struct7I, (Struct7I, Int8), a, b)
end
@test x.x == a.x+Int(b)*1
@test x.y == a.y-Int(b)*2
end
test_struct7(Struct7)
test_struct7(Struct7I)
mutable struct Struct8
x::Int32
y::Cchar
end
struct Struct8I
x::Int32
y::Cchar
end
function test_struct8(::Type{Struct}) where {Struct}
a = Struct(-384082896, 'h')
b = Int8(42)
if Struct === Struct8
r8 = ccall((:test_8, libccalltest), Struct8, (Struct8, Int8), a, b)
else
r8 = ccall((:test_8, libccalltest), Struct8I, (Struct8I, Int8), a, b)
end
@test r8.x == a.x+b*1
@test r8.y == a.y-b*2
end
test_struct8(Struct8)
test_struct8(Struct8I)
mutable struct Struct9
x::Int32
y::Int16
end
struct Struct9I
x::Int32
y::Int16
end
function test_struct9(::Type{Struct}) where {Struct}
a = Struct(-394092996, -3840)
b = Int16(42)
if Struct === Struct9
x = ccall((:test_9, libccalltest), Struct9, (Struct9, Int16), a, b)
else
x = ccall((:test_9, libccalltest), Struct9I, (Struct9I, Int16), a, b)
end
@test x.x == a.x+b*1
@test x.y == a.y-b*2
end
test_struct9(Struct9)
test_struct9(Struct9I)
mutable struct Struct10
x::Cchar
y::Cchar
z::Cchar
a::Cchar
end
struct Struct10I
x::Cchar
y::Cchar
z::Cchar
a::Cchar
end
function test_struct10(::Type{Struct}) where {Struct}
a = Struct('0', '1', '2', '3')
b = Int8(2)
if Struct === Struct10
x = ccall((:test_10, libccalltest), Struct10, (Struct10, Int8), a, b)
else
x = ccall((:test_10, libccalltest), Struct10I, (Struct10I, Int8), a, b)
end
@test x.x == a.x+b*1
@test x.y == a.y-b*2
@test x.z == a.z+b*3
@test x.a == a.a-b*4
end
test_struct10(Struct10)
test_struct10(Struct10I)
mutable struct Struct11
x::ComplexF32
end
struct Struct11I
x::ComplexF32
end
function test_struct11(::Type{Struct}) where {Struct}
a = Struct(0.8877077f0 + 0.4591081f0im)
b = Float32(42)
if Struct === Struct11
x = ccall((:test_11, libccalltest), Struct11, (Struct11, Float32), a, b)
else
x = ccall((:test_11, libccalltest), Struct11I, (Struct11I, Float32), a, b)
end
@test x.x ≈ a.x + b*1 - b*2im
end
test_struct11(Struct11)
test_struct11(Struct11I)
mutable struct Struct12
x::ComplexF32
y::ComplexF32
end
struct Struct12I
x::ComplexF32
y::ComplexF32
end
function test_struct12(::Type{Struct}) where {Struct}
a = Struct(0.8877077f5 + 0.4591081f2im, 0.0004842868f0 - 6982.3265f3im)
b = Float32(42)
if Struct === Struct12
x = ccall((:test_12, libccalltest), Struct12, (Struct12, Float32), a, b)
else
x = ccall((:test_12, libccalltest), Struct12I, (Struct12I, Float32), a, b)
end
@test x.x ≈ a.x + b*1 - b*2im
@test x.y ≈ a.y + b*3 - b*4im
end
test_struct12(Struct12)
test_struct12(Struct12I)
mutable struct Struct13
x::ComplexF64
end
struct Struct13I
x::ComplexF64
end
function test_struct13(::Type{Struct}) where {Struct}
a = Struct(42968.97560380495 - 803.0576845153616im)
b = Float64(42)
if Struct === Struct13
x = ccall((:test_13, libccalltest), Struct13, (Struct13, Float64), a, b)
else
x = ccall((:test_13, libccalltest), Struct13I, (Struct13I, Float64), a, b)
end
@test x.x ≈ a.x + b*1 - b*2im
end
test_struct13(Struct13)
test_struct13(Struct13I)
mutable struct Struct14
x::Float32
y::Float32
end
struct Struct14I
x::Float32
y::Float32
end
function test_struct14(::Type{Struct}) where {Struct}
a = Struct(0.024138331f0, 0.89759064f32)
b = Float32(42)
if Struct === Struct14
x = ccall((:test_14, libccalltest), Struct14, (Struct14, Float32), a, b)
else
x = ccall((:test_14, libccalltest), Struct14I, (Struct14I, Float32), a, b)
end
@test x.x ≈ a.x + b*1
@test x.y ≈ a.y - b*2
end
test_struct14(Struct14)
test_struct14(Struct14I)
mutable struct Struct15
x::Float64
y::Float64
end
struct Struct15I
x::Float64
y::Float64
end
function test_struct15(::Type{Struct}) where {Struct}
a = Struct(4.180997967273657, -0.404218594294923)
b = Float64(42)
if Struct === Struct15
x = ccall((:test_15, libccalltest), Struct15, (Struct15, Float64), a, b)
else
x = ccall((:test_15, libccalltest), Struct15I, (Struct15I, Float64), a, b)
end
@test x.x ≈ a.x + b*1
@test x.y ≈ a.y - b*2
end
test_struct15(Struct15)
test_struct15(Struct15I)
mutable struct Struct16
x::Float32
y::Float32
z::Float32
a::Float64
b::Float64
c::Float64
end
struct Struct16I
x::Float32
y::Float32
z::Float32
a::Float64
b::Float64
c::Float64
end
function test_struct16(::Type{Struct}, quoteplz = false) where {Struct}
a = Struct(0.1604656f0, 0.6297606f0, 0.83588994f0,
0.6460273620993535, 0.9472692581106656, 0.47328535437352093)
b = Float32(42)
if Struct === Struct16
x = ccall((:test_16, libccalltest), Struct16, (Struct16, Float32), a, b)
else
if quoteplz
x = eval(:(ccall((:test_16, libccalltest), Struct16I, (Struct16I, Float32), $(QuoteNode(a)), Float32(42))))
else
x = ccall((:test_16, libccalltest), Struct16I, (Struct16I, Float32), a, b)
end
end
@test x.x ≈ a.x + b*1
@test x.y ≈ a.y - b*2
@test x.z ≈ a.z + b*3
@test x.a ≈ a.a - b*4
@test x.b ≈ a.b + b*5
@test x.c ≈ a.c - b*6
end
test_struct16(Struct16, false)
test_struct16(Struct16I, false)
test_struct16(Struct16I, true)
mutable struct Struct17
a::Int8
b::Int16
end
struct Struct17I
a::Int8
b::Int16
end
function test_struct17(::Type{Struct}) where {Struct}
a = Struct(2, 10)
b = Int8(2)
if Struct === Struct17
x = ccall((:test_17, libccalltest), Struct17, (Struct17, Int8), a, b)
else
x = ccall((:test_17, libccalltest), Struct17I, (Struct17I, Int8), a, b)
end
@test x.a == a.a + b * 1
@test x.b == a.b - b * 2
end
test_struct17(Struct17)
test_struct17(Struct17I)
mutable struct Struct18
a::Int8
b::Int8
c::Int8
end
struct Struct18I
a::Int8
b::Int8
c::Int8
end
function test_struct18(::Type{Struct}) where {Struct}
a = Struct(2, 10, -3)
b = Int8(2)
if Struct === Struct18
x = ccall((:test_18, libccalltest), Struct18, (Struct18, Int8), a, b)
else
x = ccall((:test_18, libccalltest), Struct18I, (Struct18I, Int8), a, b)
end
@test x.a == a.a + b * 1
@test x.b == a.b - b * 2
@test x.c == a.c + b * 3
end
test_struct18(Struct18)
test_struct18(Struct18I)
let a, b, x
a = Int128(0x7f00123456789abc)<<64 + typemax(UInt64)
b = Int64(1)
x = ccall((:test_128, libccalltest), Int128, (Int128, Int64), a, b)
@test x == a + b*1
@test a == Int128(0x7f00123456789abc)<<64 + typemax(UInt64)
end
mutable struct Struct_Big
x::Int
y::Int
z::Int8
end
struct Struct_BigI
x::Int
y::Int
z::Int8
end
copy(a::Struct_Big) = Struct_Big(a.x, a.y, a.z)
copy(a::Struct_BigI) = a
function test_struct_big(::Type{Struct}) where {Struct}
a = Struct(424,-5,Int8('Z'))
a2 = copy(a)
if Struct == Struct_Big
x = ccall((:test_big, libccalltest), Struct_Big, (Struct_Big,), a2)
else
x = ccall((:test_big, libccalltest), Struct_BigI, (Struct_BigI,), a2)
end
@test a2.x == a.x && a2.y == a.y && a2.z == a.z
@test x.x == a.x + 1
@test x.y == a.y - 2
@test x.z == a.z - Int('A')
end
test_struct_big(Struct_Big)
test_struct_big(Struct_BigI)
let a, a2, x
a = Struct_Big(424,-5,Int8('Z'))
a2 = copy(a)
x = ccall((:test_big_long, libccalltest), Struct_Big, (Int, Int, Int, Struct_Big,), 2, 3, 4, a2)
@test a2.x == a.x && a2.y == a.y && a2.z == a.z
@test x.x == a.x + 10
@test x.y == a.y - 2
@test x.z == a.z - Int('A')
end
const Struct_huge1a = NTuple{8, Int64}
const Struct_huge1b = NTuple{9, Int64}
const Struct_huge2a = NTuple{8, Cdouble}
const Struct_huge2b = NTuple{9, Cdouble}
mutable struct Struct_huge3a
cf::NTuple{3, Complex{Cfloat}}
f7::Cfloat
f8::Cfloat
end
mutable struct Struct_huge3b
cf::NTuple{7, Complex{Cfloat}}
r8a::Cfloat
r8b::Cfloat
end
mutable struct Struct_huge3c
cf::NTuple{7, Complex{Cfloat}}
r8a::Cfloat
r8b::Cfloat
r9::Cfloat
end
mutable struct Struct_huge4a
r12::Complex{Cdouble}
r34::Complex{Cdouble}
r5::Complex{Cfloat}
r67::Complex{Cdouble}
r8::Cdouble
end
mutable struct Struct_huge4b
r12::Complex{Cdouble}
r34::Complex{Cdouble}
r5::Complex{Cfloat}
r67::Complex{Cdouble}
r89::Complex{Cdouble}
end
const Struct_huge5a = NTuple{8, Complex{Cint}}
const Struct_huge5b = NTuple{9, Complex{Cint}}
function verify_huge(init, a, b)
@test typeof(init) === typeof(a) === typeof(b)
verbose && @show (a, b)
# make sure a was unmodified
for i = 1:nfields(a)
@test getfield(init, i) === getfield(a, i)
end
# make sure b was modified as expected
a1, b1 = getfield(a, 1), getfield(b, 1)
while isa(a1, Tuple)
@test a1[2:end] === b1[2:end]
a1 = a1[1]
b1 = b1[1]
end
if isa(a1, VecElement)
a1 = a1.value
b1 = b1.value
end
@test oftype(a1, a1 * 39) === b1
for i = 2:nfields(a)
@test getfield(a, i) === getfield(b, i)
end
end
macro test_huge(i, b, init)
f = QuoteNode(Symbol("test_huge", i, b))
ty = Symbol("Struct_huge", i, b)
return quote
let a = $ty($(esc(init))...), f
f(b) = ccall(($f, libccalltest), $ty, (Cchar, $ty, Cchar), '0' + $i, a, $b[1])
#code_llvm(f, typeof((a,)))
verify_huge($ty($(esc(init))...), a, f(a))
end
end
end
@test_huge 1 'a' ((1, 2, 3, 4, 5, 6, 7, 8),)
@test_huge 1 'b' ((1, 2, 3, 4, 5, 6, 7, 8, 9),)
@test_huge 2 'a' ((1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0),)
@test_huge 2 'b' ((1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0),)
@test_huge 3 'a' ((1.0 + 2.0im, 3.0 + 4.0im, 5.0 + 6.0im), 7.0, 8.0)
@test_huge 3 'b' ((1.0 + 2.0im, 3.0 + 4.0im, 5.0 + 6.0im, 7.0 + 8.0im, 9.0 + 10.0im, 11.0 + 12.0im, 13.0 + 14.0im), 7.0, 8.0)
@test_huge 3 'c' ((1.0 + 2.0im, 3.0 + 4.0im, 5.0 + 6.0im, 7.0 + 8.0im, 9.0 + 10.0im, 11.0 + 12.0im, 13.0 + 14.0im), 7.0, 8.0, 9.0)
@test_huge 4 'a' (1.0 + 2.0im, 3.0 + 4.0im, 5.0f0 + 6.0f0im, 7.0 + 8.0im, 9.0)
@test_huge 4 'b' (1.0 + 2.0im, 3.0 + 4.0im, 5.0f0 + 6.0f0im, 7.0 + 8.0im, 9.0 + 10.0im)
@test_huge 5 'a' ((1 + 2im, 3 + 4im, 5 + 6im, 7 + 8im, 9 + 10im, 11 + 12im, 13 + 14im, 15 + 16im),)
@test_huge 5 'b' ((1 + 2im, 3 + 4im, 5 + 6im, 7 + 8im, 9 + 10im, 11 + 12im, 13 + 14im, 15 + 16im, 17 + 17im),)
## cfunction roundtrip
verbose && Libc.flush_cstdio()
if cfunction_closure
verbose && println("Testing cfunction closures: ")
# helper Type for testing that constructors work
# with cfunction and that object identity is preserved
mutable struct IdentityTestKV{K, V}
(T::Type{<:IdentityTestKV})(S) = (@test T === S; T)
end
@noinline function testclosure(f, a::T, permanent::Bool=false, tt::Type{S}=Any) where {T, S}
@nospecialize(f, a, tt)
# generic API 1
cf = @cfunction $f Ref{T} (Ref{T},)
GC.gc()
@test cf.ptr != C_NULL
@test cf.f === f
@test (cf._1 == C_NULL) == permanent
@test (cf._2 == C_NULL) == permanent
@assert cf === Base.cconvert(Ptr{Cvoid}, cf)
GC.@preserve cf begin
fptr = Base.unsafe_convert(Ptr{Cvoid}, cf)
b = ccall(fptr, Ref{T}, (Ref{T},), a)
end
# generic API 2
cf2 = @cfunction $f Any (Ref{S},)
GC.gc()
@test cf2.ptr != C_NULL
@test cf2.f === f
@test (cf2._1 == C_NULL) == permanent
@test (cf2._2 == C_NULL) == permanent
@assert cf2 === Base.cconvert(Ptr{Cvoid}, cf2)
GC.@preserve cf2 begin
fptr = Base.unsafe_convert(Ptr{Cvoid}, cf2)
b = ccall(fptr, Any, (Ref{S},), a)
end
return b
end
# We can't (currently) execute some of these signatures (without compile-all),
# but we can at least look at some of the generated code
function check_code_trampoline(f, t, n::Int)
@nospecialize(f, t)
@test Base.return_types(f, t) == Any[Any]
llvm = sprint(code_llvm, f, t)
@test count(Returns(true), eachmatch(r"@jl_get_cfunction_trampoline\(", llvm)) == n
end
check_code_trampoline(testclosure, (Any, Any, Bool, Type), 2)
check_code_trampoline(testclosure, (Any, Int, Bool, Type{Int}), 2)
check_code_trampoline(testclosure, (Any, String, Bool, Type{String}), 2)
check_code_trampoline(testclosure, (typeof(identity), Any, Bool, Type), 2)
check_code_trampoline(testclosure, (typeof(identity), Int, Bool, Type{Int}), 0)
check_code_trampoline(testclosure, (typeof(identity), String, Bool, Type{String}), 0)
function g(i)
x = -332210 + i
y = "foo"
a(z) = x
b(z) = y
c(z) = (y = z)
IdentityTestVK{V, K} = IdentityTestKV{K, V}
@test IdentityTestVK !== IdentityTestKV
@test IdentityTestVK == IdentityTestKV
for _ = 1:5
@test testclosure(a, 23) == -332210 + i
@test testclosure(b, "bar") == "foo"
@test testclosure(c, "bar") == "bar"
@test testclosure(b, "foo") == "bar"
@test testclosure(c, "foo") == "foo"
@test testclosure(identity, IdentityTestKV, true) === IdentityTestKV
@test testclosure(identity, IdentityTestVK, true) === IdentityTestVK
@test testclosure(IdentityTestKV, IdentityTestKV, true) === IdentityTestKV
@test testclosure(IdentityTestVK, IdentityTestVK, false) === IdentityTestVK
end
end
g(1)
g(2)
g(3)
verbose && println("Testing cfunction roundtrip: ")
cf64 = 2.84+5.2im
cf32 = 3.34f0+53.2f0im
ci32 = Complex{Int32}(Int32(10), Int32(31))
ci64 = Complex{Int64}(Int64(20), Int64(51))
s1 = Struct1(352.39422f23, 19.287577)
==(a::Struct1, b::Struct1) = (a.x == b.x && a.y == b.y)
for (t, v) in ((Complex{Int32}, :ci32), (Complex{Int64}, :ci64),
(ComplexF32, :cf32), (ComplexF64, :cf64), (Struct1, :s1))
fname = Symbol("foo", v)
fname1 = Symbol("foo1", v)
a = copy(@eval $v)
verbose && println(t)
verbose && println("A: ", a)
@eval begin
global function $fname1(s::$t)
verbose && println("B: ", s)
@test s == $v
@test s === $a
global c = s
return s
end
global function $fname1(s)
@assert false
end
global function $fname(s::$t)
verbose && println("B: ", s)
@test s == $v
if ismutable(s)
@test !(s === $a)
end
global c = s
return s
end
global function $fname(s)
@assert false
end
end
@eval let a = $a, b
b = testclosure($fname1, a, true)
verbose && println("C: ", b)
@test b == $v
@test b === a
@test b === c
let cf = @cfunction($fname1, Ref{$t}, (Ref{$t},))
b = ccall(cf, Ref{$t}, (Ref{$t},), a)
verbose && println("C: ", b)
@test b == $v
@test b === a
@test b === c
end
let cf = @cfunction($fname, $t, ($t,))
b = ccall(cf, $t, ($t,), a)
verbose && println("C: ",b)
@test b == $v
if ismutable($v)
@test !(b === c)
@test !(b === a)
end
end
let cf = @cfunction($fname1, $t, (Ref{$t},))
b = ccall(cf, $t, (Ref{$t},), a)
verbose && println("C: ",b)
@test b == $v
if ismutable($v)
@test !(b === c)
@test !(b === a)
end
end
let cf = @cfunction($fname, Ref{$t}, ($t,))
b = ccall(cf, Ref{$t}, ($t,), a)
verbose && println("C: ",b)
@test b == $v
@test b === c
if ismutable($v)
@test !(b === a)
end
end
let cf = @cfunction($fname, Any, (Ref{$t},))
b = ccall(cf, Any, (Ref{$t},), $v)
verbose && println("C: ",b)
@test b == $v
@test b === c
if ismutable($v)
@test !(b === a)
end
end
let cf = @cfunction($fname, Any, (Ref{Any},))
b = ccall(cf, Any, (Ref{Any},), $v)
@test b == $v
@test b === c
if ismutable($v)
@test !(b === a)
end
end
a isa Complex && let cf = @cfunction($fname, Ref{Complex}, (Ref{Any},))
b = ccall(cf, Ref{Complex}, (Ref{Any},), $v)
@test b == $v
@test b === c
if ismutable($v)
@test !(b === a)
end
end
let cf = @cfunction($fname, Ref{AbstractString}, (Ref{Any},))
@test_throws TypeError ccall(cf, Any, (Ref{Any},), $v)
end
let cf = @cfunction($fname, Ref{Ptr}, (Ref{Any},))
@test_throws TypeError ccall(cf, Any, (Ref{Any},), $v)
end
let cf = @cfunction($fname, Ref{Complex{UInt32}}, (Ref{Any},))
@test_throws TypeError ccall(cf, Any, (Ref{Any},), $v)
end
if a isa Complex
local mkcb(::Type{T}) where {T} = @cfunction($fname, Ref{Complex{T}}, (Ref{Any},))
@test ccall(mkcb(typeof(a.re)), Any, (Ref{Any},), $v) === $v
end
#FIXME: @test_throws TypeError ccall(mkcb(UInt32), Any, (Ref{Any},), $v)
end
end