Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: RELEASE_5_10_0
Fetching contributors…

Cannot retrieve contributors at this time

496 lines (331 sloc) 12.393 kb
/*
Copyright (C) 2001-2012, Parrot Foundation.
=head1 NAME
src/pmc/pmcproxy.pmc - proxy class object for a PMC enabling introspection
=head1 DESCRIPTION
This class is used to describe a PMC. It can sit in an inheritance hierarchy
of a PDD15 class as well as allowing introspection of the PMC.
PMCProxy is not derived from any other PMC.
=head2 Structure
This class stores its state in the Parrot_Class structure, using the
following fields.
=over 4
=item C<id>
The type number of the PMC that is being described.
=item C<name>
The name of the PMC -- a STRING.
=item C<namespace>
The namespace the PMC is associated with, if any.
=item C<parents>
An array of immediate parent classes.
An empty ResizablePMCArray PMC is allocated during initialization.
=item C<all_parents>
A cached array of ourself and all parent classes, in MRO order.
A ResizablePMCArray PMC is allocated during initialization,
and is populated with the current class.
=back
NOTE: No doubt we will need other things later; this is a start.
=cut
*/
#include "parrot/oo_private.h"
/*
=head2 Functions
=over 4
=cut
*/
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* HEADERIZER END: static */
pmclass PMCProxy extends Class auto_attrs {
/*
=item C<void init()>
Initializes a PMCProxy PMC.
=cut
*/
VTABLE void init() {
Parrot_PMCProxy_attributes * const _pmc = PARROT_PMCPROXY(SELF);
STRING * const name = CONST_STRING(INTERP, "proxy");
PMC * const new_attribute
= Parrot_pmc_new(INTERP, enum_class_Hash);
SUPER();
/* Set up the attribute storage for the proxy instance */
VTABLE_set_string_keyed_str(INTERP, new_attribute, CONST_STRING(INTERP, "name"), name);
VTABLE_set_pmc_keyed_str(INTERP, _pmc->attrib_metadata, name, new_attribute);
CLASS_is_anon_CLEAR(SELF);
}
VTABLE void init_int(INTVAL type_num) {
Parrot_Class_attributes *proxy_info;
INTVAL mro_length, i;
/* Ensure that it's a valid type number. */
if (type_num > INTERP->n_vtable_max || type_num <= 0)
Parrot_ex_throw_from_c_args(INTERP, NULL,
EXCEPTION_INVALID_OPERATION,
"Attempt to create PMC Proxy for invalid type number = %d!", type_num);
/* Set up the object. */
SELF.init();
/* Set up number, name and namespace. */
proxy_info = PARROT_CLASS(SELF);
proxy_info->id = type_num;
proxy_info->name = INTERP->vtables[type_num]->whoami;
proxy_info->_namespace = INTERP->vtables[type_num]->_namespace;
/* Build MRO (skip ourself). */
mro_length = VTABLE_elements(INTERP, INTERP->vtables[type_num]->mro);
for (i = 1; i < mro_length; ++i) {
PMC * const pclass = VTABLE_get_pmc_keyed_int(INTERP,
INTERP->vtables[type_num]->mro, i);
PMC * const cns = pclass->vtable->_namespace;
STRING * const cname = pclass->vtable->whoami;
if (!STRING_equal(INTERP, cname, CONST_STRING(INTERP, "scalar"))) {
PMC * const pproxy = Parrot_oo_get_class(INTERP, cns);
VTABLE_push_pmc(INTERP, proxy_info->all_parents, pproxy);
}
}
/* PMCs just do single inheritance, so we'll assume that if we have a
* second entry in our MRO, it goes in the parents list. */
if (VTABLE_elements(INTERP, proxy_info->all_parents) >= 2)
VTABLE_push_pmc(INTERP, proxy_info->parents,
VTABLE_get_pmc_keyed_int(INTERP, proxy_info->all_parents, 1));
if (!PMC_IS_NULL(proxy_info->_namespace) &&
PMC_IS_NULL(VTABLE_get_class(INTERP, proxy_info->_namespace))) {
/* Link the proxy and the namespace, caching the proxy object for
* later retrieval on class lookup. */
Parrot_pcc_invoke_method_from_c_args(INTERP, proxy_info->_namespace, CONST_STRING(INTERP, "set_class"), "P->", SELF);
/* Extract any methods from the namespace */
Parrot_oo_extract_methods_from_namespace(INTERP, SELF,
proxy_info->_namespace);
}
{ /* Create inherited attributes */
/* Each attribute is prefixed with his type
* and terminated by an space,
* the list is '\0' terminated
*/
const char *attr = INTERP->vtables[type_num]->attribute_defs;
while (*attr) {
const char * const current = attr + 1;
const char attrtype = *attr;
while (*attr != ' ')
++attr;
if (attrtype != ':') {
const size_t n = attr - current;
STRING * const sattr = Parrot_str_new(INTERP, current, n);
SELF.add_attribute(sattr, NULL);
}
++attr;
}
}
}
VTABLE void init_pmc(PMC *init_data) {
const INTVAL type_num = VTABLE_get_integer(INTERP, init_data);
SELF.init_int(type_num);
}
/*
=item C<PMC *instantiate(PMC *init)>
Creates an instance of the PMC.
=cut
*/
VTABLE PMC *instantiate(PMC *init) {
const Parrot_Class_attributes * const _pmc = PARROT_CLASS(SELF);
if (!PMC_IS_NULL(init))
return Parrot_pmc_new_init(INTERP, _pmc->id, init);
return Parrot_pmc_new(INTERP, _pmc->id);
}
/*
=item C<INTVAL isa_pmc(PMC *classname)>
Returns whether the class is or inherits from C<*classname>.
=cut
*/
VTABLE INTVAL isa_pmc(PMC *lookup) {
Parrot_Class_attributes * const _proxy = PARROT_CLASS(SELF);
INTVAL i, num_classes;
const STRING *classname;
PMC *classobj;
if (PMC_IS_NULL(lookup))
return 0;
if (SUPER(lookup))
return 1;
classobj = Parrot_oo_get_class(INTERP, lookup);
if (PMC_IS_NULL(classobj))
return 0;
classname = VTABLE_get_string(INTERP, classobj);
/* Check if the passed name is the same as the stored short name. */
if (STRING_equal(INTERP, classname, _proxy->name))
return 1;
/* Check if the class object is the same as self's class object */
if (VTABLE_is_same(INTERP, SELF, classobj))
return 1;
/* Check if the passed name is the same as the fully qualified name. */
if (STRING_equal(INTERP, classname, VTABLE_get_string(INTERP, SELF)))
return 1;
/* Look in the isa hash. */
if (INTERP->vtables[_proxy->id]) {
const Hash * const isa_hash = INTERP->vtables[_proxy->id]->isa_hash;
PARROT_ASSERT(isa_hash);
if (Parrot_hash_exists(INTERP, isa_hash, classname))
return 1;
}
/* Iterate over all the parents and check if they respond true
* for 'isa' on the original comparison. */
num_classes = VTABLE_elements(INTERP, _proxy->parents);
for (i = 0; i < num_classes; ++i) {
PMC * const cur_class = VTABLE_get_pmc_keyed_int(INTERP,
_proxy->parents, i);
if (VTABLE_isa_pmc(INTERP, cur_class, lookup))
return 1;
}
return 0;
}
/*
=item C<INTVAL isa(STRING *classname)>
Returns whether the class is or inherits from C<*classname>.
=cut
*/
VTABLE INTVAL isa(STRING *classname) {
const Parrot_Class_attributes * const _proxy = PARROT_CLASS(SELF);
if (STRING_equal(INTERP, classname, CONST_STRING(INTERP, "PMCProxy")))
return 1;
if (SUPER(classname))
return 1;
/* Look in the isa hash. */
if (INTERP->vtables[_proxy->id]) {
const Hash * const isa_hash = INTERP->vtables[_proxy->id]->isa_hash;
if (!isa_hash && STRING_equal(INTERP,
INTERP->vtables[_proxy->id]->whoami, classname)) {
return 1;
}
if (isa_hash && Parrot_hash_exists(INTERP, isa_hash, classname))
return 1;
}
return 0;
}
/*
=item C<INTVAL does(STRING *role_name)>
Returns whether the class does the role with the given C<*rolename>.
=cut
*/
VTABLE INTVAL does(STRING *role_name) {
const INTVAL id = PARROT_CLASS(SELF)->id;
const INTVAL type_does = Parrot_pmc_type_does(INTERP, role_name, id);
if (type_does)
return 1;
return VTABLE_isa(INTERP, SELF, role_name);
}
/*
=item C<INTVAL type()>
Returns the integer type of the class.
=cut
*/
VTABLE INTVAL type() {
UNUSED(INTERP)
const Parrot_Class_attributes * const _class = PARROT_CLASS(SELF);
return _class->id;
}
/*
=item C<PMC *inspect()>
Returns a Hash describing the PMC, with key/value pairs as described in
inspect_str.
=cut
*/
VTABLE PMC *inspect()
{
/* Create a hash, then use inspect_str to get all of the data to
* fill it up with. */
STRING * const name_str = CONST_STRING(INTERP, "name");
STRING * const ns_str = CONST_STRING(INTERP, "namespace");
STRING * const meth_str = CONST_STRING(INTERP, "methods");
STRING * const parents_str = CONST_STRING(INTERP, "parents");
PMC * const metadata = Parrot_pmc_new(INTERP, enum_class_Hash);
VTABLE_set_pmc_keyed_str(INTERP, metadata, name_str,
VTABLE_inspect_str(INTERP, SELF, name_str));
VTABLE_set_pmc_keyed_str(INTERP, metadata, ns_str,
VTABLE_inspect_str(INTERP, SELF, ns_str));
VTABLE_set_pmc_keyed_str(INTERP, metadata, meth_str,
VTABLE_inspect_str(INTERP, SELF, meth_str));
VTABLE_set_pmc_keyed_str(INTERP, metadata, parents_str,
VTABLE_inspect_str(INTERP, SELF, parents_str));
return metadata;
}
/*
=item C<STRING *name()>
Gets the name of the PMC.
=cut
*/
METHOD name() {
Parrot_Class_attributes * const _pmc = PARROT_CLASS(SELF);
STRING * const ret_name = _pmc->name;
RETURN(STRING *ret_name);
}
/*
=item C<void get_namespace()>
Gets the namespace that this class is attached to.
=cut
*/
METHOD get_namespace() {
Parrot_Class_attributes * const _pmc = PARROT_CLASS(SELF);
PMC * const ret_namespace = _pmc->_namespace;
RETURN(PMC *ret_namespace);
}
/*
=item C<void new(PMC *args)>
Creates an instance of the PMC.
=cut
*/
METHOD new(PMC *args :optional, int got_args :opt_flag) {
PMC * const obj = VTABLE_instantiate(INTERP, SELF, args);
UNUSED(got_args);
RETURN(PMC *obj);
}
/*
=item C<void methods()>
Return a hash where the keys are method names and the values are methods.
=cut
*/
METHOD methods() {
PMC * const ret_methods = VTABLE_inspect_str(INTERP, SELF, CONST_STRING(INTERP, "methods"));
RETURN(PMC *ret_methods);
}
/*
=item C<void parents()>
Return the parents array PMC.
=cut
*/
METHOD parents() {
PMC * const ret_parents = VTABLE_inspect_str(INTERP, SELF, CONST_STRING(INTERP, "parents"));
RETURN(PMC *ret_parents);
}
/*
=item C<void inspect(STRING *what :optional)>
Gets all introspection data for the PMC or, if the optional string
parameter is supplied, a particular item of introspection data.
=cut
*/
METHOD inspect(STRING *what :optional, int got_what :opt_flag) {
/* Just delegate to the appropriate vtable. */
PMC * const found =
got_what
? VTABLE_inspect_str(INTERP, SELF, what)
: VTABLE_inspect(INTERP, SELF);
RETURN(PMC *found);
}
/*
=item C<STRING *get_string()>
Return the name of the low-level class (without the HLL namespace).
=cut
*/
VTABLE STRING *get_string() {
UNUSED(INTERP)
const Parrot_Class_attributes * const proxy_info = PARROT_CLASS(SELF);
return proxy_info->name;
}
} /* END pmclass */
/*
=back
=head1 SEE ALSO
F<docs/pdds/pdd17_pmc.pod>.
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/
Jump to Line
Something went wrong with that request. Please try again.