-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforeign.html
4505 lines (3250 loc) · 172 KB
/
foreign.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"><!-- DO NOT EDIT THIS FILE-->
<!-- Edit the .tex version instead-->
<html>
<head>
<title>Foreign Interface</title>
<link href="csug.css" rel="stylesheet" type="text/css">
</head>
<body>
<a name="g22"></a>
<a name="./foreign:h0"></a>
<h1>Chapter 4. Foreign Interface<a name="CHPTFOREIGN"></a></h1>
<p>
<i>Chez Scheme</i> provides two ways to interact with "foreign" code,
i.e., code written in other languages.
The first is via subprocess creation and communication, which is
discussed in the Section <a href="./foreign.html#g23">4.1</a>.
The second is via static or dynamic loading and invocation from Scheme
of procedures written in <a name="./foreign:s0"></a>C and
invocation from C of procedures written in Scheme.
These mechanisms are discussed in Sections <a href="./foreign.html#g28">4.2</a>
through <a href="./foreign.html#g30">4.4</a>.
<p>
The method for static loading of C object code is dependent upon which
machine you are running; see the installation instructions distributed
with <i>Chez Scheme</i>.
<p>
<h3><a name="g23"></a><a name="./foreign:h1"></a>Section 4.1. Subprocess Communication<a name="SECTFOREIGNSUBPROCESS"></a></h3>
<p>
Two procedures, <a name="./foreign:s1"></a><tt>system</tt> and <a name="./foreign:s2"></a><tt>process</tt>, are used to create
<a name="./foreign:s3"></a>subprocesses.
Both procedures accept a single string argument and create a
subprocess to execute the shell command contained in the string.
The <tt>system</tt> procedure waits for the process to exit before
returning, however,
while the <tt>process</tt> procedure returns immediately without
waiting for the process to exit.
The standard input and output files of a subprocess created by <tt>system</tt>
may be used to communicate with the user's console.
The standard
input and output files of a subprocess created by <tt>process</tt> may be used
to communicate with the Scheme process.
<p>
<a name="./foreign:s4"></a><span class=formdef><b>procedure</b>: <tt>(system <i>command</i>)</tt></span>
<br>
<b>returns: </b>see below
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><tt><i>command</i></tt> must be a string.
<p>
The <tt>system</tt> procedure creates a subprocess to perform the operation
specified by <tt><i>command</i></tt>.
The subprocess may communicate with the user through the same console
input and console output files used by the Scheme process.
After creating the subprocess, <tt>system</tt> waits for the process to exit
before returning.
<p>
When the subprocess exits, <tt>system</tt> returns the exit code for the
subprocess, unless (on Unix-based systems) a signal caused the subprocess
to terminate, in which case <tt>system</tt> returns the negation of the
signal that caused the termination, e.g., -1 for <tt>SIGHUP</tt>.
<p>
<a name="./foreign:s5"></a><span class=formdef><b>procedure</b>: <tt>(open-process-ports <i>command</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(open-process-ports <i>command</i> <i>b-mode</i>)</tt></span>
<br><span class=formdef><b>procedure</b>: <tt>(open-process-ports <i>command</i> <i>b-mode</i> <i>?transcoder</i>)</tt></span>
<br>
<b>returns: </b>see below
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><tt><i>command</i></tt> must be a string.
If <tt><i>?transcoder</i></tt> is present and not <tt>#f</tt>, it must be a
transcoder, and this procedure creates textual ports, each of whose
transcoder is <tt><i>?transcoder</i></tt>.
Otherwise, this procedure returns binary ports.
<tt><i>b-mode</i></tt> specifies the buffer mode used by each of the ports
returned by this procedure and defaults to <tt>block</tt>.
Buffer modes are described in Section <a href="http://scheme.com/tspl4/./io.html#g123">7.2</a> of
<i>The Scheme Programming Language, 4th Edition</i>.
<p>
<tt>open-process-ports</tt> creates a subprocess to perform the operation
specified by <tt><i>command</i></tt>.
Unlike <tt>system</tt>, <tt>process</tt> returns immediately after creating the
subprocess, i.e., without waiting for the subprocess to terminate.
It returns four values:
<p>
<ol>
<a name="g24"></a><li><tt><i>to-stdin</i></tt> is an output port to which Scheme can send output to the
subprocess through the subprocess's standard input file.
<p>
<a name="g25"></a><li><tt><i>from-stdout</i></tt> is an input port from which Scheme can read input from
the subprocess through the subprocess's standard output file.
<p>
<a name="g26"></a><li><tt><i>from-stderr</i></tt> is an input port from which Scheme can read input from
the subprocess through the subprocess's standard error file.
<p>
<a name="g27"></a><li><tt><i>process-id</i></tt> is an integer identifying the created subprocess
provided by the host operating system.
</ol>
<p>
<p>
If the process exits or closes its standard output file descriptor, any
procedure that reads input from <tt><i>from-stdout</i></tt> will return an
end-of-file object.
Similarly, if the process exits or closes its standard error file
descriptor, any procedure that reads input from <tt><i>from-stderr</i></tt> will
return an end-of-file object.
<p>
The predicate <a name="./foreign:s6"></a><tt>input-port-ready?</tt>
may be used to detect whether input has been sent by the subprocess to
Scheme.
<p>
It is sometimes necessary to force output to be sent immediately
to the subprocess by invoking <tt>flush-output-port</tt> on
<tt><i>to-stdin</i></tt>, since <i>Chez Scheme</i> buffers the output for efficiency.
<p>
On UNIX systems, the <tt><i>process-id</i></tt> is the process identifier
for the shell created to execute <tt><i>command</i></tt>.
If <tt><i>command</i></tt> is used to invoke an executable file rather than
a shell command, it may be useful to prepend <tt><i>command</i></tt> with
the string <tt>"exec "</tt>, which causes the shell to load and execute
the named executable directly, without forking a new
process---the shell equivalent of a tail call.
This will reduce by one the number of subprocesses created and
cause <tt><i>process-id</i></tt> to reflect the process identifier for the
executable once the shell has transferred control.
<p>
<a name="./foreign:s7"></a><span class=formdef><b>procedure</b>: <tt>(process <i>command</i>)</tt></span>
<br>
<b>returns: </b>see explanation
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><tt><i>command</i></tt> must be a string.
<p>
<tt>process</tt> is similar to <tt>open-process-ports</tt>, but less
general.
It does not return a port from which the subproces's standard error output
can be read, and it always creates textual ports.
It returns a list of three values rather than the four separate values
of <tt>open-process-ports</tt>.
The returned list contains, in order: <tt><i>from-stdout</i></tt>,
<tt><i>to-stdin</i></tt>, and <tt><i>process-id</i></tt>, which correspond to the second,
first, and fourth return values of <tt>open-process-ports</tt>.
<p>
<p>
<h3><a name="g28"></a><a name="./foreign:h2"></a>Section 4.2. Calling out of Scheme<a name="SECTFOREIGNPROCEDURES"></a></h3>
<p>
<i>Chez Scheme</i>'s <a name="./foreign:s8"></a>foreign-procedure interface allows a Scheme program
to invoke
procedures written in <a name="./foreign:s9"></a>C or in languages that obey the same
calling conventions as C.
Two steps are necessary before foreign procedures can be invoked from Scheme.
First, the foreign procedure must be compiled and loaded,
either statically or dynamically,
as described in Section <a href="./foreign.html#g32">4.6</a>.
Then, access to the foreign procedure must be established in Scheme,
as described in this section.
Once access to a foreign procedure has been established it may be called as an
ordinary Scheme procedure.
<p>
Since foreign procedures operate independently of the Scheme memory management
and exception handling system, great care must be taken when using them.
Although the foreign-procedure interface provides
type checking (at optimize levels less than 3) and
type conversion, the programmer must ensure that
the sharing of data between Scheme and foreign procedures is done safely by
specifying proper argument and result types.
<p>
Scheme-callable wrappers for foreign procedures can also be created via
<tt>ftype-ref</tt> and function ftypes (Section <a href="./foreign.html#g31">4.5</a>).
<p>
<a name="./foreign:s10"></a><span class=formdef><b>syntax</b>: <tt>(foreign-procedure <i>entry-exp</i> (<i>param-type</i> ...) <i>res-type</i>)</tt></span>
<br><span class=formdef><b>syntax</b>: <tt>(foreign-procedure <i>conv</i> <i>entry-exp</i> (<i>param-type</i> ...) <i>res-type</i>)</tt></span>
<br>
<b>returns: </b>a procedure
<br>
<b>libraries: </b><tt>(chezscheme)</tt>
<p><tt><i>entry-exp</i></tt> must evaluate to a string representing a valid foreign
procedure entry point or an integer representing the address of the
foreign procedure.
The <tt><i>param-types</i></tt> and <tt><i>res-type</i></tt> must be symbols or
structured forms as described below.
When a <tt>foreign-procedure</tt> expression is evaluated, a Scheme procedure is
created that will invoke the foreign procedure specified by <tt><i>entry-exp</i></tt>.
When the procedure is called each argument is checked and converted according to
the specified <tt><i>param-type</i></tt> before it is passed to the foreign procedure.
The result of the foreign procedure call is converted as specified
by the <tt><i>res-type</i></tt>.
Multiple procedures may be created for the same <a name="./foreign:s11"></a>foreign entry.
<p>
<a name="page:conv-description"></a>If <tt><i>conv</i></tt> is present, it specifies the calling convention to be used.
The default is <tt>#f</tt>, which specifies the default calling convention
on the target machine.
Three other conventions are currently supported, all only under
Windows: <tt>__stdcall</tt>, <tt>__cdecl</tt>, and <tt>__com</tt>.
Since <tt>__cdecl</tt> is the default, specifying <tt>__cdecl</tt> is
equivalent to specifying <tt>#f</tt> or no convention.
<p>
Use <tt>__stdcall</tt> to access most Windows API procedures.
Use <tt>__cdecl</tt> for Windows API varargs procedures,
for C library procedures, and for most other procedures.
Use <tt>__com</tt> to invoke COM interface methods; COM uses the
<tt>__stdcall</tt> convention but additionally performs the indirections
necessary to obtain the correct method from a COM instance.
The address of the COM instance must be passed as the first argument,
which should normally be declared as <tt>iptr</tt>.
For the <tt>__com</tt> interface only, <tt><i>entry-exp</i></tt> must evaluate
to the byte offset of the method in the COM vtable.
For example,
<p>
<p><tt>(foreign-procedure __com 12 (iptr double-float) integer-32)</tt>
<p>creates an interface to a COM method at offset 12 in the vtable
encapsulated within the COM instance passed as the first argument,
with the second argument being a double float and the return
value being an integer.
<p>
Complete type checking and conversion is performed on the parameters.
The types
<a name="./foreign:s12"></a><tt>scheme-object</tt>,
<a name="./foreign:s13"></a><tt>string</tt>,
<a name="./foreign:s14"></a><tt>wstring</tt>,
<a name="./foreign:s15"></a><tt>u8*</tt>,
<a name="./foreign:s16"></a><tt>u16*</tt>,
<a name="./foreign:s17"></a><tt>u32*</tt>,
<a name="./foreign:s18"></a><tt>utf-8</tt>,
<a name="./foreign:s19"></a><tt>utf-16le</tt>,
<a name="./foreign:s20"></a><tt>utf-16be</tt>,
<a name="./foreign:s21"></a><tt>utf-32le</tt>,
and
<a name="./foreign:s22"></a><tt>utf-32be</tt>,
must be used with caution, however, since they allow allocated
Scheme objects to be used in places the Scheme memory management system
cannot control.
No problems will arise as long as such objects are not
retained in
foreign variables or data structures while Scheme code is running,
since garbage collection can occur only while Scheme code is running.
All other parameter types are converted to equivalent foreign
representations and consequently can be retained indefinitely in
foreign variables and data structures.
Following are the valid parameter types:
<p>
<p><tt>integer-8</tt>: <a name="./foreign:s23"></a>Exact integers from -2<sup>7</sup> through
2<sup>8</sup> - 1 are valid.
Integers in the range 2<sup>7</sup> through 2<sup>8</sup> - 1 are treated as
two's complement representations of negative numbers, e.g.,
<tt>#xff</tt> is treated as -1.
The argument is passed to C as an integer of the appropriate size
(usually <tt>signed char</tt>).
<p>
<p><tt>unsigned-8</tt>: <a name="./foreign:s24"></a>Exact integers from -2<sup>7</sup> to
2<sup>8</sup> - 1 are valid.
Integers in the range -2<sup>7</sup> through -1 are treated as the
positive equivalents of their two's complement representation,
e.g., -1 is treated as <tt>#xff</tt>.
The argument is passed to C as an unsigned integer of the
appropriate size (usually <tt>unsigned char</tt>).
<p>
<p><tt>integer-16</tt>: <a name="./foreign:s25"></a>Exact integers from -2<sup>15</sup> through
2<sup>16</sup> - 1 are valid.
Integers in the range 2<sup>15</sup> through 2<sup>16</sup> - 1 are treated as
two's complement representations of negative numbers, e.g.,
<tt>#xffff</tt> is treated as -1.
The argument is passed to C as an integer of the appropriate size
(usually <tt>short</tt>).
<p>
<p><tt>unsigned-16</tt>: <a name="./foreign:s26"></a>Exact integers from -2<sup>15</sup> to
2<sup>16</sup> - 1 are valid.
Integers in the range -2<sup>15</sup> through -1 are treated as the
positive equivalents of their two's complement representation,
e.g., -1 is treated as <tt>#xffff</tt>.
The argument is passed to C as an unsigned integer of the
appropriate size (usually <tt>unsigned short</tt>).
<p>
<p><tt>integer-32</tt>: <a name="./foreign:s27"></a>Exact integers from -2<sup>31</sup> through
2<sup>32</sup> - 1 are valid.
Integers in the range 2<sup>31</sup> through 2<sup>32</sup> - 1 are treated as
two's complement representations of negative numbers, e.g.,
<tt>#xffffffff</tt> is treated as -1.
The argument is passed to C as an integer of the appropriate size
(usually <tt>int</tt>).
<p>
<p><tt>unsigned-32</tt>: <a name="./foreign:s28"></a>Exact integers from -2<sup>31</sup> to
2<sup>32</sup> - 1 are valid.
Integers in the range -2<sup>31</sup> through -1 are treated as the
positive equivalents of their two's complement representation,
e.g., -1 is treated as <tt>#xffffffff</tt>.
The argument is passed to C as an unsigned integer of the
appropriate size (usually <tt>unsigned int</tt>).
<p>
<p><tt>integer-64</tt>: <a name="./foreign:s29"></a>Exact integers from -2<sup>63</sup> through
2<sup>64</sup> - 1 are valid.
Integers in the range 2<sup>63</sup> through 2<sup>64</sup> - 1 are treated as
two's complement representations of negative numbers.
The argument is passed to C as an integer of the appropriate
size (usually <tt>long long</tt> or, on many 64-bit platforms,
<tt>long</tt>).
<p>
<p><tt>unsigned-64</tt>: <a name="./foreign:s30"></a>Exact integers from -2<sup>63</sup> through
2<sup>64</sup> - 1 are valid.
Integers in the range -2<sup>63</sup> through -1 are treated as the
positive equivalents of their two's complement representation,
The argument is passed to C as an integer of the appropriate
size (usually <tt>unsigned long long</tt> or, on many 64-bit
platforms, <tt>long</tt>).
<p>
<p><tt>double-float</tt>: <a name="./foreign:s31"></a>Only Scheme flonums are valid---other
Scheme numeric types are not automatically converted.
The argument is passed to C as a double float.
<p>
<p><tt>single-float</tt>: <a name="./foreign:s32"></a>Only Scheme flonums are valid---other
Scheme numeric types are not automatically converted.
The argument is passed to C as a single float.
Since <i>Chez Scheme</i> represents flonums in double-float format, the
parameter is first converted into single-float format.
<p>
<p><tt>short</tt>: <a name="./foreign:s33"></a>This type is an alias for the appropriate
fixed-size type above, depending on the size of a C <tt>short</tt>.
<p>
<p><tt>unsigned-short</tt>: <a name="./foreign:s34"></a>This type is an alias for the appropriate
fixed-size type above, depending on the size of a C <tt>unsigned short</tt>.
<p>
<p><tt>int</tt>: <a name="./foreign:s35"></a>This type is an alias for the appropriate
fixed-size type above, depending on the size of a C <tt>int</tt>.
<p>
<p><tt>unsigned</tt>: <a name="./foreign:s36"></a>This type is an alias for the appropriate
fixed-size type above, depending on the size of a C <tt>unsigned</tt>.
<p>
<p><tt>unsigned-int</tt>: <a name="./foreign:s37"></a>This type is an alias <tt>unsigned</tt>.
fixed-size type above, depending on the size of a C <tt>unsigned</tt>.
<p>
<p><tt>long</tt>: <a name="./foreign:s38"></a>This type is an alias for the appropriate
fixed-size type above, depending on the size of a C <tt>long</tt>.
<p>
<p><tt>unsigned-long</tt>: <a name="./foreign:s39"></a>This type is an alias for the appropriate
fixed-size type above, depending on the size of a C <tt>unsigned long</tt>.
<p>
<p><tt>long-long</tt>: <a name="./foreign:s40"></a>This type is an alias for the appropriate
fixed-size type above, depending on the size of the nonstandard C type
<tt>long long</tt>.
<p>
<p><tt>unsigned-long-long</tt>: <a name="./foreign:s41"></a>This type is an alias for the appropriate
fixed-size type above, depending on the size of the nonstandard C type
<tt>unsigned long long</tt>.
<p>
<p><tt>ptrdiff_t</tt>: <a name="./foreign:s42"></a>This type is an alias for the appropriate
fixed-size type above, depending on its definition in the host machine's
<tt>stddef.h</tt> include file.
<p>
<p><tt>size_t</tt>: <a name="./foreign:s43"></a>This type is an alias for the appropriate unsigned
fixed-size type above, depending on its definition in the host machine's
<tt>stddef.h</tt> include file.
<p>
<p><tt>ssize_t</tt>: <a name="./foreign:s44"></a>This type is an alias for the appropriate signed
fixed-size type above, depending on its definition in the host machine's
<tt>stddef.h</tt> include file.
<p>
<p><tt>iptr</tt>: <a name="./foreign:s45"></a>This type is an alias for the appropriate
fixed-size type above, depending on the size of a C pointer.
<p>
<p><tt>uptr</tt>: <a name="./foreign:s46"></a>This type is an alias for the appropriate
(unsigned) fixed-size type above, depending on the size of a C pointer.
<p>
<p><tt>void*</tt>: <a name="./foreign:s47"></a>This type is an alias for <tt>uptr</tt>.
<p>
<p><tt>fixnum</tt>: <a name="./foreign:s48"></a>This type is equivalent to <tt>iptr</tt>,
except only values in the fixnum range are valid.
Transmission of fixnums is slightly faster than transmission of
<tt>iptr</tt> values, but the fixnum range is smaller, so some
<tt>iptr</tt> values do not have a fixnum representation.
<p>
<p><tt>boolean</tt>: <a name="./foreign:s49"></a>Any Scheme object may be passed as a boolean.
<tt>#f</tt> is converted to 0; all other objects are converted to 1.
The argument is passed to C as an <tt>int</tt>.
<p>
<p><tt>char</tt>: <a name="./foreign:s50"></a>Only Scheme characters with Unicode scalar values
in the range 0 through 255 are valid <tt>char</tt> parameters.
The character is converted to its Unicode scalar value, as with
<tt>char->integer</tt>, and passed to C as an <tt>unsigned char</tt>.
<p>
<p><tt>wchar_t</tt>: <a name="./foreign:s51"></a>Only Scheme characters are valid <tt>wchar_t</tt> parameters.
Under Windows and any other system where <tt>wchar_t</tt> holds only
16-bit values rather than full Unicode scalar values, only characters with
16-bit Unicode scalar values are valid.
On systems where <tt>wchar_t</tt> is a full 32-bit value, any Scheme
character is valid.
The character is converted to its Unicode scalar value, as with
<tt>char->integer</tt>, and passed to C as a <tt>wchar_t</tt>.
<p>
<p><tt>wchar</tt>: <a name="./foreign:s52"></a>This type is an alias for <tt>wchar_t</tt>.
<p>
<p><tt>double</tt>: <a name="./foreign:s53"></a>This type is an alias for <tt>double-float</tt>.
<p>
<p><tt>float</tt>: <a name="./foreign:s54"></a>This type is an alias for <tt>single-float</tt>.
<p>
<p><tt>scheme-object</tt>: <a name="./foreign:s55"></a>The argument is passed directly to the
foreign procedure; no conversion or type checking is performed.
This form of parameter passing should be used with discretion.
Scheme objects should not be preserved in foreign variables or data structures
since the memory management system may relocate them between foreign procedure
calls.
<p>
<p><tt>ptr</tt>: <a name="./foreign:s56"></a>This type is an alias for <tt>scheme-object</tt>.
<p>
<p><tt>u8*</tt>: <a name="./foreign:s57"></a>The argument must be a Scheme bytevector or
<tt>#f</tt>.
For <tt>#f</tt>, the null pointer (0) is passed to the foreign procedure.
For a bytevector, a pointer to the first byte of the bytevector's data
is passed.
If the C routine to which the data is passed requires the input to be
null-terminated, a null (0) byte must be included explicitly in the
bytevector.
The bytevector should not be retained in foreign variables or data
structures, since the memory management system may relocate or discard
them between foreign procedure calls, and use their storage for some
other purpose.
<p>
<p><tt>u16*</tt>: <a name="./foreign:s58"></a>Arguments of this type are treated just like
arguments of type <tt>u8*</tt>.
If the C routine to which the data is passed requires the input to be
null-terminated, two null (0) bytes must be included explicitly in the
bytevector, aligned on a 16-bit boundary.
<p>
<p><tt>u32*</tt>: <a name="./foreign:s59"></a>Arguments of this type are treated just like
arguments of type <tt>u8*</tt>.
If the C routine to which the data is passed requires the input to be
null-terminated, four null (0) bytes must be included explicitly in the
bytevector, aligned on a 32-bit boundary.
<p>
<p><tt>utf-8</tt>: <a name="./foreign:s60"></a>The argument must be a Scheme string or
<tt>#f</tt>.
For <tt>#f</tt>, the null pointer (0) is passed to the foreign procedure.
A string is converted into a bytevector, as if via <tt>string->utf8</tt>,
with an added null byte, and the address of the first byte of the
bytevector is passed to C.
The bytevector should not be retained in foreign variables or data
structures, since the memory management system may relocate or discard
them between foreign procedure calls, and use their storage for some
other purpose.
<p>
<p><tt>utf-16le</tt>: <a name="./foreign:s61"></a>Arguments of this type are treated like arguments
of type <tt>utf-8</tt>, except they are converted as if via
<tt>string->utf16</tt> with endianness <tt>little</tt>, and they are
extended by two null bytes rather than one.
<p>
<p><tt>utf-16be</tt>: <a name="./foreign:s62"></a>Arguments of this type are treated like arguments
of type <tt>utf-8</tt>, except they are converted as if via
<tt>string->utf16</tt> with endianness <tt>big</tt>, and they are
extended by two null bytes rather than one.
<p>
<p><tt>utf-32le</tt>: <a name="./foreign:s63"></a>Arguments of this type are treated like arguments
of type <tt>utf-8</tt>, except they are converted as if via
<tt>string->utf32</tt> with endianness <tt>little</tt>, and they are
extended by four null bytes rather than one.
<p>
<p><tt>utf-32be</tt>: <a name="./foreign:s64"></a>Arguments of this type are treated like arguments
of type <tt>utf-8</tt>, except they are converted as if via
<tt>string->utf32</tt> with endianness <tt>big</tt>, and they are
extended by four null bytes rather than one.
<p>
<p><tt>string</tt>: <a name="./foreign:s65"></a>This type is an alias for <tt>utf-8</tt>.
<p>
<p><tt>wstring</tt>: <a name="./foreign:s66"></a>This type is an alias for <tt>utf-16le</tt>,
<tt>utf-16be</tt>, <tt>utf-32le</tt>, or <tt>utf-32be</tt> as
appropriate depending on the size of a C <tt>wchar_t</tt> and
the endianness of the target machine.
For example, <tt>wstring</tt> is equivalent to <tt>utf-16le</tt>
under Windows running on Intel hardware.
<p>
<p><tt>(* <i>ftype</i>)</tt>: <a name="./foreign:s67"></a>This type allows a pointer to a foreign
type (ftype) to be passed.
The argument must be an ftype pointer of with type <tt><i>ftype</i></tt>,
and the actual argument is the address encapsulated in the
ftype pointer.
See Section <a href="./foreign.html#g31">4.5</a> for a description of
foreign types.
<p>
<p>The result types are similar to the parameter types with the addition of a
<a name="./foreign:s68"></a><tt>void</tt> type.
In general, the type conversions are the inverse of the parameter type
conversions.
No error checking is performed on return, since the system cannot determine
whether a foreign result is actually of the indicated type.
Particular caution should be exercised with the result types
<a name="./foreign:s69"></a><tt>scheme-object</tt>,
<a name="./foreign:s70"></a><tt>double-float</tt>,
<a name="./foreign:s71"></a><tt>double</tt>,
<a name="./foreign:s72"></a><tt>single-float</tt>,
<a name="./foreign:s73"></a><tt>float</tt>,
and the types that result in the construction of bytevectors or strings,
since invalid
return values may lead to invalid memory references as well as incorrect
computations.
Following are the valid result types:
<p>
<p><tt>void</tt>: <a name="./foreign:s74"></a>The result of the foreign procedure call is
ignored and an unspecified Scheme object is returned.
<tt>void</tt> should be used when foreign procedures are called for effect only.
<p>
<p><tt>integer-8</tt>: <a name="./foreign:s75"></a>The result is interpreted as a signed
8-bit integer and is converted to a Scheme exact integer.
<p>
<p><tt>unsigned-8</tt>: <a name="./foreign:s76"></a>The result is interpreted as an unsigned
8-bit integer and is converted to a Scheme nonnegative exact integer.
<p>
<p><tt>integer-16</tt>: <a name="./foreign:s77"></a>The result is interpreted as a signed
16-bit integer and is converted to a Scheme exact integer.
<p>
<p><tt>unsigned-16</tt>: <a name="./foreign:s78"></a>The result is interpreted as an unsigned
16-bit integer and is converted to a Scheme nonnegative exact integer.
<p>
<p><tt>integer-32</tt>: <a name="./foreign:s79"></a>The result is interpreted as a signed
32-bit integer and is converted to a Scheme exact integer.
<p>
<p><tt>unsigned-32</tt>: <a name="./foreign:s80"></a>The result is interpreted as an unsigned
32-bit integer and is converted to a Scheme nonnegative exact integer.
<p>
<p><tt>integer-64</tt>: <a name="./foreign:s81"></a>The result is interpreted as a signed
64-bit integer and is converted to a Scheme exact integer.
<p>
<p><tt>unsigned-64</tt>: <a name="./foreign:s82"></a>The result is interpreted as an unsigned
64-bit integer and is converted to a Scheme nonnegative exact integer.
<p>
<p><tt>double-float</tt>: <a name="./foreign:s83"></a>The result is interpreted as a double float
and is translated into a <i>Chez Scheme</i> flonum.
<p>
<p><tt>single-float</tt>: <a name="./foreign:s84"></a>The result is interpreted as a single float
and is translated into a <i>Chez Scheme</i> flonum.
Since <i>Chez Scheme</i> represents flonums in double-float format, the
result is first converted into double-float format.
<p>
<p><tt>short</tt>: <a name="./foreign:s85"></a>This type is an alias for the appropriate
fixed-size type above, depending on the size of a C <tt>short</tt>.
<p>
<p><tt>unsigned-short</tt>: <a name="./foreign:s86"></a>This type is an alias for the appropriate
fixed-size type above, depending on the size of a C <tt>unsigned short</tt>.
<p>
<p><tt>int</tt>: <a name="./foreign:s87"></a>This type is an alias for the appropriate
fixed-size type above, depending on the size of a C <tt>int</tt>.
<p>
<p><tt>unsigned</tt>: <a name="./foreign:s88"></a>This type is an alias for the appropriate
fixed-size type above, depending on the size of a C <tt>unsigned</tt>.
<p>
<p><tt>unsigned-int</tt>: <a name="./foreign:s89"></a>This type is an alias <tt>unsigned</tt>.
fixed-size type above, depending on the size of a C <tt>unsigned</tt>.
<p>
<p><tt>long</tt>: <a name="./foreign:s90"></a>This type is an alias for the appropriate
fixed-size type above, depending on the size of a C <tt>long</tt>.
<p>
<p><tt>unsigned-long</tt>: <a name="./foreign:s91"></a>This type is an alias for the appropriate
fixed-size type above, depending on the size of a C <tt>unsigned long</tt>.
<p>
<p><tt>long-long</tt>: <a name="./foreign:s92"></a>This type is an alias for the appropriate
fixed-size type above, depending on the size of the nonstandard C type
<tt>long long</tt>.
<p>
<p><tt>unsigned-long-long</tt>: <a name="./foreign:s93"></a>This type is an alias for the appropriate
fixed-size type above, depending on the size of the nonstandard C type
<tt>unsigned long long</tt>.
<p>
<p><tt>ptrdiff_t</tt>: <a name="./foreign:s94"></a>This type is an alias for the appropriate
fixed-size type above, depending on its definition in the host machine's
<tt>stddef.h</tt> include file.
<p>
<p><tt>size_t</tt>: <a name="./foreign:s95"></a>This type is an alias for the appropriate unsigned
fixed-size type above, depending on its definition in the host machine's
<tt>stddef.h</tt> include file.
<p>
<p><tt>ssize_t</tt>: <a name="./foreign:s96"></a>This type is an alias for the appropriate signed
fixed-size type above, depending on its definition in the host machine's
<tt>stddef.h</tt> include file.
<p>
<p><tt>iptr</tt>: <a name="./foreign:s97"></a>This type is an alias for the appropriate
fixed-size type above, depending on the size of a C pointer.
<p>
<p><tt>uptr</tt>: <a name="./foreign:s98"></a>This type is an alias for the appropriate
(unsigned) fixed-size type above, depending on the size of a C pointer.
<p>
<p><tt>void*</tt>: <a name="./foreign:s99"></a>This type is an alias for <tt>uptr</tt>.
<p>
<p><tt>boolean</tt>: <a name="./foreign:s100"></a>This type converts a C <tt>int</tt> return value
into a Scheme boolean.
0 is converted to <tt>#f</tt>; all other values are converted to <tt>#t</tt>.
<p>
<p><tt>char</tt>: <a name="./foreign:s101"></a>This type converts a C <tt>unsigned char</tt> return value
into a Scheme character, as if via <tt>integer->char</tt>.
<p>
<p><tt>wchar_t</tt>: <a name="./foreign:s102"></a>This type converts a C <tt>wchar_t</tt> return value
into a Scheme character, as if via <tt>integer->char</tt>.
The <tt>wchar_t</tt> value must be a valid Unicode scalar value.
<p>
<p><tt>wchar</tt>: <a name="./foreign:s103"></a>This type is an alias for <tt>wchar_t</tt>.
<p>
<p><tt>double</tt>: <a name="./foreign:s104"></a>This type is an alias for <tt>double-float</tt>.
<p>
<p><tt>float</tt>: <a name="./foreign:s105"></a>This type is an alias for <tt>single-float</tt>.
<p>
<p><tt>scheme-object</tt>: <a name="./foreign:s106"></a>The result is assumed to be a valid Scheme
object, and no conversion is performed.
This type is inherently dangerous, since an invalid Scheme object can corrupt
the memory management system with unpredictable (but always unpleasant) results.
Since Scheme objects are actually typed pointers, even integers cannot
safely be returned as type <tt>scheme-object</tt> unless they were created by
the Scheme system.
<p>
<p><tt>ptr</tt>: <a name="./foreign:s107"></a>This type is an alias for <tt>scheme-object</tt>.
<p>
<p><tt>u8*</tt>: <a name="./foreign:s108"></a>The result is interpreted as a pointer to a
null-terminated sequence of 8-bit unsigned integers (bytes).
If the result is a null pointer, <tt>#f</tt> is returned.
Otherwise, the sequence of bytes is stored in a freshly allocated
bytevector of the appropriate length, and the bytevector is returned to
Scheme.
<p>
<p><tt>u16*</tt>: <a name="./foreign:s109"></a>The result is interpreted as a pointer to a
null-terminated sequence of 16-bit unsigned integers.
If the result is a null pointer, <tt>#f</tt> is returned.
Otherwise, the sequence of 16-bit integers is stored in a freshly allocated
bytevector of the appropriate length, and the bytevector is returned to
Scheme.
The null terminator must be a properly aligned 16-bit word,
i.e., two bytes of zero aligned on a 16-bit boundary.
<p>
<p><tt>u32*</tt>: <a name="./foreign:s110"></a>The result is interpreted as a pointer to a
null-terminated sequence of 32-bit unsigned integers.
If the result is a null pointer, <tt>#f</tt> is returned.
Otherwise, the sequence of 16-bit integers is stored in a freshly allocated
bytevector of the appropriate length, and the bytevector is returned to
Scheme.
The null terminator must be a properly aligned 32-bit word,
i.e., four bytes of zero aligned on a 32-bit boundary.
<p>
<p><tt>utf-8</tt>: <a name="./foreign:s111"></a>The result is interpreted as a pointer to a
null-terminated sequence of 8-bit unsigned character values.
If the result is a null pointer, <tt>#f</tt> is returned.
Otherwise, the sequence of bytes is converted into a Scheme string, as if
via <tt>utf8->string</tt>, and the string is returned to Scheme.
<p>
<p><tt>utf-16le</tt>: <a name="./foreign:s112"></a>The result is interpreted as a pointer to a
null-terminated sequence of 16-bit unsigned integers.
If the result is a null pointer, <tt>#f</tt> is returned.
Otherwise, the sequence of integers is converted into a Scheme string, as if
via <tt>utf16->string</tt> with endianness <tt>little</tt>,
and the string is returned to Scheme.
A byte-order mark in the sequence of integers as treated as an ordinary
character value and does not affect the byte ordering.
<p>
<p><tt>utf-16be</tt>: <a name="./foreign:s113"></a>The result is interpreted as a pointer to a
null-terminated sequence of 16-bit unsigned integers.
If the result is a null pointer, <tt>#f</tt> is returned.
Otherwise, the sequence of integers is converted into a Scheme string, as if
via <tt>utf16->string</tt> with endianness <tt>big</tt>,
and the string is returned to Scheme.
A byte-order mark in the sequence of integers as treated as an ordinary
character value and does not affect the byte ordering.
<p>
<p><tt>utf-32le</tt>: <a name="./foreign:s114"></a>The result is interpreted as a pointer to a
null-terminated sequence of 32-bit unsigned integers.
If the result is a null pointer, <tt>#f</tt> is returned.
Otherwise, the sequence of integers is converted into a Scheme string, as if
via <tt>utf32->string</tt> with endianness <tt>little</tt>,
and the string is returned to Scheme.
A byte-order mark in the sequence of integers as treated as an ordinary
character value and does not affect the byte ordering.
<p>
<p><tt>utf-32be</tt>: <a name="./foreign:s115"></a>The result is interpreted as a pointer to a
null-terminated sequence of 32-bit unsigned integers.
If the result is a null pointer, <tt>#f</tt> is returned.
Otherwise, the sequence of integers is converted into a Scheme string, as if
via <tt>utf32->string</tt> with endianness <tt>big</tt>,
and the string is returned to Scheme.
A byte-order mark in the sequence of integers as treated as an ordinary
character value and does not affect the byte ordering.
<p>
<p><tt>string</tt>: <a name="./foreign:s116"></a>This type is an alias for <tt>utf-8</tt>.
<p>
<p><tt>wstring</tt>: <a name="./foreign:s117"></a>This type is an alias for <tt>utf-16le</tt>,
<tt>utf-16be</tt>, <tt>utf-32le</tt>, or <tt>utf-32be</tt> as
appropriate depending on the size of a C <tt>wchar_t</tt> and
the endianness of the target machine.
For example, <tt>wstring</tt> is equivalent to <tt>utf-16le</tt>
under Windows running on Intel hardware.
<p>
<p><tt>(* <i>ftype</i>)</tt>: <a name="./foreign:s118"></a>The result is interpreted as the address of a foreign object
whose structure is described by <tt><i>ftype</i></tt>, and a freshly allocated
ftype pointer encapsulating the address is returned.
See Section <a href="./foreign.html#g31">4.5</a> for a description of
foreign types.
<p>
<p>Consider a C identity procedure:
<p><tt>int id(x) int x; { return x; }</tt>
<p>After a file containing this procedure has been compiled and loaded
(see Section <a href="./foreign.html#g32">4.6</a>) it can be accessed as follows:
<p>
<p><tt>(foreign-procedure "id"<br>
(int) int) <img src="math/csug/0.gif" alt="<graphic>"> #<procedure><br>
((foreign-procedure "id"<br>
(int) int)<br>
1) <img src="math/csug/0.gif" alt="<graphic>"> 1<br>
(define int-id<br>
(foreign-procedure "id"<br>
(int) int))<br>
(int-id 1) <img src="math/csug/0.gif" alt="<graphic>"> 1</tt>
<p>The <tt>"id"</tt> entry can also be interpreted as accepting and returning
a boolean:
<p>
<p><tt>(define bool-id<br>
(foreign-procedure "id"<br>
(boolean) boolean))<br>
(bool-id #f) <img src="math/csug/0.gif" alt="<graphic>"> #f<br>
(bool-id #t) <img src="math/csug/0.gif" alt="<graphic>"> #t<br>
(bool-id 1) <img src="math/csug/0.gif" alt="<graphic>"> #t</tt>
<p>As the last example reveals, <tt>bool-id</tt> is actually a conversion procedure.
When a Scheme object is passed as type <tt>boolean</tt> it is converted to
0 or 1, and when it is returned it is converted to <tt>#f</tt> or <tt>#t</tt>.
As a result objects are converted to normalized boolean values.
The <tt>"id"</tt> entry can be used to create other conversion procedures by
varying the type specifications:
<p>
<p><tt>(define int->bool<br>
(foreign-procedure "id"<br>
(int) boolean))<br>
(int->bool 0) <img src="math/csug/0.gif" alt="<graphic>"> #f<br>
(int->bool 5) <img src="math/csug/0.gif" alt="<graphic>"> #t<br>
(map (foreign-procedure "id"<br>
(boolean) int)<br>
'(#t #f)) <img src="math/csug/0.gif" alt="<graphic>"> (1 0)<br>
(define void<br>
(foreign-procedure "id"<br>
(int) void))<br>
(void 10) <img src="math/csug/0.gif" alt="<graphic>"> <i>unspecified</i></tt>
<p>There are, of course, simpler and more efficient ways of accomplishing
these conversions directly in Scheme.
<p>
A foreign entry is resolved when a
<a name="./foreign:s119"></a><tt>foreign-procedure</tt> expression
is evaluated, rather than either when the code is loaded or each time
the procedure is invoked.
Thus, the following definition is always valid since the
<tt>foreign-procedure</tt> expression is not immediately evaluated:
<p>
<p><tt>(define doit<br>
(lambda ()<br>
((foreign-procedure "doit" () void))))</tt>
<p><tt>doit</tt> should not be invoked, however, before an entry for
<tt>"doit"</tt> has been provided.
Similarly, an entry for <tt>"doit"</tt> must exist before the following code
is evaluated:
<p>
<p><tt>(define doit<br>
(foreign-procedure "doit" () void))</tt>
<p>Although the second definition is more constraining on the load order
of foreign files, it is more efficient since the entry resolution need
be done only once.
<p>
It is often useful to define a template to be used
in the creation of several foreign procedures with similar argument
types and return values.
For example, the following code creates two foreign procedures from
a single foreign procedure expression, by abstracting out the foreign
procedure name:
<p>
<p><tt>(define double->double<br>
(lambda (proc-name)<br>
(foreign-procedure proc-name<br>
(double)<br>
double)))
<br>
<br>
(define log10 (double->double "log10"))<br>
(define gamma (double->double "gamma"))</tt>
<p>Both <tt>"log10"</tt> and <tt>"gamma"</tt> must be available as foreign
entries (see Section <a href="./foreign.html#g32">4.6</a>)
before the corresponding definitions.
The use of foreign procedure templates can simplify the coding process
and reduce the amount of code generated when a large number of
foreign procedures are involved, e.g., when an entire library of
foreign procedures is imported into Scheme.
<p>
<h3><a name="g29"></a><a name="./foreign:h3"></a>Section 4.3. Calling into Scheme<a name="SECTFOREIGNCALLABLE"></a></h3>
<p>
Section <a href="./foreign.html#g28">4.2</a> describes the <tt>foreign-procedure</tt>
form, which permits Scheme code to invoke C or C-compatible foreign
procedures.
This section describes the <tt>foreign-callable</tt> form, which permits
C or C-compatible code to call Scheme procedures.
A more primitive mechanism for calling Scheme procedures from C is
described in Section <a href="./foreign.html#g34">4.8</a>.
<p>
As when calling foreign procedures from Scheme,
great care must be taken when sharing data between Scheme and
foreign code that calls Scheme to avoid corrupting Scheme's memory
management system.
<p>
A foreign-callable wrapper for a Scheme procedure can also be created by
passing the procedure to <tt>make-ftype-pointer</tt> with an appropriate
function ftype (Section <a href="./foreign.html#g31">4.5</a>).
<p>
<a name="./foreign:s120"></a><span class=formdef><b>syntax</b>: <tt>(foreign-callable <i>proc-exp</i> (<i>param-type</i> ...) <i>res-type</i>)</tt></span>