private final long getOffset(int index) { if (index < 0 || index >= arrayType.length()) { throw getRuntime().newIndexError("index " + index + " out of bounds"); } return (long) (index * arrayType.getComponentType().getNativeSize()); }
public ArrayFieldIO(Type.Array arrayType) { this.arrayType = arrayType; this.op = MemoryOp.getMemoryOp(arrayType.getComponentType()); if (op == null) { throw arrayType .getRuntime() .newNotImplementedError("unsupported array field type: " + arrayType); } }
/** Needed for Enumerable implementation */ @JRubyMethod(name = "each") public IRubyObject each(ThreadContext context, Block block) { if (!block.isGiven()) { throw context.getRuntime().newLocalJumpErrorNoBlock(); } for (int i = 0; i < arrayType.length(); ++i) { block.yield(context, get(context.getRuntime(), i)); } return this; }
@JRubyMethod(name = {"to_a", "to_ary"}) public IRubyObject get(ThreadContext context) { Ruby runtime = context.getRuntime(); IRubyObject[] elems = new IRubyObject[arrayType.length()]; for (int i = 0; i < elems.length; ++i) { elems[i] = get(runtime, i); } return RubyArray.newArrayNoCopy(runtime, elems); }
ArrayProxy( Ruby runtime, RubyClass klass, IRubyObject ptr, long offset, Type.Array type, MemoryOp aio) { super(runtime, klass); this.ptr = ((AbstractMemory) ptr).slice(runtime, offset, type.getNativeSize()); this.arrayType = type; this.aio = aio; }
public void put( ThreadContext context, StructLayout.Storage cache, Member m, IRubyObject ptr, IRubyObject value) { if (isCharArray() && value instanceof RubyString) { ByteList bl = value.convertToString().getByteList(); m.getMemoryIO(ptr) .putZeroTerminatedByteArray( m.offset, bl.getUnsafeBytes(), bl.begin(), Math.min(bl.length(), arrayType.length() - 1)); } else if (false) { RubyArray ary = value.convertToArray(); int count = ary.size(); if (count > arrayType.length()) { throw context.getRuntime().newIndexError("array too big"); } AbstractMemory memory = (AbstractMemory) ptr; // Clear any elements that will not be filled by the array if (count < arrayType.length()) { memory .getMemoryIO() .setMemory( m.offset + (count * arrayType.getComponentType().getNativeSize()), (arrayType.length() - count) * arrayType.getComponentType().getNativeSize(), (byte) 0); } for (int i = 0; i < count; ++i) { op.put( context.getRuntime(), memory, m.offset + (i * arrayType.getComponentType().getNativeSize()), ary.entry(i)); } } else { throw context.getRuntime().newNotImplementedError("cannot set array field"); } }
private final boolean isCharArray() { return arrayType.getComponentType().nativeType == NativeType.CHAR || arrayType.getComponentType().nativeType == NativeType.UCHAR; }
@JRubyMethod(name = {"size"}) public IRubyObject size(ThreadContext context) { return context.getRuntime().newFixnum(arrayType.getNativeSize()); }