13514
|
1 /*
|
|
2 * Copyright (c) 2013 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 1.0
|
|
7 * GNU General Public License version 2
|
|
8 * GNU Lesser General Public License version 2.1
|
|
9 */
|
|
10 package com.oracle.truffle.ruby.nodes.core;
|
|
11
|
|
12 import com.oracle.truffle.api.*;
|
|
13 import com.oracle.truffle.api.dsl.*;
|
|
14 import com.oracle.truffle.api.nodes.*;
|
|
15 import com.oracle.truffle.ruby.nodes.*;
|
|
16 import com.oracle.truffle.ruby.runtime.*;
|
|
17 import com.oracle.truffle.ruby.runtime.core.array.*;
|
|
18
|
|
19 /**
|
|
20 * Index an array, without using any method lookup. This isn't a call - it's an operation on a core
|
|
21 * class.
|
|
22 */
|
|
23 @NodeInfo(shortName = "array-index")
|
|
24 @NodeChildren({@NodeChild(value = "array", type = RubyNode.class)})
|
|
25 public abstract class ArrayIndexNode extends RubyNode {
|
|
26
|
|
27 final int index;
|
|
28
|
|
29 public ArrayIndexNode(RubyContext context, SourceSection sourceSection, int index) {
|
|
30 super(context, sourceSection);
|
|
31 this.index = index;
|
|
32 }
|
|
33
|
|
34 public ArrayIndexNode(ArrayIndexNode prev) {
|
|
35 super(prev);
|
|
36 index = prev.index;
|
|
37 }
|
|
38
|
|
39 @Specialization(guards = "isEmptyStore", order = 1)
|
|
40 public NilPlaceholder indexEmpty(@SuppressWarnings("unused") RubyArray array) {
|
|
41 return NilPlaceholder.INSTANCE;
|
|
42 }
|
|
43
|
|
44 @Specialization(guards = "isFixnumStore", rewriteOn = UnexpectedResultException.class, order = 2)
|
|
45 public int indexFixnum(RubyArray array) throws UnexpectedResultException {
|
|
46 final FixnumArrayStore store = (FixnumArrayStore) array.getArrayStore();
|
|
47 return store.getFixnum(ArrayUtilities.normaliseIndex(store.size(), index));
|
|
48 }
|
|
49
|
|
50 @Specialization(guards = "isFixnumStore", order = 3)
|
|
51 public Object indexMaybeFixnum(RubyArray array) {
|
|
52 final FixnumArrayStore store = (FixnumArrayStore) array.getArrayStore();
|
|
53
|
|
54 try {
|
|
55 return store.getFixnum(ArrayUtilities.normaliseIndex(store.size(), index));
|
|
56 } catch (UnexpectedResultException e) {
|
|
57 return e.getResult();
|
|
58 }
|
|
59 }
|
|
60
|
|
61 @Specialization(guards = "isFixnumImmutablePairStore", rewriteOn = UnexpectedResultException.class, order = 4)
|
|
62 public int indexFixnumImmutablePair(RubyArray array) throws UnexpectedResultException {
|
|
63 final FixnumImmutablePairArrayStore store = (FixnumImmutablePairArrayStore) array.getArrayStore();
|
|
64 return store.getFixnum(ArrayUtilities.normaliseIndex(store.size(), index));
|
|
65 }
|
|
66
|
|
67 @Specialization(guards = "isFixnumImmutablePairStore", order = 5)
|
|
68 public Object indexMaybeFixnumImmutablePair(RubyArray array) {
|
|
69 final FixnumImmutablePairArrayStore store = (FixnumImmutablePairArrayStore) array.getArrayStore();
|
|
70
|
|
71 try {
|
|
72 return store.getFixnum(ArrayUtilities.normaliseIndex(store.size(), index));
|
|
73 } catch (UnexpectedResultException e) {
|
|
74 return e.getResult();
|
|
75 }
|
|
76 }
|
|
77
|
|
78 @Specialization(guards = "isObjectStore", order = 6)
|
|
79 public Object indexObject(RubyArray array) {
|
|
80 final ObjectArrayStore store = (ObjectArrayStore) array.getArrayStore();
|
|
81 return store.get(ArrayUtilities.normaliseIndex(store.size(), index));
|
|
82 }
|
|
83
|
|
84 @Specialization(guards = "isObjectImmutablePairStore", order = 7)
|
|
85 public Object indexObjectImmutablePair(RubyArray array) {
|
|
86 final ObjectImmutablePairArrayStore store = (ObjectImmutablePairArrayStore) array.getArrayStore();
|
|
87 return store.get(ArrayUtilities.normaliseIndex(store.size(), index));
|
|
88 }
|
|
89
|
|
90 protected boolean isEmptyStore(RubyArray receiver) {
|
|
91 return receiver.getArrayStore() instanceof EmptyArrayStore;
|
|
92 }
|
|
93
|
|
94 protected boolean isFixnumStore(RubyArray receiver) {
|
|
95 return receiver.getArrayStore() instanceof FixnumArrayStore;
|
|
96 }
|
|
97
|
|
98 protected boolean isFixnumImmutablePairStore(RubyArray receiver) {
|
|
99 return receiver.getArrayStore() instanceof FixnumImmutablePairArrayStore;
|
|
100 }
|
|
101
|
|
102 protected boolean isObjectStore(RubyArray receiver) {
|
|
103 return receiver.getArrayStore() instanceof ObjectArrayStore;
|
|
104 }
|
|
105
|
|
106 protected boolean isObjectImmutablePairStore(RubyArray receiver) {
|
|
107 return receiver.getArrayStore() instanceof ObjectImmutablePairArrayStore;
|
|
108 }
|
|
109
|
|
110 }
|