Skip to content

Commit 695e094

Browse files
committed
[GR-32001] Always-inline more methods needing the caller frame
PullRequest: truffleruby/2746
2 parents 2d36691 + 0851118 commit 695e094

34 files changed

+368
-347
lines changed

spec/truffle/always_inlined_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,33 @@
2020
}.should raise_error(TypeError) { |e| e.backtrace_locations[0].label.should == 'public_send' }
2121
end
2222

23+
it "for #block_given?" do
24+
-> {
25+
block_given?(:wrong)
26+
}.should raise_error(ArgumentError) { |e| e.backtrace_locations[0].label.should == 'block_given?' }
27+
-> {
28+
iterator?(:wrong) # rubocop:disable Lint/DeprecatedClassMethods
29+
}.should raise_error(ArgumentError) { |e| e.backtrace_locations[0].label.should == 'iterator?' }
30+
end
31+
32+
it "for #binding" do
33+
-> {
34+
binding(:wrong)
35+
}.should raise_error(ArgumentError) { |e| e.backtrace_locations[0].label.should == 'binding' }
36+
end
37+
38+
it "for #eval" do
39+
-> {
40+
eval(Object.new)
41+
}.should raise_error(TypeError) { |e| e.backtrace_locations[0].label.should == 'eval' }
42+
end
43+
44+
it "for #local_variables" do
45+
-> {
46+
local_variables(:wrong)
47+
}.should raise_error(ArgumentError) { |e| e.backtrace_locations[0].label.should == 'local_variables' }
48+
end
49+
2350
guard -> { RUBY_ENGINE != "ruby" } do
2451
it "for #send" do
2552
-> {

spec/truffle/caller_data_spec.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
require_relative '../ruby/spec_helper'
1212

1313
module TruffleCallerSpecFixtures
14-
1514
def self.last_line_set(last_line)
1615
Primitive.io_last_line_set(Primitive.caller_special_variables, last_line)
1716
last_line
@@ -22,17 +21,12 @@ def self.last_match_set(match)
2221
match
2322
end
2423

25-
def self.caller_binding
26-
Primitive.caller_binding
27-
end
28-
2924
def self.caller_binding_and_variables(last_line, last_match)
3025
b = Primitive.caller_binding
3126
Primitive.io_last_line_set(Primitive.caller_special_variables, last_line)
3227
Primitive.regexp_last_match_set(Primitive.caller_special_variables, last_match)
3328
b
3429
end
35-
3630
end
3731

3832
describe "A caller" do

spec/truffle/capi/foreign_caller_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
it "directly from C code raises a RuntimeError" do
1919
-> {
2020
@s.call_binding
21-
}.should raise_error(RuntimeError, 'Cannot call Ruby method which needs caller data directly in a foreign language')
21+
}.should raise_error(RuntimeError, 'Kernel#binding needs the caller frame but it was not passed (cannot be called directly from a foreign language)')
2222
end
2323

2424
it "using rb_funcall() yields the Binding of rb_funcall()" do

