Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Initial implementation of is default(42) on any hash/array
  • Loading branch information
lizmat committed Aug 11, 2013
1 parent b03cc1c commit 948210e
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 33 deletions.
83 changes: 63 additions & 20 deletions src/core/Array.pm
Expand Up @@ -22,22 +22,42 @@ class Array { # declared in BOOTSTRAP
my int $p = nqp::unbox_i($pos.Int);
my Mu $items := nqp::p6listitems(self);
# hotpath check for element existence (RT #111848)
nqp::existspos($items, $p)
|| nqp::getattr(self, List, '$!nextiter').defined
&& self.exists($p)
?? nqp::atpos($items, $p)
!! nqp::p6bindattrinvres(my $v, Scalar, '$!whence',
-> { nqp::bindpos($items, $p, $v) } )
if nqp::existspos($items, $p)
|| nqp::getattr(self, List, '$!nextiter').defined
&& self.exists($p) {
nqp::atpos($items, $p);
}
else {
my $default := self.VAR.default;
nqp::p6bindattrinvres(
my $v = (nqp::istype($default,Any)
?? $default
!! (nqp::istype(self.VAR.of,Any) ?? self.VAR.of !! Any)),
Scalar,
'$!whence',
-> { nqp::bindpos($items, $p, $v) }
);
}
}
multi method at_pos(Array:D: int $pos) is rw {
my Mu $items := nqp::p6listitems(self);
# hotpath check for element existence (RT #111848)
nqp::existspos($items, $pos)
|| nqp::getattr(self, List, '$!nextiter').defined
&& self.exists($pos)
?? nqp::atpos($items, $pos)
!! nqp::p6bindattrinvres(my $v, Scalar, '$!whence',
-> { nqp::bindpos($items, $pos, $v) } )
if nqp::existspos($items, $pos)
|| nqp::getattr(self, List, '$!nextiter').defined
&& self.exists($pos) {
nqp::atpos($items, $pos);
}
else {
my $default := self.VAR.default;
nqp::p6bindattrinvres(
my $v = (nqp::istype($default,Any)
?? $default
!! (nqp::istype(self.VAR.of,Any) ?? self.VAR.of !! Any)),
Scalar,
'$!whence',
-> { nqp::bindpos($items, $pos, $v) }
);
}
}

