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: b0f7aaf01b04
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: b69aa17d35c1
Choose a head ref
  • 6 commits
  • 6 files changed
  • 2 contributors

Commits on Mar 26, 2018

  1. Fix time interval test

    and use RubyTime.convertTimeInterval for parsing to raise correct ArgumentError and TypeError exceptions
    ChrisBr authored and kares committed Mar 26, 2018

    Verified

    This commit was signed with the committer’s verified signature.
    Copy the full SHA
    fd9c772 View commit details
  2. Copy the full SHA
    d470d71 View commit details
  3. Copy the full SHA
    d19e850 View commit details
  4. Copy the full SHA
    3073115 View commit details
  5. Copy the full SHA
    cfa66dd View commit details
  6. Copy the full SHA
    b69aa17 View commit details
21 changes: 16 additions & 5 deletions core/src/main/java/org/jruby/RubyTime.java
Original file line number Diff line number Diff line change
@@ -51,6 +51,8 @@
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.RaiseException;
import org.jruby.exceptions.TypeError;
import org.jruby.java.proxies.JavaProxy;
import org.jruby.runtime.Block;
import org.jruby.runtime.ClassIndex;
import org.jruby.runtime.Helpers;
@@ -1328,9 +1330,9 @@ public static double convertTimeInterval(ThreadContext context, IRubyObject sec)
}
// NOTE: we can probably do better here, but we're matching MRI behavior
// this is for converting custom objects such as ActiveSupport::Duration
else if ( sec.respondsTo("divmod") ) {
else if ( sites(context).respond_to_divmod.respondsTo(context, sec, sec) ) {
final Ruby runtime = context.runtime;
IRubyObject result = sec.callMethod(context, "divmod", RubyFixnum.newFixnum(runtime, 1));
IRubyObject result = sites(context).divmod.call(context, sec, sec, 1);
if ( result instanceof RubyArray ) {
seconds = ((RubyNumeric) ((RubyArray) result).eltOk(0) ).getDoubleValue(); // div
seconds += ((RubyNumeric) ((RubyArray) result).eltOk(1) ).getDoubleValue(); // mod
@@ -1340,7 +1342,16 @@ else if ( sec.respondsTo("divmod") ) {
}
}
else {
throw context.runtime.newTypeError("can't convert " + sec.getMetaClass().getName() + " into time interval");
seconds = 0; boolean raise = true;
if ( sec instanceof JavaProxy ) {
try { // support java.lang.Number proxies
seconds = sec.convertToFloat().getDoubleValue(); raise = false;
} catch (TypeError ex) { /* fallback bellow to raising a TypeError */ }
}

if (raise) {
throw context.runtime.newTypeError("can't convert " + sec.getMetaClass().getName() + " into time interval");
}
}

if ( seconds < 0 ) throw context.runtime.newArgumentError("time interval must be positive");
@@ -1405,7 +1416,7 @@ private static RubyTime s_mload(ThreadContext context, final RubyTime time, IRub
try {
offset = offsetVar.convertToInteger().getIntValue() * 1000;
}
catch (RaiseException typeError) {
catch (TypeError typeError) {
context.setErrorInfo($ex); // restore $!
}
}
@@ -1416,7 +1427,7 @@ private static RubyTime s_mload(ThreadContext context, final RubyTime time, IRub
try {
zone = zoneVar.convertToString().toString();
}
catch (RaiseException typeError) {
catch (TypeError typeError) {
context.setErrorInfo($ex); // restore $!
}
}
7 changes: 3 additions & 4 deletions core/src/main/java/org/jruby/ext/thread/Mutex.java
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@
import org.jruby.RubyClass;
import org.jruby.RubyObject;
import org.jruby.RubyThread;
import org.jruby.RubyTime;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.Block;
@@ -128,10 +129,8 @@ public IRubyObject sleep(ThreadContext context) {

@JRubyMethod
public IRubyObject sleep(ThreadContext context, IRubyObject timeout) {
long beg = System.currentTimeMillis();
double t = timeout.convertToFloat().getDoubleValue();

if (t < 0) throw context.runtime.newArgumentError("negative sleep timeout");
final long beg = System.currentTimeMillis();
double t = RubyTime.convertTimeInterval(context, timeout);

unlock(context);

15 changes: 15 additions & 0 deletions core/src/main/java/org/jruby/javasupport/ext/JavaLang.java
Original file line number Diff line number Diff line change
@@ -340,6 +340,15 @@ public static IRubyObject to_f(final ThreadContext context, final IRubyObject se
return context.runtime.newFloat(val.doubleValue());
}

@JRubyMethod(name = "real?")
public static IRubyObject real_p(final ThreadContext context, final IRubyObject self) {
java.lang.Number val = (java.lang.Number) self.toJava(java.lang.Number.class);
return context.runtime.newBoolean(val instanceof Integer || val instanceof Long ||
val instanceof Short || val instanceof Byte ||
val instanceof Float || val instanceof Double ||
val instanceof java.math.BigInteger || val instanceof java.math.BigDecimal);
}

@JRubyMethod(name = { "to_i", "to_int" })
public static IRubyObject to_i(final ThreadContext context, final IRubyObject self) {
java.lang.Number val = (java.lang.Number) self.toJava(java.lang.Number.class);
@@ -360,6 +369,12 @@ public static IRubyObject integer_p(final ThreadContext context, final IRubyObje
val instanceof java.math.BigInteger);
}

@JRubyMethod(name = "zero?")
public static IRubyObject zero_p(final ThreadContext context, final IRubyObject self) {
java.lang.Number val = (java.lang.Number) self.toJava(java.lang.Number.class);
return context.runtime.newBoolean(Double.compare(val.doubleValue(), 0) == 0);
}

@JRubyMethod(name = "coerce")
public static IRubyObject coerce(final ThreadContext context, final IRubyObject self, final IRubyObject type) {
java.lang.Number val = (java.lang.Number) self.toJava(java.lang.Number.class);
3 changes: 3 additions & 0 deletions core/src/main/java/org/jruby/runtime/JavaSites.java
Original file line number Diff line number Diff line change
@@ -295,6 +295,9 @@ public IRubyObject call(ThreadContext context, IRubyObject recv, IRubyObject oth
public final CachingCallSite to_i = new FunctionalCachingCallSite("to_i");
public final CachingCallSite to_r = new FunctionalCachingCallSite("to_r");
public final CheckedSites checked_to_r = new CheckedSites("to_r");

public final RespondToCallSite respond_to_divmod = new RespondToCallSite("divmod");
public final CachingCallSite divmod = new FunctionalCachingCallSite("divmod");
}

public static class EnumerableSites {
7 changes: 7 additions & 0 deletions spec/java_integration/addons/number_spec.rb
Original file line number Diff line number Diff line change
@@ -8,6 +8,13 @@
m = Mutex.new
m.lock
m.sleep(0.01.to_java)

begin
m.sleep('0'.to_java)
fail('did-not-raise')
rescue TypeError => ex
expect(ex.message).to eq "can't convert Java::JavaLang::String into time interval"
end
end
end
end
1 change: 0 additions & 1 deletion test/mri/excludes/TestTime.rb
Original file line number Diff line number Diff line change
@@ -12,6 +12,5 @@
exclude :test_strftime_seconds_from_epoch, "Integer unification, needs Bignum support (#4687)"
exclude :test_strftime_too_wide, "needs investigation"
exclude :test_strftime_year, "Integer unification, needs Bignum support (#4687)"
exclude :test_time_interval, "needs investigation"
exclude :test_utc_or_local, "needs investigation"
exclude :test_xmlschema, "raising Joda errors recently, #2237"