Refine search
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); }
case UINT64: if (!Utils.isRubyNum(value)) { throw runtime.newTypeError("Expected number type for integral field."); if (!(value instanceof RubyBoolean)) throw runtime.newTypeError("Invalid argument for boolean field."); val = value.isTrue(); break; case BYTES: case MESSAGE: RubyClass typeClass = (RubyClass) ((RubyDescriptor) getDescriptorForField(context, fieldDescriptor)).msgclass(context); if (!value.getMetaClass().equals(typeClass)) throw runtime.newTypeError(value, "Invalid type to assign to submessage field."); val = ((RubyMessage) value).build(context, depth + 1); val = enumDescriptor.findValueByNumberCreatingIfUnknown(RubyNumeric.num2int(value)); } else if (value instanceof RubySymbol || value instanceof RubyString) { val = enumDescriptor.findValueByName(value.asJavaString()); } else { throw runtime.newTypeError("Expected number or symbol type for enum field.");
public static void createRubyDescriptorPool(Ruby runtime) { RubyModule protobuf = runtime.getClassFromPath("Google::Protobuf"); RubyClass cDescriptorPool = protobuf.defineClassUnder("DescriptorPool", runtime.getObject(), new ObjectAllocator() { @Override public IRubyObject allocate(Ruby runtime, RubyClass klazz) { return new RubyDescriptorPool(runtime, klazz); } }); cDescriptorPool.defineAnnotatedMethods(RubyDescriptorPool.class); descriptorPool = (RubyDescriptorPool) cDescriptorPool.newInstance(runtime.getCurrentContext(), Block.NULL_BLOCK); }
private RubyModule buildClassFromDescriptor(ThreadContext context) { Ruby runtime = context.runtime; ObjectAllocator allocator = new ObjectAllocator() { @Override public IRubyObject allocate(Ruby runtime, RubyClass klazz) { return new RubyMessage(runtime, klazz, descriptor); } }; // rb_define_class_id RubyClass klass = RubyClass.newClass(runtime, runtime.getObject()); klass.setAllocator(allocator); klass.makeMetaClass(runtime.getObject().getMetaClass()); klass.inherit(runtime.getObject()); RubyModule messageExts = runtime.getClassFromPath("Google::Protobuf::MessageExts"); klass.include(new IRubyObject[] {messageExts}); klass.instance_variable_set(runtime.newString(Utils.DESCRIPTOR_INSTANCE_VAR), this); klass.defineAnnotatedMethods(RubyMessage.class); return klass; }
public static void createDigestBubbleBabble(Ruby runtime) { runtime.getLoadService().require("digest"); RubyModule Digest = runtime.getModule("Digest"); RubyClass Base = Digest.getClass("Base"); RubyClass MD5 = Digest.defineClassUnder("BubbleBabble", Base, Base.getAllocator()); MD5.setInternalVariable("metadata", new Metadata("BubbleBabble", 64)); }
public static void createRubyFileDescriptor(Ruby runtime) { RubyModule mProtobuf = runtime.getClassFromPath("Google::Protobuf"); RubyClass cFieldDescriptor = mProtobuf.defineClassUnder("FieldDescriptor", runtime.getObject(), new ObjectAllocator() { @Override public IRubyObject allocate(Ruby runtime, RubyClass klazz) { return new RubyFieldDescriptor(runtime, klazz); } }); cFieldDescriptor.defineAnnotatedMethods(RubyFieldDescriptor.class); }
@JRubyMethod(name = "==", required = 1) public static IRubyObject op_equal(ThreadContext ctx, IRubyObject self, IRubyObject oth) { RubyString str1, str2; RubyModule instance = (RubyModule)self.getRuntime().getModule("Digest").getConstantAt("Instance"); if (oth.getMetaClass().getRealClass().hasModuleInHierarchy(instance)) { str1 = digest(ctx, self, null).convertToString(); str2 = digest(ctx, oth, null).convertToString(); } else { str1 = to_s(ctx, self).convertToString(); str2 = oth.convertToString(); } boolean ret = str1.length().eql(str2.length()) && (str1.eql(str2)); return ret ? self.getRuntime().getTrue() : self.getRuntime().getFalse(); }
public static RubyClass createEnumsClass(Ruby runtime, RubyModule ffiModule) { RubyClass enumsClass = ffiModule.defineClassUnder("Enums", runtime.getObject(), Allocator.INSTANCE); enumsClass.defineAnnotatedMethods(Enums.class); enumsClass.defineAnnotatedConstants(Enums.class); enumsClass.includeModule(ffiModule.getConstant("DataConverter")); return enumsClass; }
@JRubyMethod(name = "new", rest = true, meta = true) public static IRubyObject newInstance(IRubyObject recv, IRubyObject[] args, Block block) { IRubyObject obj = ((RubyClass)recv).allocate(); obj.callMethod(recv.getRuntime().getCurrentContext(), "initialize", args, block); return obj; }
@JRubyMethod(name = "initialize", visibility = PRIVATE, compat = RUBY2_0) public IRubyObject initialize20(ThreadContext context, IRubyObject object, Block block) { Ruby runtime = context.runtime; RubySymbol each = runtime.newSymbol("each"); // check for size if ((object.isNil() || runtime.getProc().isInstance(object)) || runtime.getFloat().isInstance(object) && ((RubyFloat)object).getDoubleValue() == Float.POSITIVE_INFINITY) { // object is nil, a proc, or infinity; use it for size IRubyObject gen = context.runtime.getModule("JRuby").getClass("Generator").callMethod(context, "new", new IRubyObject[0], block); return initialize20(gen, each, NULL_ARRAY, object); } return initialize(object, each, NULL_ARRAY); }
static ReaderImpl createReader(Ruby runtime, List<String> lines) { RubyArray rubyLines = runtime.newArray(lines.size()); for (String line : lines) { rubyLines.add(runtime.newString(line)); } RubyClass readerClass = runtime.getModule("Asciidoctor").getClass("Reader"); return new ReaderImpl(readerClass.callMethod("new", rubyLines)); }
public static RubyString typeAsString(IRubyObject obj) { if (obj.isNil()) return obj.getRuntime().newString("nil"); if (obj instanceof RubyBoolean) return obj.getRuntime().newString(obj.isTrue() ? "true" : "false"); return obj.getMetaClass().getRealClass().rubyName(); }
@Deprecated public static IRubyObject convertToTypeWithCheck(IRubyObject obj, RubyClass target, int convertMethodIndex, String convertMethod) { if (target.isInstance(obj)) return obj; IRubyObject val = TypeConverter.convertToType(obj, target, convertMethod, false); if (val.isNil()) return val; if (!target.isInstance(val)) { Ruby runtime = obj.getRuntime(); throw runtime.newTypeError(str(runtime, types(runtime, obj.getMetaClass()), "#" + convertMethod + " should return ", types(runtime, target))); } return val; }
private String formatMessage(final IRubyObject msg) { if (getRuntime().getString().equals(msg.getType())) { return msg.asJavaString(); } else if (getRuntime().getHash().equals(msg.getType())) { final RubyHash hash = (RubyHash) msg; return Objects.toString(hash.get(getRuntime().newSymbol(LOG_PROPERTY_TEXT))); } throw new IllegalArgumentException(Objects.toString(msg)); }
static RubyClass createJavaClassClass(final Ruby runtime, final RubyModule Java, final RubyClass JavaObject) { // TODO: Determine if a real allocator is needed here. Do people want to extend // JavaClass? Do we want them to do that? Can you Class.new(JavaClass)? Should you be able to? // NOTE: NOT_ALLOCATABLE_ALLOCATOR is probably OK here, since we don't intend for people to monkey with // this type and it can't be marshalled. Confirm. JRUBY-415 RubyClass JavaClass = Java.defineClassUnder("JavaClass", JavaObject, ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR); JavaClass.includeModule(runtime.getModule("Comparable")); JavaClass.defineAnnotatedMethods(JavaClass.class); JavaClass.getMetaClass().undefineMethod("new"); JavaClass.getMetaClass().undefineMethod("allocate"); return JavaClass; }
@JRubyMethod(name = "==", required = 1) public static IRubyObject op_equal(ThreadContext context, IRubyObject self, IRubyObject oth) { if(oth.isNil()) return context.fals; RubyString str1, str2; RubyModule instance = (RubyModule)context.runtime.getModule("Digest").getConstantAt("Instance"); if (oth.getMetaClass().getRealClass().hasModuleInHierarchy(instance)) { str1 = digest(context, self, null).convertToString(); str2 = digest(context, oth, null).convertToString(); } else { str1 = to_s(context, self).convertToString(); str2 = oth.convertToString(); } boolean ret = str1.bytesize().eql(str2.bytesize()) && (str1.eql(str2)); return ret ? context.tru : context.fals; }
/** rb_mod_extend_object * */ @JRubyMethod(name = "extend_object", required = 1, visibility = PRIVATE) public IRubyObject extend_object(IRubyObject obj) { if (!isModule()) { throw getRuntime().newTypeError(this, getRuntime().getModule()); } obj.getSingletonClass().includeModule(this); return obj; }
public void load(Ruby runtime, boolean wrap) { RubyClass rb_cCGI = runtime.defineClass("CGI", runtime.getObject(), runtime.getObject().getAllocator()); RubyModule rb_mEscape = rb_cCGI.defineModuleUnder("Escape"); rb_mEscape.defineAnnotatedMethods(CGIEscape.class); // We do this in cgi/util.rb to work around jruby/jruby#4531. // rb_mUtil.prependModule(rb_mEscape); // rb_mEscape.extend_object(rb_cCGI); }
private static void reifyWithAncestors(RubyClass klazz) { RubyClass realSuper = klazz.getSuperClass().getRealClass(); if (realSuper.getReifiedClass() == null) reifyWithAncestors(realSuper); synchronized (klazz) { klazz.reify(); klazz.setAllocator(new ReifyingAllocator(klazz.getReifiedClass())); } } }