Пример #1
0
  protected Block prepareBlock(
      ThreadContext context, IRubyObject self, DynamicScope currDynScope, Object[] temp) {
    if (closure == null) return Block.NULL_BLOCK;

    Object value = closure.retrieve(context, self, currDynScope, temp);

    Block b = null;
    if (value instanceof Block) b = (Block) value;
    else if (value instanceof RubyProc) b = ((RubyProc) value).getBlock();
    else if (value instanceof RubyMethod)
      b = ((RubyProc) ((RubyMethod) value).to_proc(context, null)).getBlock();
    else if ((value instanceof IRubyObject) && ((IRubyObject) value).isNil()) b = Block.NULL_BLOCK;
    else if (value instanceof IRubyObject)
      b =
          ((RubyProc)
                  TypeConverter.convertToType(
                      (IRubyObject) value, context.getRuntime().getProc(), "to_proc", true))
              .getBlock();
    else
      throw new RuntimeException(
          "Unhandled case in CallInstr:prepareBlock.  Got block arg: " + value);

    // Blocks passed in through calls are always normal blocks, no matter where they came from
    b.type = Block.Type.NORMAL;
    return b;
  }
Пример #2
0
 private static IRubyObject checkMaxInt(ThreadContext context, IRubyObject vmax) {
   if (!(vmax instanceof RubyFloat)) {
     IRubyObject v = TypeConverter.checkIntegerType(context, vmax);
     if (!v.isNil()) {
       return v;
     }
   }
   return null;
 }
Пример #3
0
 @JRubyMethod(
     name = {"include?", "member?"},
     frame = true,
     compat = CompatVersion.RUBY1_9)
 public IRubyObject include_p19(ThreadContext context, IRubyObject obj) {
   Ruby runtime = context.getRuntime();
   if (begin instanceof RubyNumeric
       || end instanceof RubyNumeric
       || !TypeConverter.convertToTypeWithCheck(begin, runtime.getInteger(), "to_int").isNil()
       || !TypeConverter.convertToTypeWithCheck(end, runtime.getInteger(), "to_int").isNil()) {
     if (rangeLe(context, begin, obj) != null) {
       if (isExclusive) {
         if (rangeLt(context, obj, end) != null) return runtime.getTrue();
       } else {
         if (rangeLe(context, obj, end) != null) return runtime.getTrue();
       }
     }
     return runtime.getFalse();
   } else if (begin instanceof RubyString
       && end instanceof RubyString
       && ((RubyString) begin).getByteList().realSize == 1
       && ((RubyString) end).getByteList().realSize == 1) {
     if (obj.isNil()) return runtime.getFalse();
     if (obj instanceof RubyString) {
       ByteList Vbytes = ((RubyString) obj).getByteList();
       if (Vbytes.realSize != 1) return runtime.getFalse();
       int v = Vbytes.bytes[Vbytes.begin] & 0xff;
       ByteList Bbytes = ((RubyString) begin).getByteList();
       int b = Bbytes.bytes[Bbytes.begin] & 0xff;
       ByteList Ebytes = ((RubyString) end).getByteList();
       int e = Ebytes.bytes[Ebytes.begin] & 0xff;
       if (Encoding.isAscii(v) && Encoding.isAscii(b) && Encoding.isAscii(e)) {
         if ((b <= v && v < e) || (!isExclusive && v == e)) return runtime.getTrue();
         return runtime.getFalse();
       }
     }
   }
   return RuntimeHelpers.invokeSuper(context, this, obj, Block.NULL_BLOCK);
 }
Пример #4
0
  public static RubyArray convertToRubyArrayWithCoerce(Ruby runtime, IRubyObject value) {
    if (value instanceof RubyArray) return ((RubyArray) value);

    IRubyObject newValue = TypeConverter.convertToType(value, runtime.getArray(), "to_ary", false);

    if (newValue.isNil()) {
      return RubyArray.newArrayLight(runtime, value);
    }

    // empirically it appears that to_ary coersions always return array or nil, so this
    // should always be an array by now.
    return (RubyArray) newValue;
  }
Пример #5
0
  @JRubyMethod(visibility = PRIVATE)
  @Override
  public IRubyObject initialize_copy(IRubyObject other) {
    RubyStringIO otherIO =
        (RubyStringIO)
            TypeConverter.convertToType(other, getRuntime().getClass("StringIO"), "to_strio");

    if (this == otherIO) return this;

    data = otherIO.data;
    if (otherIO.isTaint()) setTaint(true);

    return this;
  }
