Refine search
public static IRubyObject validateStringEncoding(ThreadContext context, Descriptors.FieldDescriptor.Type type, IRubyObject value) { if (!(value instanceof RubyString)) throw context.runtime.newTypeError("Invalid argument for string field."); switch(type) { case BYTES: value = ((RubyString)value).encode(context, context.runtime.evalScriptlet("Encoding::ASCII_8BIT")); break; case STRING: value = ((RubyString)value).encode(context, context.runtime.evalScriptlet("Encoding::UTF_8")); break; default: break; } value.setFrozen(true); return value; }
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); }
switch (fieldType) { case INT32: return runtime.newFixnum((Integer) value); case INT64: return runtime.newFixnum((Long) value); case UINT32: return runtime.newFixnum(((Integer) value) & (-1l >>> 32)); case UINT64: long ret = (Long) value; return ret >= 0 ? runtime.newFixnum(ret) : RubyBignum.newBignum(runtime, UINT64_COMPLEMENTARY.add(new BigInteger(ret + ""))); case FLOAT: return runtime.newFloat((Float) value); case DOUBLE: return runtime.newFloat((Double) value); case BOOL: return (Boolean) value ? runtime.getTrue() : runtime.getFalse(); case BYTES: { IRubyObject wrapped = runtime.newString(((ByteString) value).toStringUtf8()); wrapped.setFrozen(true); return wrapped; IRubyObject wrapped = runtime.newString(value.toString()); wrapped.setFrozen(true); return wrapped; return runtime.getNil();
public static void checkNameAvailability(ThreadContext context, String name) { if (context.runtime.getObject().getConstantAt(name) != null) throw context.runtime.newNameError(name + " is already defined", name); }
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); }
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(required = 2, optional = 1, frame = true) public static IRubyObject scalar(IRubyObject self, IRubyObject[] args, Block block) { Ruby runtime = self.getRuntime(); ThreadContext ctx = runtime.getCurrentContext(); IRubyObject type_id = args[0]; IRubyObject str = args[1]; IRubyObject style = args.length == 2 ? runtime.getNil() : args[2]; IRubyObject scalar = ((RubyModule)((RubyModule)runtime.getModule("YAML")).getConstant("Yecht")).getConstant("Scalar").callMethod(ctx, "new", new IRubyObject[]{type_id, str, style}); outMark((IRubyObject)((RubyObject)self).fastGetInstanceVariable("@emitter"), scalar); return scalar; } }
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; }
@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); }
IRubyObject valueType = args[2]; IRubyObject number = args[3]; IRubyObject typeClass = args.length > 4 ? args[4] : context.runtime.getNil(); keyType.equals(RubySymbol.newSymbol(runtime, "enum")) || keyType.equals(RubySymbol.newSymbol(runtime, "message"))) throw runtime.newArgumentError("Cannot add a map field with a float, double, enum, or message type."); RubyFieldDescriptor keyField = (RubyFieldDescriptor) cFieldDescriptor.newInstance(context, Block.NULL_BLOCK); keyField.setName(context, runtime.newString("key")); keyField.setLabel(context, RubySymbol.newSymbol(runtime, "optional")); keyField.setNumber(context, runtime.newFixnum(1)); keyField.setType(context, keyType); mapentryDesc.addField(context, keyField); RubyFieldDescriptor valueField = (RubyFieldDescriptor) cFieldDescriptor.newInstance(context, Block.NULL_BLOCK); valueField.setName(context, runtime.newString("value")); valueField.setLabel(context, RubySymbol.newSymbol(runtime, "optional")); valueField.setNumber(context, runtime.newFixnum(2)); valueField.setType(context, valueType); if (! typeClass.isNil()) valueField.setSubmsgName(context, typeClass); mapentryDesc.addField(context, valueField); msgdefAddField(context, "repeated", name, runtime.newSymbol("message"), number, mapentryDescName); return runtime.getNil();
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 RubyModule createJavaInterfaceTemplateModule(ThreadContext context) { Ruby runtime = context.runtime; RubyModule javaInterfaceTemplate = runtime.defineModule("JavaInterfaceTemplate"); RubyClass singleton = javaInterfaceTemplate.getSingletonClass(); singleton.addReadAttribute(context, "java_class"); singleton.defineAnnotatedMethods(JavaInterfaceTemplate.class); return javaInterfaceTemplate; }
@JRubyMethod(name = "method_missing", rest = true) public IRubyObject methodMissing(ThreadContext context, IRubyObject[] args) { if (args.length == 1) { RubyDescriptor rubyDescriptor = (RubyDescriptor) getDescriptor(context, metaClass); IRubyObject oneofDescriptor = rubyDescriptor.lookupOneof(context, args[0]); if (oneofDescriptor.isNil()) { if (!hasField(args[0])) { return Helpers.invokeSuper(context, this, metaClass, "method_missing", args, Block.NULL_BLOCK); } return index(context, args[0]); } RubyOneofDescriptor rubyOneofDescriptor = (RubyOneofDescriptor) oneofDescriptor; Descriptors.FieldDescriptor fieldDescriptor = oneofCases.get(rubyOneofDescriptor.getOneofDescriptor()); if (fieldDescriptor == null) return context.runtime.getNil(); return context.runtime.newSymbol(fieldDescriptor.getName()); } else { // fieldName is RubySymbol RubyString field = args[0].asString(); RubyString equalSign = context.runtime.newString(Utils.EQUAL_SIGN); if (field.end_with_p(context, equalSign).isTrue()) { field.chomp_bang(context, equalSign); } if (!hasField(field)) { return Helpers.invokeSuper(context, this, metaClass, "method_missing", args, Block.NULL_BLOCK); } return indexSet(context, field, args[1]); } }
protected IRubyObject setField(ThreadContext context, Descriptors.FieldDescriptor fieldDescriptor, IRubyObject value) { if (Utils.isMapEntry(fieldDescriptor)) { if (!(value instanceof RubyMap)) { throw context.runtime.newTypeError("Expected Map instance"); addRepeatedField(fieldDescriptor, (RubyRepeatedField) value); } else { RubyArray ary = value.convertToArray(); RubyRepeatedField repeatedField = rubyToRepeatedField(context, fieldDescriptor, ary); addRepeatedField(fieldDescriptor, repeatedField); fields.remove(oneofCase); if (value.isNil()) { oneofCases.remove(oneofDescriptor); fields.remove(fieldDescriptor); IRubyObject typeClass = context.runtime.getObject(); boolean addValue = true; if (fieldType == Descriptors.FieldDescriptor.Type.MESSAGE) { typeClass = ((RubyDescriptor) getDescriptorForField(context, fieldDescriptor)).msgclass(context); if (value.isNil()){ addValue = false; Descriptors.EnumValueDescriptor val = enumDescriptor.findValueByNumberCreatingIfUnknown(RubyNumeric.num2int(value)); if (val.getIndex() != -1) value = context.runtime.newSymbol(val.getName()); return context.runtime.getNil();
private RubyModule buildModuleFromDescriptor(ThreadContext context) { Ruby runtime = context.runtime; Utils.checkNameAvailability(context, name.asJavaString()); RubyModule enumModule = RubyModule.newModule(runtime); for (Descriptors.EnumValueDescriptor value : descriptor.getValues()) { enumModule.defineConstant(value.getName(), runtime.newFixnum(value.getNumber())); } enumModule.instance_variable_set(runtime.newString(Utils.DESCRIPTOR_INSTANCE_VAR), this); enumModule.defineAnnotatedMethods(RubyEnum.class); return enumModule; }
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(); }
private IRubyObject wrapField(ThreadContext context, Descriptors.FieldDescriptor fieldDescriptor, Object value) { if (value == null) { return context.runtime.getNil(); } Ruby runtime = context.runtime; switch (fieldDescriptor.getType()) { case INT32: case INT64: case UINT32: case UINT64: case FLOAT: case DOUBLE: case BOOL: case BYTES: case STRING: return Utils.wrapPrimaryValue(context, fieldDescriptor.getType(), value); case MESSAGE: RubyClass typeClass = (RubyClass) ((RubyDescriptor) getDescriptorForField(context, fieldDescriptor)).msgclass(context); RubyMessage msg = (RubyMessage) typeClass.newInstance(context, Block.NULL_BLOCK); return msg.buildFrom(context, (DynamicMessage) value); case ENUM: Descriptors.EnumValueDescriptor enumValueDescriptor = (Descriptors.EnumValueDescriptor) value; if (enumValueDescriptor.getIndex() == -1) { // UNKNOWN ENUM VALUE return runtime.newFixnum(enumValueDescriptor.getNumber()); } return runtime.newSymbol(enumValueDescriptor.getName()); default: return runtime.newString(value.toString()); } }
@Override public void register() { IRubyObject callback = extensionGroupClass.newInstance(rubyRuntime.getCurrentContext(), Block.NULL_BLOCK); asciidoctorModule.callMethod("register_extension_group", rubyRuntime.newString(this.groupName), callback, JavaEmbedUtils.javaToRuby(rubyRuntime, registrators)); }
private RubyArray getLines(ParserConfiguration configuration, Ruby runtime, String file) { RubyArray list = null; IRubyObject scriptLines = runtime.getObject().getConstantAt("SCRIPT_LINES__"); if (!configuration.isEvalParse() && scriptLines != null) { if (scriptLines instanceof RubyHash) { list = runtime.newArray(); ((RubyHash) scriptLines).op_aset(runtime.getCurrentContext(), runtime.newString(file), list); } } return list; }
/** 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; }