Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: d684a31f7d4c^
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 415bef51c054
Choose a head ref
  • 2 commits
  • 31 files changed
  • 1 contributor

Commits on Feb 27, 2018

  1. Initial pass for parallel exception hierarchy. #4781

    This work is the start of producing a Java-domain Throwable
    hierarchy that matches the Ruby hierarchy, so that Ruby exceptions
    can be caught using their actual Throwable type rather than using
    RaiseException and checking the exceptions type manually.
    
    A few changes of note:
    
    * RubyException has most logic moved up to new
      AbstractRubyException.
    * All RubyException now hold a reference to their RaiseException,
      created lazily once as needed.
      * Throw a Ruby exception using the form
        `throw ex.getRaiseException()`.
      * Provide construction logic specific to a given exception by
        overriding createRaiseException.
    * The nativeException parameter is eliminated from all exception
      construction and initialization paths. It was unused.
    headius committed Feb 27, 2018

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    nomadium Miguel Landaeta
    Copy the full SHA
    d684a31 View commit details
  2. Begin propagating exception hierarchy down stack.

    This forms the meat of wiring up the new parallel exception
    hierarchy. All public constructors of RaiseException are now
    deprecated and unused. RaiseException.from() forms take their
    place and know how to properly construct a Ruby exception and
    wrap it with an appropriate RaiseException subclass.
    
    Three native exceptions are added, mapping to the Ruby exception
    type of the same name: Exception, StandardError, and
    SignalException.
    
    One location in the code that caught RaiseException is now able
    to catch StandardError (RubyNumeric coerce logic). There are
    likely others.
    
    See #4781.
    headius committed Feb 27, 2018

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    nomadium Miguel Landaeta
    Copy the full SHA
    415bef5 View commit details
Showing with 705 additions and 480 deletions.
  1. +363 −0 core/src/main/java/org/jruby/AbstractRubyException.java
  2. +13 −3 core/src/main/java/org/jruby/NativeException.java
  3. +14 −14 core/src/main/java/org/jruby/Ruby.java
  4. +1 −1 core/src/main/java/org/jruby/RubyEnumerator.java
  5. +23 −302 core/src/main/java/org/jruby/RubyException.java
  6. +1 −6 core/src/main/java/org/jruby/RubyInterrupt.java
  7. +7 −7 core/src/main/java/org/jruby/RubyKernel.java
  8. +9 −11 core/src/main/java/org/jruby/RubyNumeric.java
  9. +8 −6 core/src/main/java/org/jruby/RubySignalException.java
  10. +48 −0 core/src/main/java/org/jruby/RubyStandardError.java
  11. +2 −2 core/src/main/java/org/jruby/RubyThread.java
  12. +43 −0 core/src/main/java/org/jruby/exceptions/Exception.java
  13. +47 −70 core/src/main/java/org/jruby/exceptions/RaiseException.java
  14. +43 −0 core/src/main/java/org/jruby/exceptions/SignalException.java
  15. +43 −0 core/src/main/java/org/jruby/exceptions/StandardError.java
  16. +1 −1 core/src/main/java/org/jruby/ext/JRubyPOSIXHelper.java
  17. +1 −1 core/src/main/java/org/jruby/ext/ffi/InvalidMemoryIO.java
  18. +4 −4 core/src/main/java/org/jruby/ext/ffi/MemoryPointer.java
  19. +2 −2 core/src/main/java/org/jruby/ext/fiber/FiberQueue.java
  20. +6 −6 core/src/main/java/org/jruby/ext/net/protocol/NetProtocolBufferedIO.java
  21. +2 −4 core/src/main/java/org/jruby/ext/socket/SocketUtils.java
  22. +1 −4 core/src/main/java/org/jruby/ext/strscan/RubyStringScanner.java
  23. +1 −1 core/src/main/java/org/jruby/ext/thread/Queue.java
  24. +2 −7 core/src/main/java/org/jruby/ext/zlib/RubyZlib.java
  25. +1 −1 core/src/main/java/org/jruby/internal/runtime/GlobalVariables.java
  26. +1 −2 core/src/main/java/org/jruby/management/Runtime.java
  27. +3 −3 core/src/main/java/org/jruby/runtime/ThreadContext.java
  28. +11 −14 core/src/main/java/org/jruby/runtime/backtrace/TraceType.java
  29. +1 −1 core/src/main/java/org/jruby/util/io/OpenFile.java
  30. +1 −5 core/src/main/java/org/jruby/util/io/Sockaddr.java
  31. +2 −2 core/src/test/java/org/jruby/exceptions/TestRaiseException.java
