Scheme 48 Manual -- Using Scheme records in C code

Scheme 48 Manual | Contents | In Chapter: Mixing Scheme 48 and C
Previous: Using Scheme records in C code | Next: Using Scheme records in C code

## Using Scheme records in C code

External modules can create records and access their slots positionally.

- `s48_value S48_MAKE_RECORD(s48_value)` | (may GC)

- `int S48_RECORD_P(s48_value)`
- `s48_value S48_RECORD_TYPE(s48_value)`
- `s48_value S48_RECORD_REF(s48_value, long)`
- `void S48_RECORD_SET(s48_value, long, s48_value)`

The argument to `S48_MAKE_RECORD` should be a shared binding whose value is a record type. In C the fields of Scheme records are only accessible via offsets, with the first field having offset zero, the second offset one, and so forth. If the order of the fields is changed in the Scheme definition of the record type the C code must be updated as well.

For example, given the following 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:

>     (define-exported-binding "thing-record-type" :thing)

`Thing` records can then be made in C:

>     static scheme_value
>       thing_record_type_binding = SCHFALSE;

>     void initialize_things(void)
>     {
>       S48_GC_PROTECT_GLOBAL(thing_record_type_binding);
>       thing_record_type_binding =
>          s48_get_imported_binding("thing-record-type");
>     }

>     scheme_value make_thing(scheme_value a, scheme_value b)
>     {
>       s48_value thing;
>       s48_DECLARE_GC_PROTECT(2);

>       S48_GC_PROTECT_2(a, b);

>       thing = s48_make_record(thing_record_type_binding);
>       S48_RECORD_SET(thing, 0, a);
>       S48_RECORD_SET(thing, 1, b);

>       S48_GC_UNPROTECT();

>       return thing;
>     }

Note that the variables `a` and `b` must be protected against the possibility of a garbage collection occuring during the call to `s48_make_record()`.

Previous: Using Scheme records in C code | Next: Using Scheme records in C code