Skip to content

Commit

Permalink
Parrot's Class.attributes method no longer returns attribute
Browse files Browse the repository at this point in the history
names in the same order in which they were declared (indeed,
the order is somewhat unpredictable).  Since we need to initialize
attributes in the order they were declared, add a @!attribute_list
property and 'attriter' method to the Class PMC that allows Rakudo
keep track of the order in which attributes are declared.
  • Loading branch information
pmichaud committed Jul 19, 2009
1 parent faf91f9 commit 02dd257
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/builtins/guts.pir
Expand Up @@ -704,6 +704,12 @@ and C<type>.
$I0 = exists $P0[name]
if $I0 goto attr_exists
addattribute metaclass, name
$P1 = getprop '@!attribute_list', metaclass
unless null $P1 goto have_attrlist
$P1 = root_new ['parrot';'ResizableStringArray']
setprop metaclass, '@!attribute_list', $P1
have_attrlist:
push $P1, name
$P0 = metaclass.'attributes'()
attr_exists:

Expand Down
6 changes: 3 additions & 3 deletions src/classes/Object.pir
Expand Up @@ -44,7 +44,7 @@ like this.
parrotclass = p6meta.'get_parrotclass'(result)
if null parrotclass goto attrinit_done
attributes = inspect parrotclass, 'attributes'
it = iter attributes
it = parrotclass.'attriter'()
attrinit_loop:
unless it goto attrinit_done
.local string attrname, shortname
Expand Down Expand Up @@ -298,7 +298,7 @@ the object's type and address.
parentproto = find_caller_lex '$CLASS'
parrotclass = p6meta.'get_parrotclass'(parentproto)
attributes = inspect parrotclass, 'attributes'
it = iter attributes
it = parrotclass.'attriter'()
attrinit_loop:
unless it goto attrinit_done
.local string attrname, keyname
Expand Down Expand Up @@ -422,7 +422,7 @@ XXX This had probably best really just tailcall .^CREATE; move this stuff later.
unless class_it goto classinit_loop_end
cur_class = shift class_it
attributes = inspect cur_class, 'attributes'
it = iter attributes
it = cur_class.'attriter'()
attrinit_loop:
unless it goto attrinit_done
.local string attrname
Expand Down
22 changes: 22 additions & 0 deletions src/parrot/misc.pir
Expand Up @@ -80,3 +80,25 @@ Gets the name of the routine.
done:
.return ($S0)
.end


=item Class.attriter

Return an iterator that iterates over a Class' attributes.
If the Class object has a @!attribute_list property, use
that as the order of attributes, otherwise introspect the
class and use its list. (As of Parrot 1.4.0 we can't
always introspect the class directly, as the order of
attributes in the class isn't guaranteed.)
=cut
.namespace ['Class']
.sub 'attriter' :method
$P0 = getprop '@!attribute_list', self
unless null $P0 goto have_list
$P0 = inspect self, 'attributes'
have_list:
$P1 = iter $P0
.return ($P1)
.end

0 comments on commit 02dd257

Please sign in to comment.