Exemplo n.º 1
0
  final int[] begLenInt(int len, int err) {
    int beg = RubyNumeric.num2int(this.begin);
    int end = RubyNumeric.num2int(this.end);

    if (beg < 0) {
      beg += len;
      if (beg < 0) {
        if (err != 0)
          throw getRuntime()
              .newRangeError(beg + ".." + (isExclusive ? "." : "") + end + " out of range");
        return null;
      }
    }

    if (err == 0 || err == 2) {
      if (beg > len) {
        if (err != 0)
          throw getRuntime()
              .newRangeError(beg + ".." + (isExclusive ? "." : "") + end + " out of range");
        return null;
      }
      if (end > len) end = len;
    }

    if (end < 0) end += len;
    if (!isExclusive) end++;
    len = end - beg;
    if (len < 0) len = 0;

    return new int[] {beg, len};
  }
Exemplo n.º 2
0
  @JRubyMethod(name = "seek", required = 1, optional = 1, frame = true)
  public IRubyObject seek(IRubyObject[] args) {
    // MRI 1.8.7 behavior:
    // checkOpen();
    checkFinalized();
    long amount = RubyNumeric.num2long(args[0]);
    int whence = Stream.SEEK_SET;
    long newPosition = pos;

    if (args.length > 1 && !args[0].isNil()) whence = RubyNumeric.fix2int(args[1]);

    if (whence == Stream.SEEK_CUR) {
      newPosition += amount;
    } else if (whence == Stream.SEEK_END) {
      newPosition = internal.getByteList().length() + amount;
    } else {
      newPosition = amount;
    }

    if (newPosition < 0) throw getRuntime().newErrnoEINVALError();

    pos = newPosition;
    eof = false;

    return RubyFixnum.zero(getRuntime());
  }
Exemplo n.º 3
0
  @JRubyMethod(required = 1, optional = 1)
  public IRubyObject seek(ThreadContext context, IRubyObject[] args) {
    Ruby runtime = context.runtime;

    checkFrozen();
    checkFinalized();

    int offset = RubyNumeric.num2int(args[0]);
    IRubyObject whence = context.nil;

    if (args.length > 1 && !args[0].isNil()) whence = args[1];

    checkOpen();

    switch (whence.isNil() ? 0 : RubyNumeric.num2int(whence)) {
      case 0:
        break;
      case 1:
        offset += ptr.pos;
        break;
      case 2:
        offset += ptr.string.size();
        break;
      default:
        throw runtime.newErrnoEINVALError("invalid whence");
    }

    if (offset < 0) throw runtime.newErrnoEINVALError("invalid seek value");

    ptr.pos = offset;

    return RubyFixnum.zero(runtime);
  }
Exemplo n.º 4
0
  @JRubyMethod(required = 1, optional = 1)
  @Override
  public IRubyObject seek(IRubyObject[] args) {
    checkOpen();
    checkFinalized();
    long amount = RubyNumeric.num2long(args[0]);
    int whence = Stream.SEEK_SET;
    long newPosition = data.pos;

    if (args.length > 1 && !args[0].isNil()) whence = RubyNumeric.fix2int(args[1]);

    if (whence == Stream.SEEK_CUR) {
      newPosition += amount;
    } else if (whence == Stream.SEEK_END) {
      newPosition = data.internal.getByteList().length() + amount;
    } else if (whence == Stream.SEEK_SET) {
      newPosition = amount;
    } else {
      throw getRuntime().newErrnoEINVALError("invalid whence");
    }

    if (newPosition < 0) throw getRuntime().newErrnoEINVALError("invalid seek value");

    data.pos = newPosition;
    data.eof = false;

    return RubyFixnum.zero(getRuntime());
  }
Exemplo n.º 5
0
  @JRubyMethod
  public IRubyObject iconv(IRubyObject str, IRubyObject startArg, IRubyObject endArg) {
    int start = 0;
    int end = -1;

    if (!startArg.isNil()) start = RubyNumeric.fix2int(startArg);
    if (!endArg.isNil()) end = RubyNumeric.fix2int(endArg);

    return iconv(str, start, end);
  }
Exemplo n.º 6
0
 /** num_div */
 @JRubyMethod(name = "div")
 public IRubyObject div19(ThreadContext context, IRubyObject other) {
   if (other instanceof RubyNumeric) {
     RubyNumeric numeric = (RubyNumeric) other;
     if (numeric.zero_p(context).isTrue()) {
       throw context.runtime.newZeroDivisionError();
     }
   }
   return callMethod(context, "/", other).callMethod(context, "floor");
 }
