shared.texi 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. @node Shared bindings between Scheme and C
  2. @section Shared bindings between Scheme and C
  3. @cindex sharing data between Scheme and C
  4. @stindex shared-bindings
  5. Shared bindings are the means by which named values are shared between
  6. Scheme & C code. There are two separate tables of shared bindings, one
  7. for values defined in Scheme and accessed from C and the other for the
  8. opposite direction. Shared bindings actually bind names to cells, to
  9. allow a name to be resolved before it has been assigned. This is
  10. necessary because C initialization code may run before or after the
  11. corresponding Scheme code, depending on whether the Scheme code is in
  12. the resumed image or run in the current session. The Scheme bindings
  13. described here are available from the @code{shared-bindings}
  14. structure.
  15. @subsection Scheme shared binding interface
  16. @deffn {Scheme procedure} shared-binding? object @returns{} boolean
  17. @deffnx {Scheme procedure} shared-binding-is-import? shared-binding @returns{} boolean
  18. @code{Shared-binding?} is the disjoint type predicate for all shared
  19. bindings, imported or exported; @code{shared-binding-is-import?}
  20. returns true if @var{shared-binding} was imported into Scheme from C,
  21. and false if it has the converse direction.
  22. @end deffn
  23. @deffn {Scheme procedure} shared-binding-ref shared-binding @returns{} value
  24. @deffnx {Scheme procedure} shared-binding-set! shared-binding value @returns{} unspecified
  25. @code{Shared-binding-ref} returns the value of @var{shared-binding};
  26. @code{shared-binding-set!} sets the value of @var{shared-binding} to
  27. be @var{value}.
  28. @end deffn
  29. @cindex importing bindings into Scheme from C
  30. @deffn {Scheme procedure} lookup-imported-binding name @returns{} shared-binding
  31. @deffnx {Scheme procedure} define-imported-binding name value @returns{} unspecified
  32. @deffnx {Scheme procedure} undefine-imported-binding name @returns{} unspecified
  33. @code{Lookup-imported-binding} returns the binding imported from C to
  34. Scheme with the given name; a binding is created if none exists.
  35. @code{Define-imported-binding} creates a new such binding, anomalously
  36. from within Scheme; such bindings are usually created instead from
  37. within C using the C @code{s48_define_exported_binding} function.
  38. @code{Undefine-imported-binding} removes the shared binding whose name
  39. is @var{name} from the table of imported bindings.
  40. @end deffn
  41. @cindex exporting bindings from Scheme to C
  42. @deffn {Scheme procedure} lookup-exported-binding name @returns{} shared-binding
  43. @deffnx {Scheme procedure} define-exported-binding name value @returns{} unspecified
  44. @deffnx {Scheme procedure} undefine-exported-binding name @returns{} unspecified
  45. Equivalents of the above three procedures, but for bindings exported
  46. from Scheme to C. @code{Define-imported-binding}, unlike
  47. @code{define-exported-binding}, is customary to use in Scheme, as its
  48. intended use is to make a Scheme value available to C code from within
  49. Scheme.
  50. @end deffn
  51. @deffn {Scheme procedure} find-undefined-imported-bindings @returns{} vector
  52. Returns a vector of all bindings imported into Scheme from C with
  53. undefined values, @ie{} those created implicitly by lookups that have
  54. not yet been assigned rather than those created explicitly by the
  55. shared binding definers (@code{define-exported-binding}, @etc{}).
  56. @end deffn
  57. @subsection C shared binding interface
  58. @deftypefn {C macro} s48_value S48_SHARED_BINDING_P (s48_value @var{obj})
  59. @deftypefnx {C macro} s48_value S48_SHARED_BINDING_NAME (s48_value @var{shared_binding})
  60. @deftypefnx {C macro} s48_value S48_SHARED_BINDING_IS_IMPORTP (s48_value @var{shared-binding})
  61. @deftypefnx {C macro} s48_value S48_SHARED_BINDING_REF (s48_value @var{shared_binding})
  62. @deftypefnx {C macro} void S48_SHARED_BINDING_SET (s48_value @var{shared_binding}, s48_value @var{value})
  63. These macros are C counterparts to Scheme's @code{shared-binding?},
  64. @code{shared-binding-name}, @code{shared-binding-is-import?},
  65. @code{shared-binding-ref}, and @code{shared-binding-set!},
  66. respectively.
  67. @end deftypefn
  68. @deftypefn {C macro} @i{statement} S48_SHARED_BINDING_CHECK (s48_value @var{binding})
  69. Signals an exception if and only if @var{binding}'s value is
  70. Scheme48's `unspecific' value.
  71. @strong{Huh?:} Undefined shared bindings are not initialized with the
  72. `unspecific' value, but rather with an entirely different special
  73. token referred to internally as `undefined,' used in circumstances
  74. such as this --- yet @code{S48_SHARED_BINDING_CHECK}, as defined in
  75. @file{scheme48.h}, definitely checks whether @var{binding}'s value is
  76. the `unspecific' value.
  77. @end deftypefn
  78. @cindex importing bindings into C from Scheme
  79. @deftypefn {C function} s48_value s48_get_imported_binding (char *@var{name})
  80. Returns the shared binding defined in Scheme for @var{name}, creating
  81. it if necessary.
  82. @end deftypefn
  83. @cindex exporting bindings from C to Scheme
  84. @deftypefn {C function} void s48_define_exported_binding (char *@var{name}, s48_value @var{value})
  85. Defines a shared binding named @var{name} with the value @var{value}
  86. that can be accessed from Scheme.
  87. @end deftypefn
  88. @cindex exporting C functions to Scheme
  89. @deftypefn {C macro} void S48_EXPORT_FUNCTION (@var{fn})
  90. This is a convenience for the common case of exporting a C function to
  91. Scheme. This expands into
  92. @example
  93. s48_define_exported_binding("@var{fn}",
  94. s48_enter_pointer(@var{fn}))@end example
  95. @noindent
  96. which boxes the function into a Scheme48 byte vector and then exports
  97. it as a shared binding. Note that @code{s48_enter_pointer} allocates
  98. space in the Scheme heap and may trigger a garbage collection;
  99. @pxref{Interacting with the Scheme heap in C}.
  100. @end deftypefn