Skip to content
Browse files

Add a 'call' method to the Function/Invoker's singleton class, instea…

…d of having a separate RubyModule for it.
  • Loading branch information...
1 parent 2ca84d8 commit 36f1d9ba7592e2263a387a2924090cabf56dc66d Wayne Meissner committed Jul 15, 2009
View
12 src/org/jruby/ext/ffi/AbstractInvoker.java
@@ -87,13 +87,15 @@ protected AbstractInvoker(Ruby runtime, RubyClass klass, int arity, DirectMemory
* @param methodName The ruby name to attach the function as.
*/
@JRubyMethod(name="attach")
- public IRubyObject attach(ThreadContext context, IRubyObject module, IRubyObject methodName) {
- if (!(module instanceof RubyModule)) {
- throw context.getRuntime().newTypeError(module, context.getRuntime().getModule());
+ public IRubyObject attach(ThreadContext context, IRubyObject obj, IRubyObject methodName) {
+
+ DynamicMethod m = createDynamicMethod(obj.getSingletonClass());
+ obj.getSingletonClass().addMethod(methodName.asJavaString(), m);
+ if (obj instanceof RubyModule) {
+ ((RubyModule) obj).addMethod(methodName.asJavaString(), m);
}
- DynamicMethod m = createDynamicMethod((RubyModule) module);
refmap.put(m, this);
- ((RubyModule) module).addModuleFunction(methodName.asJavaString(), m);
+
return this;
}
protected abstract DynamicMethod createDynamicMethod(RubyModule module);
View
18 src/org/jruby/ext/ffi/jffi/Function.java
@@ -29,9 +29,7 @@
private final Type[] parameterTypes;
private final CallingConvention convention;
private final IRubyObject enums;
- private final RubyModule callModule;
- private final DynamicMethod callMethod;
-
+
public static RubyClass createFunctionClass(Ruby runtime, RubyModule module) {
RubyClass result = module.defineClassUnder("Function",
module.fastGetClass("Pointer"),
@@ -64,8 +62,8 @@ public static RubyClass createFunctionClass(Ruby runtime, RubyModule module) {
this.returnType = returnType;
this.convention = convention;
this.enums = enums;
- this.callModule = RubyModule.newModule(runtime);
- this.callModule.addModuleFunction("call", callMethod = createDynamicMethod(callModule));
+ // Wire up Function#call(*args) to use the super-fast native invokers
+ getSingletonClass().addMethod("call", createDynamicMethod(getSingletonClass()));
}
@JRubyMethod(name = { "new" }, meta = true, required = 2, optional = 1)
@@ -125,16 +123,6 @@ public static IRubyObject newInstance(ThreadContext context, IRubyObject recv, I
returnType, parameterTypes, callConvention, enums);
}
- /**
- * Invokes the native function with the supplied ruby arguments.
- * @param rubyArgs The ruby arguments to pass to the native function.
- * @return The return value from the native function, as a ruby object.
- */
- @JRubyMethod(name= { "invoke", "call", "call0", "call1", "call2", "call3" }, rest = true)
- public IRubyObject invoke(ThreadContext context, IRubyObject[] args) {
- return callMethod.call(context, callModule, callModule.getSingletonClass(), "call", args, Block.NULL_BLOCK);
- }
-
@JRubyMethod(name = "free")
public final IRubyObject free(ThreadContext context) {
if (getMemoryIO() instanceof AllocatedNativeMemoryIO) {
View
16 src/org/jruby/ext/ffi/jffi/JFFIInvoker.java
@@ -25,8 +25,6 @@
private final Type[] parameterTypes;
private final CallingConvention convention;
private final IRubyObject enums;
- private final RubyModule callModule;
- private final DynamicMethod callMethod;
public static RubyClass createInvokerClass(Ruby runtime, RubyModule module) {
RubyClass result = module.defineClassUnder("Invoker",
@@ -69,8 +67,8 @@ public static RubyClass createInvokerClass(Ruby runtime, RubyModule module) {
this.convention = "stdcall".equals(convention)
? CallingConvention.STDCALL : CallingConvention.DEFAULT;
this.enums = enums;
- this.callModule = RubyModule.newModule(runtime);
- this.callModule.addModuleFunction("call", callMethod = createDynamicMethod(callModule));
+ // Wire up Function#call(*args) to use the super-fast native invokers
+ getSingletonClass().addMethod("call", createDynamicMethod(getSingletonClass()));
}
@JRubyMethod(name = { "new" }, meta = true, required = 4)
@@ -122,16 +120,6 @@ public static IRubyObject newInstance(ThreadContext context, IRubyObject recv, I
(Type) returnType, parameterTypes, convention, enums);
}
- /**
- * Invokes the native function with the supplied ruby arguments.
- * @param rubyArgs The ruby arguments to pass to the native function.
- * @return The return value from the native function, as a ruby object.
- */
- @JRubyMethod(name= { "invoke", "call", "call0", "call1", "call2", "call3" }, rest = true)
- public IRubyObject invoke(ThreadContext context, IRubyObject[] args) {
- return callMethod.call(context, callModule, callModule.getSingletonClass(), "call", args, Block.NULL_BLOCK);
- }
-
@Override
public DynamicMethod createDynamicMethod(RubyModule module) {
return (enums == null || enums.isNil())

0 comments on commit 36f1d9b

Please sign in to comment.
Something went wrong with that request. Please try again.