1. my class Stash { # declared in BOOTSTRAP
  2. # class Stash is Hash
  3. # has str $!longname;
  4. multi method AT-KEY(Stash:D: Str:D $key) is raw {
  5. nqp::if(
  6. nqp::getattr(self,Map,'$!storage')
  7. && nqp::existskey(nqp::getattr(self,Map,'$!storage'),$key),
  8. nqp::atkey(nqp::getattr(self,Map,'$!storage'),$key),
  9. nqp::p6bindattrinvres(
  10. my $v,Scalar,'$!whence',
  11. -> { nqp::bindkey(
  12. nqp::getattr(self,Map,'$!storage')
  13. || nqp::bindattr(self,Map,'$!storage',nqp::hash),
  14. $key,
  15. $v
  16. )
  17. }
  18. )
  19. )
  20. }
  21. multi method AT-KEY(Stash:D: Str() $key, :$global_fallback!) is raw {
  22. nqp::if(
  23. nqp::getattr(self,Map,'$!storage')
  24. && nqp::existskey(nqp::getattr(self,Map,'$!storage'),$key),
  25. nqp::atkey(nqp::getattr(self,Map,'$!storage'),$key),
  26. nqp::if(
  27. $global_fallback,
  28. nqp::if(
  29. nqp::existskey(GLOBAL.WHO,$key),
  30. nqp::atkey(GLOBAL.WHO,$key),
  31. Failure.new("Could not find symbol '$key'")
  32. ),
  33. nqp::p6bindattrinvres(
  34. my $v,Scalar,'$!whence',
  35. -> { nqp::bindkey(
  36. nqp::getattr(self,Map,'$!storage')
  37. || nqp::bindattr(self,Map,'$!storage',nqp::hash),
  38. $key,
  39. $v
  40. )
  41. }
  42. )
  43. )
  44. )
  45. }
  46. method package_at_key(Stash:D: str $key) {
  47. my Mu $storage := nqp::defined(nqp::getattr(self, Map, '$!storage')) ??
  48. nqp::getattr(self, Map, '$!storage') !!
  49. nqp::bindattr(self, Map, '$!storage', nqp::hash());
  50. if nqp::existskey($storage, nqp::unbox_s($key)) {
  51. nqp::atkey($storage, $key)
  52. }
  53. else {
  54. my $pkg := Metamodel::PackageHOW.new_type(:name($key));
  55. $pkg.^compose;
  56. nqp::bindkey($storage, $key, $pkg)
  57. }
  58. }
  59. multi method gist(Stash:D:) {
  60. self.Str
  61. }
  62. multi method Str(Stash:D:) {
  63. nqp::isnull_s($!longname) ?? '<anon>' !! $!longname
  64. }
  65. method merge-symbols(Stash:D: Hash $globalish) { # NQP gives a Hash, not a Stash
  66. nqp::gethllsym('perl6','ModuleLoader').merge_globals(self,$globalish)
  67. if $globalish.defined;
  68. }
  69. }