/
Handle.pm
77 lines (69 loc) · 2.38 KB
/
Handle.pm
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class CompUnit::Handle {
has Mu $!module_ctx;
has Mu $!unit;
submethod new(Mu \module_ctx) {
nqp::p6bindattrinvres(
nqp::create(self),CompUnit::Handle,'$!module_ctx', module_ctx
)
}
submethod from-unit(Stash $unit) {
nqp::p6bindattrinvres(
nqp::create(self),CompUnit::Handle,'$!unit',nqp::decont($unit)
)
}
# If the compilation unit has a callable EXPORT subroutine, it will
# be returned here. A Callable type object otherwise.
method export-sub() returns Callable {
my $module := self.unit;
$module && nqp::existskey($module, '&EXPORT')
?? nqp::atkey($module, '&EXPORT')
!! Nil
}
# The EXPORT package from the UNIT of the compilation unit; a
# Stash type object if none
method export-package() returns Stash {
my $module := self.unit;
if $module and nqp::existskey($module, 'EXPORT') {
my $EXPORT := nqp::atkey($module, 'EXPORT');
nqp::istype($EXPORT.WHO, Stash)
?? $EXPORT.WHO
!! nqp::p6bindattrinvres(nqp::create(Stash), Map, '$!storage', $EXPORT.WHO);
}
else {
Nil
}
}
# The EXPORTHOW package from the UNIT of the compilation unit;
# a Stash type object if none.
method export-how-package() returns Stash {
my $module := self.unit;
if $module and nqp::existskey($module, 'EXPORTHOW') {
my $EXPORTHOW := nqp::atkey($module, 'EXPORTHOW');
my $who := $EXPORTHOW.WHO;
nqp::istype($who, Stash)
?? $who
!! nqp::p6bindattrinvres(nqp::create(Stash), Map, '$!storage', $who);
}
else {
Nil
}
}
# The GLOBALish package from the UNIT of the compilation unit
# (the module's contributions to GLOBAL, for merging); a Stash
# type object if none.
method globalish-package() { # returns Stash {
if nqp::defined($!module_ctx) {
my $lexpad := nqp::ctxlexpad($!module_ctx);
nqp::isnull(nqp::atkey($lexpad, 'GLOBALish')) ?? Nil !! nqp::atkey($lexpad, 'GLOBALish')
}
else {
Nil
}
}
method unit() {
nqp::defined($!unit)
?? $!unit
!! nqp::defined($!module_ctx) ?? nqp::ctxlexpad($!module_ctx) !! {}
}
}
# vim: ft=perl6 expandtab sw=4