rt/vm/src/main/java/org/apidesign/vm4brwsr/StackMapper.java
branchReducedStack
changeset 1455 398911a8f401
parent 1454 ca83b2adebb5
child 1456 6212993ac686
     1.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/StackMapper.java	Sat Feb 15 15:47:28 2014 +0100
     1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/StackMapper.java	Sat Feb 15 17:23:24 2014 +0100
     1.3 @@ -22,13 +22,16 @@
     1.4  
     1.5  final class StackMapper {
     1.6      private final TypeArray stackTypeIndexPairs;
     1.7 +    private final StringArray stackValues;
     1.8  
     1.9      public StackMapper() {
    1.10          stackTypeIndexPairs = new TypeArray();
    1.11 +        stackValues = new StringArray();
    1.12      }
    1.13  
    1.14      public void clear() {
    1.15          stackTypeIndexPairs.clear();
    1.16 +        stackValues.clear();
    1.17      }
    1.18  
    1.19      public void syncWithFrameStack(final TypeArray frameStack) {
    1.20 @@ -65,7 +68,20 @@
    1.21      }
    1.22  
    1.23      void assign(Appendable out, int varType, CharSequence s) throws IOException {
    1.24 -        ByteCodeToJavaScript.emit(out, "var @1 = @2;", pushT(varType), s);
    1.25 +        pushTypeAndValue(varType, s);
    1.26 +        flush(out);
    1.27 +    }
    1.28 +    
    1.29 +    void flush(Appendable out) throws IOException {
    1.30 +        int count = stackTypeIndexPairs.getSize();
    1.31 +        for (int i = 0; i < count; i++) {
    1.32 +            String val = stackValues.getAndClear(i);
    1.33 +            if (val == null) {
    1.34 +                continue;
    1.35 +            }
    1.36 +            CharSequence var = getVariable(stackTypeIndexPairs.get(i));
    1.37 +            ByteCodeToJavaScript.emitImpl(out, "var @1 = @2;", var, val);
    1.38 +        }
    1.39      }
    1.40      
    1.41      public Variable popI() {
    1.42 @@ -157,8 +173,26 @@
    1.43          final int count = stackTypeIndexPairs.getSize();
    1.44          final int value = (count << 8) | (type & 0xff);
    1.45          stackTypeIndexPairs.add(value);
    1.46 +        
    1.47 +        addStackValue(count, null);
    1.48 +        return value;
    1.49 +    }
    1.50  
    1.51 -        return value;
    1.52 +    private void pushTypeAndValue(final int type, CharSequence v) {
    1.53 +        final int count = stackTypeIndexPairs.getSize();
    1.54 +        final int value = (count << 8) | (type & 0xff);
    1.55 +        stackTypeIndexPairs.add(value);
    1.56 +        final String val = v.toString();
    1.57 +        addStackValue(count, val);
    1.58 +    }
    1.59 +
    1.60 +    private void addStackValue(int at, final String val) {
    1.61 +        final String[] arr = stackValues.toArray();
    1.62 +        if (arr.length > at) {
    1.63 +            arr[at] = val;
    1.64 +        } else {
    1.65 +            stackValues.add(val);
    1.66 +        }
    1.67      }
    1.68  
    1.69      private void popImpl(final int count) {