Exemple #1
0
 /** match_end */
 @JRubyMethod(name = "end", compat = CompatVersion.RUBY1_8)
 public IRubyObject end(ThreadContext context, IRubyObject index) {
   int i = RubyNumeric.num2int(index);
   Ruby runtime = context.getRuntime();
   int e = endCommon(runtime, i);
   return e < 0 ? runtime.getNil() : RubyFixnum.newFixnum(runtime, e);
 }
Exemple #2
0
 /** match_aref */
 @JRubyMethod(name = "[]")
 public IRubyObject op_aref(IRubyObject idx, IRubyObject rest) {
   if (!rest.isNil() || !(idx instanceof RubyFixnum) || ((RubyFixnum) idx).getLongValue() < 0) {
     return ((RubyArray) to_a()).aref(idx, rest);
   }
   return RubyRegexp.nth_match(RubyNumeric.fix2int(idx), this);
 }
Exemple #3
0
 @JRubyMethod(name = "===", required = 1)
 @Override
 public IRubyObject op_eqq(ThreadContext context, IRubyObject other) {
   return (RubyNumeric.fix2int(invokedynamic(context, this, OP_CMP, other)) == 0)
       ? getRuntime().getTrue()
       : getRuntime().getFalse();
 }
Exemple #4
0
  @JRubyMethod(name = "+", required = 1, compat = CompatVersion.RUBY1_8)
  public IRubyObject op_plus(IRubyObject other) {
    if (other instanceof RubyTime) {
      throw getRuntime().newTypeError("time + time ?");
    }
    long adjustment = Math.round(RubyNumeric.num2dbl(other) * 1000000);

    return opPlusCommon(adjustment);
  }
Exemple #5
0
 public final int backrefNumber(IRubyObject obj) {
   check();
   if (obj instanceof RubySymbol) {
     return nameToBackrefNumber((RubyString) ((RubySymbol) obj).id2name());
   } else if (obj instanceof RubyString) {
     return nameToBackrefNumber((RubyString) obj);
   } else {
     return RubyNumeric.num2int(obj);
   }
 }
Exemple #6
0
  @JRubyMethod(name = "+", required = 1, compat = CompatVersion.RUBY1_9)
  public IRubyObject op_plus19(ThreadContext context, IRubyObject other) {
    checkOpCoercion(context, other);
    if (other instanceof RubyTime) {
      throw getRuntime().newTypeError("time + time ?");
    }
    other = other.callMethod(context, "to_r");

    long adjustment = new Double(RubyNumeric.num2dbl(other) * 1000000).longValue();
    return opPlusCommon(adjustment);
  }
Exemple #7
0
  @JRubyMethod(name = "to_r", backtrace = true, compat = CompatVersion.RUBY1_9)
  public IRubyObject to_r(ThreadContext context) {
    IRubyObject rational = to_f().to_r(context);
    if (rational instanceof RubyRational) {
      IRubyObject denominator = ((RubyRational) rational).denominator(context);
      if (RubyNumeric.num2long(denominator) == 1) {
        return ((RubyRational) rational).numerator(context);
      }
    }

    return rational;
  }
Exemple #8
0
 private IRubyObject op_arefCommon(IRubyObject idx) {
   if (idx instanceof RubyFixnum) {
     int num = RubyNumeric.fix2int(idx);
     if (num >= 0) return RubyRegexp.nth_match(num, this);
   } else {
     if (idx instanceof RubySymbol) {
       return RubyRegexp.nth_match(
           nameToBackrefNumber((RubyString) ((RubySymbol) idx).id2name()), this);
     } else if (idx instanceof RubyString) {
       return RubyRegexp.nth_match(nameToBackrefNumber((RubyString) idx), this);
     }
   }
   return null;
 }
Exemple #9
0
  @JRubyMethod(name = "at", meta = true)
  public static IRubyObject at(
      ThreadContext context, IRubyObject recv, IRubyObject arg1, IRubyObject arg2) {
    Ruby runtime = context.getRuntime();

    RubyTime time =
        new RubyTime(runtime, (RubyClass) recv, new DateTime(0L, getLocalTimeZone(runtime)));

    long seconds = RubyNumeric.num2long(arg1);
    long millisecs = 0;
    long microsecs = 0;

    long tmp = RubyNumeric.num2long(arg2);
    millisecs = tmp / 1000;
    microsecs = tmp % 1000;

    time.setUSec(microsecs);
    time.dt = time.dt.withMillis(seconds * 1000 + millisecs);

    time.getMetaClass().getBaseCallSites()[RubyClass.CS_IDX_INITIALIZE].call(context, recv, time);

    return time;
  }
