scm-from-c.texi 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. @node Accessing Scheme data from C
  2. @section Accessing Scheme data from C
  3. @c No better place to put s48_value's index entry.
  4. @bnindex s48_value
  5. @cindex @file{scheme48.h}
  6. The C header file @file{scheme48.h} provides access to Scheme48 data
  7. structures. The type @code{s48_value} is used for Scheme values. When
  8. the type of a value is known, such as the integer returned by the
  9. Scheme procedure @code{vector-length} or the boolean returned by
  10. @code{pair}, the corresponding C function returns a C value of the
  11. appropriate type, not an @code{s48_value}. Predicates return @code{1}
  12. for true and @code{0} for false.
  13. @cindex Scheme constants in C
  14. @cindex C macros for Scheme constants
  15. @deftypevr {C macro} s48_value S48_FALSE
  16. @deftypevrx {C macro} s48_value S48_TRUE
  17. @deftypevrx {C macro} s48_value S48_NULL
  18. @deftypevrx {C macro} s48_value S48_UNSPECIFIC
  19. @deftypevrx {C macro} s48_value S48_EOF
  20. @deftypevrx {C macro} long S48_MAX_FIXNUM_VALUE
  21. @deftypevrx {C macro} long S48_MIN_FIXNUM_VALUE
  22. These C macros denote various Scheme constants. @code{S48_FALSE} is
  23. the boolean false value, written in Scheme as @code{#f}.
  24. @code{S48_TRUE} is the boolean true value, or @code{#t}.
  25. @code{S48_NULL} is the empty list @code{()}. @code{S48_UNSPECIFIC} is
  26. a miscellaneous value returned by procedures that have no meaningful
  27. return value (accessed in Scheme48 by the nullary procedure
  28. @code{unspecific} in the @code{util} structure). @code{S48_EOF} is
  29. the end-of-file object (which the Scheme procedure @code{eof-object?}
  30. answers true for). @code{S48_MAX_FIXNUM_VALUE} is the maximum integer
  31. as a @code{long} that can be represented in a Scheme48 fixnum.
  32. @code{S48_MIN_FIXNUM_VALUE} is similar, but the minimum integer.
  33. @end deftypevr
  34. @cindex C and Scheme data conversion
  35. @cindex Scheme and C data conversion
  36. @deftypefn {C macro} int S48_EXTRACT_BOOLEAN (s48_value @var{boolean})
  37. @deftypefnx {C function} {unsigned char} s48_extract_char (s48_value @var{char})
  38. @deftypefnx {C function} {char *} s48_extract_string (s48_value @var{string})
  39. @deftypefnx {C function} {char *} s48_extract_byte_vector (s48_value @var{bytev})
  40. @deftypefnx {C function} long s48_extract_integer (s48_value @var{integer})
  41. @deftypefnx {C function} double s48_extract_double (s48_value @var{double})
  42. @deftypefnx {C macro} s48_value S48_ENTER_BOOLEAN (int @var{boolean})
  43. @deftypefnx {C function} s48_value s48_enter_char (unsigned char @var{char})
  44. @deftypefnx {C function} s48_value s48_enter_string (char *@var{string})
  45. @deftypefnx {C function} s48_value s48_enter_byte_vector (char *@var{bytev}, long @var{length})
  46. @deftypefnx {C function} s48_value s48_enter_integer (long @var{integer})
  47. @deftypefnx {C function} s48_value s48_enter_double (double @var{double})
  48. These functions & macros convert values between their respective Scheme
  49. & C representations.
  50. @code{S48_EXTRACT_BOOLEAN} returns @code{0} if @var{boolean} is
  51. @code{#f} and @code{1} otherwise. @code{S48_ENTER_BOOLEAN} returns the
  52. Scheme value @code{#f} if its argument is zero and @code{#t} otherwise.
  53. @code{s48_extract_char} & @code{s48_enter_char} convert between Scheme
  54. characters and C @code{char}s.
  55. @code{s48_extract_string} & @code{s48_extract_byte_vector} return
  56. pointers to the actual storage used by @var{string} or @var{bytev}.
  57. These pointers are valid only until the next garbage collection,
  58. however; @pxref{Interacting with the Scheme heap in C}.
  59. @code{s48_enter_string} & @code{s48_enter_byte_vector} allocate
  60. space on the Scheme48 heap for the given strings or byte vectors.
  61. @code{s48_enter_string} copies the data starting from the pointer it
  62. is given up to the first ASCII @code{NUL} character, whereas
  63. @code{s48_enter_byte_vector} is given the number of bytes to copy into
  64. the Scheme heap.
  65. @code{s48_extract_integer} returns a C @code{long} that represents the
  66. Scheme integer as input. If the Scheme integer is too large to be
  67. represented in a long, an exception is signalled. (The Scheme integer
  68. may be a fixnum or a bignum.) @code{s48_enter_integer} converts back
  69. to Scheme integers, and it will never signal an exception.
  70. @code{s48_extract_double} & @code{s48_enter_double} convert between
  71. Scheme & C double-precision floating point representations.
  72. Of these, @code{s48_enter_string}, @code{s48_enter_byte_vector},
  73. @code{s48_enter_integer}, & @code{s48_enter_double} may cause the
  74. garbage collector to be invoked: the former two copy the string or
  75. byte vector onto the Scheme heap first, @code{s48_enter_integer} may
  76. need to allocate a bignum (since C @code{long}s are wider than Scheme48
  77. fixnums), and floats are heap-allocated in Scheme48.
  78. @end deftypefn
  79. @cindex Scheme boolean testing in C
  80. @cindex C macros on Scheme booleans
  81. @deftypefn {C macro} int S48_TRUE_P (s48_value @var{object})
  82. @deftypefnx {C macro} int S48_FALSE_P (s48_value @var{object})
  83. @code{S48_TRUE_P} returns true if @var{object} is the true constant
  84. @code{S48_TRUE} and false if otherwise. @code{S48_FALSE_P} returns
  85. true if its argument is the false constant @code{S48_FALSE} and false
  86. if otherwise.
  87. @end deftypefn
  88. @cindex Scheme fixnums from C
  89. @cindex C access to Scheme fixnums
  90. @deftypefn {C macro} int S48_FIXNUM_P (s48_value @var{object})
  91. @deftypefnx {C function} long s48_extract_fixnum (s48_value @var{fixnum})
  92. @deftypefnx {C function} s48_value s48_enter_fixnum (long @var{integer})
  93. @code{S48_FIXNUM_P} is the C predicate for Scheme48 fixnums, delimited
  94. in range by @code{S48_MIN_FIXNUM_VALUE} & @code{S48_MAX_FIXNUM_VALUE}.
  95. @code{s48_extract_fixnum} returns the C @code{long} representation of
  96. the Scheme fixnum, and @code{s48_enter_fixnum} returns the Scheme
  97. fixnum representation of the C @code{long}. These are identical to
  98. @code{s48_extract_integer} & @code{s48_enter_integer}, except that
  99. @code{s48_extract_fixnum} will never raise a range exception, but
  100. @code{s48_enter_fixnum} may, and @code{s48_enter_fixnum} will never
  101. return a bignum; this is due to the fact that C @code{long}s have a
  102. wider range than Scheme48 fixnums.
  103. @end deftypefn
  104. @cindex Scheme data predicates in C
  105. @cindex C predicates for Scheme data
  106. @deftypefn {C macro} int S48_EQ_P (s48_value @var{a}, s48_value @var{b})
  107. @deftypefnx {C macro} int S48_CHAR_P (s48_value @var{object})
  108. @deftypefnx {C macro} int S48_PAIR_P (s48_value @var{object})
  109. @deftypefnx {C macro} int S48_VECTOR_P (s48_value @var{object})
  110. @deftypefnx {C macro} int S48_STRING_P (s48_value @var{object})
  111. @deftypefnx {C macro} int S48_SYMBOL_P (s48_value @var{object})
  112. @deftypefnx {C macro} int S48_BYTE_VECTOR_P (s48_value @var{object})
  113. @cindex Scheme pair operations in C
  114. @cindex C access to Scheme pairs
  115. @deftypefnx {C macro} s48_value S48_CAR (s48_value @var{pair})
  116. @deftypefnx {C macro} s48_value S48_CDR (s48_value @var{pair})
  117. @deftypefnx {C macro} void S48_SET_CAR (s48_value @var{pair}, s48_value @var{object})
  118. @deftypefnx {C macro} void S48_SET_CDR (s48_value @var{pair}, s48_value @var{object})
  119. @deftypefnx {C function (may GC)} s48_value s48_cons (s48_value @var{car}, s48_value @var{cdr})
  120. @deftypefnx {C function} s48_value s48_length (s48_value @var{list})
  121. @cindex Scheme vector operations in C
  122. @cindex C access to Scheme vectors
  123. @deftypefnx {C macro} long S48_VECTOR_LENGTH (s48_value @var{vector})
  124. @deftypefnx {C macro} s48_value S48_VECTOR_REF (s48_value @var{vector}, long @var{index})
  125. @deftypefnx {C macro} void S48_VECTOR_SET (s48_value @var{vector}, long @var{index}, s48_value @var{object})
  126. @deftypefnx {C function (may GC)} s48_value s48_make_vector (long @var{length}, s48_value @var{fill})
  127. @cindex Scheme string operations in C
  128. @cindex C access to Scheme strings
  129. @deftypefnx {C macro} long S48_STRING_LENGTH (s48_value @var{string})
  130. @deftypefnx {C macro} char S48_STRING_REF (s48_value @var{string}, long @var{index})
  131. @deftypefnx {C macro} void S48_STRING_SET (s48_value @var{string}, long @var{index}, char @var{char})
  132. @deftypefnx {C function (may GC)} s48_value s48_make_string (long @var{length}, char @var{fill})
  133. @deftypefnx {C macro} s48_value S48_SYMBOL_TO_STRING (s48_value @var{symbol})
  134. @cindex Scheme byte vector operations in C
  135. @cindex C access to Scheme byte vectors
  136. @deftypefnx {C macro} long S48_BYTE_VECTOR_LENGTH (s48_value @var{bytev})
  137. @deftypefnx {C macro} char S48_BYTE_VECTOR_REF (s48_value @var{bytev}, long @var{index})
  138. @deftypefnx {C macro} void S48_BYTE_VECTOR_SET (s48_value @var{bytev}, long @var{index}, char @var{byte})
  139. @deftypefnx {C function (may GC)} s48_value s48_make_byte_vector (long @var{length})
  140. C versions of miscellaneous Scheme procedures. The names were derived
  141. from their Scheme counterparts by replacing hyphens with underscores,
  142. @code{?} suffixes with @code{_P}, and dropping @code{!} suffixes.
  143. @end deftypefn
  144. @node Calling Scheme procedures from C
  145. @section Calling Scheme procedures from C
  146. @cindex Scheme callbacks in C
  147. @deftypefn {C function} s48_value s48_call_scheme (s48_value @var{proc}, long @var{nargs}, ...)
  148. Calls the Scheme procedure @var{proc} on @var{nargs} arguments, which
  149. are passed as additional arguments to @code{s48_call_scheme}. There
  150. may be at most twelve arguments. The value returned by the Scheme
  151. procedure is returned to the C procedure. Calling any Scheme procedure
  152. may potentially cause a garbage collection.
  153. @end deftypefn
  154. @cindex callbacks from C and continuations
  155. @cindex continuations and callbacks from C
  156. @cindex callbacks from C and threads
  157. @cindex threads and callbacks from C
  158. @cindex interaction between continuations and C
  159. There are some complications that arise when mixing calls from C to
  160. Scheme with continuations & threads. C supports only downward
  161. continuations (via @code{longjmp()}). Scheme continuations that
  162. capture a portion of the C stack have to follow the same restriction.
  163. For example, suppose Scheme procedure @code{s0} captures continuation
  164. @code{a} and then calls C function @code{c0}, which in turn calls
  165. Scheme procedure @code{s1}. @code{S1} can safely call the continuation
  166. @code{a}, because that is a downward use. When @code{a} is called,
  167. Scheme48 will remove the portion of the C stack used by the call to
  168. @code{c0}. On the other hand, if @code{s1} captures a continuation,
  169. that continuation cannot be used from @code{s0}, because, by the time
  170. control returns to @code{s0}, the C stack used by @code{s0} will no
  171. longer be valid. An attempt to invoke an upward continuation that is
  172. closed over a portion of the C stack will raise an exception.
  173. In Scheme48, threads are implemented using continuations, so the
  174. downward restriction applies to them as well. An attempt to return
  175. from Scheme to C at a time when the appropriate C frame is not on the
  176. top of the C stack will cause the current thread to block until the
  177. frame is available. For example, suppose thread @code{t0} calls a C
  178. function that calls back to Scheme, at which point control switches to
  179. thread @code{t1}, which also calls C & then back to Scheme. At this
  180. point, both @code{t0} & @code{t1} have active calls to C on the C
  181. stack, with @code{t1}'s C frame above @code{t0}'s. If @code{t0}
  182. attempts to return from Scheme to C, it will block, because the frame
  183. is not yet accessible. Once @code{t1} has returned to C and from there
  184. back to Scheme, @code{t0} will be able to resume. The return to Scheme
  185. is required because context switches can occur only while Scheme code
  186. is running. @code{T0} will also be able to resume if @code{t1} uses a
  187. continuation to throw past its call out to C.