pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/jruby/jruby/commit/ccf15cbe0bd20fdd09036f7bc1a62d1483389ea6

/> numToInt and numToLong · jruby/jruby@ccf15cb · GitHub
Skip to content

Commit ccf15cb

Browse files
committed
numToInt and numToLong
1 parent 4c907f0 commit ccf15cb

49 files changed

Lines changed: 319 additions & 345 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

core/src/main/java/org/jruby/RubyArray.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ public IRubyObject fetch_values(ThreadContext context, IRubyObject[] args, Block
10431043
int arraySize = size();
10441044
var result = Create.newRawArray(context, length);
10451045
for (int i = 0; i < length; i++) {
1046-
int index = args[i].convertToInteger().getIntValue();
1046+
int index = numToInt(context, args[i]);
10471047
// FIXME: lookup the bounds part of this in error message??
10481048
if (index >= arraySize) {
10491049
if (!block.isGiven()) throw context.runtime.newIndexError("index " + index + " outside of array bounds: 0...0");
@@ -1066,7 +1066,7 @@ public IRubyObject fetch(ThreadContext context, IRubyObject arg0, Block block) {
10661066
if (index < 0) index += realLength;
10671067
if (index < 0 || index >= realLength) {
10681068
if (block.isGiven()) return block.yield(context, arg0);
1069-
throw context.runtime.newIndexError("index " + index + " out of array");
1069+
throw indexError(context, "index " + index + " out of array");
10701070
}
10711071

10721072
return eltOk((int) index);
@@ -1406,7 +1406,7 @@ public IRubyObject subseq_step(ThreadContext context, RubyArithmeticSequence arg
14061406
if (step < 0) {
14071407
if (aseqExcl && !aseqEnd.isNil()) {
14081408
/* Handle exclusion before range reversal */
1409-
aseqEnd = asFixnum(context, aseqEnd.convertToInteger().getIntValue() + 1);
1409+
aseqEnd = asFixnum(context, numToLong(context, aseqEnd) + 1);
14101410

14111411
/* Don't exclude the previous beginning */
14121412
aseqExcl = false;
@@ -1491,7 +1491,7 @@ private IRubyObject getEnd(ThreadContext context, RubyArithmeticSequence arg0) {
14911491
}
14921492

14931493
private Long getStep(ThreadContext context, RubyArithmeticSequence arg0) {
1494-
return arg0.step(context).isNil() ? null: arg0.step(context).convertToInteger().asLong(context);
1494+
return arg0.step(context).isNil() ? null: numToLong(context, arg0.step(context));
14951495
}
14961496

14971497
/** rb_ary_subseq

core/src/main/java/org/jruby/RubyBasicObject.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@
9696
import static org.jruby.api.Convert.asFixnum;
9797
import static org.jruby.api.Convert.asSymbol;
9898
import static org.jruby.api.Convert.castAsModule;
99+
import static org.jruby.api.Convert.numToInt;
100+
import static org.jruby.api.Convert.numToLong;
99101
import static org.jruby.api.Create.newArray;
100102
import static org.jruby.api.Create.newEmptyArray;
101103
import static org.jruby.api.Create.newRawArray;
@@ -1239,9 +1241,7 @@ public int compareTo(IRubyObject other) {
12391241
IRubyObject cmp = invokedynamic(context, this, OP_CMP, other);
12401242

12411243
// if RubyBasicObject#op_cmp is used, the result may be nil (not comparable)
1242-
if ( ! cmp.isNil() ) {
1243-
return (int) cmp.convertToInteger().asLong(context);
1244-
}
1244+
if (!cmp.isNil()) return numToInt(context, cmp);
12451245

12461246
/* We used to raise an error if two IRubyObject were not comparable, but
12471247
* in order to support the new ConcurrentHashMapV8 and other libraries
@@ -1920,7 +1920,7 @@ public IRubyObject specificEval(ThreadContext context, RubyModule mod, IRubyObje
19201920
// We just want the TypeError if the argument doesn't convert to a String (JRUBY-386)
19211921
RubyString evalStr = arg0 instanceof RubyString str ? str : arg0.convertToString();
19221922
String file = arg1.convertToString().asJavaString();
1923-
int line = (int)(arg2.convertToInteger().asLong(context) - 1);
1923+
int line = numToInt(context, arg2) - 1;
19241924

19251925
return evalUnder(context, mod, evalStr, file, line, evalType);
19261926
}
@@ -2904,10 +2904,7 @@ public int hashCode() {
29042904
}
29052905

29062906
protected static int nonFixnumHashCode(IRubyObject hashValue) {
2907-
RubyInteger integer = hashValue.convertToInteger();
2908-
return integer instanceof RubyBignum ?
2909-
integer.getBigIntegerValue().intValue() :
2910-
(int) ((RubyFixnum) integer).getValue();
2907+
return numToInt(hashValue.getRuntime().getCurrentContext(), hashValue);
29112908
}
29122909

29132910
/**

core/src/main/java/org/jruby/RubyBignum.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -291,37 +291,32 @@ public static BigInteger long2big(long arg) {
291291
*/
292292
@Override
293293
public IRubyObject ceil(ThreadContext context, IRubyObject arg){
294-
int ndigits = arg.convertToInteger().getIntValue();
294+
int ndigits = numToInt(context, arg);
295295
BigInteger self = value;
296-
if (ndigits >= 0){
297-
return this;
298-
} else {
299-
int posdigits = Math.abs(ndigits);
300-
BigInteger exp = BigInteger.TEN.pow(posdigits);
301-
BigInteger mod = self.mod(exp);
302-
BigInteger res = self;
303-
if (mod.signum() != 0) {
304-
res = self.add( exp.subtract(mod) );// self + (exp - (mod));
305-
}
306-
return newBignum(context.runtime, res);
307-
}
296+
if (ndigits >= 0) return this;
297+
298+
int posdigits = Math.abs(ndigits);
299+
BigInteger exp = BigInteger.TEN.pow(posdigits);
300+
BigInteger mod = self.mod(exp);
301+
BigInteger res = self;
302+
if (mod.signum() != 0) res = self.add( exp.subtract(mod) );// self + (exp - (mod));
303+
304+
return newBignum(context.runtime, res);
308305
}
309306

310307
/** rb_big_floor
311308
*
312309
*/
313310
@Override
314311
public IRubyObject floor(ThreadContext context, IRubyObject arg){
315-
int ndigits = arg.convertToInteger().getIntValue();
312+
int ndigits = numToInt(context, arg);
316313
BigInteger self = value;
317-
if (ndigits >= 0){
318-
return this;
319-
} else {
320-
int posdigits = Math.abs(ndigits);
321-
BigInteger exp = BigInteger.TEN.pow(posdigits);
322-
BigInteger res = self.subtract(self.mod(exp));
323-
return newBignum(context.runtime, res);
324-
}
314+
if (ndigits >= 0) return this;
315+
316+
int posdigits = Math.abs(ndigits);
317+
BigInteger exp = BigInteger.TEN.pow(posdigits);
318+
BigInteger res = self.subtract(self.mod(exp));
319+
return newBignum(context.runtime, res);
325320
}
326321

327322
/** rb_big_truncate
@@ -1068,7 +1063,7 @@ public final int compareTo(IRubyObject other) {
10681063
if (other instanceof RubyBignum bignum) return value.compareTo(bignum.value);
10691064

10701065
ThreadContext context = getRuntime().getCurrentContext();
1071-
return (int) coerceCmp(context, sites(context).op_cmp, other).convertToInteger().asLong(context);
1066+
return numToInt(context, coerceCmp(context, sites(context).op_cmp, other));
10721067
}
10731068

10741069
@Override

core/src/main/java/org/jruby/RubyConverter.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public IRubyObject primitive_convert(ThreadContext context, IRubyObject[] args)
236236
hashArg = 2;
237237
} else {
238238
outputByteOffsetObj = args[2];
239-
outputByteoffset = (int)args[2].convertToInteger().asLong(context);
239+
outputByteoffset = numToInt(context, args[2]);
240240
}
241241
}
242242

@@ -245,7 +245,7 @@ public IRubyObject primitive_convert(ThreadContext context, IRubyObject[] args)
245245
hashArg = 3;
246246
} else {
247247
outputBytesizeObj = args[3];
248-
outputBytesize = (int)args[3].convertToInteger().asLong(context);
248+
outputBytesize = numToInt(context, args[3]);
249249
}
250250
}
251251

@@ -255,7 +255,7 @@ public IRubyObject primitive_convert(ThreadContext context, IRubyObject[] args)
255255
if (args[4] instanceof RubyHash) {
256256
hashArg = 4;
257257
} else {
258-
flags = (int)args[4].convertToInteger().asLong(context);
258+
flags = numToInt(context, args[4]);
259259
}
260260
}
261261

@@ -549,9 +549,7 @@ public IRubyObject insert_output(ThreadContext context, IRubyObject string) {
549549
public IRubyObject putback(ThreadContext context, IRubyObject[] argv) {
550550
int argc = Arity.checkArgumentCount(context, argv, 0, 1);
551551
IRubyObject max = argc == 0 ? context.nil : argv[0];
552-
int n = max.isNil() ?
553-
ec.putbackable() :
554-
(int) Math.min(max.convertToInteger().asLong(context), ec.putbackable());
552+
int n = max.isNil() ? ec.putbackable() : Math.min(numToInt(context, max), ec.putbackable());
555553
RubyString str = RubyString.newStringLight(context.runtime, n);
556554
ByteList strBL = str.getByteList();
557555

core/src/main/java/org/jruby/RubyDir.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import static org.jruby.api.Check.checkEmbeddedNulls;
7474
import static org.jruby.api.Convert.asBoolean;
7575
import static org.jruby.api.Convert.asFixnum;
76+
import static org.jruby.api.Convert.numToInt;
7677
import static org.jruby.api.Create.newString;
7778
import static org.jruby.api.Define.defineClass;
7879
import static org.jruby.api.Error.argumentError;
@@ -713,7 +714,7 @@ private static IRubyObject mkdirCommon(ThreadContext context, String path, IRuby
713714
File newDir = res.unwrap(File.class);
714715
if (File.separatorChar == '\\') newDir = new File(newDir.getPath());
715716

716-
int mode = args.length == 2 ? ((int) args[1].convertToInteger().asLong(context)) : 0777;
717+
int mode = args.length == 2 ? numToInt(context, args[1]) : 0777;
717718

718719
if (context.runtime.getPosix().mkdir(newDir.getAbsolutePath(), mode) < 0) {
719720
// FIXME: This is a system error based on errno

core/src/main/java/org/jruby/RubyEnumerable.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -240,27 +240,19 @@ public IRubyObject doYield(ThreadContext context1, Block unused, IRubyObject arg
240240
* @see SizeFn#size(ThreadContext, IRubyObject, IRubyObject[])
241241
*/
242242
private static IRubyObject cycleSize(ThreadContext context, IRubyObject self, IRubyObject[] args) {
243-
Ruby runtime = context.runtime;
244243
long mul = 0;
245-
IRubyObject n = runtime.getNil();
244+
IRubyObject n = context.nil;
246245

247246
if (args != null && args.length > 0) {
248247
n = args[0];
249-
if (!n.isNil()) mul = n.convertToInteger().asLong(context);
248+
if (!n.isNil()) mul = numToLong(context, n);
250249
}
251250

252251
IRubyObject size = size(context, self, args);
253-
if (size == null || size.isNil() || size.equals(RubyFixnum.zero(runtime))) {
254-
return size;
255-
}
256-
257-
if (n == null || n.isNil()) {
258-
return RubyFloat.newFloat(runtime, RubyFloat.INFINITY);
259-
}
260252

261-
if (mul <= 0) {
262-
return RubyFixnum.zero(runtime);
263-
}
253+
if (size == null || size.isNil() || size.equals(asFixnum(context, 0))) return size;
254+
if (n == null || n.isNil()) return asFloat(context, RubyFloat.INFINITY);
255+
if (mul <= 0) return asFixnum(context, 0);
264256

265257
return sites(context).cycle_op_mul.call(context, size, size, mul);
266258
}

core/src/main/java/org/jruby/RubyFile.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ public IRubyObject initialize(ThreadContext context, IRubyObject[] args, Block b
319319
@JRubyMethod
320320
public IRubyObject chmod(ThreadContext context, IRubyObject arg) {
321321
checkClosed(context);
322-
int mode = (int) arg.convertToInteger().asLong(context);
322+
int mode = numToInt(context, arg);
323323
final String path = getPath();
324324
if (!new File(path).exists()) throw context.runtime.newErrnoENOENTError(path);
325325

@@ -1118,7 +1118,7 @@ public static IRubyObject umask(ThreadContext context, IRubyObject recv, IRubyOb
11181118
int argc = Arity.checkArgumentCount(context, args, 0, 1);
11191119
int oldMask = argc == 0 ?
11201120
PosixShim.umask(context.runtime.getPosix()) :
1121-
PosixShim.umask(context.runtime.getPosix(), (int) args[0].convertToInteger().asLong(context));
1121+
PosixShim.umask(context.runtime.getPosix(), numToInt(context, args[0]));
11221122

11231123
return asFixnum(context, oldMask);
11241124
}

core/src/main/java/org/jruby/RubyFixnum.java

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262

6363
import static org.jruby.api.Convert.asBoolean;
6464
import static org.jruby.api.Convert.asFixnum;
65+
import static org.jruby.api.Convert.numToInt;
66+
import static org.jruby.api.Convert.numToLong;
6567
import static org.jruby.api.Create.newArray;
6668
import static org.jruby.api.Create.newEmptyArray;
6769
import static org.jruby.api.Error.argumentError;
@@ -329,28 +331,25 @@ public IRubyObject times(ThreadContext context, Block block) {
329331
*/
330332
@Override
331333
public IRubyObject ceil(ThreadContext context, IRubyObject arg){
332-
long ndigits = arg.convertToInteger().asLong(context);
333-
if (ndigits >= 0) {
334-
return this;
335-
} else {
336-
long self = this.value;
337-
long posdigits = Math.abs(ndigits);
338-
long exp = (long) Math.pow(10, posdigits);
339-
long mod = (self % exp + exp) % exp;
340-
long res = self;
341-
if (mod != 0) {
342-
res = self + (exp - (mod));
343-
}
344-
return newFixnum(context.runtime, res);
345-
}
334+
long ndigits = numToLong(context, arg);
335+
if (ndigits >= 0) return this;
336+
337+
long self = this.value;
338+
long posdigits = Math.abs(ndigits);
339+
long exp = (long) Math.pow(10, posdigits);
340+
long mod = (self % exp + exp) % exp;
341+
long res = self;
342+
if (mod != 0) res = self + (exp - (mod));
343+
344+
return asFixnum(context, res);
346345
}
347346

348347
/** rb_fix_floor
349348
*
350349
*/
351350
@Override
352351
public IRubyObject floor(ThreadContext context, IRubyObject arg){
353-
long ndigits = arg.convertToInteger().asLong(context);
352+
long ndigits = numToLong(context, arg);
354353
if (ndigits >= 0) return this;
355354

356355
long self = this.value;
@@ -888,22 +887,20 @@ protected IRubyObject intPowTmp1(ThreadContext context, RubyInteger y, long mm,
888887
long tmp = 1L;
889888
long yy;
890889

891-
for (/*NOP*/; !(y instanceof RubyFixnum); y = (RubyInteger) sites(context).op_rshift.call(context, y, y, RubyFixnum.one(context.runtime))) {
892-
if (f_odd_p(context, y)) {
893-
tmp = (tmp * xx) % mm;
894-
}
890+
var one = asFixnum(context, 1);
891+
for (/*NOP*/; !(y instanceof RubyFixnum); y = (RubyInteger) sites(context).op_rshift.call(context, y, y, one)) {
892+
if (f_odd_p(context, y)) tmp = (tmp * xx) % mm;
893+
895894
xx = (xx * xx) % mm;
896895
}
897-
for (yy = y.convertToInteger().asLong(context); yy != 0L; yy >>= 1L) {
898-
if ((yy & 1L) != 0L) {
899-
tmp = (tmp * xx) % mm;
900-
}
896+
for (yy = numToLong(context, y); yy != 0L; yy >>= 1L) {
897+
if ((yy & 1L) != 0L) tmp = (tmp * xx) % mm;
898+
901899
xx = (xx * xx) % mm;
902900
}
903901

904-
if (negaFlg && (tmp != 0)) {
905-
tmp -= mm;
906-
}
902+
if (negaFlg && (tmp != 0)) tmp -= mm;
903+
907904
return asFixnum(context, tmp);
908905
}
909906

@@ -1040,10 +1037,10 @@ public final int compareTo(IRubyObject other) {
10401037
}
10411038

10421039
private int compareToOther(IRubyObject other) {
1043-
if (other instanceof RubyBignum) return BigInteger.valueOf(value).compareTo(((RubyBignum) other).value);
1044-
if (other instanceof RubyFloat) return Double.compare((double) value, ((RubyFloat) other).value);
1045-
ThreadContext context = metaClass.runtime.getCurrentContext();
1046-
return (int) coerceCmp(context, sites(context).op_cmp, other).convertToInteger().asLong(context);
1040+
if (other instanceof RubyBignum bignum) return BigInteger.valueOf(value).compareTo(bignum.value);
1041+
if (other instanceof RubyFloat flote) return Double.compare((double) value, flote.value);
1042+
ThreadContext context = getRuntime().getCurrentContext();
1043+
return numToInt(context, coerceCmp(context, sites(context).op_cmp, other));
10471044
}
10481045

10491046
/** fix_cmp

core/src/main/java/org/jruby/RubyGlobal.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
import static org.jruby.api.Access.objectClass;
9494
import static org.jruby.api.Convert.asBoolean;
9595
import static org.jruby.api.Convert.asFixnum;
96+
import static org.jruby.api.Convert.numToInt;
9697
import static org.jruby.api.Create.newArray;
9798
import static org.jruby.api.Create.newFrozenString;
9899
import static org.jruby.api.Create.newRawArray;
@@ -981,7 +982,7 @@ public LineNumberGlobalVariable(Ruby runtime, String name) {
981982

982983
@Override
983984
public IRubyObject set(IRubyObject value) {
984-
int line = (int)value.convertToInteger().asLong(runtime.getCurrentContext());
985+
int line = numToInt(runtime.getCurrentContext(), value);
985986
runtime.setCurrentLine(line);
986987
return value;
987988
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy