rt/vm/src/main/java/org/apidesign/vm4brwsr/StackMapper.java
branchReducedStack
changeset 1457 b9386cc3ff7b
parent 1456 6212993ac686
child 1466 39d26d3686d9
     1.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/StackMapper.java	Sat Feb 15 17:30:47 2014 +0100
     1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/StackMapper.java	Sat Feb 15 20:18:26 2014 +0100
     1.3 @@ -69,7 +69,16 @@
     1.4  
     1.5      void assign(Appendable out, int varType, CharSequence s) throws IOException {
     1.6          pushTypeAndValue(varType, s);
     1.7 -        flush(out);
     1.8 +    }
     1.9 +
    1.10 +    void replace(Appendable out, int varType, String format, CharSequence... arr) 
    1.11 +    throws IOException {
    1.12 +        StringBuilder sb = new StringBuilder();
    1.13 +        ByteCodeToJavaScript.emitImpl(sb, format, arr);
    1.14 +        String[] values = stackValues.toArray();
    1.15 +        final int last = stackTypeIndexPairs.getSize() - 1;
    1.16 +        values[last] = sb.toString();
    1.17 +        stackTypeIndexPairs.set(last, varType);
    1.18      }
    1.19      
    1.20      void flush(Appendable out) throws IOException {
    1.21 @@ -84,33 +93,39 @@
    1.22          }
    1.23      }
    1.24      
    1.25 -    public Variable popI() {
    1.26 +    public CharSequence popI() {
    1.27          return popT(VarType.INTEGER);
    1.28      }
    1.29  
    1.30 -    public Variable popL() {
    1.31 +    public CharSequence popL() {
    1.32          return popT(VarType.LONG);
    1.33      }
    1.34  
    1.35 -    public Variable popF() {
    1.36 +    public CharSequence popF() {
    1.37          return popT(VarType.FLOAT);
    1.38      }
    1.39  
    1.40 -    public Variable popD() {
    1.41 +    public CharSequence popD() {
    1.42          return popT(VarType.DOUBLE);
    1.43      }
    1.44  
    1.45 -    public Variable popA() {
    1.46 +    public CharSequence popA() {
    1.47          return popT(VarType.REFERENCE);
    1.48      }
    1.49  
    1.50 -    public Variable popT(final int type) {
    1.51 -        final Variable variable = getT(0, type);
    1.52 +    public CharSequence popT(final int type) {
    1.53 +        final CharSequence variable = getT(0, type);
    1.54          popImpl(1);
    1.55          return variable;
    1.56      }
    1.57  
    1.58 -    public Variable pop() {
    1.59 +    public CharSequence popValue() {
    1.60 +        final CharSequence variable = getT(0, -1);
    1.61 +        popImpl(1);
    1.62 +        return variable;
    1.63 +    }
    1.64 +    public Variable pop(Appendable out) throws IOException {
    1.65 +        flush(out);
    1.66          final Variable variable = get(0);
    1.67          popImpl(1);
    1.68          return variable;
    1.69 @@ -124,37 +139,41 @@
    1.70          popImpl(count);
    1.71      }
    1.72  
    1.73 -    public Variable getI(final int indexFromTop) {
    1.74 +    public CharSequence getI(final int indexFromTop) {
    1.75          return getT(indexFromTop, VarType.INTEGER);
    1.76      }
    1.77  
    1.78 -    public Variable getL(final int indexFromTop) {
    1.79 +    public CharSequence getL(final int indexFromTop) {
    1.80          return getT(indexFromTop, VarType.LONG);
    1.81      }
    1.82  
    1.83 -    public Variable getF(final int indexFromTop) {
    1.84 +    public CharSequence getF(final int indexFromTop) {
    1.85          return getT(indexFromTop, VarType.FLOAT);
    1.86      }
    1.87  
    1.88 -    public Variable getD(final int indexFromTop) {
    1.89 +    public CharSequence getD(final int indexFromTop) {
    1.90          return getT(indexFromTop, VarType.DOUBLE);
    1.91      }
    1.92  
    1.93 -    public Variable getA(final int indexFromTop) {
    1.94 +    public CharSequence getA(final int indexFromTop) {
    1.95          return getT(indexFromTop, VarType.REFERENCE);
    1.96      }
    1.97  
    1.98 -    public Variable getT(final int indexFromTop, final int type) {
    1.99 +    public CharSequence getT(final int indexFromTop, final int type) {
   1.100          final int stackSize = stackTypeIndexPairs.getSize();
   1.101          if (indexFromTop >= stackSize) {
   1.102              throw new IllegalStateException("Stack underflow");
   1.103          }
   1.104          final int stackValue =
   1.105                  stackTypeIndexPairs.get(stackSize - indexFromTop - 1);
   1.106 -        if ((stackValue & 0xff) != type) {
   1.107 +        if (type != -1 && (stackValue & 0xff) != type) {
   1.108              throw new IllegalStateException("Type mismatch");
   1.109          }
   1.110 -
   1.111 +        String value =
   1.112 +                stackValues.getAndClear(stackSize - indexFromTop - 1);
   1.113 +        if (value != null) {
   1.114 +            return value;
   1.115 +        }
   1.116          return getVariable(stackValue);
   1.117      }
   1.118