/
Dispatchers.nqp
200 lines (177 loc) Β· 7.24 KB
/
Dispatchers.nqp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
class Perl6::Metamodel::BaseDispatcher {
has @!candidates;
has $!idx;
has $!next_dispatcher; # The dispatcher we must pass control to when own queue exhausts
method candidates() { @!candidates }
method exhausted() { $!idx >= +@!candidates && (!nqp::defined($!next_dispatcher) || $!next_dispatcher.exhausted()) }
method last_candidate() { $!idx >= +@!candidates }
method last() { @!candidates := [] }
method set_next_dispatcher($next_dispatcher)
{ $!next_dispatcher := $next_dispatcher }
# Wrapper-like dispatchers handle their last candidate differently.
method is_wrapper_like() { 0 }
method get_call() {
my $call := @!candidates[$!idx];
++$!idx;
if (nqp::can($call, 'is_dispatcher') && $call.is_dispatcher)
|| (nqp::can($call, 'is_wrapped') && $call.is_wrapped)
{
nqp::nextdispatcherfor(self, $call);
}
else {
if self.is_wrapper_like && self.last_candidate {
nqp::setdispatcherfor($!next_dispatcher, $call) if $!next_dispatcher;
}
else {
nqp::setdispatcherfor(self, $call);
}
}
$call
}
method call_with_args(*@pos, *%named) {
if self.last_candidate {
if nqp::defined($!next_dispatcher) {
$!next_dispatcher.call_with_args(|@pos, |%named);
}
else {
die(self.HOW.shortname(self) ~ " is already exhausted");
}
}
else {
my $call := self.get_call;
if self.has_invocant {
$call(self.invocant, |@pos, |%named);
}
else {
$call(|@pos, |%named);
}
}
}
method call_with_capture($capture) {
if self.last_candidate {
if nqp::defined($!next_dispatcher) {
$!next_dispatcher.call_with_capture($capture)
}
else {
die(self.HOW.shortname(self) ~ " is already exhausted");
}
}
else {
my $call := self.get_call;
nqp::invokewithcapture($call, $capture);
}
}
method shift_callee() {
if self.last_candidate {
if nqp::defined($!next_dispatcher) {
$!next_dispatcher.shift_callee;
}
else {
nqp::null()
}
}
else {
self.get_call;
}
}
method add_from_mro(@methods, $class, $sub, :$skip_first = 0) {
my @mro := nqp::can($class.HOW, 'mro_unhidden')
?? $class.HOW.mro_unhidden($class)
!! $class.HOW.mro($class);
my $name := $sub.name;
my %seen;
for @mro {
my $mt := nqp::hllize($_.HOW.method_table($_));
if nqp::existskey($mt, $name) {
my $meth := nqp::atkey($mt, $name);
if $meth.is_dispatcher {
my $proto_pkg_id := nqp::objectid($meth.package);
# Skip proto if it's been seen before. Prevents from multiple dispatching over the same multi
# candidates.
$meth := nqp::null() if %seen{$proto_pkg_id};
%seen{$proto_pkg_id} := 1
}
# Skipping the first method obtained from MRO because it should have been invoked already
nqp::if(
nqp::isgt_i($skip_first, 0),
(--$skip_first),
nqp::unless(
nqp::isnull($meth),
nqp::push(@methods, $meth)
)
)
}
}
@methods
}
}
class Perl6::Metamodel::MethodDispatcher is Perl6::Metamodel::BaseDispatcher {
has $!obj;
method new(:@candidates, :$idx, :$obj) {
my $disp := nqp::create(self);
nqp::bindattr($disp, Perl6::Metamodel::BaseDispatcher, '@!candidates', @candidates);
nqp::bindattr($disp, Perl6::Metamodel::BaseDispatcher, '$!idx', $idx);
nqp::bindattr($disp, Perl6::Metamodel::MethodDispatcher, '$!obj', $obj);
$disp
}
method vivify_for($sub, $lexpad, $args) {
my $obj := nqp::decont($lexpad<self>);
my @methods := [];
self.add_from_mro(@methods, $obj, $sub);
self.new(:candidates(@methods), :obj($obj), :idx(1))
}
method has_invocant() { 1 }
method invocant() { $!obj }
}
class Perl6::Metamodel::MultiDispatcher is Perl6::Metamodel::BaseDispatcher {
has $!has_invocant;
has $!invocant;
method new(:@candidates, :$idx, :$invocant, :$has_invocant, :$next_dispatcher) {
my $disp := nqp::create(self);
nqp::bindattr($disp, Perl6::Metamodel::BaseDispatcher, '@!candidates', @candidates);
nqp::bindattr($disp, Perl6::Metamodel::BaseDispatcher, '$!idx', $idx);
nqp::bindattr($disp, Perl6::Metamodel::BaseDispatcher, '$!next_dispatcher', $next_dispatcher);
nqp::bindattr($disp, Perl6::Metamodel::MultiDispatcher, '$!invocant', $invocant);
nqp::bindattr($disp, Perl6::Metamodel::MultiDispatcher, '$!has_invocant', $has_invocant);
$disp
}
method vivify_for($sub, $lexpad, $args) {
my $disp := $sub.dispatcher();
my $has_invocant := nqp::existskey($lexpad, 'self');
my @cands := $disp.find_best_dispatchee($args, 1);
my $invocant := $has_invocant && $lexpad<self>;
my $next_dispatcher := nqp::existskey($lexpad, '$*NEXT-DISPATCHER')
?? nqp::atkey($lexpad, '$*NEXT-DISPATCHER')
!! nqp::null();
# The first candidate has already been invoked, throw it away from the list;
# If called in a method then only take control if MethodDispatcher is in charge.
if $has_invocant && !nqp::defined($next_dispatcher) {
self.add_from_mro(@cands, $invocant, $sub, :skip_first(1));
Perl6::Metamodel::MethodDispatcher.new(:candidates(@cands), :idx(1), :obj($invocant))
}
else {
self.new(:candidates(@cands), :idx(1), :$invocant, :$has_invocant, :$next_dispatcher)
}
}
method has_invocant() { $!has_invocant }
method invocant() { $!invocant }
}
class Perl6::Metamodel::WrapDispatcher is Perl6::Metamodel::BaseDispatcher {
method new(:@candidates, :$idx, :$invocant, :$has_invocant, :$next_dispatcher) {
my $disp := nqp::create(self);
nqp::bindattr($disp, Perl6::Metamodel::BaseDispatcher, '@!candidates', @candidates);
nqp::bindattr($disp, Perl6::Metamodel::BaseDispatcher, '$!idx', 1);
nqp::bindattr($disp, Perl6::Metamodel::BaseDispatcher, '$!next_dispatcher', $next_dispatcher);
$disp
}
method vivify_for($sub, $lexpad, $capture) {
my @candidates := $sub.wrappers;
my $next_dispatcher := nqp::existskey($lexpad, '$*NEXT-DISPATCHER')
?? nqp::atkey($lexpad, '$*NEXT-DISPATCHER')
!! nqp::null();
self.new(:@candidates, :idx(1), :$next_dispatcher)
}
method has_invocant() { 0 }
method invocant() { NQPMu }
method is_wrapper_like() { 1 }
}