|
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; |
46 | 47 | import org.jruby.runtime.ObjectAllocator;
|
47 | 48 | import org.jruby.runtime.ThreadContext;
|
48 | 49 | import org.jruby.runtime.builtin.IRubyObject;
|
@@ -247,32 +248,21 @@ public IRubyObject to_bn() {
|
247 | 248 | public IRubyObject coerce(IRubyObject other) {
|
248 | 249 | final Ruby runtime = getRuntime();
|
249 | 250 | IRubyObject self;
|
250 |
| -// switch (other.getMetaClass().index) { |
251 |
| -// case ClassIndex.STRING: |
252 |
| -// self = runtime.newString(value.toString()); |
253 |
| -// break; |
254 |
| -// case ClassIndex.FIXNUM: |
255 |
| -// case ClassIndex.BIGNUM: |
256 |
| -// // FIXME: s/b faster way to convert than going through RubyString |
257 |
| -// self = RubyNumeric.str2inum(runtime, runtime.newString(value.toString()), 10, true); |
258 |
| -// break; |
259 |
| -// default: |
260 |
| -// if (other instanceof BN) { |
261 |
| -// self = this; |
262 |
| -// } else { |
263 |
| -// throw runtime.newTypeError("Don't know how to coerce"); |
264 |
| -// } |
265 |
| - if ( other instanceof RubyString ) { |
266 |
| - self = runtime.newString(value.toString()); |
267 |
| - } |
268 |
| - else if ( other instanceof RubyInteger ) { |
269 |
| - self = to_i(); |
270 |
| - } |
271 |
| - else if ( other instanceof BN ) { |
272 |
| - self = this; |
273 |
| - } |
274 |
| - else { |
275 |
| - throw runtime.newTypeError("don't know how to coerce to " + other.getMetaClass().getName()); |
| 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 | + } |
276 | 266 | }
|
277 | 267 | return runtime.newArray(other, self);
|
278 | 268 | }
|
@@ -376,43 +366,27 @@ public IRubyObject bn_exp(final ThreadContext context, IRubyObject other) {
|
376 | 366 | // exponent even approaching Integer.MAX_VALUE would be silly big, and
|
377 | 367 | // the value would take a very, very long time to calculate.)
|
378 | 368 | // we'll check for values < 0 (illegal) while we're at it
|
379 |
| -// int exp; |
380 |
| -// switch(other.getMetaClass().index) { |
381 |
| -// case ClassIndex.FIXNUM: { |
382 |
| -// long val = ((RubyFixnum)other).getLongValue(); |
383 |
| -// if (val >= 0 && val <= Integer.MAX_VALUE) { |
384 |
| -// exp = (int)val; |
385 |
| -// break; |
386 |
| -// } |
387 |
| -// } |
388 |
| -// case ClassIndex.BIGNUM: |
389 |
| -// // Bignum is inherently too big |
390 |
| -// throw newBNError(getRuntime(), "invalid exponent"); |
391 |
| -// default: { |
392 |
| -// if (!(other instanceof BN)) { |
393 |
| -// throw getRuntime().newTypeError("Cannot convert into OpenSSL::BN"); |
394 |
| - int exp = -1; |
395 |
| - |
396 |
| - if ( other instanceof RubyInteger ) { |
397 |
| - long val = ((RubyInteger) other).getLongValue(); |
398 |
| - if ( val >= 0 && val <= Integer.MAX_VALUE ) { |
399 |
| - exp = (int) val; |
400 |
| - } |
401 |
| - else if ( other instanceof RubyBignum ) { // inherently too big |
402 |
| - throw newBNError(context.runtime, "invalid exponent"); |
403 |
| - } |
404 |
| - } |
405 |
| - |
406 |
| - if ( exp == -1 ) { |
407 |
| - if ( ! (other instanceof BN) ) { |
408 |
| - throw context.runtime.newTypeError("Cannot convert into " + other.getMetaClass().getName()); |
409 |
| - } |
410 |
| - BigInteger val = ((BN) other).value; |
411 |
| - if (val.compareTo(BigInteger.ZERO) < 0 || val.compareTo(MAX_INT) > 0) { |
412 |
| - throw newBNError(context.runtime, "invalid exponent"); |
413 |
| - } |
414 |
| - exp = val.intValue(); |
415 |
| - } |
| 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(); |
416 | 390 |
|
417 | 391 | try {
|
418 | 392 | return newBN(context.runtime, value.pow(exp));
|
@@ -822,27 +796,17 @@ public static RaiseException newBNError(Ruby runtime, String message) {
|
822 | 796 | return new RaiseException(runtime, runtime.getModule("OpenSSL").getClass("BNError"), message, true);
|
823 | 797 | }
|
824 | 798 |
|
825 |
| -// public static BigInteger getBigInteger(IRubyObject arg) { |
826 |
| -// if (arg.isNil()) return null; |
827 |
| -// switch(arg.getMetaClass().index) { |
828 |
| -// case ClassIndex.FIXNUM: |
829 |
| -// case ClassIndex.BIGNUM: |
830 |
| -// return new BigInteger(arg.toString()); |
831 |
| -// default: |
832 |
| -// if (arg instanceof BN) { |
833 |
| -// return ((BN)arg).value; |
834 |
| -// } |
835 |
| -// throw arg.getRuntime().newTypeError("Cannot convert into OpenSSL::BN"); |
836 |
| - public static BigInteger getBigInteger(final IRubyObject arg) { |
837 |
| - if ( arg.isNil() ) return null; |
838 |
| - |
839 |
| - if ( arg instanceof RubyInteger ) { |
840 |
| - return ((RubyInteger) arg).getBigIntegerValue(); |
841 |
| - } |
842 |
| - |
843 |
| - if ( arg instanceof BN ) return ((BN) arg).value; |
844 |
| - |
845 |
| - throw arg.getRuntime().newTypeError("Cannot convert into OpenSSL::BN"); |
| 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 | + } |
846 | 811 | }
|
847 |
| - |
848 | 812 | }
|
0 commit comments