@RubyLevelMethod(name = "methods")
 public static RubyValue objMethods(RubyValue receiver) {
   RubyArray a = new RubyArray();
   RubyClass klass = receiver.getRubyClass();
   klass.collectClassMethodNames(a, RubyMethod.ALL);
   return a;
 }
 @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);
 }
  private static RubyValue objSingletonMethod(RubyValue receiver, boolean all) {
    RubyArray a = new RubyArray();
    if (receiver.getRubyClass().isSingleton()) {
      RubyClass rubyClass = receiver.getRubyClass();
      rubyClass.collectOwnMethodNames(a, RubyMethod.PUBLIC);
      rubyClass = rubyClass.getSuperClass();

      if (all) {
        while (rubyClass != null && rubyClass.isSingleton()) {
          rubyClass.collectOwnMethodNames(a, RubyMethod.PUBLIC);
          rubyClass = rubyClass.getSuperClass();
        }
      }
    }
    return a;
  }
예제 #5
0
 public static void initMethods(RubyClass klass) {
   klass
       .getSingletonClass()
       .defineMethod(
           "set_property_by_name",
           new RubyTwoArgMethod() {
             protected RubyValue run(
                 RubyValue receiver, RubyValue arg0, RubyValue arg1, RubyBlock block) {
               try {
                 RhoConf.getInstance().setPropertyByName(arg0.toString(), arg1.toString());
                 RhoConf.getInstance().saveToFile();
                 RhoConf.getInstance().loadFromFile();
               } catch (Exception e) {
                 LOG.ERROR("set_property_by_name failed", e);
                 throw (e instanceof RubyException
                     ? (RubyException) e
                     : new RubyException(e.getMessage()));
               }
               return RubyConstant.QNIL;
             }
           });
 }
예제 #6
0
  public static void initMethods(RubyClass klass) {

    klass.defineMethod(
        "alive?",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyThread) receiver).alive();
          }
        });
    klass.defineMethod(
        "[]",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyThread) receiver).getVariable(arg);
          }
        });
    klass
        .getSingletonClass()
        .defineMethod(
            "list",
            new RubyNoArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyBlock block) {
                return RubyThread.list(receiver);
              }
            });
    klass.defineMethod(
        "kill",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyThread) receiver).kill();
          }
        });
    klass.aliasMethod("exit", "kill");
    klass.defineMethod(
        "run",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyThread) receiver).run();
          }
        });
    klass.defineMethod(
        "priority=",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyThread) receiver).set_priority(arg);
          }
        });
    klass.defineMethod(
        "wakup",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyThread) receiver).wakup();
          }
        });
    klass.defineMethod(
        "stop?",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyThread) receiver).getStoped();
          }
        });
    klass
        .getSingletonClass()
        .defineMethod(
            "main",
            new RubyNoArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyBlock block) {
                return RubyThread.getMainThread(receiver);
              }
            });
    klass.defineMethod(
        "key?",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyThread) receiver).is_key(arg);
          }
        });
    klass.defineMethod(
        "==",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyThread) receiver).equal(arg);
          }
        });
    klass.defineMethod(
        "priority",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyThread) receiver).priority();
          }
        });
    klass.defineMethod(
        "value",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyThread) receiver).value();
          }
        });
    klass
        .getSingletonClass()
        .defineMethod(
            "new",
            new RubyVarArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
                return RubyThread.newThread(receiver, args, block);
              }
            });
    klass.getSingletonClass().aliasMethod("fork", "new");
    klass.getSingletonClass().aliasMethod("start", "new");
    klass
        .getSingletonClass()
        .defineMethod(
            "pass",
            new RubyNoArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyBlock block) {
                return RubyThread.pass(receiver);
              }
            });
    klass.defineMethod(
        "[]=",
        new RubyTwoArgMethod() {
          protected RubyValue run(
              RubyValue receiver, RubyValue arg0, RubyValue arg1, RubyBlock block) {
            return ((RubyThread) receiver).setVariable(arg0, arg1);
          }
        });
    klass.defineMethod(
        "status",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyThread) receiver).status();
          }
        });
    klass
        .getSingletonClass()
        .defineMethod(
            "current",
            new RubyNoArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyBlock block) {
                return RubyThread.current(receiver);
              }
            });
    klass.defineMethod(
        "keys",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyThread) receiver).keys();
          }
        });
    klass.defineMethod(
        "join",
        new RubyVarArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
            return ((RubyThread) receiver).join(args, block);
          }
        });
    klass
        .getSingletonClass()
        .defineMethod(
            "critical",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
                return RubyThread.critical(receiver, arg);
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "critical=",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
                return RubyThread.set_critical(receiver, arg);
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "stop",
            new RubyNoArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyBlock block) {
                return RubyThread.stop(receiver);
              }
            });
    klass.defineMethod(
        "group",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyThread) receiver).getThreadGroup();
          }
        });
    klass.defineMethod(
        "inspect",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyThread) receiver).rubyInspect();
          }
        });
  }
