Browse files

Instead of calling respondsTo() and then callMethod(), just lookup th…

…e method and call via the DynamicMethod instance.
  • Loading branch information...
1 parent 56c7716 commit 1ff797e8e17a4147d26dddf64eadd033af2726a1 Wayne Meissner committed Jan 2, 2013
Showing with 19 additions and 8 deletions.
  1. +11 −4 src/org/jruby/ext/ffi/AbstractMemory.java
  2. +8 −4 src/org/jruby/ext/ffi/StructLayout.java
View
15 src/org/jruby/ext/ffi/AbstractMemory.java
@@ -41,6 +41,7 @@
import org.jruby.RubySymbol;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
+import org.jruby.internal.runtime.methods.DynamicMethod;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
@@ -82,11 +83,12 @@ protected static final int calculateTypeSize(ThreadContext context, IRubyObject
return ((Type) sizeArg).getNativeSize();
} else {
+ DynamicMethod sizeMethod;
if (sizeArg instanceof RubyClass && Struct.isStruct(context.runtime, (RubyClass) sizeArg)) {
return Struct.getStructSize(context.runtime, sizeArg);
- } else if (sizeArg.respondsTo("size")) {
- return (int) RubyFixnum.num2long(sizeArg.callMethod(context, "size"));
+ } else if (!(sizeMethod = sizeArg.getMetaClass().searchMethod("size")).isUndefined()) {
+ return (int) RubyFixnum.num2long(sizeMethod.call(context, sizeArg, sizeArg.getMetaClass(), "size"));
} else {
throw context.runtime.newArgumentError("Invalid size argument");
@@ -1901,12 +1903,17 @@ public IRubyObject get_pointer(ThreadContext context, IRubyObject offset) {
}
private void putPointer(ThreadContext context, long offset, IRubyObject value) {
+ DynamicMethod conversionMethod;
+
if (value instanceof Pointer) {
putPointer(context, offset, (Pointer) value);
+
} else if (value.isNil()) {
getMemoryIO().putAddress(offset, 0L);
- } else if (value.respondsTo("to_ptr")) {
- putPointer(context, offset, value.callMethod(context, "to_ptr"));
+
+ } else if (!(conversionMethod = value.getMetaClass().searchMethod("to_ptr")).isUndefined()) {
+ putPointer(context, offset, conversionMethod.call(context, value, value.getMetaClass(), "to_ptr"));
+
} else {
throw context.runtime.newTypeError(value, context.runtime.getFFI().pointerClass);
}
View
12 src/org/jruby/ext/ffi/StructLayout.java
@@ -49,6 +49,7 @@
import org.jruby.RubySymbol;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
+import org.jruby.internal.runtime.methods.DynamicMethod;
import org.jruby.runtime.Block;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
@@ -1171,6 +1172,7 @@ public final boolean isValueReferenceNeeded() {
public static final FieldIO INSTANCE = new PointerFieldIO();
public void put(ThreadContext context, StructLayout.Storage cache, Member m, AbstractMemory ptr, IRubyObject value) {
+ DynamicMethod conversionMethod;
if (value instanceof Pointer) {
ptr.getMemoryIO().putMemoryIO(m.offset, ((Pointer) value).getMemoryIO());
} else if (value instanceof Struct) {
@@ -1183,15 +1185,17 @@ public void put(ThreadContext context, StructLayout.Storage cache, Member m, Abs
} else if (value instanceof RubyInteger) {
ptr.getMemoryIO().putAddress(m.offset, Util.int64Value(ptr));
- } else if (value.respondsTo("to_ptr")) {
- IRubyObject addr = value.callMethod(context, "to_ptr");
+
+ } else if (value.isNil()) {
+ ptr.getMemoryIO().putAddress(m.offset, 0L);
+
+ } else if (!(conversionMethod = value.getMetaClass().searchMethod("to_ptr")).isUndefined()) {
+ IRubyObject addr = conversionMethod.call(context, value, value.getMetaClass(), "to_ptr");
if (addr instanceof Pointer) {
ptr.getMemoryIO().putMemoryIO(m.offset, ((Pointer) addr).getMemoryIO());
} else {
throw context.runtime.newArgumentError("Invalid pointer value");
}
- } else if (value.isNil()) {
- ptr.getMemoryIO().putAddress(m.offset, 0L);
} else {
throw context.runtime.newArgumentError("Invalid pointer value");
}

0 comments on commit 1ff797e

Please sign in to comment.