Skip to content

Commit 5c164d6

Browse files
committed
AST-inline Array#at
* So it is AST-inlined too and not just Array#[].
1 parent aa0d8b5 commit 5c164d6

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/main/java/org/truffleruby/core/inlined/CoreMethods.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public class CoreMethods {
8888
public final InternalMethod MODULE_CASE_EQUAL;
8989
public final InternalMethod STRING_EQUAL;
9090
public final InternalMethod SYMBOL_TO_PROC;
91+
public final InternalMethod ARRAY_AT;
9192
public final InternalMethod ARRAY_INDEX_GET;
9293
public final InternalMethod ARRAY_INDEX_SET;
9394

@@ -153,6 +154,7 @@ public CoreMethods(RubyContext context) {
153154
MODULE_CASE_EQUAL = getMethod(moduleClass, "===");
154155
STRING_EQUAL = getMethod(stringClass, "==");
155156
SYMBOL_TO_PROC = getMethod(symbolClass, "to_proc");
157+
ARRAY_AT = getMethod(arrayClass, "at");
156158
ARRAY_INDEX_GET = getMethod(arrayClass, "[]");
157159
ARRAY_INDEX_SET = getMethod(arrayClass, "[]=");
158160
}
@@ -245,6 +247,8 @@ public RubyNode createCallNode(RubyCallNodeParameters callParameters, Translator
245247
return InlinedGreaterOrEqualNodeGen.create(context, callParameters, self, args[0]);
246248
case "[]":
247249
return InlinedIndexGetNodeGen.create(context, callParameters, self, args[0]);
250+
case "at":
251+
return InlinedAtNodeGen.create(context, callParameters, self, args[0]);
248252
case "is_a?":
249253
return InlinedIsANodeGen.create(context, callParameters, self, args[0]);
250254
case "kind_of?":
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2020 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.core.inlined;
11+
12+
import org.truffleruby.RubyContext;
13+
import org.truffleruby.core.array.ArrayIndexNodes;
14+
import org.truffleruby.core.array.RubyArray;
15+
import org.truffleruby.language.dispatch.RubyCallNodeParameters;
16+
import org.truffleruby.language.methods.LookupMethodOnSelfNode;
17+
18+
import com.oracle.truffle.api.dsl.Cached;
19+
import com.oracle.truffle.api.dsl.Specialization;
20+
import com.oracle.truffle.api.frame.VirtualFrame;
21+
import com.oracle.truffle.api.profiles.ConditionProfile;
22+
23+
public abstract class InlinedAtNode extends BinaryInlinedOperationNode {
24+
25+
protected static final String METHOD = "at";
26+
27+
public InlinedAtNode(RubyContext context, RubyCallNodeParameters callNodeParameters) {
28+
super(context, callNodeParameters);
29+
}
30+
31+
@Specialization(
32+
guards = "lookupNode.lookupProtected(frame, array, METHOD) == coreMethods().ARRAY_AT",
33+
assumptions = "assumptions",
34+
limit = "1")
35+
protected Object arrayAt(VirtualFrame frame, RubyArray array, int index,
36+
@Cached LookupMethodOnSelfNode lookupNode,
37+
@Cached ArrayIndexNodes.ReadNormalizedNode readNormalizedNode,
38+
@Cached ConditionProfile denormalized) {
39+
if (denormalized.profile(index < 0)) {
40+
index += array.size;
41+
}
42+
return readNormalizedNode.executeRead(array, index);
43+
}
44+
45+
@Specialization
46+
protected Object fallback(VirtualFrame frame, Object a, Object b) {
47+
return rewriteAndCall(frame, a, b);
48+
}
49+
50+
}

0 commit comments

Comments
 (0)