FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[ji] support java.lang.Throwable#backtrace_locations by kares · Pull Request #9461 · jruby/jruby · GitHub

/ jruby Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (2) .rb  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/RubyThread.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

}

return RubyArray.newArrayNoCopy(runtime, ary);
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL