9
9
*/
10
10
package org .truffleruby .language .arguments ;
11
11
12
+ import com .oracle .truffle .api .profiles .ConditionProfile ;
12
13
import org .truffleruby .language .RubyContextSourceNode ;
13
14
import org .truffleruby .language .RubyGuards ;
14
15
@@ -18,27 +19,33 @@ public class ReadPostArgumentNode extends RubyContextSourceNode {
18
19
19
20
private final int indexFromCount ;
20
21
private final boolean keywordArguments ;
21
- private final int minimumForKWargs ;
22
+ private final int required ;
23
+ private final ConditionProfile enoughArguments = ConditionProfile .create ();
22
24
23
- public ReadPostArgumentNode (int indexFromCount , boolean keywordArguments , int minimumForKWargs ) {
25
+ public ReadPostArgumentNode (int indexFromCount , boolean keywordArguments , int required ) {
24
26
this .indexFromCount = indexFromCount ;
25
27
this .keywordArguments = keywordArguments ;
26
- this .minimumForKWargs = minimumForKWargs ;
28
+ this .required = required ;
27
29
}
28
30
29
31
@ Override
30
32
public Object execute (VirtualFrame frame ) {
31
33
int count = RubyArguments .getArgumentsCount (frame );
32
34
33
- if (keywordArguments && count > minimumForKWargs ) {
35
+ if (keywordArguments && count > required ) {
34
36
final Object lastArgument = RubyArguments .getArgument (frame , count - 1 );
35
37
if (RubyGuards .isRubyHash (lastArgument )) {
36
38
count --;
37
39
}
38
40
}
39
41
40
- final int effectiveIndex = count - indexFromCount ;
41
- return RubyArguments .getArgument (frame , effectiveIndex );
42
+ if (enoughArguments .profile (count >= required )) {
43
+ final int effectiveIndex = count - indexFromCount ;
44
+ return RubyArguments .getArgument (frame , effectiveIndex );
45
+ } else {
46
+ // CheckArityNode will prevent this case for methods & lambdas, but it is still possible for procs.
47
+ return nil ;
48
+ }
42
49
}
43
50
44
51
@ Override
0 commit comments