Example #1
0
  /** do_coerce */
  protected final RubyArray doCoerce(ThreadContext context, IRubyObject other, boolean err) {
    if (!other.respondsTo("coerce")) {
      if (err) {
        coerceRescue(context, other);
      }
      return null;
    }
    final Ruby runtime = context.runtime;
    final IRubyObject $ex = context.getErrorInfo();
    final IRubyObject result;
    try {
      result = coerceBody(context, other);
    } catch (RaiseException e) { // e.g. NoMethodError: undefined method `coerce'
      if (e.getException().kind_of_p(context, runtime.getStandardError()).isTrue()) {
        context.setErrorInfo($ex); // restore $!
        RubyWarnings warnings = context.runtime.getWarnings();
        warnings.warn("Numerical comparison operators will no more rescue exceptions of #coerce");
        warnings.warn("in the next release. Return nil in #coerce if the coercion is impossible.");
        if (err) {
          coerceFailed(context, other);
        }
        return null;
      } else {
        throw e;
      }
    }

    return coerceResult(runtime, result, err);
  }
Example #2
0
  private static IRubyObject callCmpMethod(
      ThreadContext context, IRubyObject recv, IRubyObject other, IRubyObject returnValueOnError) {
    Ruby runtime = context.runtime;

    if (recv == other) return runtime.getTrue();

    try {
      IRubyObject result = invokedynamic(context, recv, OP_CMP, other);

      // This is only to prevent throwing exceptions by cmperr - it has poor performance
      if (result.isNil()) {
        return returnValueOnError;
      }

      return RubyBoolean.newBoolean(runtime, cmpint(context, result, recv, other) == 0);
    } catch (RaiseException e) {
      if (e.getException().kind_of_p(context, runtime.getStandardError()).isTrue()) {
        // clear error info resulting from failure to compare (JRUBY-3292)
        context.setErrorInfo(runtime.getNil());
        return returnValueOnError;
      } else {
        throw e;
      }
    }
  }
Example #3
0
  /** Create the Zlib module and add it to the Ruby runtime. */
  public static RubyModule createZlibModule(Ruby runtime) {
    RubyModule result = runtime.defineModule("Zlib");

    RubyClass gzfile =
        result.defineClassUnder("GzipFile", runtime.getObject(), RubyGzipFile.GZIPFILE_ALLOCATOR);
    gzfile.defineAnnotatedMethods(RubyGzipFile.class);

    RubyClass gzreader =
        result.defineClassUnder("GzipReader", gzfile, RubyGzipReader.GZIPREADER_ALLOCATOR);
    gzreader.includeModule(runtime.getEnumerable());
    gzreader.defineAnnotatedMethods(RubyGzipReader.class);

    RubyClass standardError = runtime.getStandardError();
    RubyClass zlibError =
        result.defineClassUnder("Error", standardError, standardError.getAllocator());
    gzreader.defineClassUnder("Error", zlibError, zlibError.getAllocator());

    RubyClass gzwriter =
        result.defineClassUnder("GzipWriter", gzfile, RubyGzipWriter.GZIPWRITER_ALLOCATOR);
    gzwriter.defineAnnotatedMethods(RubyGzipWriter.class);

    result.defineConstant("ZLIB_VERSION", runtime.newString("1.2.1"));
    result.defineConstant("VERSION", runtime.newString("0.6.0"));

    result.defineConstant("BINARY", runtime.newFixnum(0));
    result.defineConstant("ASCII", runtime.newFixnum(1));
    result.defineConstant("UNKNOWN", runtime.newFixnum(2));

    result.defineConstant("DEF_MEM_LEVEL", runtime.newFixnum(8));
    result.defineConstant("MAX_MEM_LEVEL", runtime.newFixnum(9));

    result.defineConstant("OS_UNIX", runtime.newFixnum(3));
    result.defineConstant("OS_UNKNOWN", runtime.newFixnum(255));
    result.defineConstant("OS_CODE", runtime.newFixnum(11));
    result.defineConstant("OS_ZSYSTEM", runtime.newFixnum(8));
    result.defineConstant("OS_VMCMS", runtime.newFixnum(4));
    result.defineConstant("OS_VMS", runtime.newFixnum(2));
    result.defineConstant("OS_RISCOS", runtime.newFixnum(13));
    result.defineConstant("OS_MACOS", runtime.newFixnum(7));
    result.defineConstant("OS_OS2", runtime.newFixnum(6));
    result.defineConstant("OS_AMIGA", runtime.newFixnum(1));
    result.defineConstant("OS_QDOS", runtime.newFixnum(12));
    result.defineConstant("OS_WIN32", runtime.newFixnum(11));
    result.defineConstant("OS_ATARI", runtime.newFixnum(5));
    result.defineConstant("OS_MSDOS", runtime.newFixnum(0));
    result.defineConstant("OS_CPM", runtime.newFixnum(9));
    result.defineConstant("OS_TOPS20", runtime.newFixnum(10));

    result.defineConstant("DEFAULT_STRATEGY", runtime.newFixnum(0));
    result.defineConstant("FILTERED", runtime.newFixnum(1));
    result.defineConstant("HUFFMAN_ONLY", runtime.newFixnum(2));

    result.defineConstant("NO_FLUSH", runtime.newFixnum(0));
    result.defineConstant("SYNC_FLUSH", runtime.newFixnum(2));
    result.defineConstant("FULL_FLUSH", runtime.newFixnum(3));
    result.defineConstant("FINISH", runtime.newFixnum(4));

    result.defineConstant("NO_COMPRESSION", runtime.newFixnum(0));
    result.defineConstant("BEST_SPEED", runtime.newFixnum(1));
    result.defineConstant("DEFAULT_COMPRESSION", runtime.newFixnum(-1));
    result.defineConstant("BEST_COMPRESSION", runtime.newFixnum(9));

    result.defineConstant("MAX_WBITS", runtime.newFixnum(15));

    result.defineAnnotatedMethods(RubyZlib.class);

    result.defineClassUnder("StreamEnd", zlibError, zlibError.getAllocator());
    result.defineClassUnder("StreamError", zlibError, zlibError.getAllocator());
    result.defineClassUnder("BufError", zlibError, zlibError.getAllocator());
    result.defineClassUnder("NeedDict", zlibError, zlibError.getAllocator());
    result.defineClassUnder("MemError", zlibError, zlibError.getAllocator());
    result.defineClassUnder("VersionError", zlibError, zlibError.getAllocator());
    result.defineClassUnder("DataError", zlibError, zlibError.getAllocator());

    RubyClass gzError = gzfile.defineClassUnder("Error", zlibError, zlibError.getAllocator());
    gzfile.defineClassUnder("CRCError", gzError, gzError.getAllocator());
    gzfile.defineClassUnder("NoFooter", gzError, gzError.getAllocator());
    gzfile.defineClassUnder("LengthError", gzError, gzError.getAllocator());

    // ZStream actually *isn't* allocatable
    RubyClass zstream =
        result.defineClassUnder(
            "ZStream", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
    zstream.defineAnnotatedMethods(ZStream.class);
    zstream.undefineMethod("new");

    RubyClass infl = result.defineClassUnder("Inflate", zstream, Inflate.INFLATE_ALLOCATOR);
    infl.defineAnnotatedMethods(Inflate.class);

    RubyClass defl = result.defineClassUnder("Deflate", zstream, Deflate.DEFLATE_ALLOCATOR);
    defl.defineAnnotatedMethods(Deflate.class);

    runtime
        .getKernel()
        .callMethod(runtime.getCurrentContext(), "require", runtime.newString("stringio"));

    return result;
  }