@JRubyMethod public IRubyObject lookup(ThreadContext context, IRubyObject fieldName) { return this.fieldDefMap.get(fieldName.asJavaString()); }
protected IRubyObject deepCopy(ThreadContext context) { RubyMap newMap = newThisType(context); switch (valueType) { case MESSAGE: for (IRubyObject key : table.keySet()) { RubyMessage message = (RubyMessage) table.get(key); newMap.table.put(key.dup(), message.deepCopy(context)); } break; default: for (IRubyObject key : table.keySet()) { newMap.table.put(key.dup(), table.get(key).dup()); } } return newMap; }
@JRubyMethod(name = "==") public IRubyObject eq(ThreadContext context, IRubyObject other) { return this.toArray(context).op_equal(context, other); }
@JRubyMethod(name = "[]=") public IRubyObject indexSet(ThreadContext context, IRubyObject key, IRubyObject value) { key = Utils.checkType(context, keyType, key, (RubyModule) valueTypeClass); value = Utils.checkType(context, valueType, value, (RubyModule) valueTypeClass); IRubyObject symbol; if (valueType == Descriptors.FieldDescriptor.Type.ENUM && Utils.isRubyNum(value) && ! (symbol = RubyEnum.lookup(context, valueTypeClass, value)).isNil()) { value = symbol; } this.table.put(key, value); return value; }
public static int num2uint(IRubyObject value) { long longVal = RubyNumeric.num2long(value); if (longVal > UINT_MAX) throw value.getRuntime().newRangeError("Integer " + longVal + " too big to convert to 'unsigned int'"); long num = longVal; if (num > Integer.MAX_VALUE || num < Integer.MIN_VALUE) // encode to UINT32 num = (-longVal ^ (-1l >>> 32) ) + 1; RubyNumeric.checkInt(value, num); return (int) num; }
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 Descriptors.FieldDescriptor.Type rubyToFieldType(IRubyObject typeClass) { return Descriptors.FieldDescriptor.Type.valueOf(typeClass.asJavaString().toUpperCase()); }
protected static void validateTypeClass(ThreadContext context, Descriptors.FieldDescriptor.Type type, IRubyObject value) { Ruby runtime = context.runtime; if (!(value instanceof RubyModule)) { throw runtime.newArgumentError("TypeClass has incorrect type"); } RubyModule klass = (RubyModule) value; IRubyObject descriptor = klass.getInstanceVariable(DESCRIPTOR_INSTANCE_VAR); if (descriptor.isNil()) { throw runtime.newArgumentError("Type class has no descriptor. Please pass a " + "class or enum as returned by the DescriptorPool."); } if (type == Descriptors.FieldDescriptor.Type.MESSAGE) { if (! (descriptor instanceof RubyDescriptor)) { throw runtime.newArgumentError("Descriptor has an incorrect type"); } } else if (type == Descriptors.FieldDescriptor.Type.ENUM) { if (! (descriptor instanceof RubyEnumDescriptor)) { throw runtime.newArgumentError("Descriptor has an incorrect type"); } } }
private Descriptors.FieldDescriptor findField(ThreadContext context, IRubyObject fieldName) { String nameStr = fieldName.asJavaString(); Descriptors.FieldDescriptor ret = this.descriptor.findFieldByName(Utils.escapeIdentifier(nameStr)); if (ret == null) throw context.runtime.newArgumentError("field " + fieldName.asJavaString() + " is not found"); return ret; }
public static RubyFieldDescriptor msgdefCreateField(ThreadContext context, String label, IRubyObject name, IRubyObject type, IRubyObject number, IRubyObject typeClass, RubyClass cFieldDescriptor) { Ruby runtime = context.runtime; RubyFieldDescriptor fieldDef = (RubyFieldDescriptor) cFieldDescriptor.newInstance(context, Block.NULL_BLOCK); fieldDef.setLabel(context, runtime.newString(label)); fieldDef.setName(context, name); fieldDef.setType(context, type); fieldDef.setNumber(context, number); if (!typeClass.isNil()) { if (!(typeClass instanceof RubyString)) { throw runtime.newArgumentError("expected string for type class"); } fieldDef.setSubmsgName(context, typeClass); } return fieldDef; }
private boolean hasField(IRubyObject fieldName) { String nameStr = fieldName.asJavaString(); return this.descriptor.findFieldByName(Utils.escapeIdentifier(nameStr)) != null; }
@JRubyMethod(name = "name=") public IRubyObject setName(ThreadContext context, IRubyObject name) { this.name = context.runtime.newString(name.asJavaString()); this.builder.setName(name.asJavaString()); return context.runtime.getNil(); }
@JRubyMethod(name = "type=") public IRubyObject setType(ThreadContext context, IRubyObject value) { this.builder.setType(DescriptorProtos.FieldDescriptorProto.Type.valueOf("TYPE_" + value.asJavaString().toUpperCase())); return context.runtime.getNil(); }
@JRubyMethod(name = "label=") public IRubyObject setLabel(ThreadContext context, IRubyObject value) { String labelName = value.asJavaString(); this.label = context.runtime.newSymbol(labelName.toLowerCase()); this.builder.setLabel( DescriptorProtos.FieldDescriptorProto.Label.valueOf("LABEL_" + labelName.toUpperCase())); return context.runtime.getNil(); }
@JRubyMethod(name = "name=") public IRubyObject setName(ThreadContext context, IRubyObject name) { this.name = name; this.builder.setName(Utils.escapeIdentifier(name.asJavaString())); return context.runtime.getNil(); }
@JRubyMethod(name = "submsg_name=") public IRubyObject setSubmsgName(ThreadContext context, IRubyObject name) { this.builder.setTypeName("." + Utils.escapeIdentifier(name.asJavaString())); return context.runtime.getNil(); }
@JRubyMethod(name = "name=") public IRubyObject setName(ThreadContext context, IRubyObject name) { this.name = name; this.builder.setName(Utils.escapeIdentifier(this.name.asJavaString())); return context.runtime.getNil(); }
@JRubyMethod(name = "add_field") public IRubyObject addField(ThreadContext context, IRubyObject obj) { RubyFieldDescriptor fieldDef = (RubyFieldDescriptor) obj; this.fieldDefMap.put(fieldDef.getName(context).asJavaString(), fieldDef); this.builder.addField(fieldDef.build()); return context.runtime.getNil(); }
@JRubyMethod(name = "name=") public IRubyObject setName(ThreadContext context, IRubyObject value) { String nameStr = value.asJavaString(); this.name = context.runtime.newString(nameStr); this.builder.setName(Utils.escapeIdentifier(nameStr)); 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; }