1. class CompUnit::Handle {
  2. has Mu $!module_ctx;
  3. has Mu $!unit;
  4. multi submethod new() {
  5. nqp::create(self)
  6. }
  7. method ctxsave() {
  8. $!module_ctx := nqp::ctxcaller(nqp::ctx()) unless $!module_ctx;
  9. }
  10. multi submethod new(Mu \module_ctx) {
  11. nqp::p6bindattrinvres(
  12. nqp::create(self),CompUnit::Handle,'$!module_ctx', module_ctx
  13. )
  14. }
  15. submethod from-unit(Stash $unit) {
  16. nqp::p6bindattrinvres(
  17. nqp::create(self),CompUnit::Handle,'$!unit',nqp::decont($unit)
  18. )
  19. }
  20. # If the compilation unit has a callable EXPORT subroutine, it will
  21. # be returned here. Nil otherwise.
  22. method export-sub(--> Callable:D) {
  23. my $module := self.unit;
  24. $module && nqp::existskey($module, '&EXPORT')
  25. ?? nqp::atkey($module, '&EXPORT')
  26. !! Nil
  27. }
  28. # The EXPORT package from the UNIT of the compilation unit; a
  29. # Nil if none
  30. method export-package(--> Stash:D) {
  31. my $module := self.unit;
  32. if $module and nqp::existskey($module, 'EXPORT') {
  33. my $EXPORT := nqp::atkey($module, 'EXPORT');
  34. nqp::istype($EXPORT.WHO, Stash)
  35. ?? $EXPORT.WHO
  36. !! nqp::p6bindattrinvres(nqp::create(Stash), Map, '$!storage', $EXPORT.WHO);
  37. }
  38. else {
  39. Nil
  40. }
  41. }
  42. # The EXPORTHOW package from the UNIT of the compilation unit;
  43. # Nil if none.
  44. method export-how-package(--> Stash:D) {
  45. my $module := self.unit;
  46. if $module and nqp::existskey($module, 'EXPORTHOW') {
  47. my $EXPORTHOW := nqp::atkey($module, 'EXPORTHOW');
  48. my $who := $EXPORTHOW.WHO;
  49. nqp::istype($who, Stash)
  50. ?? $who
  51. !! nqp::p6bindattrinvres(nqp::create(Stash), Map, '$!storage', $who);
  52. }
  53. else {
  54. Nil
  55. }
  56. }
  57. # The GLOBALish package from the UNIT of the compilation unit
  58. # (the module's contributions to GLOBAL, for merging);
  59. # Nil if none.
  60. method globalish-package() { # returns Stash {
  61. nqp::if(
  62. nqp::defined($!module_ctx),
  63. nqp::ifnull(nqp::atkey(nqp::ctxlexpad($!module_ctx),'GLOBALish').WHO, Nil),
  64. nqp::if(nqp::defined($!unit), $!unit, Nil)
  65. )
  66. }
  67. method unit() {
  68. nqp::defined($!unit)
  69. ?? $!unit
  70. !! nqp::defined($!module_ctx) ?? nqp::ctxlexpad($!module_ctx) !! {}
  71. }
  72. }