Ejemplo n.º 1
0
  @JRubyMethod(
      name = "raise",
      optional = 3,
      frame = true,
      module = true,
      visibility = Visibility.PRIVATE,
      omit = true)
  public static IRubyObject rbRaise(
      ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
    Ruby runtime = context.runtime;

    // Check for a Java exception
    ConcreteJavaProxy exception = null;
    if (args.length == 0 && runtime.getGlobalVariables().get("$!") instanceof ConcreteJavaProxy) {
      exception = (ConcreteJavaProxy) runtime.getGlobalVariables().get("$!");
    } else if (args.length == 1 && args[0] instanceof ConcreteJavaProxy) {
      exception = (ConcreteJavaProxy) args[0];
    }

    if (exception != null) {
      // looks like someone's trying to raise a Java exception. Let them.
      Object maybeThrowable = exception.getObject();

      if (maybeThrowable instanceof Throwable) {
        // yes, we're cheating here.
        Helpers.throwException((Throwable) maybeThrowable);
        return recv; // not reached
      } else {
        throw runtime.newTypeError("can't raise a non-Throwable Java object");
      }
    } else {
      return RubyKernel.raise(context, recv, args, block);
    }
  }
Ejemplo n.º 2
0
  public static RubyClass createTempfileClass(Ruby runtime) {
    RubyClass tempfileClass =
        runtime.defineClass("Tempfile", runtime.getFile(), TEMPFILE_ALLOCATOR);

    RubyKernel.require(tempfileClass, runtime.newString("tmpdir"), Block.NULL_BLOCK);

    tempfileClass.defineAnnotatedMethods(Tempfile.class);

    return tempfileClass;
  }
Ejemplo n.º 3
0
  private static IRubyObject raiseBecauseCritical(ThreadContext context) {
    Ruby runtime = context.runtime;

    return RubyKernel.raise(
        context,
        runtime.getKernel(),
        new IRubyObject[] {
          runtime.getThreadError(), runtime.newString("timeout within critical section")
        },
        Block.NULL_BLOCK);
  }
Ejemplo n.º 4
0
  private static IRubyObject raiseTimeoutError(ThreadContext context, RaiseException re) {
    Ruby runtime = context.runtime;

    return RubyKernel.raise(
        context,
        runtime.getKernel(),
        new IRubyObject[] {
          runtime.getClassFromPath("Timeout::Error"),
          re.getException().callMethod(context, "message"),
          re.getException().callMethod(context, "backtrace")
        },
        Block.NULL_BLOCK);
  }
