Beispiel #1
0
  public static RubyClass createStringIOClass(final Ruby runtime) {
    RubyClass stringIOClass =
        runtime.defineClass("StringIO", runtime.fastGetClass("Data"), STRINGIO_ALLOCATOR);

    stringIOClass.defineAnnotatedMethods(RubyStringIO.class);
    stringIOClass.includeModule(runtime.getEnumerable());

    if (runtime.getObject().isConstantDefined("Java")) {
      stringIOClass.defineAnnotatedMethods(IOJavaAddons.AnyIO.class);
    }

    return stringIOClass;
  }
 public static void createRubyDescriptor(Ruby runtime) {
   RubyModule protobuf = runtime.getClassFromPath("Google::Protobuf");
   RubyClass cDescriptor =
       protobuf.defineClassUnder(
           "Descriptor",
           runtime.getObject(),
           new ObjectAllocator() {
             @Override
             public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
               return new RubyDescriptor(runtime, klazz);
             }
           });
   cDescriptor.includeModule(runtime.getEnumerable());
   cDescriptor.defineAnnotatedMethods(RubyDescriptor.class);
 }
Beispiel #3
0
  public static RubyClass createRangeClass(Ruby runtime) {
    RubyClass result = runtime.defineClass("Range", runtime.getObject(), RANGE_ALLOCATOR);
    runtime.setRange(result);
    result.kindOf =
        new RubyModule.KindOf() {
          public boolean isKindOf(IRubyObject obj, RubyModule type) {
            return obj instanceof RubyRange;
          }
        };

    result.setMarshal(RANGE_MARSHAL);
    result.includeModule(runtime.getEnumerable());

    // We override Enumerable#member? since ranges in 1.8.1 are continuous.
    //        result.defineMethod("member?", callbackFactory.getMethod("include_p",
    // RubyKernel.IRUBY_OBJECT));
    //        result.defineMethod("===", callbackFactory.getMethod("include_p",
    // RubyKernel.IRUBY_OBJECT));

    result.defineAnnotatedMethods(RubyRange.class);
    return result;
  }
Beispiel #4
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;
  }