Пример #6
0
  @JRubyMethod(visibility = PRIVATE)
  public IRubyObject initialize_copy(ThreadContext context, IRubyObject other) {
    StringIO otherIO =
        (StringIO)
            TypeConverter.convertToType(other, context.runtime.getClass("StringIO"), "to_strio");

    if (this == otherIO) return this;

    ptr = otherIO.ptr;
    infectBy(otherIO);
    flags &= ~STRIO_READWRITE;
    flags |= otherIO.flags & STRIO_READWRITE;

    return this;
  }
Пример #7
0
  public static RubyArray convertToRubyArrayWithCoerce(Ruby runtime, IRubyObject value) {
    if (value instanceof RubyArray) return ((RubyArray) value);

    IRubyObject newValue = TypeConverter.convertToType(value, runtime.getArray(), "to_ary", false);

    if (newValue.isNil()) {
      return RubyArray.newArrayLight(runtime, value);
    }

    // must be array by now, or error
    if (!(newValue instanceof RubyArray)) {
      throw runtime.newTypeError(newValue.getMetaClass() + "#" + "to_ary" + " should return Array");
    }

    return (RubyArray) newValue;
  }
Пример #8
0
  @JRubyMethod(visibility = Visibility.PRIVATE)
  public IRubyObject initialize_copy(IRubyObject other) {

    RubyStringIO otherIO =
        (RubyStringIO)
            TypeConverter.convertToType(other, getRuntime().fastGetClass("StringIO"), "to_strio");

    if (this == otherIO) {
      return this;
    }

    pos = otherIO.pos;
    lineno = otherIO.lineno;
    eof = otherIO.eof;
    closedRead = otherIO.closedRead;
    closedWrite = otherIO.closedWrite;
    internal = otherIO.internal;
    modes = otherIO.modes;
    if (otherIO.isTaint()) {
      setTaint(true);
    }

    return this;
  }
Пример #9
0
 public static IRubyObject getOptionsArg(Ruby runtime, IRubyObject arg) {
   return TypeConverter.checkHashType(runtime, arg);
 }
Пример #10
0
 public static IRubyObject getOptionsArg(Ruby runtime, IRubyObject... args) {
   if (args.length >= 1) {
     return TypeConverter.checkHashType(runtime, args[args.length - 1]);
   }
   return runtime.getNil();
 }
Пример #11
0
  @JRubyMethod(required = 2, optional = 4)
  public IRubyObject primitive_convert(ThreadContext context, IRubyObject[] args) {
    Ruby runtime = context.runtime;

    RubyString input;
    RubyString output;
    int outputByteoffset = -1;
    int outputBytesize = 0;
    int flags = 0;

    int hashArg = -1;

    if (args.length > 2 && !args[2].isNil()) {
      if (args.length == 3 && args[2] instanceof RubyHash) {
        hashArg = 2;
      } else {
        outputByteoffset = (int) args[2].convertToInteger().getLongValue();
        if (outputByteoffset < 0) throw runtime.newArgumentError("negative offset");
      }
    }

    if (args.length > 3 && !args[3].isNil()) {
      if (args.length == 4 && args[3] instanceof RubyHash) {
        hashArg = 3;
      } else {
        outputBytesize = (int) args[3].convertToInteger().getLongValue();
        if (outputBytesize < 0) throw runtime.newArgumentError("negative bytesize");
      }
    }

    if (args.length > 4 && !args[4].isNil()) {
      if (args.length > 5 && !args[5].isNil()) {
        throw runtime.newArgumentError(args.length, 5);
      }

      if (args[4] instanceof RubyHash) {
        hashArg = 4;
      } else {
        flags = (int) args[4].convertToInteger().getLongValue();
      }
    }

    IRubyObject opt = context.nil;
    if (hashArg != -1 && !(opt = TypeConverter.checkHashType(runtime, args[hashArg])).isNil()) {
      IRubyObject v = ((RubyHash) opt).op_aref(context, runtime.newSymbol("partial_input"));
      if (v.isTrue()) {
        flags |= EncodingUtils.ECONV_PARTIAL_INPUT;
      }
      v = ((RubyHash) opt).op_aref(context, runtime.newSymbol("after_output"));
      if (v.isTrue()) {
        flags |= EncodingUtils.ECONV_AFTER_OUTPUT;
      }
    } else {
      flags = 0;
    }

    ByteList inBytes;
    ByteList outBytes;

    if (args[0].isNil()) {
      inBytes = new ByteList();
    } else {
      input = args[0].convertToString();
      input.modify19();
      inBytes = input.getByteList();
    }

    output = args[1].convertToString();
    output.modify19();
    outBytes = output.getByteList();

    if (outputByteoffset == -1) {
      outputByteoffset = outBytes.getRealSize();
    } else if (outputByteoffset > outBytes.getRealSize()) {
      throw runtime.newArgumentError("offset too big");
    }

    int outputByteEnd = outputByteoffset + outputBytesize;

    if (outputByteEnd > outBytes.getRealSize()) {
      outBytes.ensure(outputByteEnd);
    }

    RubyCoderResult result =
        transcoder.primitiveConvert(
            context,
            inBytes,
            output.getByteList(),
            outputByteoffset,
            outputBytesize,
            inBytes.getEncoding(),
            inBytes.getEncoding().isAsciiCompatible(),
            flags);

    outBytes.setEncoding(
        transcoder.outEncoding != null ? transcoder.outEncoding : inBytes.getEncoding());

    return symbolFromResult(result, runtime, flags, context);
  }