Ejemplo n.º 5
0
  /**
   * Just enough configuration settings (most don't make sense in Java) to run the rubytests unit
   * tests. The tests use <code>bindir</code>, <code>RUBY_INSTALL_NAME</code> and <code>EXEEXT
   * </code>.
   */
  public void load(Ruby runtime, boolean wrap) {
    RubyModule configModule;

    configModule = runtime.defineModule("RbConfig");
    RubyKernel.autoload(
        runtime.getObject(),
        runtime.newSymbol("Config"),
        runtime.newString("rbconfig/obsolete.rb"));

    configModule.defineAnnotatedMethods(RbConfigLibrary.class);

    RubyHash configHash = RubyHash.newHash(runtime);
    configModule.defineConstant("CONFIG", configHash);

    String[] versionParts;
    versionParts = Constants.RUBY_VERSION.split("\\.");

    setConfig(configHash, "MAJOR", versionParts[0]);
    setConfig(configHash, "MINOR", versionParts[1]);
    setConfig(configHash, "TEENY", versionParts[2]);
    setConfig(configHash, "ruby_version", versionParts[0] + '.' + versionParts[1] + ".0");
    // Rubygems is too specific on host cpu so until we have real need lets default to universal
    // setConfig(configHash, "arch", System.getProperty("os.arch") + "-java" +
    // System.getProperty("java.specification.version"));
    setConfig(
        configHash, "arch", "universal-java" + System.getProperty("java.specification.version"));

    normalizedHome = getNormalizedHome(runtime);

    // Use property for binDir if available, otherwise fall back to common bin default
    String binDir = SafePropertyAccessor.getProperty("jruby.bindir");
    if (binDir == null) {
      binDir = new NormalizedFile(normalizedHome, "bin").getPath();
    }
    setConfig(configHash, "bindir", binDir);

    setConfig(configHash, "RUBY_INSTALL_NAME", jrubyScript());
    setConfig(configHash, "RUBYW_INSTALL_NAME", IS_WINDOWS ? "jrubyw.exe" : jrubyScript());
    setConfig(configHash, "ruby_install_name", jrubyScript());
    setConfig(configHash, "rubyw_install_name", IS_WINDOWS ? "jrubyw.exe" : jrubyScript());
    setConfig(configHash, "SHELL", jrubyShell());
    setConfig(configHash, "prefix", normalizedHome);
    setConfig(configHash, "exec_prefix", normalizedHome);

    setConfig(configHash, "host_os", getOSName());
    setConfig(configHash, "host_vendor", System.getProperty("java.vendor"));
    setConfig(configHash, "host_cpu", getArchitecture());

    setConfig(configHash, "target_os", getOSName());

    setConfig(configHash, "target_cpu", getArchitecture());

    String jrubyJarFile = "jruby.jar";
    URL jrubyPropertiesUrl = Ruby.getClassLoader().getResource("/org/jruby/Ruby.class");
    if (jrubyPropertiesUrl != null) {
      Pattern jarFile =
          Pattern.compile("jar:file:.*?([a-zA-Z0-9.\\-]+\\.jar)!" + "/org/jruby/Ruby.class");
      Matcher jarMatcher = jarFile.matcher(jrubyPropertiesUrl.toString());
      jarMatcher.find();
      if (jarMatcher.matches()) {
        jrubyJarFile = jarMatcher.group(1);
      }
    }
    setConfig(configHash, "LIBRUBY", jrubyJarFile);
    setConfig(configHash, "LIBRUBY_SO", jrubyJarFile);
    setConfig(configHash, "LIBRUBY_SO", jrubyJarFile);
    setConfig(configHash, "LIBRUBY_ALIASES", jrubyJarFile);

    setConfig(configHash, "build", Constants.BUILD);
    setConfig(configHash, "target", Constants.TARGET);

    String shareDir = new NormalizedFile(normalizedHome, "share").getPath();
    String includeDir = new NormalizedFile(normalizedHome, "lib/native/" + getOSName()).getPath();

    String vendorDirGeneral = getVendorDirGeneral(runtime);
    String siteDirGeneral = getSiteDirGeneral(runtime);
    String rubySharedLibDir = getRubySharedLibDir(runtime);
    String rubyLibDir = getRubyLibDir(runtime);
    String archDir = getArchDir(runtime);
    String vendorDir = getVendorDir(runtime);
    String vendorLibDir = getVendorLibDir(runtime);
    String vendorArchDir = getVendorArchDir(runtime);
    String siteDir = getSiteDir(runtime);
    String siteLibDir = getSiteLibDir(runtime);
    String siteArchDir = getSiteArchDir(runtime);
    String sysConfDir = getSysConfDir(runtime);

    setConfig(configHash, "libdir", vendorDirGeneral);
    setConfig(configHash, "rubylibprefix", vendorDirGeneral + "/ruby");
    setConfig(configHash, "rubylibdir", rubyLibDir);
    setConfig(configHash, "rubysharedlibdir", rubySharedLibDir);
    if (!isSiteVendorSame(runtime)) {
      setConfig(configHash, "vendordir", vendorDir);
      setConfig(configHash, "vendorlibdir", vendorLibDir);
      setConfig(configHash, "vendorarchdir", vendorArchDir);
    }
    setConfig(configHash, "sitedir", siteDir);
    setConfig(configHash, "sitelibdir", siteLibDir);
    setConfig(configHash, "sitearchdir", siteArchDir);
    setConfig(configHash, "sitearch", "java");
    setConfig(configHash, "archdir", archDir);
    setConfig(configHash, "topdir", archDir);
    setConfig(configHash, "includedir", includeDir);
    setConfig(configHash, "configure_args", "");
    setConfig(configHash, "datadir", shareDir);
    setConfig(configHash, "mandir", new NormalizedFile(normalizedHome, "man").getPath());
    setConfig(configHash, "sysconfdir", sysConfDir);
    setConfig(configHash, "localstatedir", new NormalizedFile(normalizedHome, "var").getPath());
    setConfig(configHash, "DLEXT", "jar");
    if (getRubygemsDir(runtime) != null) {
      setConfig(configHash, "rubygemsdir", new NormalizedFile(getRubygemsDir(runtime)).getPath());
    }

    if (Platform.IS_WINDOWS) {
      setConfig(configHash, "EXEEXT", ".exe");
    } else {
      setConfig(configHash, "EXEEXT", "");
    }

    setConfig(configHash, "ridir", new NormalizedFile(shareDir, "ri").getPath());

    // These will be used as jruby defaults for rubygems if found
    String gemhome = SafePropertyAccessor.getProperty("jruby.gem.home");
    String gempath = SafePropertyAccessor.getProperty("jruby.gem.path");
    if (gemhome != null) setConfig(configHash, "default_gem_home", gemhome);
    if (gempath != null) setConfig(configHash, "default_gem_path", gempath);

    setConfig(configHash, "joda-time.version", Constants.JODA_TIME_VERSION);
    setConfig(configHash, "tzdata.version", Constants.TZDATA_VERSION);

    RubyHash mkmfHash = RubyHash.newHash(runtime);

    setConfig(mkmfHash, "libdir", vendorDirGeneral);
    setConfig(mkmfHash, "arch", "java");
    setConfig(mkmfHash, "rubylibdir", rubyLibDir);
    setConfig(mkmfHash, "rubysharedlibdir", rubySharedLibDir);
    if (!isSiteVendorSame(runtime)) {
      setConfig(mkmfHash, "vendordir", vendorDir);
      setConfig(mkmfHash, "vendorlibdir", vendorLibDir);
      setConfig(mkmfHash, "vendorarchdir", vendorArchDir);
    }
    setConfig(mkmfHash, "sitedir", siteDir);
    setConfig(mkmfHash, "sitelibdir", siteLibDir);
    setConfig(mkmfHash, "sitearchdir", siteArchDir);
    setConfig(mkmfHash, "sitearch", "java");
    setConfig(mkmfHash, "archdir", archDir);
    setConfig(mkmfHash, "topdir", archDir);
    setConfig(mkmfHash, "configure_args", "");
    setConfig(mkmfHash, "datadir", new NormalizedFile(normalizedHome, "share").getPath());
    setConfig(mkmfHash, "mandir", new NormalizedFile(normalizedHome, "man").getPath());
    setConfig(mkmfHash, "sysconfdir", sysConfDir);
    setConfig(mkmfHash, "localstatedir", new NormalizedFile(normalizedHome, "var").getPath());
    if (getRubygemsDir(runtime) != null) {
      setConfig(mkmfHash, "rubygemsdir", new NormalizedFile(getRubygemsDir(runtime)).getPath());
    }

    setupMakefileConfig(configModule, mkmfHash);

    runtime.getLoadService().load("jruby/kernel/rbconfig.rb", false);
  }
