@@ -1029,6 +1029,28 @@ public static IRubyObject sprintf(ThreadContext context, IRubyObject recv, IRuby
10291029 public static IRubyObject sprintf (IRubyObject recv , IRubyObject [] args ) {
10301030 return sprintf (((RubyBasicObject ) recv ).getCurrentContext (), recv , args );
10311031 }
1032+
1033+ @ JRubyMethod (name = {"raise" , "fail" }, module = true , visibility = PRIVATE , omit = true )
1034+ public static IRubyObject raise (ThreadContext context , IRubyObject recv ) {
1035+ IRubyObject errorInfo = context .getErrorInfo ();
1036+ maybeRaiseJavaException (context , errorInfo );
1037+
1038+ RaiseException raise ;
1039+ IRubyObject lastException = errorInfo ;
1040+ if (lastException .isNil ()) {
1041+ raise = RaiseException .from (context .runtime , runtimeErrorClass (context ), "" );
1042+ } else {
1043+ // non RubyException value is allowed to be assigned as $!.
1044+ raise = ((RubyException ) lastException ).toThrowable ();
1045+ }
1046+
1047+ var exception = raise .getException ();
1048+ if (context .runtime .isDebug ()) printExceptionSummary (context , exception );
1049+
1050+ throw raise ;
1051+ }
1052+
1053+ @ JRubyMethod (name = {"raise" , "fail" }, module = true , visibility = PRIVATE , omit = true )
10321054 public static IRubyObject raise (ThreadContext context , IRubyObject self , IRubyObject arg0 ) {
10331055 // semi extract_raise_opts :
10341056 if (arg0 instanceof RubyHash opt && !opt .isEmpty () &&
@@ -1040,46 +1062,71 @@ public static IRubyObject raise(ThreadContext context, IRubyObject self, IRubyOb
10401062
10411063 maybeRaiseJavaException (context , arg0 );
10421064
1043- RaiseException raise = arg0 instanceof RubyString ?
1044- ((RubyException ) context .runtime .getRuntimeError ().newInstance (context , arg0 )).toThrowable () :
1045- convertToException (context , arg0 , null ).toThrowable ();
1065+ raiseCommon (context , arg0 , null , null , 1 , true , cause );
10461066
1047- var exception = raise .getException ();
1067+ return null ; // not reached
1068+ }
10481069
1049- if (context .runtime .isDebug ()) printExceptionSummary (context , exception );
1050- if (exception .getCause () == null && cause != exception ) exception .setCause (cause );
1070+ @ JRubyMethod (name = {"raise" , "fail" }, module = true , visibility = PRIVATE , omit = true )
1071+ public static IRubyObject raise (ThreadContext context , IRubyObject recv , IRubyObject arg0 , IRubyObject arg1 ) {
1072+ boolean forceCause = false ;
10511073
1052- throw raise ;
1074+ // semi extract_raise_opts :
1075+ IRubyObject cause = null ;
1076+ IRubyObject last = arg1 ;
1077+ int argc = 2 ;
1078+ cause = getCauseOption (context , last , cause );
1079+
1080+ if (cause != null ) {
1081+ forceCause = true ;
1082+ argc --;
1083+ } else {
1084+ cause = context .getErrorInfo (); // returns nil for no error-info
1085+ }
1086+
1087+ maybeRaiseJavaException (context , arg0 );
1088+
1089+ raiseCommon (context , arg0 , arg1 , null , argc , forceCause , cause );
1090+
1091+ return null ; // not reached
10531092 }
10541093
1055- @ JRubyMethod (name = {"raise" , "fail" }, optional = 3 , checkArity = false , module = true , visibility = PRIVATE , omit = true )
1056- public static IRubyObject raise (ThreadContext context , IRubyObject recv , IRubyObject [] args , Block block ) {
1057- int argc = Arity .checkArgumentCount (context , args , 0 , 3 );
1094+ @ JRubyMethod (name = {"raise" , "fail" }, module = true , visibility = PRIVATE , omit = true )
1095+ public static IRubyObject raise (ThreadContext context , IRubyObject recv , IRubyObject arg0 , IRubyObject arg1 , IRubyObject arg2 ) {
10581096 boolean forceCause = false ;
10591097
10601098 // semi extract_raise_opts :
10611099 IRubyObject cause = null ;
1062- if (argc > 0 ) {
1063- IRubyObject last = args [argc - 1 ];
1064- if (last instanceof RubyHash opt ) {
1065- RubySymbol key ;
1066- if (!opt .isEmpty () && (opt .has_key_p (context , key = asSymbol (context , "cause" )) == context .tru )) {
1067- cause = opt .delete (context , key , Block .NULL_BLOCK );
1068- forceCause = true ;
1069- if (opt .isEmpty () && --argc == 0 ) { // more opts will be passed along
1070- throw argumentError (context , "only cause is given with no arguments" );
1071- }
1072- }
1073- }
1074- }
1100+ IRubyObject last = arg2 ;
1101+ int argc = 3 ;
1102+ cause = getCauseOption (context , last , cause );
10751103
1076- if ( argc > 0 ) { // for argc == 0 we will be raising $!
1077- // NOTE: getErrorInfo needs to happen before new RaiseException(...)
1078- if ( cause == null ) cause = context .getErrorInfo (); // returns nil for no error-info
1104+ if (cause != null ) {
1105+ forceCause = true ;
1106+ argc --;
1107+ } else {
1108+ cause = context .getErrorInfo (); // returns nil for no error-info
10791109 }
10801110
1081- maybeRaiseJavaException (context , args , argc );
1111+ raiseCommon (context , arg0 , arg1 , arg2 , argc , forceCause , cause );
1112+
1113+ return null ; // not reached
1114+ }
10821115
1116+ private static IRubyObject getCauseOption (ThreadContext context , IRubyObject last , IRubyObject cause ) {
1117+ if (last instanceof RubyHash opt ) {
1118+ RubySymbol key ;
1119+ if (!opt .isEmpty () && (opt .has_key_p (context , key = asSymbol (context , "cause" )) == context .tru )) {
1120+ cause = opt .delete (context , key , Block .NULL_BLOCK );
1121+ if (opt .isEmpty ()) { // more opts will be passed along
1122+ throw argumentError (context , "only cause is given with no arguments" );
1123+ }
1124+ }
1125+ }
1126+ return cause ;
1127+ }
1128+
1129+ private static void raiseCommon (ThreadContext context , IRubyObject arg0 , IRubyObject arg1 , IRubyObject arg2 , int argc , boolean forceCause , IRubyObject cause ) {
10831130 RaiseException raise ;
10841131 switch (argc ) {
10851132 case 0 :
@@ -1092,18 +1139,18 @@ public static IRubyObject raise(ThreadContext context, IRubyObject recv, IRubyOb
10921139 }
10931140 break ;
10941141 case 1 :
1095- if (args [ 0 ] instanceof RubyString ) {
1096- raise = ((RubyException ) runtimeErrorClass (context ).newInstance (context , args , block )).toThrowable ();
1142+ if (arg0 instanceof RubyString ) {
1143+ raise = ((RubyException ) runtimeErrorClass (context ).newInstance (context , arg0 )).toThrowable ();
10971144 } else {
1098- raise = convertToException (context , args [ 0 ] , null ).toThrowable ();
1145+ raise = convertToException (context , arg0 , null ).toThrowable ();
10991146 }
11001147 break ;
11011148 case 2 :
1102- raise = convertToException (context , args [ 0 ], args [ 1 ] ).toThrowable ();
1149+ raise = convertToException (context , arg0 , arg1 ).toThrowable ();
11031150 break ;
11041151 default :
1105- RubyException exception = convertToException (context , args [ 0 ], args [ 1 ] );
1106- exception .setBacktrace (context , args [ 2 ] );
1152+ RubyException exception = convertToException (context , arg0 , arg1 );
1153+ exception .setBacktrace (context , arg2 );
11071154 raise = exception .toThrowable ();
11081155 break ;
11091156 }
@@ -1115,19 +1162,19 @@ public static IRubyObject raise(ThreadContext context, IRubyObject recv, IRubyOb
11151162 throw raise ;
11161163 }
11171164
1118- private static void maybeRaiseJavaException (ThreadContext context , final IRubyObject [] args , final int argc ) {
1119- // Check for a Java exception
1120- IRubyObject maybeException = null ;
1121- switch (argc ) {
1122- case 0 :
1123- maybeException = globalVariables (context ).get ("$!" );
1124- break ;
1125- case 1 :
1126- if (args .length == 1 ) maybeException = args [0 ];
1127- break ;
1128- }
1165+ public static IRubyObject raise (ThreadContext context , IRubyObject recv , IRubyObject [] args ) {
1166+ return switch (args .length ) {
1167+ case 0 -> raise (context , recv );
1168+ case 1 -> raise (context , recv , args [0 ]);
1169+ case 2 -> raise (context , recv , args [0 ], args [1 ]);
1170+ case 3 -> raise (context , recv , args [0 ], args [1 ], args [2 ]);
1171+ default -> throw argumentError (context , args , 0 , 3 );
1172+ };
1173+ }
11291174
1130- maybeRaiseJavaException (context , maybeException );
1175+ @ Deprecated (since = "10.0.3.0" )
1176+ public static IRubyObject raise (ThreadContext context , IRubyObject recv , IRubyObject [] args , Block unused ) {
1177+ return raise (context , recv , args );
11311178 }
11321179
11331180 private static void maybeRaiseJavaException (ThreadContext context , final IRubyObject arg ) {
0 commit comments