Next: Library Functions
Up: MzScheme Architecture
Previous: Library Functions
Primitive units can be created by allocating an instance of
the Scheme_Unit data type:
typedef struct Scheme_Unit {
Scheme_Type type; /* = scheme_unit_type */
short num_imports;
short num_exports;
Scheme_Object **exports;
Scheme_Object **export_debug_names; /* NULL */
Scheme_Object *(*init_func)(Scheme_Object **boxes, Scheme_Object **anchors,
struct Scheme_Unit *m, void *debug_request);
Scheme_Object *data;
} Scheme_Unit;
The fields are filled as follows:
- The type field is always scheme_unit_type.
- The num_imports field specifies the number of variables
imported by the unit and the num_exports field specifies the
number of variables exported.
- Exported variables are named; the exports field must point
to an array of symbols for the variable names.
- The export_debug_names field is NULL for primitive units.
- The init_func field points to a function that is called
when the unit is instantiated. (A single unit can be instantiated
multiple times.) The first argument to this procedure is an array of
boxes for import and export variables (import variables first); the
value of an imported or exported variable is the value in the
corresponding box, accessed or set with the SCHEME_ENVBOX_VAL
macro. Boxes for imported variables should never be mutated. Boxes
for exported variables will be initialized to scheme_undefined
and should be properly initialized by the init_func function.
The second argument to init_func is an array of anchor pointers
associated with the boxes in the first argument. Whenever a box
pointer is kept, the corresponding anchor pointer must also be kept to
keep the box from being collected as garbage. Note that the anchor is
for the box itself, not the value within the box.
The final argument to init_func should be ignored.
The return value of init_func corresponds to the value
of the last expression in the body of a Scheme-based unit.
- The data field is not used directly by MzScheme; it is
available to store unit-specific data needed by init_func.
PLT