Ejemplo n.º 6
0
  @JRubyMethod
  public IRubyObject parse(ThreadContext context, IRubyObject target) {
    Ruby runtime = context.runtime;

    // FIXME? only supports Unicode, since we have to produces strings...
    StreamReader reader;
    if (target.respondsTo("read")) {
      reader = new StreamReader(new InputStreamReader(new IOInputStream(target)));
    } else {
      reader = new StreamReader(new StringReader(target.convertToString().asJavaString()));
    }
    Parser parser = new ParserImpl(reader);
    IRubyObject handler = getInstanceVariable("@handler");
    Event event;

    while (true) {
      try {
        event = parser.getEvent();

        // FIXME: Event should expose a getID, so it can be switched
        if (event.is(ID.StreamStart)) {
          invoke(context, handler, "start_stream", runtime.newFixnum(YAML_ANY_ENCODING));
        } else if (event.is(ID.DocumentStart)) {
          DocumentStartEvent dse = (DocumentStartEvent) event;

          Integer[] versionInts = dse.getVersion();
          IRubyObject version =
              versionInts == null
                  ? runtime.getNil()
                  : RubyArray.newArray(
                      runtime,
                      runtime.newFixnum(versionInts[0]),
                      runtime.newFixnum(versionInts[1]));

          Map<String, String> tagsMap = dse.getTags();
          RubyArray tags = RubyArray.newArray(runtime);
          if (tags.size() > 0) {
            for (Map.Entry<String, String> tag : tagsMap.entrySet()) {
              tags.append(
                  RubyArray.newArray(
                      runtime,
                      RubyString.newString(runtime, tag.getKey()),
                      RubyString.newString(runtime, tag.getValue())));
            }
          }

          invoke(
              context,
              handler,
              "start_document",
              version,
              tags,
              runtime.newBoolean(dse.getExplicit()));
        } else if (event.is(ID.DocumentEnd)) {
          DocumentEndEvent dee = (DocumentEndEvent) event;
          invoke(context, handler, "end_document", runtime.newBoolean(dee.getExplicit()));
        } else if (event.is(ID.Alias)) {
          AliasEvent ae = (AliasEvent) event;
          IRubyObject alias = runtime.getNil();
          if (ae.getAnchor() != null) {
            alias = RubyString.newString(runtime, ae.getAnchor());
          }

          invoke(context, handler, "alias", alias);
        } else if (event.is(ID.Scalar)) {
          ScalarEvent se = (ScalarEvent) event;
          IRubyObject anchor =
              se.getAnchor() == null
                  ? runtime.getNil()
                  : RubyString.newString(runtime, se.getAnchor());
          IRubyObject tag =
              se.getTag() == null ? runtime.getNil() : RubyString.newString(runtime, se.getTag());
          IRubyObject plain_implicit = runtime.newBoolean(se.getImplicit().isFirst());
          IRubyObject quoted_implicit = runtime.newBoolean(se.getImplicit().isSecond());
          IRubyObject style = runtime.newFixnum(se.getStyle());
          IRubyObject val = RubyString.newString(runtime, se.getValue());

          invoke(
              context, handler, "scalar", val, anchor, tag, plain_implicit, quoted_implicit, style);
        } else if (event.is(ID.SequenceStart)) {
          SequenceStartEvent sse = (SequenceStartEvent) event;
          IRubyObject anchor =
              sse.getAnchor() == null
                  ? runtime.getNil()
                  : RubyString.newString(runtime, sse.getAnchor());
          IRubyObject tag =
              sse.getTag() == null ? runtime.getNil() : RubyString.newString(runtime, sse.getTag());
          IRubyObject implicit = runtime.newBoolean(sse.getImplicit());
          IRubyObject style = runtime.newFixnum(sse.getFlowStyle() ? 1 : 0);

          invoke(context, handler, "start_sequence", anchor, tag, implicit, style);
        } else if (event.is(ID.SequenceEnd)) {
          invoke(context, handler, "end_sequence");
        } else if (event.is(ID.MappingStart)) {
          MappingStartEvent mse = (MappingStartEvent) event;
          IRubyObject anchor =
              mse.getAnchor() == null
                  ? runtime.getNil()
                  : RubyString.newString(runtime, mse.getAnchor());
          IRubyObject tag =
              mse.getTag() == null ? runtime.getNil() : RubyString.newString(runtime, mse.getTag());
          IRubyObject implicit = runtime.newBoolean(mse.getImplicit());
          IRubyObject style = runtime.newFixnum(mse.getFlowStyle() ? 1 : 0);

          invoke(context, handler, "start_mapping", anchor, tag, implicit, style);
        } else if (event.is(ID.MappingEnd)) {
          invoke(context, handler, "end_mapping");
        } else if (event.is(ID.StreamEnd)) {
          invoke(context, handler, "end_stream");
          break;
        }
      } catch (ParserException pe) {
        parser = null;
        RubyKernel.raise(
            context,
            runtime.getModule("Psych").getConstant("SyntaxError"),
            new IRubyObject[] {runtime.newString(pe.getLocalizedMessage())},
            Block.NULL_BLOCK);
      }
    }

    return this;
  }
Ejemplo n.º 7
0
 @JRubyMethod(name = "printf", required = 1, rest = true)
 public IRubyObject printf(ThreadContext context, IRubyObject[] args) {
   callMethod(context, "write", RubyKernel.sprintf(context, this, args));
   return getRuntime().getNil();
 }
Ejemplo n.º 8
0
 @JRubyMethod(name = "printf", required = 1, rest = true)
 @Override
 public IRubyObject printf(ThreadContext context, IRubyObject[] args) {
   append(context, RubyKernel.sprintf(context, this, args));
   return getRuntime().getNil();
 }