コード例 #1
0
  @RubyLevelMethod(name = "trace_var", module = true)
  public static RubyValue trace_var(RubyValue receiver, RubyArray args, RubyBlock block) {
    if (null == args || args.size() < 1) {
      int actual_argc = (null == args) ? 0 : args.size();
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "in `trace_var': wrong number of arguments (" + actual_argc + " for 1)");
    }

    if (!(args.get(0) instanceof RubySymbol)) {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass, args.get(0).toString() + " is not a symbol");
    }

    String name = ((RubySymbol) args.get(0)).toString();

    RubyValue v = args.get(1);
    if (v instanceof RubyProc) {
      GlobalVariables.addTraceProc(name, (RubyProc) v);
    } else if (null != block) {
      GlobalVariables.addTraceProc(name, ObjectFactory.createProc(block));
    } else {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass, "tried to create Proc object without a block");
    }

    return RubyConstant.QNIL;
  }
コード例 #2
0
ファイル: RubyString.java プロジェクト: sumitk/rhodes
  // @RubyLevelMethod(name="gsub!")
  public RubyValue gsub_danger(RubyArray args, RubyBlock block) {
    if (null == block) {
      RubyString result = gsub(this, args);

      if (result == this) {
        return RubyConstant.QNIL;
      } else {
        return setString(result.toString());
      }
    } else {
      if (null == args || args.size() != 1) {
        int actual_argc = (null == args) ? 0 : args.size();
        throw new RubyException(
            RubyRuntime.ArgumentErrorClass,
            "in `gsub!': wrong number of arguments (" + actual_argc + " for 1)");
      }
      if (!(args.get(0) instanceof RubyRegexp)) {
        throw new RubyException(
            RubyRuntime.ArgumentErrorClass,
            "wrong argument type " + args.get(0).getRubyClass().getName() + " (expected Regexp)");
      }

      RubyRegexp r = (RubyRegexp) args.get(0);
      return setString(r.gsub(this, block).toString());
    }
  }
コード例 #3
0
ファイル: RubyString.java プロジェクト: sumitk/rhodes
  private RubyString sub(RubyString g, RubyArray args) {
    if (null == args || args.size() != 2) {
      int actual_argc = (null == args) ? 0 : args.size();
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "in `sub': wrong number of arguments (" + actual_argc + " for 2)");
    }

    if (!(args.get(1) instanceof RubyString)) {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "can't convert " + args.get(1).getRubyClass().getName() + " into String");
    }

    RubyString s = (RubyString) args.get(1);

    if (args.get(0) instanceof RubyRegexp) {
      RubyRegexp r = (RubyRegexp) args.get(0);
      return r.sub(g, s);
    } else if (args.get(0) instanceof RubyString) {
      RubyString r = (RubyString) args.get(0);
      String result = StringMe.replaceFirst(g.toString(), r.toString(), s.toString());
      return ObjectFactory.createString(result);
    } else {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "wrong argument type " + args.get(0).getRubyClass().getName() + " (expected Regexp)");
    }
  }
コード例 #4
0
  // FIXME:This method should be module.
  @RubyLevelMethod(name = "raise", alias = "fail")
  public static RubyValue raise(RubyValue value, RubyArray args) {
    RubyExceptionValue e;
    if (null == args) {
      // With no arguments, raises the exception in $! or raises a RuntimeError if $! is nil.
      RubyValue v = GlobalVariables.get("$!");
      if (RubyConstant.QNIL == v) {
        e = new RubyExceptionValue(RubyRuntime.RuntimeErrorClass, "");
      } else {
        e = (RubyExceptionValue) v;
      }
    } else if (1 == args.size() && (args.get(0) instanceof RubyString)) {
      // With a single String argument, raises a RuntimeError with the string as a message.
      e =
          new RubyExceptionValue(
              RubyRuntime.RuntimeErrorClass, ((RubyString) args.get(0)).toString());
    } else if (args.get(0) instanceof RubyExceptionValue) {
      // Otherwise, the first parameter should be the name of an Exception class
      // (or an object that returns an Exception when sent exception). The optional second
      // parameter sets the message associated with the exception, and the third parameter
      // is an array of callback information.
      e = (RubyExceptionValue) args.get(0);
      if (args.size() > 1) {
        e.setMessage(((RubyString) args.get(1)).toString());
      }
    } else {
      RubyClass v = (RubyClass) args.get(0);
      e = new RubyExceptionValue(v, 1 == args.size() ? "" : ((RubyString) args.get(1)).toString());
    }

    throw new RubyException(e);
  }
