Skip to content

Commit

Permalink
UnboundMethod does NOT extend Method and does NOT define to_proc.
Browse files Browse the repository at this point in the history
I swear UnboundMethod used to extend Method and remember having
conversations about it, but back as far as 1.8.7 that is not the
case. Perhaps Ruby 1.6.

Fixes 2426.
  • Loading branch information
headius committed Jan 6, 2015
1 parent d7db423 commit 72234f9
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 41 deletions.
146 changes: 146 additions & 0 deletions core/src/main/java/org/jruby/AbstractRubyMethod.java
@@ -0,0 +1,146 @@
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2001 Alan Moore <alan_moore@gmx.net>
* Copyright (C) 2001-2002 Jan Arne Petersen <jpetersen@uni-bonn.de>
* Copyright (C) 2002 Anders Bengtsson <ndrsbngtssn@yahoo.se>
* Copyright (C) 2004 Charles O Nutter <headius@headius.com>
* Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby;

import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.ext.jruby.JRubyLibrary;
import org.jruby.internal.runtime.methods.DynamicMethod;
import org.jruby.internal.runtime.methods.ProcMethod;
import org.jruby.runtime.Block;
import org.jruby.runtime.BlockBody;
import org.jruby.runtime.ClassIndex;
import org.jruby.runtime.CompiledBlockCallback19;
import org.jruby.runtime.CompiledBlockLight19;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.PositionAware;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.marshal.DataType;

/**
* The RubyMethod class represents a RubyMethod object.
*
* You can get such a method by calling the "method" method of an object.
*
* Note: This was renamed from Method.java
*
* @author jpetersen
* @since 0.2.3
*/
public abstract class AbstractRubyMethod extends RubyObject implements DataType {
protected RubyModule implementationModule;
protected String methodName;
protected RubyModule originModule;
protected String originName;
protected DynamicMethod method;

protected AbstractRubyMethod(Ruby runtime, RubyClass rubyClass) {
super(runtime, rubyClass);
}

public DynamicMethod getMethod() {
return method;
}

/** Returns the number of arguments a method accepted.
*
* @return the number of arguments of a method.
*/
@JRubyMethod(name = "arity")
public RubyFixnum arity() {
return getRuntime().newFixnum(method.getArity().getValue());
}

@JRubyMethod(name = "eql?", required = 1, compat = CompatVersion.RUBY1_9)
public IRubyObject op_eql19(ThreadContext context, IRubyObject other) {
return op_equal(context, other);
}

public abstract AbstractRubyMethod rbClone();

@JRubyMethod(name = "name", compat = CompatVersion.RUBY1_8)
public IRubyObject name(ThreadContext context) {
return context.runtime.newString(methodName);
}

@JRubyMethod(name = "name", compat = CompatVersion.RUBY1_9)
public IRubyObject name19(ThreadContext context) {
return context.runtime.newSymbol(methodName);
}

public String getMethodName() {
return methodName;
}

@JRubyMethod(name = "owner")
public IRubyObject owner(ThreadContext context) {
return implementationModule;
}

@JRubyMethod(name = "source_location", compat = CompatVersion.RUBY1_9)
public IRubyObject source_location(ThreadContext context) {
Ruby runtime = context.runtime;

String filename = getFilename();
if (filename != null) {
return runtime.newArray(runtime.newString(filename), runtime.newFixnum(getLine()));
}

return context.runtime.getNil();
}

public String getFilename() {
DynamicMethod realMethod = method.getRealMethod(); // Follow Aliases
if (realMethod instanceof PositionAware) {
PositionAware poser = (PositionAware) realMethod;
return poser.getFile();
}
return null;
}

public int getLine() {
DynamicMethod realMethod = method.getRealMethod(); // Follow Aliases
if (realMethod instanceof PositionAware) {
PositionAware poser = (PositionAware) realMethod;
return poser.getLine() + 1;
}
return -1;
}

@JRubyMethod(name = "parameters", compat = CompatVersion.RUBY1_9)
public IRubyObject parameters(ThreadContext context) {
return JRubyLibrary.MethodExtensions.methodArgs(this);
}
}