Exemplo n.º 7
0
  @JRubyMethod
  public IRubyObject getsockopt(ThreadContext context, IRubyObject lev, IRubyObject optname) {
    int level = RubyNumeric.fix2int(lev);
    int opt = RubyNumeric.fix2int(optname);
    Ruby runtime = context.getRuntime();

    try {
      switch (SocketLevel.valueOf(level)) {
        case SOL_IP:
        case SOL_SOCKET:
        case SOL_TCP:
        case SOL_UDP:
          switch (SocketOption.valueOf(opt)) {
            case SO_BROADCAST:
              return getBroadcast(runtime);
            case SO_KEEPALIVE:
              return getKeepAlive(runtime);
            case SO_LINGER:
              return getLinger(runtime);
            case SO_OOBINLINE:
              return getOOBInline(runtime);
            case SO_RCVBUF:
              return getRcvBuf(runtime);
            case SO_REUSEADDR:
              return getReuseAddr(runtime);
            case SO_SNDBUF:
              return getSndBuf(runtime);
            case SO_RCVTIMEO:
            case SO_SNDTIMEO:
              return getTimeout(runtime);
            case SO_TYPE:
              return getSoType(runtime);

              // Can't support the rest with Java
            case SO_RCVLOWAT:
              return number(runtime, 1);
            case SO_SNDLOWAT:
              return number(runtime, 2048);
            case SO_DEBUG:
            case SO_ERROR:
            case SO_DONTROUTE:
            case SO_TIMESTAMP:
              return trueFalse(runtime, false);
            default:
              throw context.getRuntime().newErrnoENOPROTOOPTError();
          }
        default:
          throw context.getRuntime().newErrnoENOPROTOOPTError();
      }
    } catch (IOException e) {
      throw context.getRuntime().newErrnoENOPROTOOPTError();
    }
  }
Exemplo n.º 8
0
    @JRubyMethod(name = "read", optional = 1)
    public IRubyObject read(IRubyObject[] args) throws IOException {
      if (args.length == 0 || args[0].isNil()) {
        ByteList val = new ByteList(10);
        byte[] buffer = new byte[BUFF_SIZE];
        int read = io.read(buffer);
        while (read != -1) {
          val.append(buffer, 0, read);
          read = io.read(buffer);
        }
        return RubyString.newString(getRuntime(), val);
      }

      int len = RubyNumeric.fix2int(args[0]);
      if (len < 0) {
        throw getRuntime().newArgumentError("negative length " + len + " given");
      } else if (len > 0) {
        byte[] buffer = new byte[len];
        int toRead = len;
        int offset = 0;
        int read = 0;
        while (toRead > 0) {
          read = io.read(buffer, offset, toRead);
          if (read == -1) {
            break;
          }
          toRead -= read;
          offset += read;
        } // hmm...
        return RubyString.newString(getRuntime(), new ByteList(buffer, 0, len - toRead, false));
      }

      return RubyString.newEmptyString(getRuntime());
    }
Exemplo n.º 9
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();
 }
Exemplo n.º 10
0
  @JRubyMethod(optional = 1)
  public IRubyObject shutdown(ThreadContext context, IRubyObject[] args) {
    int how = 2;

    if (args.length > 0) {
      String howString = null;
      if (args[0] instanceof RubyString) {
        howString = ((RubyString) args[0]).asJavaString();
      } else if (args[0] instanceof RubySymbol) {
        howString = ((RubySymbol) args[0]).asJavaString();
      }

      if (howString != null) {
        if (howString.equals("RD") || howString.equals("SHUT_RD")) {
          how = 0;
        } else if (howString.equals("WR") || howString.equals("SHUT_WR")) {
          how = 1;
        } else if (howString.equals("RDWR") || howString.equals("SHUT_RDWR")) {
          how = 2;
        }
      } else {
        how = RubyNumeric.fix2int(args[0]);
      }
    }

    try {
      return shutdownInternal(context, how);

    } catch (BadDescriptorException e) {
      throw context.runtime.newErrnoEBADFError();
    }
  }
Exemplo n.º 11
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;
 }
Exemplo n.º 12
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;
  }
