Skip to content

Commit

Permalink
Merge pull request #5948 from headius/update_ffi
Browse files Browse the repository at this point in the history
Update shared FFI code
  • Loading branch information
headius committed Feb 18, 2020
2 parents f8d6233 + f40efb1 commit b267aa1
Show file tree
Hide file tree
Showing 138 changed files with 8,392 additions and 8,848 deletions.
78 changes: 68 additions & 10 deletions core/src/main/java/org/jruby/ext/ffi/AbstractMemory.java
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 Expand Up @@ -594,7 +602,7 @@ public IRubyObject get_int32(ThreadContext context, IRubyObject offset) {
* @param value The value to write.
* @return The value written.
*/
@JRubyMethod(name = { "write_uint" }, required = 1)
@JRubyMethod(name = { "write_uint32", "write_uint" }, required = 1)
public IRubyObject write_uint(ThreadContext context, IRubyObject value) {
getMemoryIO().putInt(0, (int) Util.uint32Value(value));

Expand Down Expand Up @@ -628,12 +636,32 @@ public IRubyObject put_uint32(ThreadContext context, IRubyObject offset, IRubyOb
return this;
}

/**
* Reads a 8 bit unsigned integer value from the memory address.
*
* @return The value read from the address.
*/
@JRubyMethod(name = { "read_uint8" })
public IRubyObject read_uint8(ThreadContext context) {
return Util.newUnsigned8(context.runtime, getMemoryIO().getByte(0));
}

/**
* Reads a 16 bit unsigned integer value from the memory address.
*
* @return The value read from the address.
*/
@JRubyMethod(name = { "read_uint16" })
public IRubyObject read_uint16(ThreadContext context) {
return Util.newUnsigned16(context.runtime, getMemoryIO().getShort(0));
}

/**
* Reads a 32 bit unsigned integer value from the memory address.
*
* @return The value read from the address.
*/
@JRubyMethod(name = { "read_uint" })
@JRubyMethod(name = { "read_uint", "read_uint32" })
public IRubyObject read_uint(ThreadContext context) {
return Util.newUnsigned32(context.runtime, getMemoryIO().getInt(0));
}
Expand Down Expand Up @@ -665,7 +693,7 @@ public IRubyObject get_uint32(ThreadContext context, IRubyObject offset) {
* @param value The value to write.
* @return The value written.
*/
@JRubyMethod(name = { "write_long_long" }, required = 1)
@JRubyMethod(name = { "write_int64", "write_long_long" }, required = 1)
public IRubyObject write_long_long(ThreadContext context, IRubyObject value) {
getMemoryIO().putLong(0, Util.int64Value(value));

Expand Down Expand Up @@ -704,7 +732,7 @@ public IRubyObject put_int64(ThreadContext context, IRubyObject offset, IRubyObj
*
* @return The value read from the address.
*/
@JRubyMethod(name = { "read_long_long" })
@JRubyMethod(name = { "read_int64", "read_long_long" })
public IRubyObject read_long_long(ThreadContext context) {
return Util.newSigned64(context.runtime, getMemoryIO().getLong(0));
}
Expand Down Expand Up @@ -736,7 +764,7 @@ public IRubyObject get_int64(ThreadContext context, IRubyObject offset) {
* @param value The value to write.
* @return The value written.
*/
@JRubyMethod(name = { "write_ulong_long" }, required = 1)
@JRubyMethod(name = { "write_uint64", "write_ulong_long" }, required = 1)
public IRubyObject write_ulong_long(ThreadContext context, IRubyObject value) {
getMemoryIO().putLong(0, Util.uint64Value(value));

Expand Down Expand Up @@ -775,7 +803,7 @@ public IRubyObject put_uint64(ThreadContext context, IRubyObject offset, IRubyOb
*
* @return The value read from the address.
*/
@JRubyMethod(name = { "read_ulong_long" })
@JRubyMethod(name = { "read_uint64", "read_ulong_long" })
public IRubyObject read_ulong_long(ThreadContext context) {
return Util.newUnsigned64(context.runtime, getMemoryIO().getLong(0));
}
Expand Down Expand Up @@ -1880,13 +1908,14 @@ public IRubyObject get_bytes(ThreadContext context, IRubyObject offArg, IRubyObj
}

private IRubyObject putBytes(ThreadContext context, long off, ByteList bl, int idx, int len) {
if (idx < 0 || idx > bl.length()) {
throw context.runtime.newRangeError("invalid string index");
if (idx < 0) {
throw context.runtime.newRangeError("index can not be less than zero");
}

if (len < 0 || len > (bl.length() - idx)) {
throw context.runtime.newRangeError("invalid length");
if ((idx + len) > bl.length()) {
throw context.runtime.newRangeError("index+length is greater than size of string");
}

getMemoryIO().put(off, bl.getUnsafeBytes(), bl.begin() + idx, len);

return this;
Expand Down Expand Up @@ -2051,6 +2080,35 @@ public final IRubyObject slice(ThreadContext context, IRubyObject offset, IRubyO
return slice(context.getRuntime(), RubyNumeric.num2int(offset), RubyNumeric.num2int(size));
}

@JRubyMethod(name = "get")
public final IRubyObject put(ThreadContext context, IRubyObject typeName, IRubyObject offset) {
Ruby runtime = context.runtime;

Type type = runtime.getFFI().getTypeResolver().findType(runtime, typeName);
MemoryOp op = MemoryOp.getMemoryOp(type);

if(op != null) {
return op.get(context, getMemoryIO(), RubyNumeric.num2long(offset));
}

throw runtime.newArgumentError("undefined type " + typeName);
}

@JRubyMethod(name = "put")
public final IRubyObject get(ThreadContext context, IRubyObject typeName, IRubyObject offset, IRubyObject value) {
Ruby runtime = context.runtime;

Type type = runtime.getFFI().getTypeResolver().findType(runtime, typeName);
MemoryOp op = MemoryOp.getMemoryOp(type);
if(op != null) {
op.put(context, getMemoryIO(), RubyNumeric.num2long(offset), value);

return context.nil;
}

throw runtime.newArgumentError("undefined type " + typeName);
}

abstract public AbstractMemory order(Ruby runtime, ByteOrder order);
abstract protected AbstractMemory slice(Ruby runtime, long offset);
abstract protected AbstractMemory slice(Ruby runtime, long offset, long size);
Expand Down

0 comments on commit b267aa1

Please sign in to comment.