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; }
addRepeatedField(fieldDescriptor, (RubyRepeatedField) value); } else { RubyArray ary = value.convertToArray(); RubyRepeatedField repeatedField = rubyToRepeatedField(context, fieldDescriptor, ary); addRepeatedField(fieldDescriptor, repeatedField);
RubyArray arr = ary.convertToArray(); for (int i = 0; i < arr.size(); i++) { this.storage.add(arr.eltInternal(i));
@JRubyMethod(name = {"to_h", "to_hash"}) public IRubyObject toHash(ThreadContext context) { Ruby runtime = context.runtime; RubyHash ret = RubyHash.newHash(runtime); for (Descriptors.FieldDescriptor fdef : this.descriptor.getFields()) { IRubyObject value = getField(context, fdef); if (!value.isNil()) { if (fdef.isRepeated() && !fdef.isMapField()) { if (fdef.getType() != Descriptors.FieldDescriptor.Type.MESSAGE) { value = Helpers.invoke(context, value, "to_a"); } else { RubyArray ary = value.convertToArray(); for (int i = 0; i < ary.size(); i++) { IRubyObject submsg = Helpers.invoke(context, ary.eltInternal(i), "to_h"); ary.eltInternalSet(i, submsg); } value = ary.to_ary(); } } else if (value.respondsTo("to_h")) { value = Helpers.invoke(context, value, "to_h"); } else if (value.respondsTo("to_a")) { value = Helpers.invoke(context, value, "to_a"); } } ret.fastASet(runtime.newSymbol(fdef.getName()), value); } return ret; }
public static IRubyObject[] openArgsToArgs(Ruby runtime, IRubyObject firstElement, RubyHash options) { IRubyObject value = hashARef(runtime, options, "open_args"); if (value.isNil()) return new IRubyObject[] { firstElement, options }; RubyArray array = value.convertToArray(); IRubyObject[] openArgs = new IRubyObject[array.size()]; value.convertToArray().toArray(openArgs); IRubyObject[] args = new IRubyObject[openArgs.length + 1]; args[0] = firstElement; System.arraycopy(openArgs, 0, args, 1, openArgs.length); return args; }
@JIT public static IRubyObject[] singleBlockArgToArray(IRubyObject value) { IRubyObject[] args; if (value instanceof RubyArray) { args = value.convertToArray().toJavaArray(); } else { args = new IRubyObject[] { value }; } return args; }
/** rb_ary_concat * */ @JRubyMethod(name = "concat") public RubyArray concat(ThreadContext context, IRubyObject obj) { modifyCheck(); concat(context.runtime, obj.convertToArray()); return this; }
@JRubyMethod(meta = true) public static IRubyObject java_send(ThreadContext context, IRubyObject recv, IRubyObject rubyName, IRubyObject argTypes) { String name = rubyName.asJavaString(); RubyArray argTypesAry = argTypes.convertToArray(); final Ruby runtime = context.runtime; checkArgSizeMismatch(runtime, 0, argTypesAry); JavaMethod method = new JavaMethod(runtime, getMethodFromClass(context, recv, name)); return method.invokeStaticDirect(context); }
@Override public IRubyObject op_plus(IRubyObject obj) { if (!packed()) return super.op_plus(obj); RubyArray y = obj.convertToArray(); if (y.size() == 0) return new RubyArrayOneObject(this); return super.op_plus(y); }
@Override public IRubyObject op_plus(IRubyObject obj) { if (!packed()) return super.op_plus(obj); RubyArray y = obj.convertToArray(); if (y.size() == 0) return new RubyArrayTwoObject(this); return super.op_plus(y); }
@JRubyMethod public IRubyObject java_method(ThreadContext context, IRubyObject rubyName, IRubyObject argTypes) { String name = rubyName.asJavaString(); RubyArray argTypesAry = argTypes.convertToArray(); Class[] argTypesClasses = (Class[]) argTypesAry.toArray(new Class[argTypesAry.size()]); return getRubyMethod(context, name, argTypesClasses); }
@JRubyMethod(meta = true) public static IRubyObject java_method(ThreadContext context, IRubyObject proxyClass, IRubyObject rubyName, IRubyObject argTypes) { String name = rubyName.asJavaString(); RubyArray argTypesAry = argTypes.convertToArray(); Class[] argTypesClasses = (Class[])argTypesAry.toArray(new Class[argTypesAry.size()]); return getRubyMethod(context, proxyClass, name, argTypesClasses); }
private static RubyArray mapToPathnames(ThreadContext context, RubyClass clazz, IRubyObject ary) { RubyArray paths = ary.convertToArray(); for (int i = 0; i < paths.size(); i++) { RubyString path = paths.eltOk(i).convertToString(); paths.store(i, newInstance(context, clazz, path)); } return paths; }
@Override public IRubyObject op_plus(IRubyObject obj) { if (!packed()) return super.op_plus(obj); RubyArray y = obj.convertToArray(); if (y.size() == 0) return new RubyArrayTwoObject(this); return super.op_plus(y); }
@JRubyMethod(module = true, visibility = PRIVATE) public static IRubyObject warn(ThreadContext context, IRubyObject recv, IRubyObject _message) { if (_message instanceof RubyArray) { RubyArray messageArray = _message.convertToArray(); for (int i = 0; i < messageArray.size(); i++) warn(context, recv, messageArray.eltOk(i)); return context.nil; } return warn(context, recv, _message.convertToString()); }
@JRubyMethod public IRubyObject each_entry(ThreadContext context, Block block) { if (block.isGiven()) { // TODO: yield block while iterating RubyArray entries = callMethod(context, "entries").convertToArray(); entries.each(context, block); return context.nil; } else { return context.runtime.getDir().callMethod(context, "foreach"); } }
@JRubyMethod public IRubyObject java_send(ThreadContext context, IRubyObject rubyName, IRubyObject argTypes) { String name = rubyName.asJavaString(); RubyArray argTypesAry = argTypes.convertToArray(); Ruby runtime = context.runtime; checkArgSizeMismatch(runtime, 0, argTypesAry); JavaMethod method = new JavaMethod(runtime, getMethod(context, name)); return method.invokeDirect(context, getObject()); }
/** nucomp_marshal_load * */ @JRubyMethod(name = "marshal_load") public IRubyObject marshal_load(ThreadContext context, IRubyObject arg) { RubyArray load = arg.convertToArray(); real = load.size() > 0 ? load.eltInternal(0) : context.nil; image = load.size() > 1 ? load.eltInternal(1) : context.nil; if (load.hasVariables()) syncVariables((IRubyObject)load); return this; }
/** nucomp_marshal_load * */ @JRubyMethod(name = "marshal_load") public IRubyObject marshal_load(ThreadContext context, IRubyObject arg) { RubyArray load = arg.convertToArray(); real = load.size() > 0 ? load.eltInternal(0) : context.nil; image = load.size() > 1 ? load.eltInternal(1) : context.nil; if (load.hasVariables()) syncVariables((IRubyObject)load); return this; }
@JRubyMethod(name = { "write_array_of_type" }, required = 3) public IRubyObject write_array_of_type(ThreadContext context, IRubyObject typeArg, IRubyObject writer, IRubyObject aryArg) { Type type = context.runtime.getFFI().getTypeResolver().findType(context.runtime, typeArg); DynamicMethod method = getMetaClass().searchMethod(writer.asJavaString()); RubyArray arr = aryArg.convertToArray(); int len = arr.size(); for (int i = 0, off = 0; i < len; i++, off += type.size) { method.call(context, this.slice(context.runtime, off, type.size), this.getMetaClass(), writer.asJavaString(), arr.entry(i)); } return this; }