예제 #7
0
  public static void initMethods(RubyClass klass) {

    klass
        .getSingletonClass()
        .defineMethod(
            "rmdir",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
                return RubyDir.rmdir(receiver, arg);
              }
            });
    klass.getSingletonClass().aliasMethod("delete", "rmdir");
    klass.getSingletonClass().aliasMethod("unlink", "rmdir");
    klass
        .getSingletonClass()
        .defineMethod(
            "entries",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
                return RubyDir.entries(receiver, arg, block);
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "foreach",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
                return RubyDir.foreach(receiver, arg, block);
              }
            });
    klass.defineMethod(
        "pos=",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyDir) receiver).setPos(arg);
          }
        });
    klass.defineMethod(
        "rewind",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyDir) receiver).rewind();
          }
        });
    klass
        .getSingletonClass()
        .defineMethod(
            "[]",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
                return RubyDir.array_access(receiver, arg);
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "glob",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
                return RubyDir.glob(receiver, arg, block);
              }
            });
    klass.defineMethod(
        "close",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyDir) receiver).close();
          }
        });
    klass.defineMethod(
        "pos",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyDir) receiver).pos();
          }
        });
    klass.aliasMethod("tell", "pos");
    klass.defineMethod(
        "seek",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyDir) receiver).seek(arg);
          }
        });
    klass
        .getSingletonClass()
        .defineMethod(
            "open",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
                return RubyDir.open(receiver, arg, block);
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "mkdir",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
                return RubyDir.mkdir(receiver, arg);
              }
            });
    klass.defineMethod(
        "path",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyDir) receiver).getPath();
          }
        });
    klass
        .getSingletonClass()
        .defineMethod(
            "chdir",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
                return RubyDir.chdir(receiver, arg);
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "new",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
                return RubyDir.dirNew(receiver, arg);
              }
            });
    klass.defineMethod(
        "read",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyDir) receiver).read();
          }
        });
    klass
        .getSingletonClass()
        .defineMethod(
            "getwd",
            new RubyNoArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyBlock block) {
                return RubyDir.getwd(receiver);
              }
            });
    klass.getSingletonClass().aliasMethod("pwd", "getwd");
    klass.defineMethod(
        "each",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyDir) receiver).each(block);
          }
        });

    klass
        .getSingletonClass()
        .defineMethod(
            "exist?",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
                return RubyDir.exist_question(receiver, arg);
              }
            });
    klass.getSingletonClass().aliasMethod("exists?", "exist?");
  }
