Skip to content

Commit 99575b9

Browse files
committed
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.
1 parent 86697e2 commit 99575b9

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/metamodel/how/NQPMu.pm

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,31 @@ class NQPMu {
33
pir::repr_instance_of__PP(self)
44
}
55

6-
method new() {
7-
self.CREATE()
6+
7+
method bless(NQPMu:U $self: *%attributes) {
8+
my $instance := self.CREATE();
9+
$instance.BUILDALL(|%attributes);
10+
$instance
11+
}
12+
13+
method BUILDALL(NQPMu:D $self: *%attributes) {
14+
for $self.HOW.parents($self) -> $class {
15+
$self.BUILD_MAGIC($class, |%attributes);
16+
}
17+
}
18+
19+
method BUILD_MAGIC(NQPMu:D $self: $type, *%attributes) {
20+
for $type.HOW.attributes($type, :local) {
21+
my $name := $_.name;
22+
my $shortname := pir::substr($name, 2);
23+
if pir::exists(%attributes, $shortname) {
24+
pir::setattribute__vPPsP($self, $type, $name, %attributes{$shortname});
25+
}
26+
}
27+
}
28+
29+
method new(*%attributes) {
30+
self.bless(|%attributes);
831
}
932

1033
proto method Str() is parrot_vtable('get_string') { * }

t/nqp/57-construction.t

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
plan(2);
2+
3+
class Parent {
4+
has $!bar;
5+
6+
method bar() { $!bar }
7+
}
8+
class A is Parent {
9+
has @!foo;
10+
11+
method foo() { @!foo }
12+
}
13+
14+
my $x := A.bless(foo => [1, 2, 3], bar => 'BARBAR');
15+
ok(pir::join('|', $x.foo) eq '1|2|3', '.new() initializes child class attribute');
16+
ok($x.bar eq 'BARBAR', '.new() initializes parent class attribute');

0 commit comments

Comments
 (0)