Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/RubyThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,10 @@ public static RubyArray newLocationArray(Ruby runtime, RubyStackTraceElement[] e
public static RubyArray newLocationArray(Ruby runtime, RubyStackTraceElement[] elements,
final int offset, final int length) {

final RubyClass locationClass = runtime.getLocation();
IRubyObject[] ary = new IRubyObject[length];
for ( int i = 0; i < length; i++ ) {
ary[i] = newLocation(runtime, elements[i + offset]);
ary[i] = new Location(runtime, locationClass, elements[i + offset]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would a new overload of newLocation make sense here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure, literally would be the same as doing new Location() directly.

}

return RubyArray.newArrayNoCopy(runtime, ary);
Expand Down
18 changes: 16 additions & 2 deletions core/src/main/java/org/jruby/javasupport/ext/JavaLang.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.jruby.runtime.JavaInternalBlockBody;
import org.jruby.runtime.Signature;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.backtrace.RubyStackTraceElement;
import org.jruby.runtime.backtrace.TraceType;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.RubyStringBuilder;
Expand Down Expand Up @@ -223,9 +224,8 @@ public static class Throwable {
public static IRubyObject backtrace(final ThreadContext context, final IRubyObject self) {
final Ruby runtime = context.runtime;
java.lang.Throwable throwable = unwrapIfJavaObject(self);
// TODO instead this should get aligned with NativeException !?!
StackTraceElement[] stackTrace = throwable.getStackTrace();
if ( stackTrace == null ) return context.nil; // never actually happens
if ( stackTrace == null ) return context.nil;
final int len = stackTrace.length;
if ( len == 0 ) return RubyArray.newEmptyArray(runtime);
IRubyObject[] backtrace = new IRubyObject[len];
Expand All @@ -235,6 +235,20 @@ public static IRubyObject backtrace(final ThreadContext context, final IRubyObje
return RubyArray.newArrayMayCopy(runtime, backtrace);
}

@JRubyMethod
public static IRubyObject backtrace_locations(final ThreadContext context, final IRubyObject self) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and backtrace are very similar. Maybe share the common parts and just construct the elements differently.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah the preamble, but even if we dry out the loop mapping with a function you still need to return a different array.

java.lang.Throwable throwable = unwrapIfJavaObject(self);
StackTraceElement[] stackTrace = throwable.getStackTrace();
if ( stackTrace == null ) return context.nil;
final int len = stackTrace.length;
if ( len == 0 ) return RubyArray.newEmptyArray(context.runtime);
RubyStackTraceElement[] rubyStackTrace = new RubyStackTraceElement[len];
for ( int i = 0; i < len; i++ ) {
rubyStackTrace[i] = new RubyStackTraceElement(stackTrace[i]);
}
return RubyThread.Location.newLocationArray(context.runtime, rubyStackTrace);
}

@Deprecated(since = "10.0.0.0")
public static IRubyObject set_backtrace(final IRubyObject self, final IRubyObject backtrace) {
return set_backtrace(((RubyBasicObject) self).getCurrentContext(), self, backtrace);
Expand Down
16 changes: 16 additions & 0 deletions test/jruby/test_backtraces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ def test_java_backtrace
assert_equal 1, ruby_trace.length # only once!
end

def test_java_backtrace_locations
org.jruby.test.TestHelper.throwTestHelperException
raise 'did no raise exception'
rescue java.lang.Exception => e
locations = e.backtrace_locations
assert_kind_of Array, locations
assert_kind_of Thread::Backtrace::Location, locations[0]

location = locations[0]
assert_equal 'org/jruby/test/TestHelper.java', location.path

assert_operator location.lineno, :>, 1
assert_equal 'throwTestHelperException', location.label
assert_match /TestHelper\.java:\d+:in .throwTestHelperException/, location.to_s
end

def test_simple_exception_recursive
@offset = __LINE__
def meth(n)
Expand Down
Loading