コード例 #5
0
ファイル: RubyArray.java プロジェクト: visfleet/rhodes
  // @RubyLevelMethod(name="[]=")
  public RubyValue aset(RubyArray args) {
    if (3 == args.size()) {
      int index = args.get(0).toInt();
      int length = args.get(1).toInt();
      return replace(index, length, args.get(2));
    }

    throw new RubyException(
        RubyRuntime.ArgumentErrorClass, "wrong number of arguments (" + args.size() + " for 2)");
  }
コード例 #6
0
  @RubyLevelMethod(name = "respond_to?")
  public static RubyValue respond_to(RubyValue receiver, RubyArray args) {
    if (null == args || args.size() < 1) {
      int actual_argc = (null == args) ? 0 : args.size();
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "in `respond_to': wrong number of arguments (" + actual_argc + " for 1)");
    }

    boolean include_private = (RubyConstant.QTRUE == args.get(1));
    RubyID mid = RubyID.intern(args.get(0).toStr());
    return ObjectFactory.createBoolean(hasMethod(receiver, mid, include_private));
  }
コード例 #7
0
 public static Object[] buildFormatArg(RubyArray args, int start) {
   Object[] raw_args = new Object[args.size() - start];
   for (int i = 0; i < args.size() - start; ++i) {
     Object v = args.get(i + start);
     if (v instanceof RubyFixnum) {
       raw_args[i] = ((RubyFixnum) v).toInt();
     } else if (v instanceof RubyFloat) {
       raw_args[i] = ((RubyFloat) v).doubleValue();
     } else {
       raw_args[i] = v;
     }
   }
   return raw_args;
 }
コード例 #8
0
  private void processResponse(RubyValue res) {
    if (res != null && res != RubyConstant.QNIL && res instanceof RubyHash) {
      RubyHash resHash = (RubyHash) res;
      RubyValue resBody = null;

      RubyArray arKeys = resHash.keys();
      RubyArray arValues = resHash.values();
      for (int i = 0; i < arKeys.size(); i++) {
        String strKey = arKeys.get(i).toString();
        if (strKey.equals("request-body")) resBody = arValues.get(i);
        else if (strKey.equals("status")) responseCode = arValues.get(i).toInt();
        else if (strKey.equals("message")) responseMsg = arValues.get(i).toString();
        else resHeaders.addProperty(strKey, arValues.get(i).toString());
      }
      String strBody = "";

      if (resBody != null && resBody != RubyConstant.QNIL)
        strBody = resBody.toRubyString().toString();

      if (!RHOCONF().getBool("log_skip_post")) LOG.TRACE(strBody);

      try {
        responseData = new ByteArrayInputStream(strBody.getBytes("UTF-8"));
      } catch (java.io.UnsupportedEncodingException exc) {
        LOG.ERROR("Error getting utf-8 body :", exc);
      }

      if (responseData != null)
        contentLength = Integer.parseInt(resHeaders.getPropertyIgnoreCase("Content-Length"));
    }
  }
コード例 #9
0
ファイル: RhoConnection.java プロジェクト: spike/rhodes
  private void processResponse(RubyValue res) {
    if (res != null && res != RubyConstant.QNIL && res instanceof RubyHash) {
      RubyHash resHash = (RubyHash) res;
      RubyValue resBody = null;

      RubyArray arKeys = resHash.keys();
      RubyArray arValues = resHash.values();
      for (int i = 0; i < arKeys.size(); i++) {
        String strKey = arKeys.get(i).toString();
        if (strKey.equals("request-body")) resBody = arValues.get(i);
        else if (strKey.equals("status")) responseCode = arValues.get(i).toInt();
        else if (strKey.equals("message")) responseMsg = arValues.get(i).toString();
        else resHeaders.addProperty(strKey, arValues.get(i).toString());
      }
      String strBody = "";

      if (resBody != null && resBody != RubyConstant.QNIL)
        strBody = resBody.toRubyString().toString();

      log(strBody);

      responseData = new ByteArrayInputStream(strBody.getBytes());
      if (responseData != null)
        contentLength = Integer.parseInt(resHeaders.getPropertyIgnoreCase("Content-Length"));
    }
  }