363 changes: 363 additions & 0 deletions core/src/main/java/org/jruby/AbstractRubyException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,363 @@
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 2.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 2.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-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
* Copyright (C) 2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
* Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
* Copyright (C) 2002-2006 Thomas E Enebo <enebo@acm.org>
* Copyright (C) 2004 Joey Gibson <joey@joeygibson.com>
* Copyright (C) 2004-2005 Charles O Nutter <headius@headius.com>
* Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
* Copyright (C) 2005 David Corbin <dcorbin@users.sf.net>
* Copyright (C) 2006 Michael Studman <codehaus@michaelstudman.com>
*
* 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.JRubyMethod;
import org.jruby.exceptions.JumpException.FlowControlException;
import org.jruby.exceptions.RaiseException;
import org.jruby.java.proxies.ConcreteJavaProxy;
import org.jruby.runtime.Block;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.backtrace.BacktraceData;
import org.jruby.runtime.backtrace.RubyStackTraceElement;
import org.jruby.runtime.backtrace.TraceType;
import org.jruby.runtime.builtin.IRubyObject;

import java.io.PrintStream;

import static org.jruby.runtime.Visibility.PRIVATE;

/**
*
* @author jpetersen
*/
public abstract class AbstractRubyException extends RubyObject {

protected AbstractRubyException(Ruby runtime, RubyClass rubyClass) {
this(runtime, rubyClass, null);
}

public AbstractRubyException(Ruby runtime, RubyClass rubyClass, String message) {
super(runtime, rubyClass);

this.setMessage(message == null ? runtime.getNil() : runtime.newString(message));
this.cause = RubyBasicObject.UNDEF;
}

@JRubyMethod(optional = 2, visibility = PRIVATE)
public IRubyObject initialize(IRubyObject[] args, Block block) {
if ( args.length == 1 ) setMessage(args[0]);
// cause filled in at RubyKernel#raise ... Exception.new does not fill-in cause!
return this;
}

@JRubyMethod
public IRubyObject backtrace() {
return getBacktrace();
}

@JRubyMethod(required = 1)
public IRubyObject set_backtrace(IRubyObject obj) {
setBacktrace(obj);
return backtrace();
}

private void setBacktrace(IRubyObject obj) {
if (obj.isNil()) {
backtrace = null;
} else if (isArrayOfStrings(obj)) {
backtrace = obj;
} else if (obj instanceof RubyString) {
backtrace = RubyArray.newArray(getRuntime(), obj);
} else {
throw getRuntime().newTypeError("backtrace must be Array of String");
}
}

@JRubyMethod(omit = true)
public IRubyObject backtrace_locations(ThreadContext context) {
Ruby runtime = context.runtime;
RubyStackTraceElement[] elements = backtraceData.getBacktrace(runtime);

return RubyThread.Location.newLocationArray(runtime, elements);
}

@JRubyMethod(name = "exception", optional = 1, rest = true, meta = true)
public static IRubyObject exception(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
return ((RubyClass) recv).newInstance(context, args, block);
}

@JRubyMethod(optional = 1)
public AbstractRubyException exception(IRubyObject[] args) {
switch (args.length) {
case 0 :
return this;
case 1 :
if (args[0] == this) return this;
AbstractRubyException ret = (AbstractRubyException) rbClone();
ret.initialize(args, Block.NULL_BLOCK); // This looks wrong, but it's the way MRI does it.
return ret;
default :
throw getRuntime().newArgumentError("Wrong argument count");
}
}

@JRubyMethod(name = "to_s")
public IRubyObject to_s(ThreadContext context) {
final IRubyObject msg = getMessage();
if ( ! msg.isNil() ) return msg.asString();
return context.runtime.newString(getMetaClass().getRealClass().getName());
}

@Deprecated
public IRubyObject to_s19(ThreadContext context) { return to_s(context); }

@JRubyMethod(name = "message")
public IRubyObject message(ThreadContext context) {
return callMethod(context, "to_s");
}

/** inspects an object and return a kind of debug information
*
*@return A RubyString containing the debug information.
*/
@JRubyMethod(name = "inspect")
public RubyString inspect(ThreadContext context) {
// rb_class_name skips intermediate classes (JRUBY-6786)
String rubyClass = getMetaClass().getRealClass().getName();
RubyString exception = RubyString.objAsString(context, this);

if (exception.isEmpty()) return context.runtime.newString(rubyClass);

return RubyString.newString(context.runtime,
new StringBuilder(2 + rubyClass.length() + 2 + exception.size() + 1).
append("#<").append(rubyClass).append(": ").append(exception.getByteList()).append('>')
);
}

@Override
@JRubyMethod(name = "==")
public RubyBoolean op_equal(ThreadContext context, IRubyObject other) {
if (this == other) return context.runtime.getTrue();

boolean equal = context.runtime.getException().isInstance(other) &&
getMetaClass().getRealClass() == other.getMetaClass().getRealClass() &&
callMethod(context, "message").equals(other.callMethod(context, "message")) &&
callMethod(context, "backtrace").equals(other.callMethod(context, "backtrace"));
return context.runtime.newBoolean(equal);
}

@JRubyMethod(name = "===", meta = true)
public static IRubyObject op_eqq(ThreadContext context, IRubyObject recv, IRubyObject other) {
Ruby runtime = context.runtime;
// special case non-FlowControlException Java exceptions so they'll be caught by rescue Exception
if (other instanceof ConcreteJavaProxy &&
(recv == runtime.getException() || recv == runtime.getStandardError())) {

Object object = ((ConcreteJavaProxy)other).getObject();
if (object instanceof Throwable && !(object instanceof FlowControlException)) {
if (recv == runtime.getException() || object instanceof Exception) {
return context.runtime.getTrue();
}
}
}
// fall back on default logic
return ((RubyClass)recv).op_eqq(context, other);
}

@JRubyMethod(name = "cause")
public IRubyObject cause(ThreadContext context) {
assert cause != null;
return cause == RubyBasicObject.UNDEF ? context.nil : cause;
}

@Override
public Object toJava(Class target) {
if (target.isAssignableFrom(RaiseException.class)) {
return target.cast(raiseException);
}
return super.toJava(target);
}

protected abstract RaiseException constructRaiseException(String message);

public RaiseException getRaiseException() {
if (raiseException == null) {
return raiseException = constructRaiseException(getMessageAsJavaString());
}
return raiseException;
}

public void setCause(IRubyObject cause) {
this.cause = cause;
}

// NOTE: can not have IRubyObject as NativeException has getCause() returning Throwable
public Object getCause() {
return cause == RubyBasicObject.UNDEF ? null : cause;
}

public void setBacktraceData(BacktraceData backtraceData) {
this.backtraceData = backtraceData;
}

public BacktraceData getBacktraceData() {
return backtraceData;
}

public RubyStackTraceElement[] getBacktraceElements() {
if (backtraceData == null) {
return RubyStackTraceElement.EMPTY_ARRAY;
}
return backtraceData.getBacktrace(getRuntime());
}

public void prepareBacktrace(ThreadContext context) {
// if it's null, build a backtrace
if (backtraceData == null) {
backtraceData = context.runtime.getInstanceConfig().getTraceType().getBacktrace(context);
}
}

/**
* Prepare an "integrated" backtrace that includes the normal Ruby trace plus non-filtered Java frames. Used by
* Java integration to show the Java frames for a JI-called method.
*
* @param context
* @param javaTrace
*/
public void prepareIntegratedBacktrace(ThreadContext context, StackTraceElement[] javaTrace) {
// if it's null, build a backtrace
if (backtraceData == null) {
backtraceData = context.runtime.getInstanceConfig().getTraceType().getIntegratedBacktrace(context, javaTrace);
}
}

public void forceBacktrace(IRubyObject backtrace) {
backtraceData = (backtrace != null && backtrace.isNil()) ? null : BacktraceData.EMPTY;
setBacktrace(backtrace);
}

public IRubyObject getBacktrace() {
if (backtrace == null) {
initBacktrace();
}
return backtrace;
}

public void initBacktrace() {
Ruby runtime = getRuntime();
if (backtraceData == null) {
backtrace = runtime.getNil();
} else {
backtrace = TraceType.generateMRIBacktrace(runtime, backtraceData.getBacktrace(runtime));
}
}

@Override
public void copySpecialInstanceVariables(IRubyObject clone) {
AbstractRubyException exception = (AbstractRubyException)clone;
exception.backtraceData = backtraceData;
exception.backtrace = backtrace;
exception.message = message;
}

/**
* Print the Ruby exception's backtrace to the given PrintStream.
*
* @param errorStream the PrintStream to which backtrace should be printed
*/
public void printBacktrace(PrintStream errorStream) {
printBacktrace(errorStream, 0);
}

/**
* Print the Ruby exception's backtrace to the given PrintStream. This
* version accepts a number of lines to skip and is primarily used
* internally for exception printing where the first line is treated specially.
*
* @param errorStream the PrintStream to which backtrace should be printed
*/
public void printBacktrace(PrintStream errorStream, int skip) {
IRubyObject trace = callMethod(getRuntime().getCurrentContext(), "backtrace");
if ( trace.isNil() ) return;
if ( trace instanceof RubyArray ) {
IRubyObject[] elements = ((RubyArray) trace).toJavaArrayMaybeUnsafe();
for (int i = skip; i < elements.length; i++) {
IRubyObject stackTraceLine = elements[i];
if (stackTraceLine instanceof RubyString) {
errorStream.println("\tfrom " + stackTraceLine);
}
else {
errorStream.println("\t" + stackTraceLine);
}
}
}
}

private boolean isArrayOfStrings(IRubyObject backtrace) {
if (!(backtrace instanceof RubyArray)) return false;

final RubyArray rTrace = ((RubyArray) backtrace);

for (int i = 0 ; i < rTrace.getLength() ; i++) {
if (!(rTrace.eltInternal(i) instanceof RubyString)) return false;
}

return true;
}

/**
* @return error message if provided or nil
*/
public IRubyObject getMessage() {
return message == null ? getRuntime().getNil() : message;
}

/**
* Set the message for this NameError.
* @param message the message
*/
public void setMessage(IRubyObject message) {
this.message = message;
}

public String getMessageAsJavaString() {
final IRubyObject msg = getMessage();
return msg.isNil() ? null : msg.toString();
}

private BacktraceData backtraceData;
private IRubyObject backtrace;
IRubyObject message;
IRubyObject cause;
private RaiseException raiseException;

public static final int TRACE_HEAD = 8;
public static final int TRACE_TAIL = 4;
public static final int TRACE_MAX = TRACE_HEAD + TRACE_TAIL + 6;
}
16 changes: 13 additions & 3 deletions core/src/main/java/org/jruby/NativeException.java
Original file line number Diff line number Diff line change
@@ -43,11 +43,20 @@ public class NativeException extends RubyException {
public static final String CLASS_NAME = "NativeException";

public NativeException(Ruby runtime, RubyClass rubyClass, Throwable cause) {
super(runtime, rubyClass);
this(runtime, rubyClass, cause, buildMessage(cause));
}

private NativeException(Ruby runtime, RubyClass rubyClass, Throwable cause, String message) {
super(runtime, rubyClass, message);
this.cause = cause;
this.messageAsJavaString = cause.getClass().getName() + ": " + searchStackMessage(cause);
String s = buildMessage(cause);
this.messageAsJavaString = message;
}


private static String buildMessage(Throwable cause) {
return cause.getClass().getName() + ": " + searchStackMessage(cause);
}

private NativeException(Ruby runtime, RubyClass rubyClass) {
super(runtime, rubyClass, null);
this.cause = new Throwable();
@@ -64,6 +73,7 @@ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {

public static RubyClass createClass(Ruby runtime, RubyClass baseClass) {
RubyClass exceptionClass = runtime.defineClass(CLASS_NAME, baseClass, NATIVE_EXCEPTION_ALLOCATOR);
runtime.getObject().deprecateConstant(runtime, CLASS_NAME);

exceptionClass.defineAnnotatedMethods(NativeException.class);

Loading