Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update shared FFI code #5948

Merged
merged 29 commits into from Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
740be10
Update configs from FFI and remove generator code.
headius Oct 28, 2019
ea20d2d
Update shared sources from FFI gem.
headius Oct 28, 2019
84b5c39
Update FFI specs from FFI gem.
headius Oct 28, 2019
7ab6328
Add JRuby-specific setup.
headius Oct 28, 2019
03d2063
Eliminate double-def of FFI::Platform consts and methods.
headius Oct 28, 2019
aab7c6b
Align VariadicInvoker with FFI gem.
headius Oct 28, 2019
8209ab6
Only add i386 and x86_64 arch if that's the platform.
headius Oct 28, 2019
221b11d
Just use the Ruby definition of this type.
headius Oct 28, 2019
de3c22a
javadoc fixes.
headius Oct 28, 2019
051e506
Partial impl of StructLayout#__union!
headius Oct 28, 2019
fa1e977
Merge branch 'master' into update_ffi
headius Jan 7, 2020
6cc8235
Enums, Enum, and DataConverter are in Ruby now
headius Jan 8, 2020
4d84cca
Align error handling with C ffi
headius Jan 8, 2020
69a1731
Fix all MemoryPointer specs
headius Jan 8, 2020
fb0740a
Fix all pointer specs.
headius Jan 8, 2020
c647799
Order type checks the same as C FFI
headius Jan 8, 2020
21a8a8c
Do not cache the struct layout in the metaclass
headius Jan 8, 2020
a5eb643
Remove whitespace.
headius Jan 8, 2020
3d0cb54
Update specs and libs from ffi gem.
headius Jan 10, 2020
95c73a6
Revert "Do not cache the struct layout in the metaclass"
headius Jan 10, 2020
24f7fc4
Merge branch 'master' into update_ffi
headius Feb 14, 2020
5996596
IS_DRAGONFLYBSD is define in Ruby elsewhere now
headius Feb 14, 2020
2ab63ec
remove struct by reference
ahorek Feb 15, 2020
bcec463
throw ArgumentError instead of SecurityError
ahorek Feb 15, 2020
16ea5d2
Add FFI::LastError.winapi_error
ahorek Feb 15, 2020
eeb3f6f
[RbConfig] RUBY_SO_NAME and RUBY_BASE_NAME
ahorek Feb 15, 2020
843067e
x32 build shouldnt contain a prefix
ahorek Feb 15, 2020
be3c5c1
Update specs from ffi gem
headius Feb 16, 2020
f40efb1
Add missing Field#get and Field#put
headius Feb 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 60 additions & 10 deletions core/src/main/java/org/jruby/ext/ffi/AbstractMemory.java
Expand Up @@ -594,7 +594,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 +628,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 +685,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 +724,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 +756,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 +795,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 +1900,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 +2072,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