コード例 #10
0
ファイル: RubyArray.java プロジェクト: visfleet/rhodes
  // RHO_COMMENT
  public boolean equals(Object o, boolean bConvToAry) {
    if (this == o) {
      return true;
    }

    if (o instanceof RubyArray) {
      RubyArray that = (RubyArray) o;
      int size = array_.size();
      if (size != that.size()) {
        return false;
      }

      for (int i = 0; i < size; ++i) {
        if (!this.get(i).equals(that.get(i))) {
          return false;
        }
      }

      return true;
    } else if (o instanceof RubyValue && bConvToAry) {
      RubyValue v = (RubyValue) o;
      if (!v.respondTo(RubyID.toAryID)) {
        return false;
      } else {
        return v.equals(this);
      }
    } else {
      return false;
    }
  }
コード例 #11
0
  @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;
    }
  }
コード例 #12
0
ファイル: RubyArray.java プロジェクト: visfleet/rhodes
 private RubyArray plus(RubyArray v) {
   int size = array_.size() + v.size();
   RubyArray resultArray = new RubyArray(size);
   resultArray.array_.addAll(array_);
   resultArray.array_.addAll(v.array_);
   return resultArray;
 }
コード例 #13
0
ファイル: RubyArray.java プロジェクト: visfleet/rhodes
 // @RubyLevelMethod(name="indexes", alias="indices")
 public RubyValue indexes(RubyArray args) {
   RubyArray a = new RubyArray(args.size());
   for (int i = 0; i < size(); i++) {
     RubyFixnum n = (RubyFixnum) args.get(i);
     a.add(get(n.toInt()));
   }
   return a;
 }
コード例 #14
0
ファイル: RubyString.java プロジェクト: sumitk/rhodes
 private Collection /*<String>*/ split(RubyString g, RubyRegexp r, RubyArray args) {
   if (args.size() <= 1) {
     return r.split(g.toString(), 0);
   } else {
     RubyFixnum i = (RubyFixnum) args.get(1);
     return r.split(g.toString(), i.toInt());
   }
 }
コード例 #15
0
ファイル: RubyArray.java プロジェクト: visfleet/rhodes
 // @RubyLevelMethod(name="collect!")
 public RubyValue collect_danger(RubyBlock block) {
   RubyArray a = (RubyArray) RubyAPI.callPublicNoArgMethod(this, block, RubyID.intern("collect"));
   clear();
   for (int i = 0; i < a.size(); i++) {
     add(a.get(i));
   }
   return this;
 }
コード例 #16
0
ファイル: RubyString.java プロジェクト: sumitk/rhodes
  // @RubyLevelMethod(name="end_with?")
  public RubyValue opEndWith(RubyArray v) {

    for (int i = 0; i < v.size(); i++) {
      RubyValue res = opEndWith(v.get(i));
      if (res == RubyConstant.QTRUE) return RubyConstant.QTRUE;
    }
    return RubyConstant.QFALSE;
  }
コード例 #17
0
ファイル: RubyArray.java プロジェクト: visfleet/rhodes
  private RubyValue compare(RubyArray other_array) {
    int length = (size() <= other_array.size()) ? size() : other_array.size();
    for (int i = 0; i < length; ++i) {
      RubyValue v =
          RubyAPI.callPublicOneArgMethod(get(i), other_array.get(i), null, RubyID.unequalID);
      if (!RubyAPI.testEqual(v, ObjectFactory.FIXNUM0)) {
        return v;
      }
    }

    if (size() == other_array.size()) {
      return ObjectFactory.FIXNUM0;
    } else if (size() > other_array.size()) {
      return ObjectFactory.FIXNUM1;
    } else {
      return ObjectFactory.FIXNUM_NEGATIVE_ONE;
    }
  }
