Skip to content

Commit

Permalink
Add missing Field#get and Field#put
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Feb 16, 2020
1 parent be3c5c1 commit f40efb1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
8 changes: 8 additions & 0 deletions core/src/main/java/org/jruby/ext/ffi/AbstractMemory.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ protected AbstractMemory(Ruby runtime, RubyClass klass, MemoryIO io, long size,
this.typeSize = typeSize;
}

static AbstractMemory cast(ThreadContext context, IRubyObject ptr) {
if (!(ptr instanceof AbstractMemory)) {
throw context.runtime.newTypeError(ptr, context.runtime.getFFI().memoryClass);
}

return (AbstractMemory) ptr;
}

@Override
protected MemoryIO allocateMemoryIO() {
throw getRuntime().newRuntimeError("allocateMemoryIO should not be called");
Expand Down
45 changes: 32 additions & 13 deletions core/src/main/java/org/jruby/ext/ffi/StructLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyClass;
import org.jruby.RubyFixnum;
import org.jruby.RubyHash;
import org.jruby.RubyInteger;
import org.jruby.RubyModule;
Expand All @@ -54,7 +53,6 @@
import org.jruby.runtime.Block;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.callsite.CachingCallSite;
import org.jruby.runtime.callsite.FunctionalCachingCallSite;
Expand Down Expand Up @@ -343,19 +341,11 @@ public IRubyObject union_bang(ThreadContext context) {
}

final IRubyObject getValue(ThreadContext context, IRubyObject name, Storage cache, IRubyObject ptr) {
if (!(ptr instanceof AbstractMemory)) {
throw context.runtime.newTypeError(ptr, context.runtime.getFFI().memoryClass);
}

return getMember(context.runtime, name).get(context, cache, (AbstractMemory) ptr);
return getMember(context.runtime, name).get(context, cache, AbstractMemory.cast(context, ptr));
}

final void putValue(ThreadContext context, IRubyObject name, Storage cache, IRubyObject ptr, IRubyObject value) {
if (!(ptr instanceof AbstractMemory)) {
throw context.runtime.newTypeError(ptr, context.runtime.getFFI().memoryClass);
}

getMember(context.runtime, name).put(context, cache, (AbstractMemory) ptr, value);
getMember(context.runtime, name).put(context, cache, AbstractMemory.cast(context, ptr), value);
}

@Override
Expand Down Expand Up @@ -624,6 +614,9 @@ public static class Field extends RubyObject {
/** The offset within the memory area of this member */
private int offset;

/** The memory operation for this field type */
private MemoryOp memoryOp;


Field(Ruby runtime, RubyClass klass) {
this(runtime, klass, DefaultFieldIO.INSTANCE);
Expand All @@ -641,12 +634,15 @@ public static class Field extends RubyObject {
this.type = type;
this.offset = offset;
this.io = io;
this.memoryOp = MemoryOp.getMemoryOp(type);
}

void init(IRubyObject name, IRubyObject type, IRubyObject offset) {
this.name = name;
this.type = checkType(type);
Type realType = checkType(type);
this.type = realType;
this.offset = RubyNumeric.num2int(offset);
this.memoryOp = MemoryOp.getMemoryOp(realType);
}

void init(IRubyObject name, IRubyObject type, IRubyObject offset, FieldIO io) {
Expand Down Expand Up @@ -761,6 +757,29 @@ public final IRubyObject type(ThreadContext context) {
public final IRubyObject name(ThreadContext context) {
return name;
}

@JRubyMethod
public final IRubyObject get(ThreadContext context, IRubyObject pointer) {
MemoryOp memoryOp = this.memoryOp;
if (memoryOp == null) {
throw context.runtime.newArgumentError("get not supported for " + type.nativeType.name());
}

return memoryOp.get(context, AbstractMemory.cast(context, pointer), offset);
}

@JRubyMethod
public final IRubyObject put(ThreadContext context, IRubyObject pointer, IRubyObject value) {
MemoryOp memoryOp = this.memoryOp;

if (memoryOp == null) {
throw context.runtime.newArgumentError("put not supported for " + type.nativeType.name());
}

memoryOp.put(context, AbstractMemory.cast(context, pointer), offset, value);

return this;
}
}

private static final class NumberFieldAllocator implements ObjectAllocator {
Expand Down

0 comments on commit f40efb1

Please sign in to comment.