Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 70bad1f0a552
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 24e62cf91a17
Choose a head ref
  • 4 commits
  • 4 files changed
  • 1 contributor

Commits on Apr 6, 2016

  1. Copy the full SHA
    a05f85b View commit details
  2. Copy the full SHA
    97172c3 View commit details
  3. Copy the full SHA
    9895e99 View commit details
  4. Copy the full SHA
    24e62cf View commit details
44 changes: 13 additions & 31 deletions core/src/main/java/org/jruby/RubyNumeric.java
Original file line number Diff line number Diff line change
@@ -175,8 +175,6 @@ public static void checkInt(IRubyObject arg, long num){
tooSmall(arg, num);
} else if (num > Integer.MAX_VALUE) {
tooBig(arg, num);
} else {
return;
}
}

@@ -193,7 +191,7 @@ private static void tooBig(IRubyObject arg, long num) {
*/
public static byte num2chr(IRubyObject arg) {
if (arg instanceof RubyString) {
String value = ((RubyString) arg).toString();
String value = arg.toString();

if (value != null && value.length() > 0) return (byte) value.charAt(0);
}
@@ -393,39 +391,25 @@ public static RubyFloat str2fnum(Ruby runtime, RubyString arg) {
* will be 0.0 if the conversion failed.
*/
public static RubyFloat str2fnum(Ruby runtime, RubyString arg, boolean strict) {
return str2fnumCommon(runtime, arg, strict, biteListCaller19);
}

private static RubyFloat str2fnumCommon(Ruby runtime, RubyString arg, boolean strict, ByteListCaller caller) {
final double ZERO = 0.0;
try {
return new RubyFloat(runtime, caller.yield(arg, strict));
} catch (NumberFormatException e) {
double value = ConvertDouble.byteListToDouble19(arg.getByteList(), strict);
return RubyFloat.newFloat(runtime, value);
}
catch (NumberFormatException e) {
if (strict) {
throw runtime.newArgumentError("invalid value for Float(): "
+ arg.callMethod(runtime.getCurrentContext(), "inspect").toString());
}
return new RubyFloat(runtime,ZERO);
return RubyFloat.newFloat(runtime, 0.0);
}
}

private interface ByteListCaller {
double yield(RubyString arg, boolean strict);
}

private static class ByteListCaller19 implements ByteListCaller {
public double yield(RubyString arg, boolean strict) {
return ConvertDouble.byteListToDouble19(arg.getByteList(),strict);
}
}
private static final ByteListCaller19 biteListCaller19 = new ByteListCaller19();


/** Numeric methods. (num_*)
*
*/

protected IRubyObject[] getCoerced(ThreadContext context, IRubyObject other, boolean error) {
protected final IRubyObject[] getCoerced(ThreadContext context, IRubyObject other, boolean error) {
final Ruby runtime = context.runtime;
final IRubyObject $ex = context.getErrorInfo();
final IRubyObject result;
@@ -442,7 +426,7 @@ protected IRubyObject[] getCoerced(ThreadContext context, IRubyObject other, boo
return null;
}

return coerceResult(runtime, result, true).toJavaArray();
return coerceResult(runtime, result, true).toJavaArrayMaybeUnsafe();
}

protected IRubyObject callCoerced(ThreadContext context, String method, IRubyObject other, boolean err) {
@@ -480,14 +464,13 @@ protected final RubyArray doCoerce(ThreadContext context, IRubyObject other, boo
}
return null;
}
final Ruby runtime = context.runtime;
final IRubyObject $ex = context.getErrorInfo();
final IRubyObject result;
try {
result = coerceBody(context, other);
}
catch (RaiseException e) { // e.g. NoMethodError: undefined method `coerce'
if (e.getException().kind_of_p(context, runtime.getStandardError()).isTrue()) {
if (context.runtime.getStandardError().isInstance( e.getException() )) {
context.setErrorInfo($ex); // restore $!
RubyWarnings warnings = context.runtime.getWarnings();
warnings.warn("Numerical comparison operators will no more rescue exceptions of #coerce");
@@ -496,12 +479,11 @@ protected final RubyArray doCoerce(ThreadContext context, IRubyObject other, boo
coerceFailed(context, other);
}
return null;
} else {
throw e;
}
throw e;
}

return coerceResult(runtime, result, err);
return coerceResult(context.runtime, result, err);
}

private static RubyArray coerceResult(final Ruby runtime, final IRubyObject result, final boolean err) {
@@ -524,7 +506,7 @@ private static RubyArray coerceResult(final Ruby runtime, final IRubyObject resu
*/
protected final IRubyObject coerceRescue(ThreadContext context, IRubyObject other) {
coerceFailed(context, other);
return context.runtime.getNil();
return context.nil;
}

/** coerce_failed
@@ -1261,7 +1243,7 @@ public Throwable fillInStackTrace() {

@Deprecated
public static RubyFloat str2fnum19(Ruby runtime, RubyString arg, boolean strict) {
return str2fnumCommon(runtime, arg, strict, biteListCaller19);
return str2fnum(runtime, arg, strict);
}

/** num_negative_p
25 changes: 5 additions & 20 deletions core/src/main/java/org/jruby/util/collections/ClassValue.java
Original file line number Diff line number Diff line change
@@ -13,34 +13,19 @@ public ClassValue(ClassValueCalculator<T> calculator) {
this.calculator = calculator;
}

public abstract T get(Class cls);
public abstract T get(Class<?> cls);

protected final ClassValueCalculator<T> calculator;

public static <T> ClassValue<T> newInstance(ClassValueCalculator<T> calculator) {
if ( JAVA7_CLASS_VALUE ) return newJava7Instance(calculator);
return new MapBasedClassValue<T>(calculator);
if ( CLASS_VALUE ) return newJava7Instance(calculator);
return new MapBasedClassValue<>(calculator);
}

private static <T> ClassValue<T> newJava7Instance(ClassValueCalculator<T> calculator) {
return new Java7ClassValue<T>(calculator);
return new Java7ClassValue<>(calculator);
}

private static final boolean JAVA7_CLASS_VALUE;

static {
boolean java7ClassValue = false;
if ( Options.INVOKEDYNAMIC_CLASS_VALUES.load() ) {
try {
Class.forName("java.lang.ClassValue");
Class.forName("org.jruby.util.collections.Java7ClassValue");
java7ClassValue = true;
}
catch (Exception ex) {
// fall through to Map version
}
}
JAVA7_CLASS_VALUE = java7ClassValue;
}
private static final boolean CLASS_VALUE = Options.INVOKEDYNAMIC_CLASS_VALUES.load();

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package org.jruby.util.collections;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
* A proxy cache that uses Java 7's ClassValue.
*/
public class Java7ClassValue<T> extends ClassValue<T> {
final class Java7ClassValue<T> extends ClassValue<T> {

public Java7ClassValue(ClassValueCalculator<T> calculator) {
super(calculator);
}

public T get(Class cls) {
public T get(Class<?> cls) {
// We don't check for null on the WeakReference since the
// value is strongly referenced by proxy's list
return (T)proxy.get(cls).get();
return proxy.get(cls).get();
}

// If we put any objects that reference an org.jruby.Ruby runtime
@@ -29,12 +29,12 @@ public T get(Class cls) {
// Strongly reference all computed values so they don't get
// garbage collected until this Java7ClassValue instance goes
// out of scope
private final List<T> values = new ArrayList<T>();
private final List<T> computedValues = new LinkedList<>();

@Override
protected WeakReference<T> computeValue(Class<?> type) {
T value = calculator.computeValue(type);
values.add(value);
computedValues.add(value);
return new WeakReference(value);
}
};
Original file line number Diff line number Diff line change
@@ -5,14 +5,14 @@
/**
* A simple Map-based cache of proxies.
*/
public class MapBasedClassValue<T> extends ClassValue<T> {
public final class MapBasedClassValue<T> extends ClassValue<T> {

public MapBasedClassValue(ClassValueCalculator<T> calculator) {
super(calculator);
}

@Override
public T get(Class cls) {
public T get(Class<?> cls) {
T obj = cache.get(cls);

if (obj != null) return obj;
@@ -35,6 +35,5 @@ public T get(Class cls) {
// expensive, for one; many lookups are performed when passing parameters to/from
// methods; etc.).
// TODO: faster custom concurrent map
private final ConcurrentHashMap<Class,T> cache =
new ConcurrentHashMap<Class, T>(128);
private final ConcurrentHashMap<Class<?>,T> cache = new ConcurrentHashMap<>(128);
}