Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
Start to get the low-level method finding in place, including stubbin…
Browse files Browse the repository at this point in the history
…g in what'll eventually support lookups optimized by gradual typing.
  • Loading branch information
jnthn committed Nov 20, 2010
1 parent c0740a4 commit cf50438
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/metamodel/rakudoobject.c
Expand Up @@ -10,12 +10,16 @@ static INTVAL stable_id = 0;
static INTVAL repr_id = 0;
static INTVAL ro_id = 0;

/* Cached find_method string. */
static STRING *find_method_str = NULL;

/* Initializes the RakudoObject object model. */
void RakudoObject_initialize(PARROT_INTERP) {
/* Look up and cache some type IDs. */
stable_id = pmc_type(interp, Parrot_str_new(interp, "STable", 0));
repr_id = pmc_type(interp, Parrot_str_new(interp, "REPR", 0));
ro_id = pmc_type(interp, Parrot_str_new(interp, "RakudoObject", 0));
/* Look up and cache some type IDs and strings. */
stable_id = pmc_type(interp, Parrot_str_new(interp, "STable", 0));
repr_id = pmc_type(interp, Parrot_str_new(interp, "REPR", 0));
ro_id = pmc_type(interp, Parrot_str_new(interp, "RakudoObject", 0));
find_method_str = Parrot_str_new_constant(interp, "find_method");

/* Build representations and initializes the representation registry. */
REPR_initialize_registry(interp);
Expand All @@ -40,9 +44,33 @@ PMC * wrap_object(PARROT_INTERP, void *obj) {
return obj_pmc;
}

/* This is the default method dispatch code. It tries to use the
* v-table first, then falls back to a lookup. */
static PMC * default_find_method(PARROT_INTERP, PMC *obj, STRING *name, INTVAL hint) {
/* See if we can find it by hint. */
STable *st = STABLE(obj);
if (hint != NO_HINT && st->vtable && hint < st->vtable_length) {
/* Yes, just grab it from the v-table. */
return st->vtable[hint];
}
else
{
/* Find the finder - the find_method method. */
/* XXX Cache here, like in 6model. */
PMC *HOW = st->HOW;
PMC *meth = STABLE(HOW)->find_method(interp, HOW, find_method_str, NO_HINT);

/* Call it to get the method to call. */
PMC *result;
Parrot_ext_call(interp, meth, "PiPS->P", HOW, obj, name, &result);
return result;
}
}

/* Creates an STable that references the given REPR and HOW. */
PMC * create_stable(PARROT_INTERP, PMC *REPR, PMC *HOW) {
PMC *st_pmc = pmc_new_init(interp, stable_id, HOW);
STABLE_STRUCT(st_pmc)->REPR = REPR;
STABLE_STRUCT(st_pmc)->find_method = default_find_method;
return st_pmc;
}
3 changes: 3 additions & 0 deletions src/metamodel/rakudoobject.h
Expand Up @@ -23,6 +23,9 @@ typedef struct {
/* The type-object. */
PMC *WHAT;

/* The method finder. */
PMC * (*find_method) (PARROT_INTERP, PMC *obj, STRING *name, INTVAL hint);

/* The computed v-table for static dispatch. */
PMC **vtable;

Expand Down
4 changes: 4 additions & 0 deletions src/pmc/rakudoobject.pmc
Expand Up @@ -34,4 +34,8 @@ pmclass RakudoObject manual_attrs dynpmc group nqp {
/* Delegate to the representation. */
REPR(SELF)->gc_free(interp, REPR_PMC(SELF), SELF);
}

VTABLE PMC * find_method(STRING *name) {
STABLE(SELF)->find_method(interp, SELF, name, NO_HINT);
}
}

0 comments on commit cf50438

Please sign in to comment.