|
43 | 43 | import org.jruby.anno.JRubyMethod;
|
44 | 44 | import org.jruby.exceptions.RaiseException;
|
45 | 45 | import org.jruby.runtime.Arity;
|
46 |
| -import org.jruby.runtime.ClassIndex; |
47 | 46 | import org.jruby.runtime.ObjectAllocator;
|
48 | 47 | import org.jruby.runtime.ThreadContext;
|
49 | 48 | import org.jruby.runtime.builtin.IRubyObject;
|
@@ -248,21 +247,17 @@ public IRubyObject to_bn() {
|
248 | 247 | public IRubyObject coerce(IRubyObject other) {
|
249 | 248 | final Ruby runtime = getRuntime();
|
250 | 249 | IRubyObject self;
|
251 |
| - switch (other.getMetaClass().index) { |
252 |
| - case ClassIndex.STRING: |
253 |
| - self = runtime.newString(value.toString()); |
254 |
| - break; |
255 |
| - case ClassIndex.FIXNUM: |
256 |
| - case ClassIndex.BIGNUM: |
257 |
| - // FIXME: s/b faster way to convert than going through RubyString |
258 |
| - self = RubyNumeric.str2inum(runtime, runtime.newString(value.toString()), 10, true); |
259 |
| - break; |
260 |
| - default: |
261 |
| - if (other instanceof BN) { |
262 |
| - self = this; |
263 |
| - } else { |
264 |
| - throw runtime.newTypeError("Don't know how to coerce"); |
265 |
| - } |
| 250 | + if ( other instanceof RubyString ) { |
| 251 | + self = runtime.newString(value.toString()); |
| 252 | + } |
| 253 | + else if ( other instanceof RubyInteger ) { |
| 254 | + self = to_i(); |
| 255 | + } |
| 256 | + else if ( other instanceof BN ) { |
| 257 | + self = this; |
| 258 | + } |
| 259 | + else { |
| 260 | + throw runtime.newTypeError("don't know how to coerce to " + other.getMetaClass().getName()); |
266 | 261 | }
|
267 | 262 | return runtime.newArray(other, self);
|
268 | 263 | }
|
@@ -366,27 +361,28 @@ public IRubyObject bn_exp(final ThreadContext context, IRubyObject other) {
|
366 | 361 | // exponent even approaching Integer.MAX_VALUE would be silly big, and
|
367 | 362 | // the value would take a very, very long time to calculate.)
|
368 | 363 | // we'll check for values < 0 (illegal) while we're at it
|
369 |
| - int exp; |
370 |
| - switch(other.getMetaClass().index) { |
371 |
| - case ClassIndex.FIXNUM: |
372 |
| - long val = ((RubyFixnum)other).getLongValue(); |
373 |
| - if (val >= 0 && val <= Integer.MAX_VALUE) { |
374 |
| - exp = (int)val; |
375 |
| - break; |
376 |
| - } |
377 |
| - case ClassIndex.BIGNUM: |
378 |
| - // Bignum is inherently too big |
379 |
| - throw newBNError(getRuntime(), "invalid exponent"); |
380 |
| - default: |
381 |
| - if (!(other instanceof BN)) { |
382 |
| - throw getRuntime().newTypeError("Cannot convert into OpenSSL::BN"); |
383 |
| - } |
384 |
| - } |
385 |
| - BigInteger val = ((BN) other).value; |
386 |
| - if (val.compareTo(BigInteger.ZERO) < 0 || val.compareTo(MAX_INT) > 0) { |
387 |
| - throw newBNError(context.runtime, "invalid exponent"); |
388 |
| - } |
389 |
| - exp = val.intValue(); |
| 364 | + int exp = -1; |
| 365 | + |
| 366 | + if ( other instanceof RubyInteger ) { |
| 367 | + long val = ((RubyInteger) other).getLongValue(); |
| 368 | + if ( val >= 0 && val <= Integer.MAX_VALUE ) { |
| 369 | + exp = (int) val; |
| 370 | + } |
| 371 | + else if ( other instanceof RubyBignum ) { // inherently too big |
| 372 | + throw newBNError(context.runtime, "invalid exponent"); |
| 373 | + } |
| 374 | + } |
| 375 | + |
| 376 | + if ( exp == -1 ) { |
| 377 | + if ( ! (other instanceof BN) ) { |
| 378 | + throw context.runtime.newTypeError("Cannot convert into " + other.getMetaClass().getName()); |
| 379 | + } |
| 380 | + BigInteger val = ((BN) other).value; |
| 381 | + if (val.compareTo(BigInteger.ZERO) < 0 || val.compareTo(MAX_INT) > 0) { |
| 382 | + throw newBNError(context.runtime, "invalid exponent"); |
| 383 | + } |
| 384 | + exp = val.intValue(); |
| 385 | + } |
390 | 386 |
|
391 | 387 | try {
|
392 | 388 | return newBN(context.runtime, value.pow(exp));
|
@@ -796,17 +792,16 @@ public static RaiseException newBNError(Ruby runtime, String message) {
|
796 | 792 | return new RaiseException(runtime, runtime.getModule("OpenSSL").getClass("BNError"), message, true);
|
797 | 793 | }
|
798 | 794 |
|
799 |
| - public static BigInteger getBigInteger(IRubyObject arg) { |
800 |
| - if (arg.isNil()) return null; |
801 |
| - switch(arg.getMetaClass().index) { |
802 |
| - case ClassIndex.FIXNUM: |
803 |
| - case ClassIndex.BIGNUM: |
804 |
| - return new BigInteger(arg.toString()); |
805 |
| - default: |
806 |
| - if (arg instanceof BN) { |
807 |
| - return ((BN)arg).value; |
808 |
| - } |
809 |
| - throw arg.getRuntime().newTypeError("Cannot convert into OpenSSL::BN"); |
810 |
| - } |
| 795 | + public static BigInteger getBigInteger(final IRubyObject arg) { |
| 796 | + if ( arg.isNil() ) return null; |
| 797 | + |
| 798 | + if ( arg instanceof RubyInteger ) { |
| 799 | + return ((RubyInteger) arg).getBigIntegerValue(); |
| 800 | + } |
| 801 | + |
| 802 | + if ( arg instanceof BN ) return ((BN) arg).value; |
| 803 | + |
| 804 | + throw arg.getRuntime().newTypeError("Cannot convert into OpenSSL::BN"); |
811 | 805 | }
|
| 806 | + |
812 | 807 | }
|
0 commit comments