Beispiel #1
0
 private IRubyObject stepCommon19(ThreadContext context, IRubyObject step, Block block) {
   Ruby runtime = context.getRuntime();
   if (begin instanceof RubyFixnum && end instanceof RubyFixnum && step instanceof RubyFixnum) {
     fixnumStep(context, runtime, ((RubyFixnum) step).getLongValue(), block);
   } else if (begin instanceof RubyFloat
       || end instanceof RubyFloat
       || step instanceof RubyFloat) {
     RubyNumeric.floatStep19(context, runtime, begin, end, step, isExclusive, block);
   } else if (begin instanceof RubyNumeric
       || !checkIntegerType(runtime, begin, "to_int").isNil()
       || !checkIntegerType(runtime, end, "to_int").isNil()) {
     numericStep19(context, runtime, step, block);
   } else {
     IRubyObject tmp = begin.checkStringType();
     if (!tmp.isNil()) {
       StepBlockCallBack callback = new StepBlockCallBack(block, RubyFixnum.one(runtime), step);
       Block blockCallback =
           CallBlock.newCallClosure(
               this, runtime.getRange(), Arity.singleArgument(), callback, context);
       ((RubyString) tmp).uptoCommon19(context, end, isExclusive, blockCallback);
     } else {
       if (!begin.respondsTo("succ"))
         throw runtime.newTypeError("can't iterate from " + begin.getMetaClass().getName());
       // range_each_func(range, step_i, b, e, args);
       rangeEach(context, new StepBlockCallBack(block, RubyFixnum.one(runtime), step));
     }
   }
   return this;
 }
Beispiel #2
0
  private IRubyObject stepCommon(ThreadContext context, IRubyObject step, Block block) {
    final Ruby runtime = context.getRuntime();
    long unit = RubyNumeric.num2long(step);
    if (unit < 0) throw runtime.newArgumentError("step can't be negative");

    if (begin instanceof RubyFixnum && end instanceof RubyFixnum) {
      if (unit == 0) throw runtime.newArgumentError("step can't be 0");
      fixnumStep(context, runtime, unit, block);
    } else {
      IRubyObject tmp = begin.checkStringType();
      if (!tmp.isNil()) {
        if (unit == 0) throw runtime.newArgumentError("step can't be 0");
        // rb_iterate((VALUE(*)_((VALUE)))str_step, (VALUE)args, step_i, (VALUE)iter);
        StepBlockCallBack callback = new StepBlockCallBack(block, RubyFixnum.one(runtime), step);
        Block blockCallback =
            CallBlock.newCallClosure(
                this, runtime.getRange(), Arity.singleArgument(), callback, context);
        ((RubyString) tmp).uptoCommon(context, end, isExclusive, blockCallback);
      } else if (begin instanceof RubyNumeric) {
        if (equalInternal(context, step, RubyFixnum.zero(runtime)))
          throw runtime.newArgumentError("step can't be 0");
        numericStep(context, runtime, step, block);
      } else {
        if (unit == 0) throw runtime.newArgumentError("step can't be 0");
        if (!begin.respondsTo("succ"))
          throw runtime.newTypeError("can't iterate from " + begin.getMetaClass().getName());
        // range_each_func(range, step_i, b, e, args);
        rangeEach(context, new StepBlockCallBack(block, RubyFixnum.one(runtime), step));
      }
    }
    return this;
  }
Beispiel #3
0
 private static RubyArray coerceResult(
     final Ruby runtime, final IRubyObject result, final boolean err) {
   if (!(result instanceof RubyArray) || ((RubyArray) result).getLength() != 2) {
     if (err) throw runtime.newTypeError("coerce must return [x, y]");
     return null;
   } else if (!result.isNil()) {
     RubyWarnings warnings = runtime.getWarnings();
     warnings.warn("Bad return value for #coerce, called by numerical comparison operators.");
     warnings.warn("#coerce must return [x, y]. The next release will raise an error for this.");
   }
   return (RubyArray) result;
 }
Beispiel #4
0
  private IRubyObject prepareRaiseException(Ruby runtime, IRubyObject[] args, Block block) {
    if (args.length == 0) {
      IRubyObject lastException = errorInfo;
      if (lastException.isNil()) {
        return new RaiseException(runtime, runtime.getRuntimeError(), "", false).getException();
      }
      return lastException;
    }

    IRubyObject exception;
    ThreadContext context = getRuntime().getCurrentContext();

    if (args.length == 1) {
      if (args[0] instanceof RubyString) {
        return runtime.getRuntimeError().newInstance(context, args, block);
      }

      if (!args[0].respondsTo("exception")) {
        return runtime.newTypeError("exception class/object expected").getException();
      }
      exception = args[0].callMethod(context, "exception");
    } else {
      if (!args[0].respondsTo("exception")) {
        return runtime.newTypeError("exception class/object expected").getException();
      }

      exception = args[0].callMethod(context, "exception", args[1]);
    }

    if (!runtime.getException().isInstance(exception)) {
      return runtime.newTypeError("exception object expected").getException();
    }

    if (args.length == 3) {
      ((RubyException) exception).set_backtrace(args[2]);
    }

    return exception;
  }
