001: /*
002: * MethodIndex.java
003: *
004: * Created on January 1, 2007, 7:39 PM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package org.jruby.runtime;
011:
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: /**
016: *
017: * @author headius
018: */
019: public class MethodIndex {
020: public static final int NO_INDEX = 0;
021: public static final int OP_PLUS = 1;
022: public static final int OP_MINUS = 2;
023: public static final int OP_LT = 3;
024: public static final int AREF = 4;
025: public static final int ASET = 5;
026: public static final int POP = 6;
027: public static final int PUSH = 7;
028: public static final int NIL_P = 8;
029: public static final int EQUALEQUAL = 9;
030: public static final int UNSHIFT = 10;
031: public static final int OP_GE = 11;
032: public static final int OP_LSHIFT = 12;
033: public static final int EMPTY_P = 13;
034: public static final int TO_S = 14;
035: public static final int TO_I = 15;
036: public static final int AT = 16;
037: public static final int TO_STR = 17;
038: public static final int TO_ARY = 18;
039: public static final int TO_INT = 19;
040: public static final int TO_F = 20;
041: public static final int TO_SYM = 21;
042: public static final int TO_A = 22;
043: public static final int HASH = 23;
044: public static final int OP_GT = 24;
045: public static final int OP_TIMES = 25;
046: public static final int OP_LE = 26;
047: public static final int OP_SPACESHIP = 27;
048: public static final int LENGTH = 28;
049: public static final int OP_MATCH = 29;
050: public static final int OP_EQQ = 30;
051: public static final int LAST = 31;
052: public static final int SHIFT = 32;
053: public static final int EQL_P = 33;
054: public static final int TO_HASH = 34;
055: public static final int INSPECT = 35;
056: public static final int METHOD_MISSING = 36;
057: public static final int DEFAULT = 37;
058: public static final int MAX_METHODS = 38;
059:
060: public static final String[] NAMES = new String[MAX_METHODS];
061: public static final Map NUMBERS = new HashMap();
062:
063: static {
064: NAMES[NO_INDEX] = "";
065: NAMES[OP_PLUS] = "+";
066: NAMES[OP_MINUS] = "-";
067: NAMES[OP_LT] = "<";
068: NAMES[AREF] = "[]";
069: NAMES[ASET] = "[]=";
070: NAMES[POP] = "pop";
071: NAMES[PUSH] = "push";
072: NAMES[NIL_P] = "nil?";
073: NAMES[EQUALEQUAL] = "==";
074: NAMES[UNSHIFT] = "unshift";
075: NAMES[OP_GE] = ">=";
076: NAMES[OP_LSHIFT] = "<<";
077: NAMES[EMPTY_P] = "empty?";
078: NAMES[TO_S] = "to_s";
079: NAMES[TO_I] = "to_i";
080: NAMES[AT] = "at";
081: NAMES[TO_STR] = "to_str";
082: NAMES[TO_ARY] = "to_ary";
083: NAMES[TO_INT] = "to_int";
084: NAMES[TO_F] = "to_f";
085: NAMES[TO_SYM] = "to_sym";
086: NAMES[TO_A] = "to_a";
087: NAMES[HASH] = "hash";
088: NAMES[OP_GT] = ">";
089: NAMES[OP_TIMES] = "*";
090: NAMES[OP_LE] = "<=";
091: NAMES[OP_SPACESHIP] = "<=>";
092: NAMES[LENGTH] = "length";
093: NAMES[OP_MATCH] = "=~";
094: NAMES[OP_EQQ] = "===";
095: NAMES[LAST] = "last";
096: NAMES[SHIFT] = "shift";
097: NAMES[EQL_P] = "eql?";
098: NAMES[TO_HASH] = "to_hash";
099: NAMES[INSPECT] = "inspect";
100: NAMES[METHOD_MISSING] = "method_missing";
101: NAMES[DEFAULT] = "default";
102:
103: for (int i = 0; i < MAX_METHODS; i++) {
104: NUMBERS.put(NAMES[i], new Integer(i));
105: }
106: }
107:
108: /** Creates a new instance of MethodIndex */
109: public MethodIndex() {
110: }
111:
112: public static int getIndex(String methodName) {
113: // fast lookup for the length 1 messages
114: switch (methodName.length()) {
115: case 1:
116: switch (methodName.charAt(0)) {
117: case '+':
118: return OP_PLUS;
119: case '-':
120: return OP_MINUS;
121: case '<':
122: return OP_LT;
123: case '>':
124: return OP_GT;
125: case '*':
126: return OP_TIMES;
127: default:
128: return NO_INDEX;
129: }
130: default:
131: if (NUMBERS.containsKey(methodName))
132: return ((Integer) NUMBERS.get(methodName)).intValue();
133: return NO_INDEX;
134: }
135: }
136: }
|