14 changes: 3 additions & 11 deletions core/src/main/java/org/jruby/RubyMethod.java
Expand Up @@ -64,12 +64,7 @@
* @since 0.2.3
*/
@JRubyClass(name="Method")
public class RubyMethod extends RubyObject implements DataType {
protected RubyModule implementationModule;
protected String methodName;
protected RubyModule originModule;
protected String originName;
protected DynamicMethod method;
public class RubyMethod extends AbstractRubyMethod {
protected IRubyObject receiver;

protected RubyMethod(Ruby runtime, RubyClass rubyClass) {
Expand All @@ -86,7 +81,8 @@ public static RubyClass createMethodClass(Ruby runtime) {

methodClass.index = ClassIndex.METHOD;
methodClass.setReifiedClass(RubyMethod.class);


methodClass.defineAnnotatedMethods(AbstractRubyMethod.class);
methodClass.defineAnnotatedMethods(RubyMethod.class);

return methodClass;
Expand All @@ -112,10 +108,6 @@ public static RubyMethod newMethod(
return newMethod;
}

public DynamicMethod getMethod() {
return method;
}

/** Call the method.
*
*/
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/org/jruby/RubyModule.java
Expand Up @@ -1486,7 +1486,7 @@ public IRubyObject newMethod(IRubyObject receiver, final String methodName, bool
originModule = ((MetaClass)originModule).getRealClass();
}

RubyMethod newMethod;
AbstractRubyMethod newMethod;
if (bound) {
newMethod = RubyMethod.newMethod(implementationModule, methodName, originModule, methodName, method, receiver);
} else {
Expand Down Expand Up @@ -1547,13 +1547,13 @@ public IRubyObject define_method(ThreadContext context, IRubyObject arg0, IRubyO
body = proc;

newMethod = createProcMethod(name, visibility, proc);
} else if (runtime.getMethod().isInstance(arg1)) {
RubyMethod method = (RubyMethod)arg1;
} else if (arg1 instanceof AbstractRubyMethod) {
AbstractRubyMethod method = (AbstractRubyMethod)arg1;
body = method;

checkValidBindTargetFrom(context, (RubyModule)method.owner(context));

newMethod = method.unbind().getMethod().dup();
newMethod = method.getMethod().dup();
newMethod.setImplementationClass(this);
} else {
throw runtime.newTypeError("wrong argument type " + arg1.getType().getName() + " (expected Proc/Method)");
Expand Down
63 changes: 41 additions & 22 deletions core/src/main/java/org/jruby/RubyUnboundMethod.java
Expand Up @@ -31,6 +31,7 @@
import org.jruby.anno.JRubyMethod;
import org.jruby.anno.JRubyClass;
import org.jruby.internal.runtime.methods.DynamicMethod;
import org.jruby.internal.runtime.methods.ProcMethod;
import org.jruby.runtime.Block;
import org.jruby.runtime.ClassIndex;
import org.jruby.runtime.ObjectAllocator;
Expand All @@ -45,7 +46,7 @@
* @author jpetersen
*/
@JRubyClass(name="UnboundMethod", parent="Method")
public class RubyUnboundMethod extends RubyMethod {
public class RubyUnboundMethod extends AbstractRubyMethod {
protected RubyUnboundMethod(Ruby runtime) {
super(runtime, runtime.getUnboundMethod());
}
Expand All @@ -68,35 +69,34 @@ public static RubyUnboundMethod newUnboundMethod(
}

public static RubyClass defineUnboundMethodClass(Ruby runtime) {
// TODO: NOT_ALLOCATABLE_ALLOCATOR is probably ok here. Confirm. JRUBY-415
RubyClass newClass =
runtime.defineClass("UnboundMethod", runtime.getMethod(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
runtime.defineClass("UnboundMethod", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
runtime.setUnboundMethod(newClass);

newClass.index = ClassIndex.UNBOUNDMETHOD;
newClass.setReifiedClass(RubyUnboundMethod.class);

newClass.defineAnnotatedMethods(AbstractRubyMethod.class);
newClass.defineAnnotatedMethods(RubyUnboundMethod.class);

return newClass;
}
newClass.getSingletonClass().undefineMethod("new");

/**
* @see org.jruby.RubyMethod#call(IRubyObject[])
*/
@JRubyMethod(name = {"call", "[]"}, rest = true)
@Override
public IRubyObject call(ThreadContext context, IRubyObject[] args, Block block) {
throw context.runtime.newTypeError("you cannot call unbound method; bind first");
return newClass;
}

/**
* @see org.jruby.RubyMethod#unbind()
*/
@JRubyMethod
@JRubyMethod(name = "==", required = 1)
@Override
public RubyUnboundMethod unbind() {
return this;
public RubyBoolean op_equal(ThreadContext context, IRubyObject other) {
if (!(other instanceof AbstractRubyMethod)) {
return context.runtime.getFalse();
}
if (method instanceof ProcMethod) {
return context.runtime.newBoolean(((ProcMethod) method).isSame(((AbstractRubyMethod) other).getMethod()));
}
AbstractRubyMethod otherMethod = (AbstractRubyMethod)other;
return context.runtime.newBoolean(
originModule == otherMethod.originModule &&
method.getRealMethod().getSerialNumber() == otherMethod.method.getRealMethod().getSerialNumber());
}

@JRubyMethod
Expand All @@ -110,14 +110,33 @@ public RubyMethod bind(ThreadContext context, IRubyObject aReceiver) {

@JRubyMethod(name = "clone")
@Override
public RubyMethod rbClone() {
public RubyUnboundMethod rbClone() {
return newUnboundMethod(implementationModule, methodName, originModule, originName, method);
}

@JRubyMethod
@JRubyMethod(name = {"inspect", "to_s"})
@Override
public IRubyObject to_proc(ThreadContext context, Block unusedBlock) {
return super.to_proc(context, unusedBlock);
public IRubyObject inspect() {
StringBuilder buf = new StringBuilder("#<");
char delimeter = '#';

buf.append(getMetaClass().getRealClass().getName()).append(": ");

if (implementationModule.isSingleton()) {
buf.append(implementationModule.inspect().toString());
} else {
buf.append(originModule.getName());

if (implementationModule != originModule) {
buf.append('(').append(implementationModule.getName()).append(')');
}
}

buf.append(delimeter).append(methodName).append('>');

RubyString str = getRuntime().newString(buf.toString());
str.setTaint(isTaint());
return str;
}

@JRubyMethod(compat = RUBY1_8)
Expand Down
4 changes: 3 additions & 1 deletion core/src/main/java/org/jruby/ext/jruby/JRubyLibrary.java
Expand Up @@ -31,6 +31,8 @@
package org.jruby.ext.jruby;

import java.util.ArrayList;

import org.jruby.AbstractRubyMethod;
import org.jruby.CompatVersion;
import org.jruby.ast.RestArgNode;
import org.jruby.anno.JRubyMethod;
Expand Down Expand Up @@ -160,7 +162,7 @@ public static class MethodExtensions {
@JRubyMethod(name = "args")
public static IRubyObject methodArgs(IRubyObject recv) {
Ruby runtime = recv.getRuntime();
RubyMethod rubyMethod = (RubyMethod)recv;
AbstractRubyMethod rubyMethod = (AbstractRubyMethod)recv;
RubyArray argsArray = RubyArray.newArray(runtime);
DynamicMethod method = rubyMethod.getMethod().getRealMethod();
RubySymbol req = runtime.newSymbol("req");
Expand Down
7 changes: 4 additions & 3 deletions core/src/main/java/org/jruby/runtime/Helpers.java
Expand Up @@ -818,12 +818,13 @@ public static Block getBlockFromBlockPassBody(Ruby runtime, IRubyObject proc, Bl
return getBlockFromProc(currentBlock, proc);
}

private static IRubyObject coerceProc(IRubyObject proc, Ruby runtime) throws RaiseException {
proc = TypeConverter.convertToType(proc, runtime.getProc(), "to_proc", false);
private static IRubyObject coerceProc(IRubyObject maybeProc, Ruby runtime) throws RaiseException {
IRubyObject proc = TypeConverter.convertToType(maybeProc, runtime.getProc(), "to_proc", false);

if (!(proc instanceof RubyProc)) {
throw runtime.newTypeError("wrong argument type " + proc.getMetaClass().getName() + " (expected Proc)");
throw runtime.newTypeError("wrong argument type " + maybeProc.getMetaClass().getName() + " (expected Proc)");
}

return proc;
}

Expand Down

0 comments on commit 72234f9

Please sign in to comment.