private RubyMap newThisType(ThreadContext context) { RubyMap newMap; if (needTypeclass(valueType)) { newMap = (RubyMap) metaClass.newInstance(context, Utils.fieldTypeToRuby(context, keyType), Utils.fieldTypeToRuby(context, valueType), valueTypeClass, Block.NULL_BLOCK); } else { newMap = (RubyMap) metaClass.newInstance(context, Utils.fieldTypeToRuby(context, keyType), Utils.fieldTypeToRuby(context, valueType), Block.NULL_BLOCK); } newMap.table = new HashMap<IRubyObject, IRubyObject>(); return newMap; }
protected IRubyObject deepCopy(ThreadContext context) { RubyMessage copy = (RubyMessage) metaClass.newInstance(context, Block.NULL_BLOCK); for (Descriptors.FieldDescriptor fdef : this.descriptor.getFields()) { if (fdef.isRepeated()) { copy.addRepeatedField(fdef, this.getRepeatedField(context, fdef).deepCopy(context)); } else if (fields.containsKey(fdef)) { copy.fields.put(fdef, fields.get(fdef)); } else if (this.builder.hasField(fdef)) { copy.fields.put(fdef, wrapField(context, fdef, this.builder.getField(fdef))); } } return copy; }
protected List<DynamicMessage> build(ThreadContext context, RubyDescriptor descriptor) { List<DynamicMessage> list = new ArrayList<DynamicMessage>(); RubyClass rubyClass = (RubyClass) descriptor.msgclass(context); Descriptors.FieldDescriptor keyField = descriptor.lookup("key").getFieldDef(); Descriptors.FieldDescriptor valueField = descriptor.lookup("value").getFieldDef(); for (IRubyObject key : table.keySet()) { RubyMessage mapMessage = (RubyMessage) rubyClass.newInstance(context, Block.NULL_BLOCK); mapMessage.setField(context, keyField, key); mapMessage.setField(context, valueField, table.get(key)); list.add(mapMessage.build(context)); } return list; }
/** * call-seq: * Message.dup => new_message * Performs a shallow copy of this message and returns the new copy. */ @JRubyMethod public IRubyObject dup(ThreadContext context) { RubyMessage dup = (RubyMessage) metaClass.newInstance(context, Block.NULL_BLOCK); IRubyObject value; for (Descriptors.FieldDescriptor fieldDescriptor : this.descriptor.getFields()) { if (fieldDescriptor.isRepeated()) { dup.addRepeatedField(fieldDescriptor, this.getRepeatedField(context, fieldDescriptor)); } else if (fields.containsKey(fieldDescriptor)) { dup.fields.put(fieldDescriptor, fields.get(fieldDescriptor)); } else if (this.builder.hasField(fieldDescriptor)) { dup.fields.put(fieldDescriptor, wrapField(context, fieldDescriptor, this.builder.getField(fieldDescriptor))); } } for (Descriptors.FieldDescriptor fieldDescriptor : maps.keySet()) { dup.maps.put(fieldDescriptor, maps.get(fieldDescriptor)); } return dup; }
@JRubyMethod public IRubyObject oneof(ThreadContext context, IRubyObject name, Block block) { RubyOneofDescriptor oneofdef = (RubyOneofDescriptor) cOneofDescriptor.newInstance(context, Block.NULL_BLOCK); RubyOneofBuilderContext ctx = (RubyOneofBuilderContext) cOneofBuilderContext.newInstance(context, oneofdef, Block.NULL_BLOCK); oneofdef.setName(context, name); Binding binding = block.getBinding(); binding.setSelf(ctx); block.yieldSpecific(context); descriptor.addOneof(context, oneofdef); return context.runtime.getNil(); }
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); }
@JRubyMethod(name = "add_enum") public IRubyObject addEnum(ThreadContext context, IRubyObject name, Block block) { RubyEnumDescriptor enumDef = (RubyEnumDescriptor) cEnumDescriptor.newInstance(context, Block.NULL_BLOCK); IRubyObject ctx = cEnumBuilderContext.newInstance(context, enumDef, Block.NULL_BLOCK); enumDef.setName(context, name); if (block.isGiven()) { if (block.arity() == Arity.ONE_ARGUMENT) { block.yield(context, ctx); } else { Binding binding = block.getBinding(); binding.setSelf(ctx); block.yieldSpecific(context); } } this.pendingList.add(enumDef); return context.runtime.getNil(); }
@JRubyMethod(meta = true) public static IRubyObject decode(ThreadContext context, IRubyObject recv, IRubyObject data) { byte[] bin = data.convertToString().getBytes(); RubyMessage ret = (RubyMessage) ((RubyClass) recv).newInstance(context, Block.NULL_BLOCK); try { ret.builder.mergeFrom(bin); } catch (InvalidProtocolBufferException e) { throw context.runtime.newRuntimeError(e.getMessage()); } return ret; }
@JRubyMethod(name = "add_message") public IRubyObject addMessage(ThreadContext context, IRubyObject name, Block block) { RubyDescriptor msgdef = (RubyDescriptor) cDescriptor.newInstance(context, Block.NULL_BLOCK); IRubyObject ctx = cMessageBuilderContext.newInstance(context, msgdef, this, Block.NULL_BLOCK); msgdef.setName(context, name); if (block.isGiven()) { if (block.arity() == Arity.ONE_ARGUMENT) { block.yield(context, ctx); } else { Binding binding = block.getBinding(); binding.setSelf(ctx); block.yieldSpecific(context); } } this.pendingList.add(msgdef); return context.runtime.getNil(); }
@JRubyMethod(name = "decode_json", meta = true) public static IRubyObject decodeJson(ThreadContext context, IRubyObject recv, IRubyObject json) { Ruby runtime = context.runtime; RubyMessage ret = (RubyMessage) ((RubyClass) recv).newInstance(context, Block.NULL_BLOCK); RubyModule jsonModule = runtime.getClassFromPath("JSON"); RubyHash opts = RubyHash.newHash(runtime); opts.fastASet(runtime.newSymbol("symbolize_names"), runtime.getTrue()); IRubyObject[] args = new IRubyObject[] { Helpers.invoke(context, jsonModule, "parse", json, opts) }; ret.initialize(context, args); return ret; }
@Override public void visit(IRubyObject key, IRubyObject value) { if (!(key instanceof RubySymbol) && !(key instanceof RubyString)) throw runtime.newTypeError("Expected string or symbols as hash keys in initialization map."); final Descriptors.FieldDescriptor fieldDescriptor = findField(context, key); if (Utils.isMapEntry(fieldDescriptor)) { if (!(value instanceof RubyHash)) throw runtime.newArgumentError("Expected Hash object as initializer value for map field '" + key.asJavaString() + "'."); final RubyMap map = newMapForField(context, fieldDescriptor); map.mergeIntoSelf(context, value); maps.put(fieldDescriptor, map); } else if (fieldDescriptor.isRepeated()) { if (!(value instanceof RubyArray)) throw runtime.newArgumentError("Expected array as initializer value for repeated field '" + key.asJavaString() + "'."); RubyRepeatedField repeatedField = rubyToRepeatedField(context, fieldDescriptor, value); addRepeatedField(fieldDescriptor, repeatedField); } else { Descriptors.OneofDescriptor oneof = fieldDescriptor.getContainingOneof(); if (oneof != null) { oneofCases.put(oneof, fieldDescriptor); } if (value instanceof RubyHash && fieldDescriptor.getType() == Descriptors.FieldDescriptor.Type.MESSAGE) { RubyDescriptor descriptor = (RubyDescriptor) getDescriptorForField(context, fieldDescriptor); RubyClass typeClass = (RubyClass) descriptor.msgclass(context); value = (IRubyObject) typeClass.newInstance(context, value, Block.NULL_BLOCK); } fields.put(fieldDescriptor, value); } } });
private RubyMap newMapForField(ThreadContext context, Descriptors.FieldDescriptor fieldDescriptor) { RubyDescriptor mapDescriptor = (RubyDescriptor) getDescriptorForField(context, fieldDescriptor); Descriptors.FieldDescriptor keyField = fieldDescriptor.getMessageType().findFieldByNumber(1); Descriptors.FieldDescriptor valueField = fieldDescriptor.getMessageType().findFieldByNumber(2); IRubyObject keyType = RubySymbol.newSymbol(context.runtime, keyField.getType().name()); IRubyObject valueType = RubySymbol.newSymbol(context.runtime, valueField.getType().name()); if (valueField.getType() == Descriptors.FieldDescriptor.Type.MESSAGE) { RubyFieldDescriptor rubyFieldDescriptor = (RubyFieldDescriptor) mapDescriptor.lookup(context, context.runtime.newString("value")); RubyDescriptor rubyDescriptor = (RubyDescriptor) rubyFieldDescriptor.getSubType(context); return (RubyMap) cMap.newInstance(context, keyType, valueType, rubyDescriptor.msgclass(context), Block.NULL_BLOCK); } else { return (RubyMap) cMap.newInstance(context, keyType, valueType, Block.NULL_BLOCK); } }
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; }
@JRubyMethod public IRubyObject build(ThreadContext context, Block block) { RubyBuilder ctx = (RubyBuilder) cBuilder.newInstance(context, Block.NULL_BLOCK); if (block.arity() == Arity.ONE_ARGUMENT) { block.yield(context, ctx); } else { Binding binding = block.getBinding(); binding.setSelf(ctx); block.yieldSpecific(context); } ctx.finalizeToPool(context, this); buildFileDescriptor(context); return context.runtime.getNil(); }
private RubyRepeatedField rubyToRepeatedField(ThreadContext context, Descriptors.FieldDescriptor fieldDescriptor, IRubyObject value) { RubyArray arr = value.convertToArray(); RubyRepeatedField repeatedField = repeatedFieldForFieldDescriptor(context, fieldDescriptor); RubyClass typeClass = null; if (fieldDescriptor.getType() == Descriptors.FieldDescriptor.Type.MESSAGE) { RubyDescriptor descriptor = (RubyDescriptor) getDescriptorForField(context, fieldDescriptor); typeClass = (RubyClass) descriptor.msgclass(context); } for (int i = 0; i < arr.size(); i++) { IRubyObject row = arr.eltInternal(i); if (row instanceof RubyHash && typeClass != null) { row = (IRubyObject) typeClass.newInstance(context, row, Block.NULL_BLOCK); } repeatedField.push(context, row); } return repeatedField; }
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()); } }
RubyClass kvClass = (RubyClass) kvDescriptor.msgclass(context); for (int i = 0; i < mapSize; i++) { RubyMessage kvMessage = (RubyMessage) kvClass.newInstance(context, Block.NULL_BLOCK); DynamicMessage message = (DynamicMessage) this.builder.getRepeatedField(fieldDescriptor, i); kvMessage.buildFrom(context, message);
RubyDescriptor mapentryDesc = (RubyDescriptor) cDescriptor.newInstance(context, Block.NULL_BLOCK); IRubyObject mapentryDescName = RubySymbol.newSymbol(runtime, name).id2name(context); mapentryDesc.setName(context, mapentryDescName); RubyFieldDescriptor keyField = (RubyFieldDescriptor) cFieldDescriptor.newInstance(context, Block.NULL_BLOCK); keyField.setName(context, runtime.newString("key")); keyField.setLabel(context, RubySymbol.newSymbol(runtime, "optional")); RubyFieldDescriptor valueField = (RubyFieldDescriptor) cFieldDescriptor.newInstance(context, Block.NULL_BLOCK); valueField.setName(context, runtime.newString("value")); valueField.setLabel(context, RubySymbol.newSymbol(runtime, "optional"));
@JRubyMethod(name = "puts", rest = true) public IRubyObject puts(ThreadContext context, IRubyObject[] args) { final RubyClass StringIO = context.runtime.getClass("StringIO"); StringIO sio = (StringIO) StringIO.newInstance(context, IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); sio.puts(context, args); write(sio.string(context)); return context.nil; }
public static void initArgsFile(final Ruby runtime) { RubyClass argfClass = runtime.defineClass("ARGFClass", runtime.getObject(), ARGF_ALLOCATOR); argfClass.includeModule(runtime.getEnumerable()); argfClass.defineAnnotatedMethods(RubyArgsFile.class); IRubyObject argsFile = argfClass.newInstance(runtime.getCurrentContext(), new IRubyObject[] { null }, (Block) null); runtime.setArgsFile(argsFile); runtime.getGlobalVariables().defineReadonly("$<", new ArgsFileAccessor(runtime), GlobalVariable.Scope.GLOBAL); runtime.defineGlobalConstant("ARGF", argsFile); runtime.defineReadonlyVariable("$FILENAME", runtime.newString("-"), GlobalVariable.Scope.GLOBAL); }