예제 #8
0
  public static void initMethods(RubyClass klass) {

    klass.defineMethod(
        "to_f",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyBignum) receiver).to_f();
          }
        });
    klass.defineMethod(
        "[]",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyBignum) receiver).aref(arg);
          }
        });
    klass.defineMethod(
        "-@",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyBignum) receiver).uminus();
          }
        });
    klass.defineMethod(
        "&",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyBignum) receiver).op_band(arg);
          }
        });
    klass.defineMethod(
        "coerce",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyBignum) receiver).coerce(arg);
          }
        });
    klass.defineMethod(
        "%",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyBignum) receiver).op_mod(arg);
          }
        });
    klass.defineMethod(
        "*",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyBignum) receiver).op_mul(arg);
          }
        });
    klass.defineMethod(
        "+",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyBignum) receiver).op_add(arg);
          }
        });
    klass.defineMethod(
        "/",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyBignum) receiver).op_div(arg);
          }
        });
    klass.aliasMethod("fdiv", "/");
    klass.defineMethod(
        "-",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyBignum) receiver).op_sub(arg);
          }
        });
    klass.defineMethod(
        "size",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyBignum) receiver).rubySize();
          }
        });
    klass.defineMethod(
        "<=>",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyBignum) receiver).cmp(arg);
          }
        });
    klass.defineMethod(
        "==",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyBignum) receiver).equal(arg);
          }
        });
    klass.defineMethod(
        "eql?",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyBignum) receiver).eql(arg);
          }
        });
    klass.defineMethod(
        "to_s",
        new RubyNoOrOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyBignum) receiver).to_s();
          }

          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyBignum) receiver).to_s(arg);
          }
        });
    klass.defineMethod(
        "quo",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyBignum) receiver).quo(arg);
          }
        });
    klass.defineMethod(
        ">>",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyBignum) receiver).rshift(arg);
          }
        });
    klass.defineMethod(
        "<<",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyBignum) receiver).lshift(arg);
          }
        });
    klass.defineMethod(
        "~",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyBignum) receiver).op_neg();
          }
        });
    klass.defineMethod(
        "|",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyBignum) receiver).op_bor(arg);
          }
        });
    klass.defineMethod(
        "^",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyBignum) receiver).op_bxor(arg);
          }
        });
    klass.defineMethod(
        "**",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyBignum) receiver).pow(arg);
          }
        });
  }