src/main/java/org/truffleruby/core/MainNodes.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public abstract static class MainUsingNode extends UsingNode {
6262
protected Object mainUsing(Frame callerFrame, Object self, Object[] args, Object block, RootCallTarget target,
6363
@CachedContext(RubyLanguage.class) RubyContext context,
6464
@Cached BranchProfile errorProfile) {
65+
needCallerFrame(callerFrame, target);
6566
final Object refinementModule = args[0];
6667
final InternalMethod callerMethod = RubyArguments.getMethod(callerFrame);
6768
if (!isCalledFromTopLevel(callerMethod)) {

src/main/java/org/truffleruby/core/array/ArrayNodes.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.truffleruby.core.cast.ToAryNodeGen;
4646
import org.truffleruby.core.cast.ToIntNode;
4747
import org.truffleruby.core.cast.ToLongNode;
48+
import org.truffleruby.core.cast.ToStrNode;
4849
import org.truffleruby.core.cast.ToStrNodeGen;
4950
import org.truffleruby.core.format.BytesResult;
5051
import org.truffleruby.core.format.FormatExceptionTranslator;
@@ -1486,7 +1487,7 @@ public void accept(RubyArray array, RubyProc block, Object element, int index) {
14861487
}
14871488

14881489
@NodeChild(value = "array", type = RubyNode.class)
1489-
@NodeChild(value = "format", type = RubyNode.class)
1490+
@NodeChild(value = "format", type = RubyBaseNodeWithExecute.class)
14901491
@CoreMethod(names = "pack", required = 1)
14911492
@ImportStatic({ StringCachingGuards.class, StringOperations.class })
14921493
@ReportPolymorphism
@@ -1500,7 +1501,7 @@ public abstract static class PackNode extends CoreMethodNode {
15001501
private final ConditionProfile resizeProfile = ConditionProfile.create();
15011502

15021503
@CreateCast("format")
1503-
protected RubyNode coerceFormat(RubyNode format) {
1504+
protected ToStrNode coerceFormat(RubyBaseNodeWithExecute format) {
15041505
return ToStrNodeGen.create(format);
15051506
}
15061507

src/main/java/org/truffleruby/core/basicobject/BasicObjectNodes.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
import org.truffleruby.language.control.RaiseException;
5353
import org.truffleruby.language.dispatch.DispatchNode;
5454
import org.truffleruby.language.dispatch.RubyCallNode;
55-
import org.truffleruby.language.eval.CreateEvalSourceNode;
55+
import org.truffleruby.language.loader.EvalLoader;
5656
import org.truffleruby.language.library.RubyStringLibrary;
5757
import org.truffleruby.language.loader.CodeLoader;
5858
import org.truffleruby.language.methods.DeclarationContext;
@@ -312,8 +312,6 @@ public Object inlineExecute(Frame callerFrame, Object self, Object[] args, Objec
312312
@CoreMethod(names = "instance_eval", needsBlock = true, optional = 3, lowerFixnum = 3)
313313
public abstract static class InstanceEvalNode extends CoreMethodArrayArgumentsNode {
314314

315-
@Child private CreateEvalSourceNode createEvalSourceNode = new CreateEvalSourceNode();
316-
317315
@Specialization(guards = { "strings.isRubyString(string)", "stringsFileName.isRubyString(fileName)" })
318316
protected Object instanceEval(
319317
VirtualFrame frame, Object receiver, Object string, Object fileName, int line, Nil block,
@@ -392,8 +390,8 @@ private Object instanceEvalHelper(MaterializedFrame callerFrame, Object receiver
392390
Rope fileNameRope, int line, IndirectCallNode callNode) {
393391
final String fileNameString = RopeOperations.decodeRope(fileNameRope);
394392

395-
final RubySource source = createEvalSourceNode
396-
.createEvalSource(stringRope, "instance_eval", fileNameString, line);
393+
final RubySource source = EvalLoader
394+
.createEvalSource(getContext(), stringRope, "instance_eval", fileNameString, line, this);
397395

398396
final RootCallTarget callTarget = getContext().getCodeLoader().parse(
399397
source,

src/main/java/org/truffleruby/core/cast/ToStrNode.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010

1111
package org.truffleruby.core.cast;
1212

13+
import com.oracle.truffle.api.dsl.CachedContext;
14+
import com.oracle.truffle.api.dsl.GenerateUncached;
1315
import com.oracle.truffle.api.library.CachedLibrary;
16+
import org.truffleruby.RubyContext;
17+
import org.truffleruby.RubyLanguage;
1418
import org.truffleruby.core.string.RubyString;
1519
import org.truffleruby.core.string.ImmutableRubyString;
16-
import org.truffleruby.language.RubyContextSourceNode;
17-
import org.truffleruby.language.RubyNode;
20+
import org.truffleruby.language.RubyBaseNodeWithExecute;
1821
import org.truffleruby.language.control.RaiseException;
1922
import org.truffleruby.language.dispatch.DispatchNode;
2023

@@ -24,15 +27,16 @@
2427
import com.oracle.truffle.api.profiles.BranchProfile;
2528
import org.truffleruby.language.library.RubyStringLibrary;
2629

27-
@NodeChild(value = "child", type = RubyNode.class)
28-
public abstract class ToStrNode extends RubyContextSourceNode {
29-
30-
public abstract Object executeToStr(Object object);
30+
@GenerateUncached
31+
@NodeChild(value = "child", type = RubyBaseNodeWithExecute.class)
32+
public abstract class ToStrNode extends RubyBaseNodeWithExecute {
3133

3234
public static ToStrNode create() {
3335
return ToStrNodeGen.create(null);
3436
}
3537

38+
public abstract Object execute(Object object);
39+
3640
@Specialization
3741
protected RubyString coerceRubyString(RubyString string) {
3842
return string;
@@ -45,6 +49,7 @@ protected ImmutableRubyString coerceImmutableRubyString(ImmutableRubyString stri
4549

4650
@Specialization(guards = "isNotRubyString(object)")
4751
protected Object coerceObject(Object object,
52+
@CachedContext(RubyLanguage.class) RubyContext context,
4853
@Cached BranchProfile errorProfile,
4954
@Cached DispatchNode toStrNode,
5055
@CachedLibrary(limit = "2") RubyStringLibrary libString) {
@@ -53,10 +58,10 @@ protected Object coerceObject(Object object,
5358
coerced = toStrNode.call(object, "to_str");
5459
} catch (RaiseException e) {
5560
errorProfile.enter();
56-
if (e.getException().getLogicalClass() == coreLibrary().noMethodErrorClass) {
61+
if (e.getException().getLogicalClass() == context.getCoreLibrary().noMethodErrorClass) {
5762
throw new RaiseException(
58-
getContext(),
59-
coreExceptions().typeErrorNoImplicitConversion(object, "String", this));
63+
context,
64+
context.getCoreExceptions().typeErrorNoImplicitConversion(object, "String", this));
6065
} else {
6166
throw e;
6267
}
@@ -67,8 +72,8 @@ protected Object coerceObject(Object object,
6772
} else {
6873
errorProfile.enter();
6974
throw new RaiseException(
70-
getContext(),
71-
coreExceptions().typeErrorBadCoercion(object, "String", "to_str", coerced, this));
75+
context,
76+
context.getCoreExceptions().typeErrorBadCoercion(object, "String", "to_str", coerced, this));
7277
}
7378
}
7479

src/main/java/org/truffleruby/core/encoding/EncodingConverterNodes.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.truffleruby.builtins.PrimitiveArrayArgumentsNode;
3636
import org.truffleruby.core.array.ArrayUtils;
3737
import org.truffleruby.core.array.RubyArray;
38+
import org.truffleruby.core.cast.ToStrNode;
3839
import org.truffleruby.core.cast.ToStrNodeGen;
3940
import org.truffleruby.core.hash.RubyHash;
4041
import org.truffleruby.core.klass.RubyClass;
@@ -51,6 +52,7 @@
5152
import org.truffleruby.core.symbol.RubySymbol;
5253
import org.truffleruby.language.Nil;
5354
import org.truffleruby.language.NotProvided;
55+
import org.truffleruby.language.RubyBaseNodeWithExecute;
5456
import org.truffleruby.language.RubyNode;
5557
import org.truffleruby.language.Visibility;
5658
import org.truffleruby.language.control.RaiseException;
@@ -506,11 +508,11 @@ protected RubyString getReplacement(RubyEncodingConverter encodingConverter) {
506508

507509
@CoreMethod(names = "replacement=", required = 1)
508510
@NodeChild(value = "encodingConverter", type = RubyNode.class)
509-
@NodeChild(value = "replacement", type = RubyNode.class)
511+
@NodeChild(value = "replacement", type = RubyBaseNodeWithExecute.class)
510512
public abstract static class EncodingConverterSetReplacementNode extends CoreMethodNode {
511513

512514
@CreateCast("replacement")
513-
protected RubyNode coerceReplacementToString(RubyNode replacement) {
515+
protected ToStrNode coerceReplacementToString(RubyBaseNodeWithExecute replacement) {
514516
return ToStrNodeGen.create(replacement);
515517
}
516518

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public RubyException argumentErrorEmptyVarargs(Node currentNode) {
228228
public RubyException argumentErrorWrongArgumentType(Object object, String expectedType, Node currentNode) {
229229
String badClassName = LogicalClassNode.getUncached().execute(object).fields.getName();
230230
return argumentError(
231-
StringUtils.format("wrong argument type %s (expected %s)", badClassName, expectedType),
231+
StringUtils.format("wrong argument type %s (should be %s)", badClassName, expectedType),
232232
currentNode);
233233
}
234234

src/main/java/org/truffleruby/core/format/convert/ToStringObjectNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected Object toStringString(Object string,
4343
protected Object toString(VirtualFrame frame, Object object,
4444
@Cached ConditionProfile notStringProfile,
4545
@Cached ToStrNode toStrNode) {
46-
final Object value = toStrNode.executeToStr(object);
46+
final Object value = toStrNode.execute(object);
4747

4848
if (notStringProfile.profile(RubyGuards.isNotRubyString(value))) {
4949
throw new NoImplicitConversionException(object, "String");

0 commit comments

Comments
 (0)