record.texi 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. @node Using Scheme records in C
  2. @section Using Scheme records in C
  3. External C code can create records and access record slots positionally
  4. using these functions & macros. Note, however, that named access to
  5. record fields is not supported, only indexed access, so C code must be
  6. synchronized carefully with the corresponding Scheme that defines
  7. record types.
  8. @deftypefn {C function (may GC)} s48_value s48_make_record (s48_value @var{record-type})
  9. @deftypefnx {C macro} int S48_RECORD_P (s48_value @var{object})
  10. @deftypefnx {C macro} s48_value S48_RECORD_TYPE (s48_value @var{record})
  11. @deftypefnx {C macro} s48_value S48_RECORD_REF (s48_value @var{record}, long @var{index})
  12. @deftypefnx {C macro} void S48_RECORD_SET (s48_value @var{record}, long @var{index}, s48_value @var{value})
  13. @deftypefnx {C function} void s48_check_record_type (s48_value @var{record}, s48_value @var{type-binding})
  14. @code{s48_make_record} allocates a record on Scheme's heap with the
  15. given record type; its arguments must be a shared binding whose value
  16. is a record type descriptor (@pxref{Records}). @code{S48_RECORD_P}
  17. is the type predicate for records. @code{S48_RECORD_TYPE} returns the
  18. record type descriptor of @var{record}. @code{S48_RECORD_REF} &
  19. @code{S48_RECORD_SET} operate on records similarly to how
  20. @code{S48_VECTOR_REF} & @code{S48_VECTOR_SET} work on vectors.
  21. @code{s48_check_record_type} checks whether @var{record} is a record
  22. whose type is the value of the shared binding @var{type_binding}. If
  23. this is not the case, it signals an exception. (It also signals an
  24. exception if @var{type_binding}'s value is not a record.) Otherwise,
  25. it returns normally.
  26. @end deftypefn
  27. For example, with this record type definition:
  28. @lisp
  29. (define-record-type thing :thing
  30. (make-thing a b)
  31. thing?
  32. (a thing-a)
  33. (b thing-b))@end lisp
  34. @noindent
  35. the identifier @code{:thing} is bound to the record type and can be
  36. exported to C thus:
  37. @lisp
  38. (define-exported-binding "thing-record-type" :thing)@end lisp
  39. @noindent
  40. and @code{thing} records can be made in C:
  41. @example
  42. static s48_value thing_record_type = S48_FALSE;
  43. void initialize_things(void)
  44. @{
  45. S48_GC_PROTECT_GLOBAL(thing_record_type);
  46. thing_record_type = s48_get_imported_binding("thing-record-type");
  47. @}
  48. s48_value make_thing(s48_value a, s48_value b)
  49. @{
  50. s48_value thing;
  51. S48_DECLARE_GC_PROTECT(2);
  52. S48_GC_PROTECT_2(a, b);
  53. thing = s48_make_record(thing_record_type);
  54. S48_RECORD_SET(thing, 0, a);
  55. S48_RECORD_SET(thing, 1, b);
  56. S48_GC_UNPROTECT();
  57. return thing;
  58. @}@end example
  59. @noindent
  60. Note that the variables @code{a} & @code{b} must be protected against
  61. the possibility of a garbage collection occurring during the call to
  62. @code{s48_make_record}.