Skip to content

Commit c46449f

Browse files
committed
Make a copy of the Backtrace and RaiseException when copying a Ruby exception
* To maintain a 1-1-1 relationship.
1 parent 6c9994e commit c46449f

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

spec/tags/core/exception/exception_tags.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/main/java/org/truffleruby/core/exception/ExceptionNodes.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ public Object initializeCopySelfIsSameAsFrom(DynamicObject self, DynamicObject f
7777

7878
@Specialization(guards = { "self != from", "isRubyException(from)" })
7979
public Object initializeCopy(DynamicObject self, DynamicObject from) {
80-
Layouts.EXCEPTION.setBacktrace(self, Layouts.EXCEPTION.getBacktrace(from));
80+
Backtrace backtrace = Layouts.EXCEPTION.getBacktrace(from);
81+
if (backtrace != null) {
82+
Layouts.EXCEPTION.setBacktrace(self, backtrace.copy(getContext(), self));
83+
} else {
84+
Layouts.EXCEPTION.setBacktrace(self, backtrace);
85+
}
8186
Layouts.EXCEPTION.setFormatter(self, Layouts.EXCEPTION.getFormatter(from));
8287
Layouts.EXCEPTION.setMessage(self, Layouts.EXCEPTION.getMessage(from));
8388

src/main/java/org/truffleruby/language/backtrace/Backtrace.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ public Backtrace(Node location, SourceSection sourceLocation, int omitted, Throw
4444
this.javaThrowable = javaThrowable;
4545
}
4646

47+
public Backtrace copy(RubyContext context, DynamicObject exception) {
48+
Backtrace copy = new Backtrace(location, sourceLocation, omitted, javaThrowable);
49+
// A Backtrace is 1-1-1 with a RaiseException and a Ruby exception
50+
RaiseException newRaiseException = new RaiseException(context, exception, this.raiseException.isInternalError());
51+
// Copy the TruffleStackTrace
52+
TruffleStackTraceElement.fillIn(this.raiseException);
53+
assert this.raiseException.getCause() != null;
54+
newRaiseException.initCause(this.raiseException.getCause());
55+
// Another way would be to copy the activations (copy.activations = getActivations()), but
56+
// then the TruffleStrackTrace would be inconsistent.
57+
copy.setRaiseException(newRaiseException);
58+
return copy;
59+
}
60+
4761
public Node getLocation() {
4862
return location;
4963
}

0 commit comments

Comments
 (0)