protected int size() { return this.storage.size(); }
private int normalizeArrayIndex(IRubyObject index) { int arrIndex = RubyNumeric.num2int(index); int arrSize = this.storage.size(); if (arrIndex < 0 && arrSize > 0) { arrIndex = arrSize + arrIndex; } return arrIndex; }
@JRubyMethod(name = {"length", "size"}) public IRubyObject length(ThreadContext context) { return context.runtime.newFixnum(this.storage.size()); }
@JRubyMethod public IRubyObject dup(ThreadContext context) { RubyRepeatedField dup = new RubyRepeatedField(context.runtime, metaClass, fieldType, typeClass); for (int i = 0; i < this.storage.size(); i++) { dup.push(context, this.storage.eltInternal(i)); } return dup; }
@JRubyMethod(name = "finalize_to_pool") public IRubyObject finalizeToPool(ThreadContext context, IRubyObject rbPool) { RubyDescriptorPool pool = (RubyDescriptorPool) rbPool; for (int i = 0; i < this.pendingList.size(); i++) { IRubyObject defRb = this.pendingList.entry(i); if (defRb instanceof RubyDescriptor) { pool.addToSymtab(context, (RubyDescriptor) defRb); } else { pool.addToSymtab(context, (RubyEnumDescriptor) defRb); } } this.pendingList = context.runtime.newArray(); return context.runtime.getNil(); }
@JRubyMethod(name = "[]=") public IRubyObject indexSet(ThreadContext context, IRubyObject index, IRubyObject value) { int arrIndex = normalizeArrayIndex(index); value = Utils.checkType(context, fieldType, value, (RubyModule) typeClass); IRubyObject defaultValue = defaultValue(context); for (int i = this.storage.size(); i < arrIndex; i++) { this.storage.set(i, defaultValue); } this.storage.set(arrIndex, value); return context.runtime.getNil(); }
@JRubyMethod(required=1, optional=1, name = {"at", "[]"}) public IRubyObject index(ThreadContext context, IRubyObject[] args) { if (args.length == 1){ IRubyObject arg = args[0]; if (Utils.isRubyNum(arg)) { /* standard case */ int arrIndex = normalizeArrayIndex(arg); if (arrIndex < 0 || arrIndex >= this.storage.size()) { return context.runtime.getNil(); } return this.storage.eltInternal(arrIndex); } else if (arg instanceof RubyRange) { RubyRange range = ((RubyRange) arg); int beg = RubyNumeric.num2int(range.first(context)); int to = RubyNumeric.num2int(range.last(context)); int len = to - beg + 1; return this.storage.subseq(beg, len); } } /* assume 2 arguments */ int beg = RubyNumeric.num2int(args[0]); int len = RubyNumeric.num2int(args[1]); if (beg < 0) { beg += this.storage.size(); } if (beg >= this.storage.size()) { return context.runtime.getNil(); } return this.storage.subseq(beg, len); }
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; }
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; }
@Override final boolean hasNext() { return index < array.size(); } }
public static void logCaller(RubyArray trace) { StringBuilder buffer = new StringBuilder(64 + trace.size() * 48); buffer.append("Caller backtrace generated:\n"); for (int i = 0; i < trace.size(); i++) { // elements are already rendered as its an Array<RubyString> buffer.append(" ").append(trace.eltInternal(i)).append('\n'); } LOG.info(buffer.toString()); }
private Object[] convertArguments(final RubyArray arguments) { final int argsSize = arguments.size(); final Object[] args = new Object[argsSize]; final Class<?>[] parameterTypes = getParameterTypes(); for ( int i = 0; i < argsSize; i++ ) { args[i] = arguments.eltInternal(i).toJava( parameterTypes[i] ); } return args; }
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; }
public IRubyObject call(ThreadContext ctx, IRubyObject[] largs, Block blk) { result[0].append(packEnumValues(ctx, largs)); if (result[0].size() == size) { block.yield(ctx, result[0]); result[0] = runtime.newArray(size); } return ctx.nil; } });
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 boolean isInstance(IRubyObject object) { if (!super.isInstance(object)) { return false; } RubyArray array = (RubyArray) object; boolean ret = array.size() == 2 && object.getRuntime().getArray().isInstance((IRubyObject) array.get(0)) && (null == array.get(1) || LIST_ITEM_CLASS.isInstance((IRubyObject) array.get(1))); return ret; }
@JRubyMethod(name = "collect!", alias = "map!") public IRubyObject collect_bang(final ThreadContext context, Block block) { if ( ! block.isGiven() ) { return enumeratorizeWithSize(context, this, "collect!", enumSize()); } final RubyArray elems = to_a(context); clearImpl(); for ( int i=0; i<elems.size(); i++ ) { addImpl(context.runtime, block.yield(context, elems.eltInternal(i))); } 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; }