Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Setup the very basic KnowHOWAttribute in 6model core rather than leav…
…ing every HLL the problem of setting up an initial one. It's especially a pain if you're going to have a compile time meta-model and need the attribute meta-object available to the compiler. (FWIW, nqpclr ended up with this in core too, though with a different initial motivation.)
  • Loading branch information
jnthn committed Apr 27, 2011
1 parent 0ff3ff4 commit cc013b6
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 6 deletions.
77 changes: 77 additions & 0 deletions src/6model/knowhow_bootstrapper.c
Expand Up @@ -220,3 +220,80 @@ PMC * SixModelObject_bootstrap_knowhow(PARROT_INTERP, PMC *sc) {

return knowhow_pmc;
}

/* Attribute new method. */
static void attr_new(PARROT_INTERP, PMC *nci) {
PMC *capture = Parrot_pcc_get_signature(interp, CURRENT_CONTEXT(interp));
PMC *type = VTABLE_get_pmc_keyed_int(interp, capture, 0);
STRING *name = VTABLE_get_string_keyed_str(interp, capture, name_str);
PMC *self = REPR(type)->instance_of(interp, REPR_PMC(type), type);
REPR(self)->set_str(interp, REPR_PMC(self), self, name);
Parrot_pcc_build_call_from_c_args(interp, capture, "P", self);
}

/* Attribute name introspection. */
static void attr_name(PARROT_INTERP, PMC *nci) {
PMC *capture = Parrot_pcc_get_signature(interp, CURRENT_CONTEXT(interp));
PMC *self = VTABLE_get_pmc_keyed_int(interp, capture, 0);
STRING *name = REPR(self)->get_str(interp, REPR_PMC(self), self);
Parrot_pcc_build_call_from_c_args(interp, capture, "S", name);
}

/* Sets up a very simple attribute meta-object. Just supports having a
* name, and even uses the P6str representation to store it, so that's
* really all that it supports. */
PMC * SixModelObject_setup_knowhow_attribute(PARROT_INTERP, PMC *sc, PMC *knowhow) {
PMC *old_ctx, *cappy, *meth, *knowhow_attr, *how;

/* Create a new KnowHOWAttribute type using P6str repr.. */
meth = STABLE(knowhow)->find_method(interp, knowhow,
Parrot_str_new_constant(interp, "new_type"), NO_HINT);
old_ctx = Parrot_pcc_get_signature(interp, CURRENT_CONTEXT(interp));
cappy = Parrot_pmc_new(interp, enum_class_CallContext);
VTABLE_push_pmc(interp, cappy, knowhow);
VTABLE_set_string_keyed_str(interp, cappy, name_str,
Parrot_str_new_constant(interp, "KnowHOWAttribute"));
VTABLE_set_string_keyed_str(interp, cappy, repr_str,
Parrot_str_new_constant(interp, "P6str"));
Parrot_pcc_invoke_from_sig_object(interp, meth, cappy);
cappy = Parrot_pcc_get_signature(interp, CURRENT_CONTEXT(interp));
Parrot_pcc_set_signature(interp, CURRENT_CONTEXT(interp), old_ctx);
knowhow_attr = VTABLE_get_pmc_keyed_int(interp, cappy, 0);
how = STABLE(knowhow_attr)->HOW;

/* Add new method. */
meth = STABLE(how)->find_method(interp, how,
Parrot_str_new_constant(interp, "add_method"), NO_HINT);
cappy = Parrot_pmc_new(interp, enum_class_CallContext);
VTABLE_push_pmc(interp, cappy, how);
VTABLE_push_pmc(interp, cappy, knowhow_attr);
VTABLE_push_string(interp, cappy, Parrot_str_new_constant(interp, "new"));
VTABLE_push_pmc(interp, cappy, wrap_c(interp, F2DPTR(attr_new)));
Parrot_pcc_invoke_from_sig_object(interp, meth, cappy);
Parrot_pcc_set_signature(interp, CURRENT_CONTEXT(interp), old_ctx);

/* Add name method. */
cappy = Parrot_pmc_new(interp, enum_class_CallContext);
VTABLE_push_pmc(interp, cappy, how);
VTABLE_push_pmc(interp, cappy, knowhow_attr);
VTABLE_push_string(interp, cappy, name_str);
VTABLE_push_pmc(interp, cappy, wrap_c(interp, F2DPTR(attr_name)));
Parrot_pcc_invoke_from_sig_object(interp, meth, cappy);
Parrot_pcc_set_signature(interp, CURRENT_CONTEXT(interp), old_ctx);

/* Compose. */
meth = STABLE(knowhow)->find_method(interp, how,
Parrot_str_new_constant(interp, "compose"), NO_HINT);
cappy = Parrot_pmc_new(interp, enum_class_CallContext);
VTABLE_push_pmc(interp, cappy, how);
VTABLE_push_pmc(interp, cappy, knowhow_attr);
Parrot_pcc_invoke_from_sig_object(interp, meth, cappy);
Parrot_pcc_set_signature(interp, CURRENT_CONTEXT(interp), old_ctx);

/* Associate the created object with the intial core serialization
* context. */
VTABLE_set_pmc_keyed_int(interp, sc, 2, knowhow_attr);
SC_PMC(knowhow_attr) = sc;

return knowhow_attr;
}
1 change: 1 addition & 0 deletions src/6model/knowhow_bootstrapper.h
@@ -1 +1,2 @@
PMC * SixModelObject_bootstrap_knowhow(PARROT_INTERP, PMC *sc);
PMC * SixModelObject_setup_knowhow_attribute(PARROT_INTERP, PMC *sc, PMC *knowhow);
9 changes: 6 additions & 3 deletions src/6model/sixmodelobject.c
Expand Up @@ -17,7 +17,7 @@ static STRING *find_method_str = NULL;
static STRING *type_check_str = NULL;

