Beispiel #1
0
  protected RubyValue doStep(RubyValue toArg, RubyValue stepArg, RubyBlock block) {
    if ((toArg instanceof RubyFixnum) && (stepArg instanceof RubyFixnum)) {
      int i = this.value_;
      int end = ((RubyFixnum) toArg).value_;
      int diff = ((RubyFixnum) stepArg).value_;
      if (diff > 0) {
        while (i <= end) {
          RubyValue v = block.invoke(this, ObjectFactory.createFixnum(i));
          if (block.breakedOrReturned()) {
            return v;
          }
          i += diff;
        }
      } else {
        while (i >= end) {
          RubyValue v = block.invoke(this, ObjectFactory.createFixnum(i));
          if (block.breakedOrReturned()) {
            return v;
          }
          i += diff;
        }
      }

      return this;
    }

    return super.doStep(toArg, stepArg, block);
  }
Beispiel #2
0
 // @RubyLevelMethod(name="each_index")
 public RubyValue each_index(RubyBlock block) {
   for (int i = 0; i < size(); i++) {
     RubyValue v = block.invoke(this, new RubyFixnum(i));
     if (block.breakedOrReturned()) {
       return v;
     }
   }
   return this;
 }
Beispiel #3
0
 // @RubyLevelMethod(name="reverse_each")
 public RubyValue reverse_each(RubyBlock block) {
   ListIterator /*<RubyValue>*/ ite = array_.listIterator(array_.size());
   while (ite.hasPrevious()) {
     RubyValue v = block.invoke(this, (RubyValue) ite.previous());
     if (block.breakedOrReturned()) {
       return v;
     }
   }
   return this;
 }
Beispiel #4
0
 // @RubyLevelMethod(name="each")
 public RubyValue each(RubyBlock block) {
   //        for (RubyValue item : array_) {
   for (Iterator iter = array_.iterator(); iter.hasNext(); ) {
     RubyValue item = (RubyValue) iter.next();
     RubyValue v = block.invoke(this, item);
     if (block.breakedOrReturned()) {
       return v;
     }
   }
   return this;
 }
Beispiel #5
0
  public RubyValue times(RubyBlock block) {
    int value = this.value_;
    for (int i = 0; i < value; ++i) {
      RubyValue v = block.invoke(this, ObjectFactory.createFixnum(i));
      if (block.breakedOrReturned()) {
        return v;
      }
    }

    return this;
  }
  @RubyLevelMethod(name = "loop", module = true)
  public static RubyValue loop(RubyValue receiver, RubyArray args, RubyBlock block) {
    if (null == block) {
      throw new RubyException(RubyRuntime.LocalJumpErrorClass, "in `loop': no block given");
    }

    for (; ; ) {
      RubyValue v = block.invoke(receiver, args);
      if (block.breakedOrReturned()) {
        return v;
      }
    }
  }
  @RubyLevelMethod(name = "open")
  public static RubyValue open(RubyValue receiver, RubyArray args, RubyBlock block) {
    String filename = args.get(0).toStr();
    RubyIO io;
    if (args.size() <= 1) {
      io = ObjectFactory.createFile(filename, "r");
    } else if (args.get(1) instanceof RubyFixnum) {
      String mode = "r";
      int i = args.get(1).toInt();
      if ((i & RDWR) != 0) {
        mode = mode + "w";
      }
      io = ObjectFactory.createFile(filename, mode);
    } else {
      RubyString mode = (RubyString) args.get(1);
      io = ObjectFactory.createFile(filename, mode.toString());
    }

    if (null == block) {
      return io;
    } else {
      RubyValue v = block.invoke(receiver, io);
      io.close();
      return v;
    }
  }
  @RubyLevelMethod(name = "instance_eval")
  public static RubyValue instanceEval(RubyValue receiver, RubyArray args, RubyBlock block) {
    if (null == args && null == block) {
      throw new RubyException(RubyRuntime.ArgumentErrorClass, "block not supplied");
    }

    if (null != args) {
      RubyBinding binding = new RubyBinding();
      System.out.println(receiver);
      binding.setScope((RubyModule) receiver);
      binding.setSelf(receiver);
      return eval(args.get(0).toStr(), binding);
    } else {
      block.setSelf(receiver);
      return block.invoke(receiver);
    }
  }