Exemple #10
0
  @JRubyMethod(name = "at", meta = true)
  public static IRubyObject at(ThreadContext context, IRubyObject recv, IRubyObject arg) {
    Ruby runtime = context.getRuntime();
    final RubyTime time;

    if (arg instanceof RubyTime) {
      RubyTime other = (RubyTime) arg;
      time = new RubyTime(runtime, (RubyClass) recv, other.dt);
      time.setUSec(other.getUSec());
    } else {
      time = new RubyTime(runtime, (RubyClass) recv, new DateTime(0L, getLocalTimeZone(runtime)));

      long seconds = RubyNumeric.num2long(arg);
      long millisecs = 0;
      long microsecs = 0;

      // In the case of two arguments, MRI will discard the portion of
      // the first argument after a decimal point (i.e., "floor").
      // However in the case of a single argument, any portion after
      // the decimal point is honored.
      if (arg instanceof RubyFloat || arg instanceof RubyRational) {
        double dbl = RubyNumeric.num2dbl(arg);
        long micro = Math.round((dbl - seconds) * 1000000);
        if (dbl < 0 && micro != 0) {
          micro += 1000000;
        }
        millisecs = micro / 1000;
        microsecs = micro % 1000;
      }
      time.setUSec(microsecs);
      time.dt = time.dt.withMillis(seconds * 1000 + millisecs);
    }

    time.getMetaClass().getBaseCallSites()[RubyClass.CS_IDX_INITIALIZE].call(context, recv, time);

    return time;
  }
Exemple #11
0
  @JRubyMethod(name = "priority=", required = 1)
  public IRubyObject priority_set(IRubyObject priority) {
    // FIXME: This should probably do some translation from Ruby priority levels to Java priority
    // levels (until we have green threads)
    int iPriority = RubyNumeric.fix2int(priority);

    if (iPriority < Thread.MIN_PRIORITY) {
      iPriority = Thread.MIN_PRIORITY;
    } else if (iPriority > Thread.MAX_PRIORITY) {
      iPriority = Thread.MAX_PRIORITY;
    }

    if (threadImpl.isAlive()) {
      threadImpl.setPriority(iPriority);
    }

    return RubyFixnum.newFixnum(getRuntime(), iPriority);
  }
Exemple #12
0
  private IRubyObject opMinusCommon(IRubyObject other) {
    long time = getTimeInMillis();
    long adjustment = Math.round(RubyNumeric.num2dbl(other) * 1000000);
    long micro = adjustment % 1000;
    adjustment = adjustment / 1000;

    time -= adjustment;

    if (getUSec() < micro) {
      time--;
      micro = 1000 - (micro - getUSec());
    } else {
      micro = getUSec() - micro;
    }

    RubyTime newTime = new RubyTime(getRuntime(), getMetaClass());
    newTime.dt = new DateTime(time).withZone(dt.getZone());
    newTime.setUSec(micro);

    return newTime;
  }
  @JRubyMethod(name = "priority=", required = 1)
  public IRubyObject priority_set(IRubyObject priority) {
    int iPriority = RubyNumeric.fix2int(priority);

    if (iPriority < RUBY_MIN_THREAD_PRIORITY) {
      iPriority = RUBY_MIN_THREAD_PRIORITY;
    } else if (iPriority > RUBY_MAX_THREAD_PRIORITY) {
      iPriority = RUBY_MAX_THREAD_PRIORITY;
    }

    if (threadImpl.isAlive()) {
      int jPriority = rubyPriorityToJavaPriority(iPriority);
      if (jPriority < Thread.MIN_PRIORITY) {
        jPriority = Thread.MIN_PRIORITY;
      } else if (jPriority > Thread.MAX_PRIORITY) {
        jPriority = Thread.MAX_PRIORITY;
      }
      threadImpl.setPriority(jPriority);
    }

    return RubyFixnum.newFixnum(getRuntime(), iPriority);
  }
Exemple #14
0
 /** match_offset */
 @JRubyMethod(name = "offset", compat = CompatVersion.RUBY1_8)
 public IRubyObject offset(ThreadContext context, IRubyObject index) {
   return offsetCommon(context, RubyNumeric.num2int(index), false);
 }
