9
9
*/
10
10
package org .truffleruby .language .arguments ;
11
11
12
+ import com .oracle .truffle .api .CompilerAsserts ;
13
+ import com .oracle .truffle .api .TruffleLanguage .ContextReference ;
14
+ import com .oracle .truffle .api .nodes .Node ;
15
+ import org .truffleruby .RubyContext ;
12
16
import org .truffleruby .language .RubyContextSourceNode ;
13
17
import org .truffleruby .language .RubyNode ;
14
18
import org .truffleruby .language .control .RaiseException ;
@@ -31,49 +35,47 @@ public CheckArityNode(Arity arity, RubyNode body) {
31
35
32
36
@ Override
33
37
public void doExecuteVoid (VirtualFrame frame ) {
34
- checkArity (frame );
38
+ checkArity (arity , RubyArguments . getArgumentsCount ( frame ), checkFailedProfile , getContextReference (), this );
35
39
body .doExecuteVoid (frame );
36
40
}
37
41
38
42
@ Override
39
43
public Object execute (VirtualFrame frame ) {
40
- checkArity (frame );
44
+ checkArity (arity , RubyArguments . getArgumentsCount ( frame ), checkFailedProfile , getContextReference (), this );
41
45
return body .execute (frame );
42
46
}
43
47
44
- private void checkArity (VirtualFrame frame ) {
45
- final int given = RubyArguments .getArgumentsCount (frame );
46
-
47
- if (!checkArity (arity , given )) {
48
+ public static void checkArity (Arity arity , int given ,
49
+ BranchProfile checkFailedProfile ,
50
+ ContextReference <RubyContext > contextRef ,
51
+ Node currentNode ) {
52
+ CompilerAsserts .partialEvaluationConstant (arity );
53
+ if (!arity .check (given )) {
48
54
checkFailedProfile .enter ();
49
- if (arity .hasRest ()) {
50
- throw new RaiseException (
51
- getContext (),
52
- coreExceptions ().argumentErrorPlus (given , arity .getRequired (), this ));
53
- } else if (arity .getOptional () > 0 ) {
54
- throw new RaiseException (
55
- getContext (),
56
- coreExceptions ().argumentError (given , arity .getRequired (), arity .getOptional (), this ));
57
- } else {
58
- throw new RaiseException (
59
- getContext (),
60
- coreExceptions ().argumentError (given , arity .getRequired (), this ));
61
- }
55
+ checkArityError (arity , given , contextRef , currentNode );
62
56
}
63
57
}
64
58
65
- static boolean checkArity (Arity arity , int given ) {
66
- final int required = arity .getRequired ();
67
-
68
- if (required != 0 && given < required ) {
69
- return false ;
59
+ private static void checkArityError (Arity arity , int given , ContextReference <RubyContext > contextRef ,
60
+ Node currentNode ) {
61
+ final RubyContext context = contextRef .get ();
62
+ if (arity .hasRest ()) {
63
+ throw new RaiseException (
64
+ context ,
65
+ context .getCoreExceptions ().argumentErrorPlus (given , arity .getRequired (), currentNode ));
66
+ } else if (arity .getOptional () > 0 ) {
67
+ throw new RaiseException (
68
+ context ,
69
+ context .getCoreExceptions ().argumentError (
70
+ given ,
71
+ arity .getRequired (),
72
+ arity .getOptional (),
73
+ currentNode ));
74
+ } else {
75
+ throw new RaiseException (
76
+ context ,
77
+ context .getCoreExceptions ().argumentError (given , arity .getRequired (), currentNode ));
70
78
}
71
-
72
- if (!arity .hasRest () && given > required + arity .getOptional ()) {
73
- return false ;
74
- }
75
-
76
- return true ;
77
79
}
78
80
79
81
@ Override
0 commit comments