Beispiel #9
0
 // @RubyLevelMethod(name="delete_if")
 public RubyValue delete_if(RubyBlock block) {
   for (int i = 0; i < array_.size(); ) {
     RubyValue r = block.invoke(this, (RubyValue) array_.get(i));
     if (r.isTrue()) {
       array_.remove(i);
     } else {
       ++i;
     }
   }
   return this;
 }
Beispiel #10
0
  // @RubyLevelMethod(name="delete")
  public RubyValue delete(RubyValue item, RubyBlock block) {
    boolean found = false;
    while (array_.remove(item)) {
      found = true;
    }

    if (block != null && !found) {
      return block.invoke(item);
    } else {
      return found ? item : RubyConstant.QNIL;
    }
  }
Beispiel #11
0
  /**
   * Call a RubyBlock, wrapped with the exception handlers that print out reasonable backtraces for
   * Ruby exceptions.
   *
   * @param contextMessage A message to print at the top of the backtrace
   * @param rb The block
   * @return The result from the block
   * @throws FileNotFoundException
   * @throws NoSuchMethodException
   * @throws ScriptException
   */
  Object doBlockWithRubyExceptionWrapper(String contextMessage, RubyBlock rb)
      throws FileNotFoundException, NoSuchMethodException, ScriptException {
    Object result = null;

    try {
      result = rb.yield();
    } catch (NoSuchMethodException e) {
      printRubyStacktrace(contextMessage, e);
      throw (e);
    } catch (ScriptException e) {
      printRubyStacktrace(contextMessage, e);
      throw (e);
    }

    return result;
  }
Beispiel #12
0
  // @RubyLevelMethod(name="each_byte")
  public RubyValue each_byte(RubyBlock block) {
    if (block == null) return this;

    String string = toString();
    byte bytes[] = null;
    try {
      bytes = string.getBytes("UTF-8");
    } catch (java.io.UnsupportedEncodingException exc) {
    }

    for (int i = 0; bytes != null && i < bytes.length; ++i) {
      int nByte = bytes[i];
      if (nByte < 0) nByte = 256 + nByte;

      block.invoke(this, ObjectFactory.createFixnum(nByte));
    }

    return this;
  }
  @RubyLevelMethod(name = "catch", module = true)
  public static RubyValue catchMethod(RubyValue receiver, RubyValue arg, RubyBlock block) {
    if (!(arg instanceof RubySymbol)) {
      throw new RubyException(RubyRuntime.ArgumentErrorClass, arg.toString() + " is not a symbol");
    }

    try {
      block.invoke(receiver);
    } catch (RubyException e) {
      RubyValue ev = RubyAPI.convertRubyException2RubyValue(e);
      if (ev instanceof RubyExceptionValueForThrow) {
        RubyExceptionValueForThrow v = (RubyExceptionValueForThrow) ev;
        if (v.isSameSymbol(arg)) {
          return v.getReturnValue();
        }
      }
      throw e;
    }

    return RubyConstant.QNIL;
  }
Beispiel #14
0
 // @RubyLevelMethod(name="new", singleton=true)
 public static RubyValue newArray(RubyValue receiver, RubyArray args, RubyBlock block) {
   RubyArray a;
   if (null == args) {
     a = new RubyArray();
   } else if (null == block) {
     if (args.get(0) instanceof RubyArray) {
       a = (RubyArray) args.get(0).clone();
     } else {
       RubyFixnum size = (RubyFixnum) args.get(0);
       RubyValue default_value = args.get(1);
       a = ObjectFactory.createArray(size.toInt(), default_value);
     }
   } else {
     RubyFixnum size = (RubyFixnum) args.get(0);
     a = new RubyArray();
     for (int i = 0; i < size.toFloat(); i++) {
       RubyValue return_value = block.invoke(receiver, ObjectFactory.createFixnum(i));
       a.add(return_value);
     }
   }
   a.setRubyClass((RubyClass) receiver);
   return a;
 }
 @RubyLevelMethod(name = "lambda", alias = "proc", module = true)
 public static RubyValue lambda(RubyValue receiver, RubyBlock block) {
   block.setCreatedByLambda();
   return ObjectFactory.createProc(block);
 }
Beispiel #16
0
  // @RubyLevelMethod(name="each", alias="each_line")
  public RubyValue each(RubyBlock block) {
    // FIXME: for each line
    if (block != null) block.invoke(this, this);

    return this;
  }