Skip to content

Commit 1c7911f

Browse files
committed
[GR-19220] Truffle::Graal.bailout (#1808).
PullRequest: truffleruby/1141
2 parents c76d124 + 86f5de0 commit 1c7911f

File tree

7 files changed

+81
-2
lines changed

7 files changed

+81
-2
lines changed

spec/truffle/graal/assert_constant_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
-> { Truffle::Graal.send(:assert_constant, 14 + 2) }.should raise_error(RuntimeError)
1515
end
1616

17-
unless TruffleRuby.jit?
17+
guard -> { !TruffleRuby.jit? } do
1818
it "returns the value of the argument" do
1919
Truffle::Graal.assert_constant(14 + 2).should == 16
2020
end

spec/truffle/graal/assert_not_compiled_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
-> { Truffle::Graal.send(:assert_not_compiled) }.should raise_error(RuntimeError)
1515
end
1616

17-
unless TruffleRuby.jit?
17+
guard -> { !TruffleRuby.jit? } do
1818
it "returns nil" do
1919
Truffle::Graal.assert_not_compiled.should be_nil
2020
end

spec/truffle/graal/bailout_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. This
2+
# code is released under a tri EPL/GPL/LGPL license. You can use it,
3+
# redistribute it and/or modify it under the terms of the:
4+
#
5+
# Eclipse Public License version 2.0, or
6+
# GNU General Public License version 2, or
7+
# GNU Lesser General Public License version 2.1.
8+
9+
require_relative '../../ruby/spec_helper'
10+
11+
describe "Truffle::Graal.bailout" do
12+
13+
it "raises a RuntimeError when called dynamically" do
14+
-> { Truffle::Graal.send(:bailout, "message") }.should raise_error(RuntimeError)
15+
end
16+
17+
guard -> { !TruffleRuby.jit? } do
18+
it "returns nil" do
19+
Truffle::Graal.bailout("message").should be_nil
20+
end
21+
end
22+
23+
end

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ public DynamicObject runtimeErrorCompiled(Node currentNode) {
251251
return runtimeError("Truffle::Graal.assert_not_compiled can only be called lexically", currentNode);
252252
}
253253

254+
public DynamicObject runtimeErrorBailout(Node currentNode) {
255+
return runtimeError("Truffle::Graal.bailout can only be called lexically", currentNode);
256+
}
257+
254258
public DynamicObject runtimeErrorCoverageNotEnabled(Node currentNode) {
255259
return runtimeError("coverage measurement is not enabled", currentNode);
256260
}

src/main/java/org/truffleruby/extra/TruffleGraalNodes.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public abstract class TruffleGraalNodes {
3737
@CoreMethod(names = "assert_constant", onSingleton = true, required = 1)
3838
public abstract static class AssertConstantNode extends CoreMethodArrayArgumentsNode {
3939

40+
@TruffleBoundary
4041
@Specialization
4142
protected DynamicObject assertConstant(Object value) {
4243
throw new RaiseException(getContext(), coreExceptions().runtimeErrorNotConstant(this));
@@ -47,13 +48,25 @@ protected DynamicObject assertConstant(Object value) {
4748
@CoreMethod(names = "assert_not_compiled", onSingleton = true)
4849
public abstract static class AssertNotCompiledNode extends CoreMethodArrayArgumentsNode {
4950

51+
@TruffleBoundary
5052
@Specialization
5153
protected DynamicObject assertNotCompiled() {
5254
throw new RaiseException(getContext(), coreExceptions().runtimeErrorCompiled(this));
5355
}
5456

5557
}
5658

59+
@CoreMethod(names = "bailout", onSingleton = true, required = 1)
60+
public abstract static class BailoutNode extends CoreMethodArrayArgumentsNode {
61+
62+
@TruffleBoundary
63+
@Specialization(guards = "isRubyString(message)")
64+
protected DynamicObject bailout(DynamicObject message) {
65+
throw new RaiseException(getContext(), coreExceptions().runtimeErrorBailout(this));
66+
}
67+
68+
}
69+
5770
@CoreMethod(names = "always_split", onSingleton = true, required = 1, argumentNames = "method_or_proc")
5871
public abstract static class AlwaysSplitNode extends CoreMethodArrayArgumentsNode {
5972

src/main/java/org/truffleruby/parser/BodyTranslator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@
254254
import org.truffleruby.parser.parser.ParserSupport;
255255
import org.truffleruby.parser.scope.StaticScope;
256256
import org.truffleruby.platform.AssertConstantNodeGen;
257+
import org.truffleruby.platform.BailoutNode;
257258
import org.truffleruby.platform.AssertNotCompiledNodeGen;
258259

259260
import com.oracle.truffle.api.RootCallTarget;
@@ -549,6 +550,10 @@ public RubyNode visitCallNode(CallParseNode node) {
549550
final RubyNode ret = AssertNotCompiledNodeGen.create();
550551
ret.unsafeSetSourceSection(sourceSection);
551552
return addNewlineIfNeeded(node, ret);
553+
} else if (methodName.equals("bailout")) {
554+
final RubyNode ret = BailoutNode.create(((ArrayParseNode) node.getArgsNode()).get(0).accept(this));
555+
ret.unsafeSetSourceSection(sourceSection);
556+
return addNewlineIfNeeded(node, ret);
552557
}
553558
} else if (receiver instanceof VCallParseNode // undefined.equal?(obj)
554559
&& ((VCallParseNode) receiver).getName().equals("undefined") && inCore() &&
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. This
3+
* code is released under a tri EPL/GPL/LGPL license. You can use it,
4+
* redistribute it and/or modify it under the terms of the:
5+
*
6+
* Eclipse Public License version 2.0, or
7+
* GNU General Public License version 2, or
8+
* GNU Lesser General Public License version 2.1.
9+
*/
10+
package org.truffleruby.platform;
11+
12+
import com.oracle.truffle.api.CompilerDirectives;
13+
import com.oracle.truffle.api.dsl.Cached;
14+
import com.oracle.truffle.api.dsl.NodeChild;
15+
import com.oracle.truffle.api.dsl.Specialization;
16+
import com.oracle.truffle.api.object.DynamicObject;
17+
import org.truffleruby.interop.ToJavaStringNode;
18+
import org.truffleruby.language.RubyNode;
19+
20+
@NodeChild
21+
public abstract class BailoutNode extends RubyNode {
22+
23+
@Specialization(guards = "isRubyString(message)")
24+
protected DynamicObject bailout(DynamicObject message,
25+
@Cached ToJavaStringNode toJavaStringNode) {
26+
CompilerDirectives.bailout(toJavaStringNode.executeToJavaString(message));
27+
return nil();
28+
}
29+
30+
public static BailoutNode create(RubyNode messageNode) {
31+
return BailoutNodeGen.create(messageNode);
32+
}
33+
34+
}

0 commit comments

Comments
 (0)