Next: Raising exceptions from C, Previous: Interacting with the Scheme heap in C, Up: C interface
External C code can create records and access record slots positionally using these functions & macros. Note, however, that named access to record fields is not supported, only indexed access, so C code must be synchronized carefully with the corresponding Scheme that defines record types.
s48_make_recordallocates a record on Scheme's heap with the given record type; its arguments must be a shared binding whose value is a record type descriptor (see Records).S48_RECORD_Pis the type predicate for records.S48_RECORD_TYPEreturns the record type descriptor of record.S48_RECORD_REF&S48_RECORD_SEToperate on records similarly to howS48_VECTOR_REF&S48_VECTOR_SETwork on vectors.s48_check_record_typechecks whether record is a record whose type is the value of the shared binding type_binding. If this is not the case, it signals an exception. (It also signals an exception if type_binding's value is not a record.) Otherwise, it returns normally.
For example, with this record type definition:
(define-record-type thing :thing
(make-thing a b)
thing?
(a thing-a)
(b thing-b))
the identifier :thing is bound to the record type and can be
exported to C thus:
(define-exported-binding "thing-record-type" :thing)
and thing records can be made in C:
static s48_value thing_record_type = S48_FALSE;
void initialize_things(void)
{
S48_GC_PROTECT_GLOBAL(thing_record_type);
thing_record_type = s48_get_imported_binding("thing-record-type");
}
s48_value make_thing(s48_value a, s48_value b)
{
s48_value thing;
S48_DECLARE_GC_PROTECT(2);
S48_GC_PROTECT_2(a, b);
thing = s48_make_record(thing_record_type);
S48_RECORD_SET(thing, 0, a);
S48_RECORD_SET(thing, 1, b);
S48_GC_UNPROTECT();
return thing;
}
Note that the variables a & b must be protected against
the possibility of a garbage collection occurring during the call to
s48_make_record.