/
Stash.pm6
60 lines (54 loc) · 1.74 KB
/
Stash.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
my class Stash { # declared in BOOTSTRAP
# class Stash is Hash
# has str $!longname;
multi method AT-KEY(Stash:D: Str:D $key) is raw {
my \storage := nqp::getattr(self,Map,'$!storage');
nqp::if(
nqp::existskey(storage,$key),
nqp::atkey(storage,$key),
nqp::p6scalarfromdesc(
ContainerDescriptor::BindHashPos.new(Mu, self, $key)
)
)
}
multi method AT-KEY(Stash:D: Str() $key, :$global_fallback!) is raw {
my \storage := nqp::getattr(self,Map,'$!storage');
nqp::if(
nqp::existskey(storage,$key),
nqp::atkey(storage,$key),
nqp::if(
$global_fallback,
nqp::if(
nqp::existskey(GLOBAL.WHO,$key),
nqp::atkey(GLOBAL.WHO,$key),
Failure.new("Could not find symbol '$key'")
),
nqp::p6scalarfromdesc(
ContainerDescriptor::BindHashPos.new(Mu, self, $key)
)
)
)
}
method package_at_key(Stash:D: str $key) {
my \storage := nqp::getattr(self,Map,'$!storage');
nqp::ifnull(
nqp::atkey(storage,$key),
nqp::stmts(
(my $pkg := Metamodel::PackageHOW.new_type(:name($key))),
$pkg.^compose,
nqp::bindkey(storage,$key,$pkg)
)
)
}
multi method gist(Stash:D:) {
self.Str
}
multi method Str(Stash:D:) {
nqp::isnull_s($!longname) ?? '<anon>' !! $!longname
}
method merge-symbols(Stash:D: Hash $globalish) { # NQP gives a Hash, not a Stash
nqp::gethllsym('perl6','ModuleLoader').merge_globals(self,$globalish)
if $globalish.defined;
}
}
# vim: ft=perl6 expandtab sw=4