Exemplo n.º 13
0
  @JRubyMethod(name = "lineno=", required = 1)
  @Override
  public IRubyObject set_lineno(IRubyObject arg) {
    data.lineno = RubyNumeric.fix2int(arg);

    return getRuntime().getNil();
  }
Exemplo n.º 14
0
 @JRubyMethod(name = "generate", meta = true)
 public static IRubyObject generate(IRubyObject recv, IRubyObject arg) {
   int keysize = RubyNumeric.fix2int(arg);
   PKeyDSA dsa = new PKeyDSA(recv.getRuntime(), (RubyClass) recv);
   dsaGenerate(dsa, keysize);
   return dsa;
 }
Exemplo n.º 15
0
  @JRubyMethod(name = "round", compat = CompatVersion.RUBY1_9)
  public IRubyObject round19(ThreadContext context, IRubyObject arg) {
    int ndigits = RubyNumeric.num2int(arg);
    if (ndigits > 0) return RubyKernel.new_float(this, this);
    if (ndigits == 0) return this;
    Ruby runtime = context.runtime;

    long bytes = (this instanceof RubyFixnum) ? 8 : RubyFixnum.fix2long(callMethod("size"));
    /* If 10**N/2 > this, return 0 */
    /* We have log_256(10) > 0.415241 and log_256(1/2)=-0.125 */
    if (-0.415241 * ndigits - 0.125 > bytes) {
      return RubyFixnum.zero(runtime);
    }

    IRubyObject f = Numeric.int_pow(context, 10, -ndigits);

    if (this instanceof RubyFixnum && f instanceof RubyFixnum) {
      long x = ((RubyFixnum) this).getLongValue();
      long y = ((RubyFixnum) f).getLongValue();
      boolean neg = x < 0;
      if (neg) x = -x;
      x = (x + y / 2) / y * y;
      if (neg) x = -x;
      return RubyFixnum.newFixnum(runtime, x);
    } else if (f instanceof RubyFloat) {
      return RubyFixnum.zero(runtime);
    } else {
      IRubyObject h = f.callMethod(context, "/", RubyFixnum.two(runtime));
      IRubyObject r = callMethod(context, "%", f);
      IRubyObject n = callMethod(context, "-", r);
      String op = callMethod(context, "<", RubyFixnum.zero(runtime)).isTrue() ? "<=" : "<";
      if (!r.callMethod(context, op, h).isTrue()) n = n.callMethod(context, "+", f);
      return n;
    }
  }
Exemplo n.º 16
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);
 }
Exemplo n.º 17
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);
 }
Exemplo n.º 18
0
 // c: rand_int
 private static IRubyObject randInt(
     ThreadContext context, RandomType random, RubyInteger vmax, boolean restrictive) {
   if (vmax instanceof RubyFixnum) {
     long max = RubyNumeric.fix2long(vmax);
     if (max == 0) {
       return context.nil;
     }
     if (max < 0) {
       if (restrictive) {
         return context.nil;
       }
       max = -max;
     }
     return randLimitedFixnum(context, random, max - 1);
   } else {
     BigInteger big = vmax.getBigIntegerValue();
     if (big.equals(BigInteger.ZERO)) {
       return context.nil;
     }
     if (big.signum() < 0) {
       if (restrictive) {
         return context.nil;
       }
       big = big.abs();
     }
     big = big.subtract(BigInteger.ONE);
     return randLimitedBignum(context, random, RubyBignum.newBignum(context.runtime, big));
   }
 }
Exemplo n.º 19
0
  @JRubyMethod(name = "round", compat = CompatVersion.RUBY1_9)
  public IRubyObject round19(ThreadContext context, IRubyObject arg) {
    int ndigits = RubyNumeric.num2int(arg);
    if (ndigits > 0) return RubyKernel.new_float(this, this);
    if (ndigits == 0) return this;
    ndigits = -ndigits;
    Ruby runtime = context.getRuntime();
    if (ndigits < 0) throw runtime.newArgumentError("ndigits out of range");
    IRubyObject f = Numeric.int_pow(context, 10, ndigits);

    if (this instanceof RubyFixnum && f instanceof RubyFixnum) {
      long x = ((RubyFixnum) this).getLongValue();
      long y = ((RubyFixnum) f).getLongValue();
      boolean neg = x < 0;
      if (neg) x = -x;
      x = (x + y / 2) / y * y;
      if (neg) x = -x;
      return RubyFixnum.newFixnum(runtime, x);
    } else {
      IRubyObject h = f.callMethod(context, "/", RubyFixnum.two(runtime));
      IRubyObject r = callMethod(context, "%", f);
      IRubyObject n = callMethod(context, "-", r);
      if (!r.callMethod(context, "<", h).isTrue()) n = n.callMethod(context, "+", f);
      return n;
    }
  }