예제 #9
0
  public static void initMethods(RubyClass klass) {

    klass.defineMethod(
        "downcase!",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).downcaseBang();
          }
        });
    klass.defineMethod(
        "count",
        new RubyVarArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
            return ((RubyString) receiver).count(args);
          }
        });
    klass.defineMethod(
        "tr",
        new RubyTwoArgMethod() {
          protected RubyValue run(
              RubyValue receiver, RubyValue arg0, RubyValue arg1, RubyBlock block) {
            return ((RubyString) receiver).tr(arg0, arg1);
          }
        });
    klass.defineMethod(
        "swapcase!",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).swapcaseBang();
          }
        });
    klass.defineMethod(
        "<=>",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyString) receiver).operator_compare(arg);
          }
        });
    klass.defineMethod(
        "include?",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyString) receiver).include_p(arg);
          }
        });
    klass.defineMethod(
        "=~",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyString) receiver).operator_match(arg);
          }
        });
    klass.defineMethod(
        "squeeze",
        new RubyVarArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
            return ((RubyString) receiver).squeeze(args);
          }
        });
    klass.defineMethod(
        "hex",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).hex();
          }
        });
    klass.defineMethod(
        "downcase",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).downcase();
          }
        });
    klass.defineMethod(
        "initialize",
        new RubyNoOrOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).initialize();
          }

          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyString) receiver).initialize(arg);
          }
        });
    klass.aliasMethod("initialize_copy", "initialize");
    klass.defineMethod(
        "delete!",
        new RubyVarArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
            return ((RubyString) receiver).delete_danger(args);
          }
        });
    klass.defineMethod(
        "%",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyString) receiver).format(arg);
          }
        });
    klass.defineMethod(
        "*",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyString) receiver).operator_star(arg);
          }
        });
    klass.defineMethod(
        "+",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyString) receiver).plus(arg);
          }
        });
    klass.defineMethod(
        "capitalize",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).capitalize();
          }
        });
    klass.defineMethod(
        "strip",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).strip();
          }
        });
    klass.defineMethod(
        "upcase",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).upcase();
          }
        });
    klass.defineMethod(
        "split",
        new RubyVarArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
            return ((RubyString) receiver).split(args);
          }
        });
    klass.defineMethod(
        "each_byte",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).each_byte(block);
          }
        });
    klass.defineMethod(
        "each",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).each(block);
          }
        });
    klass.aliasMethod("each_line", "each");
    klass.defineMethod(
        "chop",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).chop();
          }
        });
    klass.defineMethod(
        "sub!",
        new RubyVarArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
            return ((RubyString) receiver).sub_danger(args, block);
          }
        });
    klass.defineMethod(
        "unpack",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyString) receiver).unpack(arg);
          }
        });
    klass.defineMethod(
        "sub",
        new RubyVarArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
            return ((RubyString) receiver).sub(args, block);
          }
        });
    klass.defineMethod(
        "to_f",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).toRubyFloat();
          }
        });
    klass.defineMethod(
        "[]",
        new RubyVarArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
            return ((RubyString) receiver).array_access(args);
          }
        });
    klass.aliasMethod("slice", "[]");

    klass.defineMethod(
        "dump",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).rubyDump();
          }
        });
    klass.defineMethod(
        "to_i",
        new RubyNoOrOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).to_i();
          }

          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyString) receiver).to_i(arg);
          }
        });
    klass.defineMethod(
        "gsub!",
        new RubyVarArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
            return ((RubyString) receiver).gsub_danger(args, block);
          }
        });
    klass.defineMethod(
        "to_s",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).to_s();
          }
        });
    klass.defineMethod(
        "swapcase",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).swapcase();
          }
        });
    klass.defineMethod(
        "upcase!",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).upcaseBang();
          }
        });
    klass.defineMethod(
        "==",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyString) receiver).opEqual(arg);
          }
        });
    klass.defineMethod(
        "eql?",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyString) receiver).opEql(arg);
          }
        });
    klass.defineMethod(
        "delete",
        new RubyVarArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
            return ((RubyString) receiver).delete(args);
          }
        });
    klass.defineMethod(
        "length",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).rubyLength();
          }
        });
    klass.defineMethod(
        "bytesize",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).rubyBytesize();
          }
        });

    klass.defineMethod(
        "capitalize!",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).capitalizeBang();
          }
        });
    klass.defineMethod(
        "tr_s!",
        new RubyTwoArgMethod() {
          protected RubyValue run(
              RubyValue receiver, RubyValue arg0, RubyValue arg1, RubyBlock block) {
            return ((RubyString) receiver).trs_danger(arg0, arg1);
          }
        });
    klass.defineMethod(
        "[]=",
        new RubyVarArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
            return ((RubyString) receiver).array_set(args);
          }
        });
    klass.defineMethod(
        "gsub",
        new RubyVarArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
            return ((RubyString) receiver).gsub(args, block);
          }
        });
    klass.defineMethod(
        "reverse",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).reverse();
          }
        });
    klass.defineMethod(
        "chomp",
        new RubyVarArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
            return ((RubyString) receiver).chomp(args);
          }
        });
    klass.defineMethod(
        "tr!",
        new RubyTwoArgMethod() {
          protected RubyValue run(
              RubyValue receiver, RubyValue arg0, RubyValue arg1, RubyBlock block) {
            return ((RubyString) receiver).tr_danger(arg0, arg1);
          }
        });
    klass.defineMethod(
        "reverse!",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).reverse_danger();
          }
        });
    klass.defineMethod(
        "tr_s",
        new RubyTwoArgMethod() {
          protected RubyValue run(
              RubyValue receiver, RubyValue arg0, RubyValue arg1, RubyBlock block) {
            return ((RubyString) receiver).tr_s(arg0, arg1);
          }
        });
    klass.defineMethod(
        "concat",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyString) receiver).concat(arg);
          }
        });
    klass.aliasMethod("<<", "concat");
    klass.defineMethod(
        "strip!",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).stripBang();
          }
        });
    klass.defineMethod(
        "squeeze!",
        new RubyVarArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
            return ((RubyString) receiver).squeeze_danger(args);
          }
        });
    klass.defineMethod(
        "lstrip!",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).lstripBang();
          }
        });
    klass.defineMethod(
        "chomp!",
        new RubyVarArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
            return ((RubyString) receiver).chomp_danger(args);
          }
        });
    klass.defineMethod(
        "intern",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).intern();
          }
        });
    klass.aliasMethod("to_sym", "intern");
    klass.defineMethod(
        "scan",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyString) receiver).scan(arg, block);
          }
        });
    klass.defineMethod(
        "chop!",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).chopBang();
          }
        });
    klass.defineMethod(
        "encoding",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).getEncoding();
          }
        });
    klass.defineMethod(
        "force_encoding",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyString) receiver).force_encoding(arg);
          }
        });
    klass.defineMethod(
        "end_with?",
        new RubyOneArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
            return ((RubyString) receiver).opEndWith(arg);
          }
        });
    klass.defineMethod(
        "end_with?",
        new RubyVarArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyArray arg, RubyBlock block) {
            return ((RubyString) receiver).opEndWith(arg);
          }
        });
    klass.defineMethod(
        "ord",
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return ((RubyString) receiver).ord();
          }
        });

    klass.defineAllocMethod(
        new RubyNoArgMethod() {
          protected RubyValue run(RubyValue receiver, RubyBlock block) {
            return RubyString.alloc(receiver);
          }
        });
  }
 @RubyLevelMethod(name = "class")
 public static RubyValue objRubyClass(RubyValue receiver) {
   RubyClass klass = receiver.getRubyClass();
   return klass != null ? klass.getRealClass() : RubyConstant.QNIL;
 }
