-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
Routine.pir
117 lines (90 loc) · 2.62 KB
/
Routine.pir
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
## $Id$
=head1 TITLE
Code - Perl 6 Routine class
=head1 DESCRIPTION
This file sets up the Perl 6 C<Routine> class, the base class for all
wrappable executable objects.
=cut
.namespace ['Routine']
.sub 'onload' :anon :load :init
.local pmc p6meta
p6meta = get_hll_global ['Perl6Object'], '$!P6META'
p6meta.'new_class'('Routine', 'parent'=>'Block')
.end
=head1 METHODS
=over 4
=item wrap
=cut
.sub 'wrap' :method
.param pmc wrapper
# Did we already wrap? If so, get handle and increment it to make a new
# one; otherwise, start from 1.
.local pmc handle
handle = getprop '$!wrap_handle', self
unless null handle goto have_handle
handle = box 0
have_handle:
handle = 'infix:+'(handle, 1)
# Take current Parrot-level sub and re-bless it into a block (so CALLER
# won't see it as a routine). Copy properties.
.local pmc inner
inner = get_hll_global 'Block'
inner = inner.'new'()
$P0 = getattribute self, ['Sub'], 'proxy'
setattribute inner, ['Sub'], 'proxy', $P0
$P1 = prophash self
$P2 = iter $P1
it_loop:
unless $P2 goto it_loop_end
$S0 = shift $P2
$P3 = $P1[$S0]
setprop inner, $S0, $P3
goto it_loop
it_loop_end:
setprop $P0, '$!real_self', inner
# Then assign the Parrot sub of the wrapper to ourself, and set the inner block
# and handle as properties on ourself too.
$P0 = getattribute wrapper, ['Sub'], 'proxy'
setattribute self, ['Sub'], 'proxy', $P0
setprop $P0, '$!real_self', self
setprop self, '$!wrap_handle', handle
setprop self, '$!wrap_inner', inner
.return (handle)
.end
=item unwrap
=cut
.sub 'unwrap' :method
.param pmc handle
# Search for wrap handle.
.local pmc current
current = self
search_loop:
$P0 = getprop '$!wrap_handle', current
if null $P0 goto handle_not_found
if $P0 == handle goto found
current = getprop '$!wrap_inner', current
goto search_loop
# If found, unwrap and fix up chain to eliminate now-unused sub.
found:
$P0 = getprop '$!wrap_inner', current
$P1 = getattribute $P0, ['Sub'], 'proxy'
setattribute current, ['Sub'], 'proxy', $P1
setprop $P1, '$!real_self', current
$P1 = getprop '$!wrap_inner', $P0
if null $P1 goto unwrap_done
setprop current, '$!wrap_inner', $P1
$P1 = getprop '$!wrap_handle', $P0
setprop current, '$!wrap_handle', $P1
unwrap_done:
$P0 = new 'Nil'
.return ($P0)
handle_not_found:
'die'('Could not find unwrap handle ', handle, ' on sub ', self)
.end
=back
=cut
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: