Skip to content

Commit b57aee2

Browse files
committed
Add spec for AssertionError thrown in a Ruby Thread
(cherry picked from commit 8769a29)
1 parent ac3b87a commit b57aee2

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
slow:Java exceptions AssertionError in another Thread is rethrown on the main Ruby Thread

spec/truffle/java_exception_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,18 @@
6161
}
6262
end
6363

64+
it "AssertionError in another Thread is rethrown on the main Ruby Thread" do
65+
code = <<-RUBY
66+
Thread.new do
67+
Truffle::Debug.throw_assertion_error('custom_assertion_error_message')
68+
end
69+
sleep
70+
RUBY
71+
72+
out = ruby_exe(code, args: "2>&1")
73+
$?.exitstatus.should == 1
74+
out.should.include?('terminated with internal error')
75+
out.should.include?('Caused by:')
76+
out.should.include?('custom_assertion_error_message (AssertionError)')
77+
end
6478
end

src/main/java/org/truffleruby/debug/TruffleDebugNodes.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,9 @@ static void warning(String message) {
401401
public abstract static class ThrowJavaExceptionNode extends CoreMethodArrayArgumentsNode {
402402

403403
@TruffleBoundary
404-
@Specialization
405-
protected Object throwJavaException(Object message) {
406-
callingMethod(message.toString());
404+
@Specialization(guards = "isRubyString(message)")
405+
protected Object throwJavaException(DynamicObject message) {
406+
callingMethod(StringOperations.getString(message));
407407
return nil;
408408
}
409409

@@ -423,21 +423,32 @@ private static void throwingMethod(String message) {
423423
public abstract static class ThrowJavaExceptionWithCauseNode extends CoreMethodArrayArgumentsNode {
424424

425425
@TruffleBoundary
426-
@Specialization
427-
protected DynamicObject throwJavaExceptionWithCause(Object message) {
426+
@Specialization(guards = "isRubyString(message)")
427+
protected Object throwJavaExceptionWithCause(DynamicObject message) {
428428
throw new RuntimeException(
429-
message.toString(),
429+
StringOperations.getString(message),
430430
new RuntimeException("cause 1", new RuntimeException("cause 2")));
431431
}
432432

433433
}
434434

435+
@CoreMethod(names = "throw_assertion_error", onSingleton = true, required = 1)
436+
public abstract static class ThrowAssertionErrorNode extends CoreMethodArrayArgumentsNode {
437+
438+
@TruffleBoundary
439+
@Specialization(guards = "isRubyString(message)")
440+
protected Object throwAssertionError(DynamicObject message) {
441+
throw new AssertionError(StringOperations.getString(message));
442+
}
443+
444+
}
445+
435446
@CoreMethod(names = "assert", onSingleton = true, required = 1)
436447
public abstract static class AssertNode extends CoreMethodArrayArgumentsNode {
437448

438449
@TruffleBoundary
439450
@Specialization
440-
protected Object throwJavaException(boolean condition) {
451+
protected Object doAssert(boolean condition) {
441452
assert condition;
442453
return nil;
443454
}
@@ -805,7 +816,7 @@ protected String toDisplayString(boolean allowSideEffects) {
805816
@TruffleBoundary
806817
@Specialization(guards = "isRubyString(string)")
807818
protected Object foreignString(DynamicObject string) {
808-
return new ForeignString(string.toString());
819+
return new ForeignString(StringOperations.getString(string));
809820
}
810821

811822
}

0 commit comments

Comments
 (0)