Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
A little infrastructure for dynamically registering 6model representa…
…tions outside of the 6model core.
  • Loading branch information
jnthn committed Oct 28, 2011
1 parent 65f926d commit 22f49c1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/6model/repr_registry.c
Expand Up @@ -37,14 +37,23 @@ static void register_repr(PARROT_INTERP, STRING *name, REPROps *repr) {
VTABLE_set_integer_keyed_str(interp, repr_name_to_id_map, name, ID);
}

/* Dynamically registers a representation (that is, one defined outside of
* the 6model core). */
void REPR_register_dynamic(PARROT_INTERP, STRING *name, REPROps * (*reg) (PARROT_INTERP, void *, void *)) {
REPROps *repr = reg(interp, wrap_object, create_stable);
register_repr(interp, name, repr);
}

/* Initializes the representations registry, building up all of the various
* representations. */
void REPR_initialize_registry(PARROT_INTERP) {
PMC *dyn_reg_func;

/* Allocate name to ID map, and anchor it with the GC. */
repr_name_to_id_map = pmc_new(interp, enum_class_Hash);
Parrot_pmc_gc_register(interp, repr_name_to_id_map);

/* Add all representations. */
/* Add all core representations. */
register_repr(interp, Parrot_str_new_constant(interp, "KnowHOWREPR"),
KnowHOWREPR_initialize(interp));
register_repr(interp, Parrot_str_new_constant(interp, "P6opaque"),
Expand All @@ -59,6 +68,12 @@ void REPR_initialize_registry(PARROT_INTERP) {
HashAttrStore_initialize(interp));
register_repr(interp, Parrot_str_new_constant(interp, "Uninstantiable"),
Uninstantiable_initialize(interp));

/* Set up object for dynamically registering extra representations. */
dyn_reg_func = pmc_new(interp, enum_class_Pointer);
VTABLE_set_pointer(interp, dyn_reg_func, REPR_register_dynamic);
VTABLE_set_pmc_keyed_str(interp, interp->root_namespace,
Parrot_str_new_constant(interp, "_REGISTER_REPR"), dyn_reg_func);
}

/* Get a representation's ID from its name. Note that the IDs may change so
Expand Down
8 changes: 8 additions & 0 deletions src/6model/sixmodelobject.h
Expand Up @@ -263,4 +263,12 @@ PMC * wrap_object(PARROT_INTERP, void *obj);
PMC * create_stable(PARROT_INTERP, REPROps *REPR, PMC *HOW);
PMC * decontainerize(PARROT_INTERP, PMC *var);

/* Dynamic representation registration. */
typedef void (* rf) (PARROT_INTERP, STRING *name, REPROps * (*reg) (PARROT_INTERP, void *, void *));
#define REGISTER_DYNAMIC_REPR(interp, name, reg_func) \
((rf) \
VTABLE_get_pointer(interp, \
VTABLE_get_pmc_keyed_str(interp, interp->root_namespace, \
Parrot_str_new_constant(interp, "_REGISTER_REPR"))))(interp, name, reg_func)

#endif

0 comments on commit 22f49c1

Please sign in to comment.