proto method bind_pos(|) { * }
Expand Down Expand Up @@ -114,16 +134,39 @@ class Array { # declared in BOOTSTRAP
my role TypedArray[::TValue] does Positional[TValue] {
multi method at_pos($pos is copy, TValue $v? is copy) is rw {
$pos = $pos.Int;
self.exists($pos)
?? nqp::atpos(nqp::getattr(self, List, '$!items'), nqp::unbox_i($pos))
!! nqp::p6bindattrinvres($v, Scalar, '$!whence',
-> { nqp::bindpos(nqp::getattr(self, List, '$!items'), nqp::unbox_i($pos), $v) } )
if self.exists($pos) {
nqp::atpos(
nqp::getattr(self, List, '$!items'), nqp::unbox_i($pos)
);
}
else {
my $default := self.VAR.default;
nqp::p6bindattrinvres(
$v //= (nqp::istype($default,Any)
?? $default
!! (nqp::istype(self.VAR.of,Any) ?? self.VAR.of !! Any)),
Scalar,
'$!whence',
-> { nqp::bindpos(
nqp::getattr(self,List,'$!items'), nqp::unbox_i($pos),$v) }
);
}
}
multi method at_pos(int $pos, TValue $v? is copy) is rw {
self.exists($pos)
?? nqp::atpos(nqp::getattr(self, List, '$!items'), $pos)
!! nqp::p6bindattrinvres($v, Scalar, '$!whence',
-> { nqp::bindpos(nqp::getattr(self, List, '$!items'), $pos, $v) } )
if self.exists($pos) {
nqp::atpos(nqp::getattr(self, List, '$!items'), $pos);
}
else {
my $default := self.VAR.default;
nqp::p6bindattrinvres(
$v //= (nqp::istype($default,Any)
?? $default
!! (nqp::istype(self.VAR.of,Any) ?? self.VAR.of !! Any)),
Scalar,
'$!whence',
-> { nqp::bindpos(nqp::getattr(self, List,'$!items'),$pos,$v)}
);
}
}
multi method bind_pos($pos is copy, TValue \bindval) is rw {
$pos = $pos.Int;
Expand Down
55 changes: 42 additions & 13 deletions src/core/Hash.pm
Expand Up @@ -16,10 +16,20 @@ my class Hash { # declared in BOOTSTRAP
nqp::getattr(self, EnumMap, '$!storage') !!
nqp::bindattr(self, EnumMap, '$!storage', nqp::hash());
$key = $key.Str;
nqp::existskey($storage, nqp::unbox_s($key))
?? nqp::atkey($storage, nqp::unbox_s($key))
!! nqp::p6bindattrinvres(my $v, Scalar, '$!whence',
-> { nqp::bindkey($storage, nqp::unbox_s($key), $v) } )
if nqp::existskey($storage, nqp::unbox_s($key)) {
nqp::atkey($storage, nqp::unbox_s($key));
}
else {
my $default := self.VAR.default;
nqp::p6bindattrinvres(
my $v = (nqp::istype($default,Any)
?? $default
!! (nqp::istype(self.VAR.of,Any) ?? self.VAR.of !! Any )),
Scalar,
'$!whence',
-> { nqp::bindkey($storage, nqp::unbox_s($key), $v) }
);
}
}

method bind_key($key, Mu \bindval) is rw {
Expand Down Expand Up @@ -199,10 +209,20 @@ my class Hash { # declared in BOOTSTRAP
my role TypedHash[::TValue] does Associative[TValue] {
method at_key(::?CLASS:D: $key is copy, TValue $v? is copy) is rw {
$key = $key.Str;
self.exists($key)
?? nqp::findmethod(EnumMap, 'at_key')(self, $key)
!! nqp::p6bindattrinvres($v, Scalar, '$!whence',
-> { nqp::findmethod(EnumMap, 'STORE_AT_KEY')(self, $key, $v) } )
if self.exists($key) {
nqp::findmethod(EnumMap, 'at_key')(self, $key);
}
else {
my $default := self.VAR.default;
nqp::p6bindattrinvres(
$v //= (nqp::istype($default,Any)
?? $default
!! (nqp::istype(self.VAR.of,Any) ?? self.VAR.of !! Any)),
Scalar,
'$!whence',
-> { nqp::findmethod(EnumMap, 'STORE_AT_KEY')(self,$key,$v) }
);
}
}
method STORE_AT_KEY(Str \key, TValue $x is copy) is rw {
nqp::findmethod(EnumMap, 'STORE_AT_KEY')(self, key, $x);
Expand All @@ -228,10 +248,18 @@ my class Hash { # declared in BOOTSTRAP
method keyof () { TKey }
method at_key(::?CLASS:D: TKey \key, TValue $v? is copy) is rw {
my $key_which = key.WHICH;
self.exists(key)
?? nqp::findmethod(EnumMap, 'at_key')(self, $key_which)
!! nqp::p6bindattrinvres($v, Scalar, '$!whence',
-> {
if self.exists(key) {
nqp::findmethod(EnumMap, 'at_key')(self, $key_which);
}
else {
my $default := self.VAR.default;
nqp::p6bindattrinvres(
$v //= (nqp::istype($default,Any)
?? $default
!! (nqp::istype(self.VAR.of,Any) ?? self.VAR.of !! Any)),
Scalar,
'$!whence',
-> {
nqp::defined(nqp::getattr(self, $?CLASS, '$!keys')) ||
nqp::bindattr(self, $?CLASS, '$!keys', nqp::hash());
nqp::defined(nqp::getattr(self, EnumMap, '$!storage')) ||
Expand All @@ -244,7 +272,8 @@ my class Hash { # declared in BOOTSTRAP
nqp::getattr(self, EnumMap, '$!storage'),
nqp::unbox_s($key_which),
$v);
})
});
}
}
method STORE_AT_KEY(TKey \key, TValue $x is copy) is rw {
my $key_which = key.WHICH;
Expand Down

0 comments on commit 948210e

Please sign in to comment.