public static RubyClass createPointerClass(Ruby runtime, RubyModule module) { RubyClass pointerClass = module.defineClassUnder( "Pointer", module.getClass(AbstractMemory.ABSTRACT_MEMORY_RUBY_CLASS), RubyInstanceConfig.REIFY_RUBY_CLASSES ? new ReifyingAllocator(Pointer.class) : PointerAllocator.INSTANCE); pointerClass.defineAnnotatedMethods(Pointer.class); pointerClass.defineAnnotatedConstants(Pointer.class); pointerClass.setReifiedClass(Pointer.class); pointerClass.kindOf = new RubyModule.KindOf() { @Override public boolean isKindOf(IRubyObject obj, RubyModule type) { return obj instanceof Pointer && super.isKindOf(obj, type); } }; module.defineClassUnder( "NullPointerError", runtime.getRuntimeError(), runtime.getRuntimeError().getAllocator()); // Add Pointer::NULL as a constant Pointer nullPointer = new Pointer(runtime, pointerClass, new NullMemoryIO(runtime)); pointerClass.setConstant("NULL", nullPointer); runtime .getNilClass() .addMethod("to_ptr", new NilToPointerMethod(runtime.getNilClass(), nullPointer)); return pointerClass; }
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); }
/** 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; }