|
| 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