コード例 #18
0
ファイル: RubyArray.java プロジェクト: visfleet/rhodes
  // @RubyLevelMethod(name="insert")
  public RubyArray insert(RubyArray ary) {
    int argc = ary.size();
    if (argc == 1) {
      return this;
    }

    if (argc < 1) {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass, "wrong number of arguments (at least 1)");
    }

    int pos = ary.get(0).toInt();
    if (pos < 0) {
      pos += this.array_.size() + 1;
    }

    return this.insert(pos, ary.subarray(1, ary.size() - 1));
  }
コード例 #19
0
ファイル: RubyArray.java プロジェクト: visfleet/rhodes
  // @RubyLevelMethod(name="replace")
  public RubyValue replace(RubyValue arg) {
    RubyArray anotherArray = (RubyArray) arg;
    if (this == anotherArray) return this;

    clear();
    for (int i = 0; i < anotherArray.size(); i++) {
      add(anotherArray.get(i));
    }
    return this;
  }
コード例 #20
0
ファイル: RubyString.java プロジェクト: sumitk/rhodes
  // @RubyLevelMethod(name="gsub")
  public RubyValue gsub(RubyArray args, RubyBlock block) {
    if (null == block) {
      return gsub(this, args);
    } else {
      if (null == args || args.size() != 1) {
        int actual_argc = (null == args) ? 0 : args.size();
        throw new RubyException(
            RubyRuntime.ArgumentErrorClass,
            "in `gsub': wrong number of arguments (" + actual_argc + " for 1)");
      }
      if (!(args.get(0) instanceof RubyRegexp)) {
        throw new RubyException(
            RubyRuntime.ArgumentErrorClass,
            "wrong argument type " + args.get(0).getRubyClass().getName() + " (expected Regexp)");
      }

      RubyRegexp r = (RubyRegexp) args.get(0);
      return r.gsub(this, block);
    }
  }
コード例 #21
0
ファイル: RubyArray.java プロジェクト: visfleet/rhodes
  // @RubyLevelMethod(name="slice!")
  public RubyValue slice_danger(RubyArray args) {
    if (1 == args.size()) {
      Object argValue = args.get(0);
      if (argValue instanceof RubyFixnum) {
        RubyFixnum index = (RubyFixnum) argValue;
        return delete_at(index.toInt());
      } else if (args.get(0) instanceof RubyRange) {
        int begin = ((RubyFixnum) ((RubyRange) args.get(0)).getLeft()).toInt();
        int end = ((RubyFixnum) ((RubyRange) args.get(0)).getRight()).toInt();
        if (begin < 0) {
          begin = size() + begin;
        }
        if (end < 0) {
          end = size() + end;
        }

        if (!((RubyRange) args.get(0)).isExcludeEnd()) {
          ++end;
        }

        RubyArray resultValue = delete_at(begin, end - begin);
        if (null == resultValue) return RubyConstant.QNIL;
        return resultValue;
        //                return (null == resultValue ? RubyConstant.QNIL : resultValue);
      }
    } else if (2 == args.size()) {
      Object arg0Value = args.get(0);
      Object arg1Value = args.get(1);
      if (arg0Value instanceof RubyFixnum && arg1Value instanceof RubyFixnum) {
        int begin = ((RubyFixnum) arg0Value).toInt();
        int length = ((RubyFixnum) arg1Value).toInt();
        RubyArray resultValue = delete_at(begin, length);
        if (null == resultValue) return RubyConstant.QNIL;
        return resultValue;
        //                return (null == resultValue ? RubyConstant.QNIL : resultValue);
      }
    }

    // TODO
    throw new RubyException("not implemented");
  }
コード例 #22
0
ファイル: RubyArray.java プロジェクト: visfleet/rhodes
 private boolean recursiveAdd(RubyArray receiver, RubyArray array) {
   boolean modified = false;
   for (int i = 0; i < array.size(); i++) {
     RubyValue val = array.get(i);
     if (val instanceof RubyArray) {
       modified = true;
       recursiveAdd(receiver, (RubyArray) val);
     } else {
       receiver.add(val);
     }
   }
   return modified;
 }