/* Initializes 6model and produces the KnowHOW core meta-object. */
PMC * SixModelObject_initialize(PARROT_INTERP) {
void SixModelObject_initialize(PARROT_INTERP, PMC **knowhow, PMC **knowhow_attribute) {
PMC *initial_sc;
STRING *initial_sc_name;

Expand All @@ -38,8 +38,11 @@ PMC * SixModelObject_initialize(PARROT_INTERP) {
/* Build representations and initializes the representation registry. */
REPR_initialize_registry(interp);

/* Bootstrap the KnowHOW and return it. */
return SixModelObject_bootstrap_knowhow(interp, initial_sc);
/* Bootstrap the KnowHOW. */
*knowhow = SixModelObject_bootstrap_knowhow(interp, initial_sc);

/* Set up the simple KnowHOWAttribute. */
*knowhow_attribute = SixModelObject_setup_knowhow_attribute(interp, initial_sc, *knowhow);
}

/* Takes a representation and wraps it up in a REPR PMC. */
Expand Down
2 changes: 1 addition & 1 deletion src/6model/sixmodelobject.h
Expand Up @@ -166,7 +166,7 @@ typedef struct {
#define REPR_STRUCT(p) ((REPRCommonalities *)PMC_data(p))

/* Object model initialization. */
PMC * SixModelObject_initialize(PARROT_INTERP);
void SixModelObject_initialize(PARROT_INTERP, PMC **knowhow, PMC **knowhow_attribute);

/* Some utility functions. */
PMC * wrap_repr(PARROT_INTERP, void *REPR);
Expand Down
23 changes: 21 additions & 2 deletions src/ops/nqp.ops
Expand Up @@ -25,8 +25,9 @@ static INTVAL smo_id = 0;
static INTVAL disp_id = 0;
static INTVAL ms_id = 0;

/* Built-in HOWs (meta-objects). */
/* Built-in meta-objects. */
static PMC *KnowHOW = NULL;
static PMC *KnowHOWAttribute = NULL;

END_OPS_PREAMBLE

Expand All @@ -51,7 +52,7 @@ inline op nqp_dynop_setup() :base_core {
ms_id = pmc_type(interp, Parrot_str_new(interp, "NQPMultiSig", 0));

/* Initialize the object model. */
KnowHOW = SixModelObject_initialize(interp);
SixModelObject_initialize(interp, &KnowHOW, &KnowHOWAttribute);
}
}

Expand All @@ -74,6 +75,24 @@ inline op get_knowhow(out PMC) :base_core {
}


/*

=item get_knowhow_attribute()

Returns the 6model core meta-attribute, KnowHOWAttribute.

=cut

*/
inline op get_knowhow_attribute(out PMC) :base_core {
if (KnowHOWAttribute)
$1 = KnowHOWAttribute;
else
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"6model not yet initialized; cannot use get_knowhow_attribute");
}


/*

=item get_how(obj)
Expand Down

0 comments on commit cc013b6

Please sign in to comment.