@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;
  }
Esempio n. 2
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"));
    }
  }
Esempio n. 3
0
  // @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());
    }
  }
  @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;
    }
  }
Esempio n. 5
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();

      log(strBody);

      responseData = new ByteArrayInputStream(strBody.getBytes());
      if (responseData != null)
        contentLength = Integer.parseInt(resHeaders.getPropertyIgnoreCase("Content-Length"));
    }
  }
Esempio n. 6
0
  // @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)");
  }
  @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));
  }
Esempio n. 8
0
  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)");
    }
  }
  @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);
  }
Esempio n. 10
0
  // 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;
    }
  }
Esempio n. 11
0
  // @RubyLevelMethod(name="split")
  public RubyValue split(RubyArray args) {
    RubyValue r = (null == args) ? GlobalVariables.get("$;") : args.get(0);

    Collection /*<String>*/ splitResult;
    boolean bSkipFirstEmptyItem = false;
    if (r == RubyConstant.QNIL) {
      splitResult = split(this, " ");
    } else if (r instanceof RubyRegexp) {
      RubyRegexp reg = (RubyRegexp) r;
      splitResult = split(this, reg, args);

      if (reg.getPattern().getPattern().startsWith("(?=")) bSkipFirstEmptyItem = true;
    } else if (r instanceof RubyString) {
      splitResult = split(this, ((RubyString) r).toString());
    } else {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "wrong argument type " + r.getRubyClass() + " (expected Regexp)");
    }

    RubyArray a = new RubyArray(splitResult.size());
    int i = 0;
    //        for (String str : splitResult) {
    for (Iterator iter = splitResult.iterator(); iter.hasNext(); ) {
      String str = (String) iter.next();
      if (!(bSkipFirstEmptyItem && 0 == i && (str == null || str.equals("")))) {
        // To conform ruby's behavior, discard the first empty element
        a.add(ObjectFactory.createString(str));
      }
      ++i;
    }
    return a;
  }
Esempio n. 12
0
  // @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;
  }
Esempio n. 13
0
 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());
   }
 }
Esempio n. 14
0
 // @RubyLevelMethod(name="chomp!")
 public RubyValue chomp_danger(RubyArray args) {
   RubyValue separator = (null == args) ? GlobalVariables.get("$/") : args.get(0);
   if (chomp(((RubyString) separator).toString())) {
     return this;
   } else {
     return RubyConstant.QNIL;
   }
 }
Esempio n. 15
0
 // @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;
 }
Esempio n. 16
0
 // @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;
 }
  // 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);
  }
 @RubyLevelMethod(name = "method_missing", module = true)
 public static RubyValue methodMissing(RubyValue receiver, RubyArray args) {
   RubySymbol method_name = (RubySymbol) args.get(0);
   RubyClass klass = receiver.getRubyClass();
   klass = (klass != null) ? klass.getRealClass() : null;
   throw new RubyException(
       RubyRuntime.NoMethodErrorClass,
       "undefined method '" + method_name.toString() + "' for " + klass.getName());
 }
 @RubyLevelMethod(name = "throw", module = true)
 public static RubyValue throwMethod(RubyValue receiver, RubyArray args) {
   RubyExceptionValue e;
   RubyValue tag = args.get(0);
   if (tag instanceof RubySymbol || tag instanceof RubyString) {
     e = new RubyExceptionValueForThrow(tag, args.get(1));
   } else if (tag instanceof RubyExceptionValue) {
     e = (RubyExceptionValue) tag;
   } else if (tag instanceof RubyClass) {
     RubyClass c = (RubyClass) tag;
     e = new RubyExceptionValue(c, c.getName() + " is not a symbol");
   } else {
     e =
         new RubyExceptionValue(
             RubyRuntime.ArgumentErrorClass, tag.toString() + " is not a symbol");
   }
   throw new RubyException(e);
 }
 @RubyLevelMethod(name = "sprintf", module = true)
 public static RubyValue sprintf(RubyValue receiver, RubyArray args) {
   String fmt = args.get(0).toStr();
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   PrintStream ps = new PrintStream(baos);
   ps.printf(fmt, RubyKernelModule.buildFormatArg(args, 1));
   ps.flush();
   ps.close();
   return ObjectFactory.createString(baos.toString());
 }
Esempio n. 21
0
  // @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;
  }
Esempio n. 22
0
  // @RubyLevelMethod(name="delete")
  public RubyValue delete(RubyArray args) {
    if (null == args) {
      throw new RubyException(RubyRuntime.ArgumentErrorClass, "wrong number of arguments");
    }

    RubyString string = ObjectFactory.createString(toString());
    String arg = ((RubyString) args.get(0)).toString();
    string.delete(arg);
    return string;
  }
Esempio n. 23
0
  // @RubyLevelMethod(name="delete!")
  public RubyValue delete_danger(RubyArray args) {
    if (null == args) {
      throw new RubyException(RubyRuntime.ArgumentErrorClass, "wrong number of arguments");
    }

    String arg = ((RubyString) args.get(0)).toString();
    if (delete(arg)) return this;
    return RubyConstant.QNIL;
    //        return delete(arg) ? this : RubyConstant.QNIL;
  }
Esempio n. 24
0
  // @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);
    }
  }
Esempio n. 25
0
  // @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);
    }
  }
Esempio n. 26
0
 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;
 }
Esempio n. 27
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;
 }
 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;
 }
  @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);
    }
  }
Esempio n. 30
0
  // @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));
  }