c-from-scm.texi 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. @node Calling C functions from Scheme
  2. @section Calling C functions from Scheme
  3. @cindex importing C functions to Scheme
  4. @stindex external-calls
  5. The @code{external-calls} structure exports several ways to call C
  6. functions from Scheme, along with several other related utilities,
  7. many of which are also available from other structures. There are two
  8. different ways to call C functions from Scheme, depending on how the C
  9. function was obtained:
  10. @deffn {Scheme procedure} call-imported-binding binding argument @dots{} @returns{} value
  11. @deffnx {Scheme procedure} call-external-value byte-vector name argument @dots{} @returns{} value
  12. Each of these applies its first argument, a C function, to the rest of
  13. the arguments. For @code{call-imported-binding}, the function argument
  14. must be an imported binding. For @code{call-external-value}, the
  15. function argument must be a byte vector that contains a pointer to a C
  16. function, and @var{name} should be a string that names the function.
  17. The @var{name} argument is used only for printing error messages.
  18. For both of these, the C function is passed the argument values, and
  19. the value returned is that returned by the C function. No automatic
  20. representation conversion occurs for either arguments or return
  21. values. Up to twelve arguments may be passed. There is no method
  22. supplied for returning multiple values to Scheme from C or vice versa
  23. (mainly because C does not have multiple return values).
  24. Keyboard interrupts that occur during a call to a C function are
  25. ignored until the function returns to Scheme.@footnote{This is clearly
  26. a problem; we are working on a solution.}
  27. @end deffn
  28. @cindex importing C functions to Scheme
  29. @deffn {Scheme syntax} import-definition name [c-string]
  30. @deffnx {Scheme syntax} import-lambda-definition name formals [c-string]
  31. These macros simplify importing bindings from C into Scheme and
  32. wrapping such bindings in Scheme procedures. @code{Import-definition}
  33. defines @var{name} to be the shared binding named by @var{c-string},
  34. whose value, if it is not supplied, is by default a string of
  35. @var{name}, downcased and with all hyphens translated to underscores.
  36. @lisp
  37. (define @var{name} (lookup-imported-binding @var{c-string}))@end lisp
  38. @noindent
  39. For example,
  40. @lisp
  41. (import-definition my-foo)
  42. @expansion{} (define my-foo (lookup-imported-binding "my_foo"))@end lisp
  43. @code{Import-lambda-definition} imports the named C binding, using
  44. either the provided C binding name or by translating the Scheme name
  45. as with @code{import-definition}, and defines @var{name} to be a
  46. procedure with the given formal parameter list that calls the imported
  47. C binding with its arguments:
  48. @lisp
  49. (define @var{binding} (lookup-imported-binding @var{c-string}))
  50. (define (@var{name} @var{formal} @dots{})
  51. (call-imported-binding @var{binding} @var{formal} @dots{}))@end lisp
  52. @noindent
  53. Examples:
  54. @lisp
  55. (import-lambda-definition integer->process-id (int)
  56. "posix_getpid")
  57. @expansion{}
  58. (define @var{binding@sub{0}}
  59. (lookup-imported-binding "posix_getpid"))
  60. (define (integer->process-id int)
  61. (call-imported-binding @var{binding@sub{0}} int))
  62. (import-lambda-definition s48-system (string))
  63. @expansion{}
  64. (define @var{binding@sub{1}}
  65. (lookup-imported-binding "s48_system"))
  66. (define (s48-system string)
  67. (call-imported-binding @var{binding@sub{1}} string))@end lisp
  68. @noindent
  69. where @var{binding@sub{0}} and @var{binding@sub{1}} are fresh, unused
  70. variable names.
  71. @strong{Warning:} @code{Import-lambda-definition}, as presently
  72. implemented, requires a fixed parameter list; it does not allow `rest
  73. list' arguments.
  74. @end deffn
  75. @deffn {Scheme procedure} lookup-imported-binding name @returns{} shared-binding
  76. @deffnx {Scheme procedure} define-exported-binding shared-binding @returns{} unspecified
  77. @deffnx {Scheme procedure} shared-binding-ref shared-binding @returns{} value
  78. These are identical to the procedures accessible with the same names
  79. from the @embedref{Shared bindings between Scheme and C,
  80. @code{shared-bindings} structure}.
  81. @end deffn
  82. @deffn {Scheme procedure} add-finalizer! object procedure @returns{} unspecified
  83. Registers @var{procedure} as the finalizer for @var{object}. When
  84. @var{object} is later about to be reclaimed by the garbage collector,
  85. @var{procedure} is applied to one argument, @var{object}. All
  86. finalizers are applied in a child of the root scheduler thread that is
  87. spawned after every garbage collection. If an error occurs in any
  88. finalizer, it will be printed to the standard error output port, and
  89. all other finalizers will be aborted before they are given a chance to
  90. run. Because of this, and the fact that finalizers are collected and
  91. run after every garbage collection, they should perform as little
  92. computation as possible. @var{Procedure} may also create new
  93. references to @var{object} elsewhere in the heap, in which case the
  94. object will not be reclaimed, but its associated finalizer will be
  95. forgotten.
  96. @strong{Warning:} Finalizers are expensive. Use sparingly.
  97. @end deffn
  98. @deffn {Scheme procedure} define-record-resumer record-type resumer @returns{} unspecified
  99. Identical to the procedure accessible with the same name from the
  100. @embedref{Records, @code{record-types} structure}. Record resumers
  101. are often useful in working with foreign C data, which is in many
  102. cases specific to the program image within the operating system, and
  103. which cannot straightforwardly be relocated to a different address
  104. space.
  105. @end deffn