Пример #12
0
 // c: random_rand
 private static IRubyObject randomRand(
     ThreadContext context, IRubyObject vmax, RandomType random) {
   IRubyObject v;
   RangeLike range = null;
   if (vmax.isNil()) {
     v = context.nil;
   } else if ((v = checkMaxInt(context, vmax)) != null) {
     v = randInt(context, random, (RubyInteger) v, true);
   } else if (!(v = TypeConverter.checkFloatType(context.runtime, vmax)).isNil()) {
     double max = floatValue(v);
     if (max > 0.0) {
       v = context.runtime.newFloat(max * random.genrandReal());
     } else {
       v = context.nil;
     }
   } else if ((range = rangeValues(context, vmax)) != null) {
     if ((v = checkMaxInt(context, range.range)) != null) {
       if (v instanceof RubyFixnum) {
         long max = ((RubyFixnum) v).getLongValue();
         if (range.excl) {
           max -= 1;
         }
         if (max >= 0) {
           v = randLimitedFixnum(context, random, max);
         } else {
           v = context.nil;
         }
       } else if (v instanceof RubyBignum) {
         BigInteger big = ((RubyBignum) v).getBigIntegerValue();
         if (!big.equals(BigInteger.ZERO) && (big.signum() > 0)) {
           if (range.excl) {
             big = big.subtract(BigInteger.ONE);
           }
           v = randLimitedBignum(context, random, RubyBignum.newBignum(context.runtime, big));
         } else {
           v = context.nil;
         }
       } else {
         v = context.nil;
       }
     } else if (!(v = TypeConverter.checkFloatType(context.runtime, range.range)).isNil()) {
       int scale = 1;
       double max = ((RubyFloat) v).getDoubleValue();
       double mid = 0.5;
       double r;
       if (Double.isInfinite(max)) {
         double min = floatValue(range.begin) / 2.0;
         max = floatValue(range.end) / 2.0;
         scale = 2;
         mid = max + min;
         max -= min;
       } else {
         floatValue(v);
       }
       v = context.nil;
       if (max > 0.0) {
         if (range.excl) {
           r = random.genrandReal();
         } else {
           r = random.genrandReal2();
         }
         if (scale > 1) {
           return context.runtime.newFloat(+(+(+(r - 0.5) * max) * scale) + mid);
         }
         v = context.runtime.newFloat(r * max);
       } else if (max == 0.0 && !range.excl) {
         v = context.runtime.newFloat(0.0);
       }
     }
   } else {
     v = context.nil;
     RubyNumeric.num2long(vmax); // need check here to raise TypeError
   }
   if (v.isNil()) {
     throw context.runtime.newArgumentError("invalid argument - " + vmax.toString());
   }
   if (range == null) {
     return v;
   }
   if ((range.begin instanceof RubyFixnum) && (v instanceof RubyFixnum)) {
     long x = ((RubyFixnum) range.begin).getLongValue() + ((RubyFixnum) v).getLongValue();
     return context.runtime.newFixnum(x);
   }
   if (v instanceof RubyBignum) {
     return ((RubyBignum) v).op_plus(context, range.begin);
   } else if (v instanceof RubyFloat) {
     IRubyObject f = TypeConverter.checkFloatType(context.runtime, range.begin);
     if (!f.isNil()) {
       return ((RubyFloat) v).op_plus(context, f);
     }
   }
   return Helpers.invoke(context, range.begin, "+", v);
 }