コード例 #23
0
  @RubyLevelMethod(name = "eval", module = true)
  public static RubyValue eval(RubyValue receiver, RubyArray args) {
    RubyBinding binding = null;
    if (args.get(1) instanceof RubyBinding) {
      binding = (RubyBinding) args.get(1);
    }

    String file_name = null;
    if (args.size() > 2) {
      file_name = args.get(2).toStr();
    }

    return eval(args.get(0).toStr(), binding, file_name);
  }
コード例 #24
0
ファイル: RubyString.java プロジェクト: sumitk/rhodes
  // @RubyLevelMethod(name="[]")
  public RubyValue array_access(RubyArray args) {
    String string = toString();
    if (args.size() == 1) {
      RubyValue arg = args.get(0);
      if (arg instanceof RubyString) {
        String str = ((RubyString) arg).toString();
        if (string.indexOf(str) >= 0) {
          return ObjectFactory.createString(str);
        } else {
          return RubyConstant.QNIL;
        }
      } else if (arg instanceof RubyRange) {
        RubyRange range = (RubyRange) arg;
        int start = range.getLeft().toInt();
        int end = range.getRight().toInt();
        return substring(string, start, end, range.isExcludeEnd());
      } else if (arg instanceof RubyRegexp) {
        RubyRegexp regexp = (RubyRegexp) arg;
        RubyMatchData match = regexp.match(string);
        if (match != null) {
          return ObjectFactory.createString(match.toString());
        } else {
          return RubyConstant.QNIL;
        }
      } else {
        int index = arg.toInt();
        if (index < 0) {
          index = string.length() + index;
        }

        if (index < 0 || index >= string.length()) {
          return RubyConstant.QNIL;
        } else {
          return ObjectFactory.createFixnum(string.charAt(index));
        }
      }
    } else {
      int start = args.get(0).toInt();
      int length = args.get(1).toInt() - 1;

      return substring(string, start, start + length, false);
    }
  }
コード例 #25
0
ファイル: RubyString.java プロジェクト: sumitk/rhodes
  // @RubyLevelMethod(name="[]=")
  public RubyValue array_set(RubyArray args) {
    String string = toString();
    String replacement;

    int start, end;

    if (args.size() == 2) {
      RubyValue arg = args.get(0);
      replacement = ((RubyString) args.get(1)).toString();

      if (arg instanceof RubyString) {
        String str = ((RubyString) arg).toString();
        start = string.indexOf(str);
        if (start >= 0) {
          end = start + str.length();
        } else {
          throw new RubyException(RubyRuntime.IndexErrorClass, "string not matched");
        }
      } else if (arg instanceof RubyRange) {
        RubyRange range = (RubyRange) arg;
        start = range.getLeft().toInt();
        end = range.getRight().toInt();
        if (start >= string.length()) {
          throw new RubyException(RubyRuntime.RangeClass, range.toString() + " out of range");
        }
      } else if (arg instanceof RubyRegexp) {
        RubyRegexp regexp = (RubyRegexp) arg;
        RubyMatchData match = regexp.match(string);
        if (match != null) {
          String matched = match.toString();
          start = string.indexOf(matched);
          end = matched.length() + start;
        } else {
          throw new RubyException(RubyRuntime.IndexErrorClass, "regexp not matched");
        }
      } else {
        start = arg.toInt();
        end = start + 1;
      }
    } else {
      replacement = ((RubyString) args.get(2)).toString();

      start = args.get(0).toInt();
      end = args.get(1).toInt() + start;
      if (start > /*=*/ string.length()) {
        throw new RubyException(
            RubyRuntime.RangeClass,
            "Index '"
                + start
                + "' out of string:"
                + string
                + ";end: '"
                + end
                + "';replacement:"
                + replacement
                + "");
      }
    }
    setString(replace(string, start, end, replacement));
    return ObjectFactory.createString(replacement);
  }