Exemple #15
0
  private static RubyTime createTime(IRubyObject recv, IRubyObject[] args, boolean gmt) {
    Ruby runtime = recv.getRuntime();
    int len = ARG_SIZE;
    Boolean isDst = null;

    DateTimeZone dtz;
    if (gmt) {
      dtz = DateTimeZone.UTC;
    } else if (args.length == 10 && args[9] instanceof RubyString) {
      dtz = getTimeZone(runtime, ((RubyString) args[9]).toString());
    } else {
      dtz = getLocalTimeZone(runtime);
    }

    if (args.length == 10) {
      if (args[8] instanceof RubyBoolean) {
        isDst = ((RubyBoolean) args[8]).isTrue();
      }
      args =
          new IRubyObject[] {
            args[5], args[4], args[3], args[2], args[1], args[0], runtime.getNil()
          };
    } else {
      // MRI accepts additional wday argument which appears to be ignored.
      len = args.length;

      if (len < ARG_SIZE) {
        IRubyObject[] newArgs = new IRubyObject[ARG_SIZE];
        System.arraycopy(args, 0, newArgs, 0, args.length);
        for (int i = len; i < ARG_SIZE; i++) {
          newArgs[i] = runtime.getNil();
        }
        args = newArgs;
        len = ARG_SIZE;
      }
    }

    if (args[0] instanceof RubyString) {
      args[0] = RubyNumeric.str2inum(runtime, (RubyString) args[0], 10, false);
    }

    int year = (int) RubyNumeric.num2long(args[0]);
    int month = 1;

    if (len > 1) {
      if (!args[1].isNil()) {
        IRubyObject tmp = args[1].checkStringType();
        if (!tmp.isNil()) {
          String monthString = tmp.toString().toLowerCase();
          Integer monthInt = MONTHS_MAP.get(monthString);

          if (monthInt != null) {
            month = monthInt;
          } else {
            try {
              month = Integer.parseInt(monthString);
            } catch (NumberFormatException nfExcptn) {
              throw runtime.newArgumentError("Argument out of range.");
            }
          }
        } else {
          month = (int) RubyNumeric.num2long(args[1]);
        }
      }
      if (1 > month || month > 12) {
        throw runtime.newArgumentError("Argument out of range: for month: " + month);
      }
    }

    int[] int_args = {1, 0, 0, 0, 0, 0};

    for (int i = 0; int_args.length >= i + 2; i++) {
      if (!args[i + 2].isNil()) {
        if (!(args[i + 2] instanceof RubyNumeric)) {
          args[i + 2] = args[i + 2].callMethod(runtime.getCurrentContext(), "to_i");
        }

        long value = RubyNumeric.num2long(args[i + 2]);
        if (time_min[i] > value || value > time_max[i]) {
          throw runtime.newArgumentError("argument out of range.");
        }
        int_args[i] = (int) value;
      }
    }

    if (!runtime.is1_9()) {
      if (0 <= year && year < 39) {
        year += 2000;
      } else if (69 <= year && year < 139) {
        year += 1900;
      }
    }

    DateTime dt;
    // set up with min values and then add to allow rolling over
    try {
      dt = new DateTime(year, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC);

      dt =
          dt.plusMonths(month - 1)
              .plusDays(int_args[0] - 1)
              .plusHours(int_args[1])
              .plusMinutes(int_args[2])
              .plusSeconds(int_args[3]);
      if (runtime.is1_9() && !args[5].isNil()) {
        double millis = RubyFloat.num2dbl(args[5]);
        int int_millis = (int) (millis * 1000) % 1000;
        dt = dt.plusMillis(int_millis);
      }

      dt = dt.withZoneRetainFields(dtz);

      // we might need to perform a DST correction
      if (isDst != null) {
        // the instant at which we will ask dtz what the difference between DST and
        // standard time is
        long offsetCalculationInstant = dt.getMillis();

        // if we might be moving this time from !DST -> DST, the offset is assumed
        // to be the same as it was just before we last moved from DST -> !DST
        if (dtz.isStandardOffset(dt.getMillis())) {
          offsetCalculationInstant = dtz.previousTransition(offsetCalculationInstant);
        }

        int offset =
            dtz.getStandardOffset(offsetCalculationInstant)
                - dtz.getOffset(offsetCalculationInstant);

        if (!isDst && !dtz.isStandardOffset(dt.getMillis())) {
          dt = dt.minusMillis(offset);
        }
        if (isDst && dtz.isStandardOffset(dt.getMillis())) {
          dt = dt.plusMillis(offset);
        }
      }
    } catch (org.joda.time.IllegalFieldValueException e) {
      throw runtime.newArgumentError("time out of range");
    }

    RubyTime time = new RubyTime(runtime, (RubyClass) recv, dt);
    // Ignores usec if 8 args (for compatibility with parsedate) or if not supplied.
    if (args.length != 8 && !args[6].isNil()) {
      int usec = int_args[4] % 1000;
      int msec = int_args[4] / 1000;

      if (int_args[4] < 0) {
        msec -= 1;
        usec += 1000;
      }
      time.dt = dt.withMillis(dt.getMillis() + msec);
      time.setUSec(usec);
    }

    time.callInit(IRubyObject.NULL_ARRAY, Block.NULL_BLOCK);
    return time;
  }