Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
implement NQPMu.{new,BUILDALL,MAGIC_BUILD}. Tests.
The latter is a hack to get attribute initialization working in classes that
don't define their own BUILD submethod.

Lack automatic BUILD calling.
  • Loading branch information
moritz committed Feb 16, 2011
1 parent 86697e2 commit 99575b9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/metamodel/how/NQPMu.pm
Expand Up @@ -3,8 +3,31 @@ class NQPMu {
pir::repr_instance_of__PP(self)
}

method new() {
self.CREATE()

method bless(NQPMu:U $self: *%attributes) {
my $instance := self.CREATE();
$instance.BUILDALL(|%attributes);
$instance
}

method BUILDALL(NQPMu:D $self: *%attributes) {
for $self.HOW.parents($self) -> $class {
$self.BUILD_MAGIC($class, |%attributes);
}
}

method BUILD_MAGIC(NQPMu:D $self: $type, *%attributes) {
for $type.HOW.attributes($type, :local) {
my $name := $_.name;
my $shortname := pir::substr($name, 2);
if pir::exists(%attributes, $shortname) {
pir::setattribute__vPPsP($self, $type, $name, %attributes{$shortname});
}
}
}

method new(*%attributes) {
self.bless(|%attributes);
}

proto method Str() is parrot_vtable('get_string') { * }
Expand Down
16 changes: 16 additions & 0 deletions t/nqp/57-construction.t
@@ -0,0 +1,16 @@
plan(2);

class Parent {
has $!bar;

method bar() { $!bar }
}
class A is Parent {
has @!foo;

method foo() { @!foo }
}

my $x := A.bless(foo => [1, 2, 3], bar => 'BARBAR');
ok(pir::join('|', $x.foo) eq '1|2|3', '.new() initializes child class attribute');
ok($x.bar eq 'BARBAR', '.new() initializes parent class attribute');

0 comments on commit 99575b9

Please sign in to comment.