예제 #11
0
  public static void initMethods(RubyClass klass) {

    klass
        .getSingletonClass()
        .defineMethod(
            "is_bluetooth_available",
            new RubyNoArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyBlock block) {
                RubyValue res = RubyConstant.QFALSE;
                if (is_bluetooth_available() != 0) {
                  res = RubyConstant.QTRUE;
                }
                return res;
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "off_bluetooth",
            new RubyNoArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyBlock block) {
                off_bluetooth();
                return RubyConstant.QNIL;
              }
            });

    klass
        .getSingletonClass()
        .defineMethod(
            "set_device_name",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg0, RubyBlock block) {
                if (arg0 instanceof RubyString) {
                  String device_name = arg0.toString();
                  set_device_name(device_name);
                } else {
                  throw new RubyException(
                      RubyRuntime.ArgumentErrorClass,
                      "in RhoBluetooth.set_device_name: wrong argument type.Should be String");
                }
                return RubyConstant.QNIL;
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "get_device_name",
            new RubyNoArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyBlock block) {
                return ObjectFactory.createString(get_device_name());
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "get_last_error",
            new RubyNoArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyBlock block) {
                return ObjectFactory.createString(get_last_error());
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "create_session",
            new RubyTwoArgMethod() {
              protected RubyValue run(
                  RubyValue receiver, RubyValue arg1, RubyValue arg2, RubyBlock block) {
                if ((arg1 instanceof RubyString) && (arg2 instanceof RubyString)) {
                  String role = arg1.toString();
                  String callback_url = arg2.toString();
                  return ObjectFactory.createString(create_session(role, callback_url));
                } else {
                  throw new RubyException(
                      RubyRuntime.ArgumentErrorClass,
                      "in RhoBluetooth.create_session: wrong argument type.Should be String");
                }
              }
            });

    klass
        .getSingletonClass()
        .defineMethod(
            "session_set_callback",
            new RubyTwoArgMethod() {
              protected RubyValue run(
                  RubyValue receiver, RubyValue arg1, RubyValue arg2, RubyBlock block) {
                if ((arg1 instanceof RubyString) && (arg2 instanceof RubyString)) {
                  String connected_device_name = arg1.toString();
                  String callback_url = arg2.toString();
                  session_set_callback(connected_device_name, callback_url);
                } else {
                  throw new RubyException(
                      RubyRuntime.ArgumentErrorClass,
                      "in RhoBluetooth.session_set_callback: wrong argument type.Should be String");
                }
                return RubyConstant.QNIL;
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "session_disconnect",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg0, RubyBlock block) {
                if (arg0 instanceof RubyString) {
                  String connected_device_name = arg0.toString();
                  session_disconnect(connected_device_name);
                } else {
                  throw new RubyException(
                      RubyRuntime.ArgumentErrorClass,
                      "in RhoBluetooth.session_disconnect: wrong argument type.Should be String");
                }
                return RubyConstant.QNIL;
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "session_get_status",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg0, RubyBlock block) {
                if (arg0 instanceof RubyString) {
                  String connected_device_name = arg0.toString();
                  int status = session_get_status(connected_device_name);
                  return ObjectFactory.createInteger(status);
                } else {
                  throw new RubyException(
                      RubyRuntime.ArgumentErrorClass,
                      "in RhoBluetooth.session_get_status: wrong argument type.Should be String");
                }
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "session_read_string",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg0, RubyBlock block) {
                if (arg0 instanceof RubyString) {
                  String connected_device_name = arg0.toString();
                  String str = session_read_string(connected_device_name);
                  return ObjectFactory.createString(str);
                } else {
                  throw new RubyException(
                      RubyRuntime.ArgumentErrorClass,
                      "in RhoBluetooth.session_read_string: wrong argument type.Should be String");
                }
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "session_write_string",
            new RubyTwoArgMethod() {
              protected RubyValue run(
                  RubyValue receiver, RubyValue arg1, RubyValue arg2, RubyBlock block) {
                if ((arg1 instanceof RubyString) && (arg2 instanceof RubyString)) {
                  String connected_device_name = arg1.toString();
                  String str = arg2.toString();
                  session_write_string(connected_device_name, str);
                } else {
                  throw new RubyException(
                      RubyRuntime.ArgumentErrorClass,
                      "in RhoBluetooth.session_write_string: wrong argument type.Should be String");
                }
                return RubyConstant.QNIL;
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "session_read_data",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg0, RubyBlock block) {
                if (arg0 instanceof RubyString) {
                  String connected_device_name = arg0.toString();
                  byte[] data = session_read_data(connected_device_name);
                  RubyArray ruby_ar = ObjectFactory.createArray(data.length, data.length, false);
                  int i;
                  for (i = 0; i < data.length; i++) {
                    ruby_ar.and(ObjectFactory.createFixnum(data[i]));
                  }
                  return ruby_ar;
                } else {
                  throw new RubyException(
                      RubyRuntime.ArgumentErrorClass,
                      "in session_read_data: wrong argument type.Should be String");
                }
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "session_write_data",
            new RubyTwoArgMethod() {
              protected RubyValue run(
                  RubyValue receiver, RubyValue arg1, RubyValue arg2, RubyBlock block) {
                if ((arg1 instanceof RubyString) && (arg2 instanceof RubyArray)) {
                  String connected_device_name = arg1.toString();
                  RubyArray ruby_ar = (RubyArray) arg2;
                  int i;
                  byte[] data = new byte[ruby_ar.size()];
                  for (i = 0; i < ruby_ar.size(); i++) {
                    RubyValue num = ruby_ar.get(i);
                    int b = 0;
                    if (num instanceof RubyFixnum) {
                      RubyFixnum rv = (RubyFixnum) num;
                      b = rv.toInt();
                    } else {
                      if (num instanceof RubyInteger) {
                        RubyInteger rv = (RubyInteger) num;
                        b = rv.toInt();
                      } else {
                        if (num instanceof RubySymbol) {
                          RubySymbol rv = (RubySymbol) num;
                          b = rv.toInt();
                        } else {
                          throw new RubyException(
                              RubyRuntime.ArgumentErrorClass,
                              "in RhoBluetooth.session_write_data: wrong argument type.Should be Array of Fixnums or Integers");
                        }
                      }
                    }
                    data[i] = (byte) b;
                  }
                  session_write_data(connected_device_name, data);
                } else {
                  throw new RubyException(
                      RubyRuntime.ArgumentErrorClass,
                      "in RhoBluetooth.session_write_data: wrong argument type.Should be String second should be Array");
                }
                return RubyConstant.QNIL;
              }
            });
  }