Exemplo n.º 20
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);
  }
Exemplo n.º 21
0
  @JRubyMethod(name = "pos=", required = 1)
  public IRubyObject set_pos(IRubyObject arg) {
    pos = RubyNumeric.fix2int(arg);
    if (pos < 0) {
      throw getRuntime().newErrnoEINVALError("Invalid argument");
    }

    return getRuntime().getNil();
  }
Exemplo n.º 22
0
 @JRubyMethod(name = "deflate", required = 1, optional = 1)
 public IRubyObject deflate(IRubyObject[] args) throws Exception {
   args = Arity.scanArgs(getRuntime(), args, 1, 1);
   int flush = 0; // NO_FLUSH
   if (!args[1].isNil()) {
     flush = RubyNumeric.fix2int(args[1]);
   }
   return defl.deflate(args[0].convertToString().getByteList(), flush);
 }
Exemplo n.º 23
0
 @JRubyMethod(name = "deflate", required = 1, optional = 1, meta = true)
 public static IRubyObject s_deflate(IRubyObject recv, IRubyObject[] args) throws Exception {
   args = Arity.scanArgs(recv.getRuntime(), args, 1, 1);
   int level = -1;
   if (!args[1].isNil()) {
     level = RubyNumeric.fix2int(args[1]);
   }
   return ZlibDeflate.s_deflate(recv, args[0].convertToString().getByteList(), level);
 }
Exemplo n.º 24
0
 protected static SocketLevel levelFromArg(IRubyObject _level) {
   SocketLevel level;
   if (_level instanceof RubyString || _level instanceof RubySymbol) {
     level = SocketLevel.valueOf("SOL_" + _level.toString());
   } else {
     level = SocketLevel.valueOf(RubyNumeric.fix2int(_level));
   }
   return level;
 }
Exemplo n.º 25
0
 @JRubyMethod(name = "flush", optional = 1)
 public IRubyObject flush(IRubyObject[] args) throws IOException {
   if (args.length == 0
       || args[0].isNil()
       || RubyNumeric.fix2int(args[0]) != 0) { // Zlib::NO_FLUSH
     io.flush();
   }
   return getRuntime().getNil();
 }
Exemplo n.º 26
0
 protected static SocketOption optionFromArg(IRubyObject _opt) {
   SocketOption opt;
   if (_opt instanceof RubyString || _opt instanceof RubySymbol) {
     opt = SocketOption.valueOf("SO_" + _opt.toString());
   } else {
     opt = SocketOption.valueOf(RubyNumeric.fix2int(_opt));
   }
   return opt;
 }
Exemplo n.º 27
0
 protected boolean asBoolean(IRubyObject val) {
   if (val instanceof RubyString) {
     return stringAsNumber(val) != 0;
   } else if (val instanceof RubyNumeric) {
     return RubyNumeric.fix2int(val) != 0;
   } else {
     return val.isTrue();
   }
 }
Exemplo n.º 28
0
 private int asNumber(IRubyObject val) {
   if (val instanceof RubyNumeric) {
     return RubyNumeric.fix2int(val);
   } else if (val instanceof RubyBoolean) {
     return val.isTrue() ? 1 : 0;
   } else {
     return stringAsNumber(val);
   }
 }
Exemplo n.º 29
0
  private int stringAsNumber(IRubyObject val) {
    ByteList str = val.convertToString().getByteList();
    IRubyObject res = Pack.unpack(getRuntime(), str, FORMAT_SMALL_I).entry(0);

    if (res.isNil()) {
      throw getRuntime().newErrnoEINVALError();
    }

    return RubyNumeric.fix2int(res);
  }
Exemplo n.º 30
0
  @JRubyMethod
  public IRubyObject recv(ThreadContext context, IRubyObject _length) {
    Ruby runtime = context.runtime;

    ByteList bytes = doReceive(context, RubyNumeric.fix2int(_length));

    if (bytes == null) return context.nil;

    return RubyString.newString(runtime, bytes);
  }