vm/src/main/java/org/apidesign/vm4brwsr/Variable.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 26 Jan 2013 08:47:05 +0100
changeset 592 5e13b1ac2886
parent 310 ec7d8bc17725
permissions -rw-r--r--
In order to support fields of the same name in subclasses we are now prefixing them with name of the class that defines them. To provide convenient way to access them from generated bytecode and also directly from JavaScript, there is a getter/setter function for each field. It starts with _ followed by the field name. If called with a parameter, it sets the field, with a parameter it just returns it.
lubomir@319
     1
/**
lubomir@319
     2
 * Back 2 Browser Bytecode Translator
lubomir@319
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
lubomir@319
     4
 *
lubomir@319
     5
 * This program is free software: you can redistribute it and/or modify
lubomir@319
     6
 * it under the terms of the GNU General Public License as published by
lubomir@319
     7
 * the Free Software Foundation, version 2 of the License.
lubomir@319
     8
 *
lubomir@319
     9
 * This program is distributed in the hope that it will be useful,
lubomir@319
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
lubomir@319
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
lubomir@319
    12
 * GNU General Public License for more details.
lubomir@319
    13
 *
lubomir@319
    14
 * You should have received a copy of the GNU General Public License
lubomir@319
    15
 * along with this program. Look for COPYING file in the top folder.
lubomir@319
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
lubomir@319
    17
 */
lubomir@319
    18
package org.apidesign.vm4brwsr;
lubomir@319
    19
lubomir@319
    20
final class Variable implements CharSequence {
lubomir@319
    21
    private static final String STACK_VAR_PREFIX = "st";
lubomir@319
    22
    private static final String LOCAL_VAR_PREFIX = "lc";
lubomir@319
    23
lubomir@319
    24
    private final String name;
lubomir@319
    25
    private final int type;
lubomir@319
    26
    private final int index;
lubomir@319
    27
lubomir@319
    28
    private static final char[] TYPE_IDS = { 'I', 'L', 'F', 'D', 'A' };
lubomir@319
    29
lubomir@319
    30
    private Variable(final String prefix, final int type, final int index) {
lubomir@319
    31
        this.name = prefix + TYPE_IDS[type] + index;
lubomir@319
    32
        this.type = type;
lubomir@319
    33
        this.index = index;
lubomir@319
    34
    }
lubomir@319
    35
lubomir@319
    36
    public static Variable getStackVariable(
lubomir@319
    37
            final int type, final int index) {
lubomir@319
    38
        // TODO: precreate frequently used variables
lubomir@319
    39
        return new Variable(STACK_VAR_PREFIX, type, index);
lubomir@319
    40
    }
lubomir@319
    41
lubomir@319
    42
    public static Variable getLocalVariable(
lubomir@319
    43
            final int type, final int index) {
lubomir@319
    44
        // TODO: precreate frequently used variables
lubomir@319
    45
        return new Variable(LOCAL_VAR_PREFIX, type, index);
lubomir@319
    46
    }
lubomir@319
    47
lubomir@319
    48
    public String getName() {
lubomir@319
    49
        return name;
lubomir@319
    50
    }
lubomir@319
    51
lubomir@319
    52
    public int getType() {
lubomir@319
    53
        return type;
lubomir@319
    54
    }
lubomir@319
    55
lubomir@319
    56
    public int getIndex() {
lubomir@319
    57
        return index;
lubomir@319
    58
    }
lubomir@319
    59
lubomir@319
    60
    public boolean isCategory2() {
lubomir@319
    61
        return VarType.isCategory2(type);
lubomir@319
    62
    }
lubomir@319
    63
lubomir@319
    64
    @Override
lubomir@319
    65
    public int length() {
lubomir@319
    66
        return name.length();
lubomir@319
    67
    }
lubomir@319
    68
lubomir@319
    69
    @Override
lubomir@319
    70
    public char charAt(final int index) {
lubomir@319
    71
        return name.charAt(index);
lubomir@319
    72
    }
lubomir@319
    73
lubomir@319
    74
    @Override
lubomir@319
    75
    public CharSequence subSequence(final int start, final int end) {
lubomir@319
    76
        return name.subSequence(start, end);
lubomir@319
    77
    }
lubomir@319
    78
lubomir@319
    79
    @Override
lubomir@319
    80
    public int hashCode() {
lubomir@319
    81
        return name.hashCode();
lubomir@319
    82
    }
lubomir@319
    83
lubomir@319
    84
    @Override
lubomir@319
    85
    public boolean equals(final Object other) {
lubomir@319
    86
        if (this == other) {
lubomir@319
    87
            return true;
lubomir@319
    88
        }
lubomir@319
    89
        if (!(other instanceof Variable)) {
lubomir@319
    90
            return false;
lubomir@319
    91
        }
lubomir@319
    92
lubomir@319
    93
        return name.equals(((Variable) other).name);
lubomir@319
    94
    }
lubomir@319
    95
lubomir@319
    96
    @Override
lubomir@319
    97
    public String toString() {
lubomir@319
    98
        return name;
lubomir@319
    99
    }
lubomir@319
   100
}