/
CallFrame.pm
59 lines (52 loc) · 1.72 KB
/
CallFrame.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
my class CallFrame {
has $.annotations;
has $.my;
method SET-SELF(\level, Mu \ctx is raw, Mu \bt is raw) {
nqp::stmts(
(my int $i = nqp::add_i(level,1)),
($!annotations := nqp::atkey(
nqp::atpos(nqp::getattr(bt,List,'$!reified'),$i),
'annotations'
)),
(my $ctx := ctx),
nqp::while(
nqp::isgt_i(($i = nqp::sub_i($i,1)),0),
$ctx := nqp::ctxcaller($ctx)
),
($!my :=
nqp::p6bindattrinvres(nqp::create(Stash),Map,'$!storage',$ctx)),
self
)
}
only method new(CallFrame: Int $level = 0) { # MUST BE AN only
nqp::create(CallFrame).SET-SELF( # wrt to backtrace levels
$level,
nqp::ctxcaller(nqp::ctx),
nqp::backtrace(nqp::handle(nqp::die(''),'CATCH',nqp::exception))
)
}
method line() { nqp::atkey($!annotations,'line') }
method file() { nqp::atkey($!annotations,'file') }
method code() {
nqp::getcodeobj(nqp::ctxcode(nqp::getattr($!my,Map,'$!storage')))
}
method callframe(Int $?) {
X::NYI.new(feature => 'Callframe.callframe').throw;
}
multi method gist(CallFrame:D:) {
nqp::atkey($!annotations,'file')
~ ' at line '
~ nqp::atkey($!annotations,'line')
}
method annotations() {
nqp::p6bindattrinvres(nqp::create(Map),Map,'$storage',$!annotations)
}
}
only sub callframe(Int $level = 0) { # MUST BE an only wrt to backtrace levels
nqp::create(CallFrame).SET-SELF(
$level,
nqp::ctxcaller(nqp::ctx),
nqp::backtrace(nqp::handle(nqp::die(''),'CATCH',nqp::exception))
)
}
# vim: ft=perl6 expandtab sw=4