Beispiel #5
0
  public static IRubyObject inheritanceSearchConst(
      ThreadContext context,
      IRubyObject cmVal,
      MutableCallSite site,
      String constName,
      boolean noPrivateConsts)
      throws Throwable {
    Ruby runtime = context.runtime;
    RubyModule module;

    if (cmVal instanceof RubyModule) {
      module = (RubyModule) cmVal;
    } else {
      throw runtime.newTypeError(cmVal + " is not a type/class");
    }

    IRubyObject constant =
        noPrivateConsts
            ? module.getConstantFromNoConstMissing(constName, false)
            : module.getConstantNoConstMissing(constName);

    if (constant == null) {
      constant = UndefinedValue.UNDEFINED;
    }

    SwitchPoint switchPoint = (SwitchPoint) runtime.getConstantInvalidator(constName).getData();

    // bind constant until invalidated
    MethodHandle target = Binder.from(site.type()).drop(0, 2).constant(constant);
    MethodHandle fallback =
        Binder.from(site.type())
            .append(site, constName)
            .append(noPrivateConsts)
            .invokeStatic(LOOKUP, Bootstrap.class, "inheritanceSearchConst");

    // test that module is same as before
    MethodHandle test =
        Binder.from(site.type().changeReturnType(boolean.class))
            .drop(0, 1)
            .insert(1, module.id)
            .invokeStaticQuiet(LOOKUP, Bootstrap.class, "testArg0ModuleMatch");
    target = guardWithTest(test, target, fallback);
    site.setTarget(switchPoint.guardWithTest(target, fallback));

    return constant;
  }
Beispiel #6
0
 @JRubyMethod(name = "max", frame = true, compat = CompatVersion.RUBY1_9)
 public IRubyObject max(ThreadContext context, Block block) {
   if (block.isGiven() || isExclusive && !(end instanceof RubyNumeric)) {
     return RuntimeHelpers.invokeSuper(context, this, block);
   } else {
     int c = RubyComparable.cmpint(context, begin.callMethod(context, "<=>", end), begin, end);
     Ruby runtime = context.getRuntime();
     if (isExclusive) {
       if (!(end instanceof RubyInteger))
         throw runtime.newTypeError("cannot exclude non Integer end value");
       if (c == 0) return runtime.getNil();
       if (end instanceof RubyFixnum)
         return RubyFixnum.newFixnum(runtime, ((RubyFixnum) end).getLongValue() - 1);
       return end.callMethod(context, "-", RubyFixnum.one(runtime));
     }
     return end;
   }
 }
Beispiel #7
0
  /** Numeric methods. (num_*) */
  protected IRubyObject[] getCoerced(ThreadContext context, IRubyObject other, boolean error) {
    final Ruby runtime = context.runtime;
    final IRubyObject $ex = context.getErrorInfo();
    final IRubyObject result;
    try {
      result = other.callMethod(context, "coerce", this);
    } catch (RaiseException e) { // e.g. NoMethodError: undefined method `coerce'
      context.setErrorInfo($ex); // restore $!

      if (error) {
        throw runtime.newTypeError(
            other.getMetaClass().getName() + " can't be coerced into " + getMetaClass().getName());
      }
      return null;
    }

    return coerceResult(runtime, result, true).toJavaArray();
  }
Beispiel #8
0
  @JRubyMethod(required = 1, visibility = Visibility.PRIVATE)
  @Override
  public IRubyObject initialize_copy(IRubyObject original) {
    if (this == original) return this;

    checkFrozen();

    Ruby runtime = getRuntime();
    ThreadContext context = runtime.getCurrentContext();
    if (!(original instanceof RubyMatchData)) {
      throw runtime.newTypeError("wrong argument class");
    }

    RubyMatchData origMatchData = (RubyMatchData) original;
    str = origMatchData.str;
    regs = origMatchData.regs;

    return this;
  }
Beispiel #9
0
  protected static RubyTime s_mload(IRubyObject recv, RubyTime time, IRubyObject from) {
    Ruby runtime = recv.getRuntime();

    DateTime dt = new DateTime(DateTimeZone.UTC);

    byte[] fromAsBytes = null;
    fromAsBytes = from.convertToString().getBytes();
    if (fromAsBytes.length != 8) {
      throw runtime.newTypeError("marshaled time format differ");
    }
    int p = 0;
    int s = 0;
    for (int i = 0; i < 4; i++) {
      p |= ((int) fromAsBytes[i] & 0xFF) << (8 * i);
    }
    for (int i = 4; i < 8; i++) {
      s |= ((int) fromAsBytes[i] & 0xFF) << (8 * (i - 4));
    }
    boolean utc = false;
    if ((p & (1 << 31)) == 0) {
      dt = dt.withMillis(p * 1000L);
      time.setUSec((s & 0xFFFFF) % 1000);
    } else {
      p &= ~(1 << 31);
      utc = ((p >>> 30 & 0x1) == 0x1);
      dt = dt.withYear(((p >>> 14) & 0xFFFF) + 1900);
      dt = dt.withMonthOfYear(((p >>> 10) & 0xF) + 1);
      dt = dt.withDayOfMonth(((p >>> 5) & 0x1F));
      dt = dt.withHourOfDay((p & 0x1F));
      dt = dt.withMinuteOfHour(((s >>> 26) & 0x3F));
      dt = dt.withSecondOfMinute(((s >>> 20) & 0x3F));
      // marsaling dumps usec, not msec
      dt = dt.withMillisOfSecond((s & 0xFFFFF) / 1000);
      time.setUSec((s & 0xFFFFF) % 1000);
    }
    time.setDateTime(dt);
    if (!utc) time.localtime();

    from.getInstanceVariables().copyInstanceVariablesInto(time);
    return time;
  }