rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
branchflow
changeset 1842 dd4dabfead82
parent 1816 6c1fa412c72d
     1.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Fri Mar 13 11:41:04 2015 +0100
     1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Fri Sep 11 14:51:09 2015 +0200
     1.3 @@ -22,6 +22,15 @@
     1.4  import org.apidesign.bck2brwsr.core.JavaScriptBody;
     1.5  import org.apidesign.vm4brwsr.Bck2Brwsr.Flow;
     1.6  import static org.apidesign.vm4brwsr.ByteCodeParser.*;
     1.7 +import org.apidesign.vm4brwsr.ByteCodeParser.AnnotationParser;
     1.8 +import org.apidesign.vm4brwsr.ByteCodeParser.BootMethodData;
     1.9 +import org.apidesign.vm4brwsr.ByteCodeParser.CPX2;
    1.10 +import org.apidesign.vm4brwsr.ByteCodeParser.ClassData;
    1.11 +import org.apidesign.vm4brwsr.ByteCodeParser.FieldData;
    1.12 +import org.apidesign.vm4brwsr.ByteCodeParser.MethodData;
    1.13 +import org.apidesign.vm4brwsr.ByteCodeParser.StackMapIterator;
    1.14 +import org.apidesign.vm4brwsr.ByteCodeParser.TrapData;
    1.15 +import org.apidesign.vm4brwsr.ByteCodeParser.TrapDataIterator;
    1.16  
    1.17  /** Translator of the code inside class files to JavaScript.
    1.18   *
    1.19 @@ -429,10 +438,21 @@
    1.20          
    1.21          if (defineProp) {
    1.22              append("Object.defineProperty(").append(destObject).
    1.23 -            append(", '").append(name).append("', { configurable: true, writable: true, value: m = function(");
    1.24 +            append(", '").append(name).append("', { configurable: true, writable: true, value: m = ");
    1.25          } else {
    1.26 -            append("m = ").append(destObject).append(".").append(name).append(" = function(");
    1.27 +            append("m = ").append(destObject).append(".").append(name).append(" = ");
    1.28          }
    1.29 +        Flow flow = checkFlow(m);
    1.30 +        if (flow != null) {
    1.31 +            // OK, assume generate
    1.32 +            if (defineProp) {
    1.33 +                append("\n});");
    1.34 +            } else {
    1.35 +                append("\n;");
    1.36 +            }
    1.37 +            return defineProp;
    1.38 +        }
    1.39 +        append("function(");
    1.40          lmapper.outputArguments(this, m.isStatic());
    1.41          append(") {").append("\n");
    1.42  
    1.43 @@ -450,7 +470,6 @@
    1.44              }
    1.45              return defineProp;
    1.46          }
    1.47 -        Flow flow = checkFlow(m);
    1.48  
    1.49          final StackMapper smapper = new StackMapper();
    1.50  
    1.51 @@ -504,16 +523,8 @@
    1.52                  lastStackFrame = stackMapIterator.getFrameIndex();
    1.53                  lmapper.syncWithFrameLocals(stackMapIterator.getFrameLocals());
    1.54                  smapper.syncWithFrameStack(stackMapIterator.getFrameStack());
    1.55 -                if (flow == null) {
    1.56 -                    append("    X_" + i).append(": for (;;) { IF: if (gt <= " + i + ") {\n");
    1.57 -                    openBraces++;
    1.58 -                } else {
    1.59 -                    if (!flow.isFlow(i)) {
    1.60 -                        if (i != 0) {
    1.61 -                            throw new IOException("Expecting flow suggestion at " + i);
    1.62 -                        }
    1.63 -                    }
    1.64 -                }
    1.65 +                append("    X_" + i).append(": for (;;) { IF: if (gt <= " + i + ") {\n");
    1.66 +                openBraces++;
    1.67                  changeInCatch = true;
    1.68              } else {
    1.69                  debug("    /* " + i + " */ ");
    1.70 @@ -522,20 +533,6 @@
    1.71                  append("try {");
    1.72                  previousTrap = trap.current();
    1.73              }
    1.74 -            if (flow != null) {
    1.75 -                if (flow.isLoop(i)) {
    1.76 -                    append("    for (;;) {\n");
    1.77 -                } 
    1.78 -                if (flow.isBreak(i)) {
    1.79 -                    append("\n    break;");
    1.80 -                }
    1.81 -                for (int j = 0; j < flow.isEnd(i); j++) {
    1.82 -                    append("\n    }");
    1.83 -                }
    1.84 -                if (flow.isElse(i)) {
    1.85 -                    append("    else {\n");
    1.86 -                }
    1.87 -            }
    1.88              final int c = readUByte(byteCodes, i);
    1.89              switch (c) {
    1.90                  case opc_aload_0:
    1.91 @@ -2303,13 +2300,6 @@
    1.92      }
    1.93  
    1.94      private static void goTo(Appendable out, Flow flow, int current, int to, int canBack) throws IOException {
    1.95 -        if (flow != null) {
    1.96 -            if (flow.isIf(current)) {
    1.97 -                out.append("{");
    1.98 -                return;
    1.99 -            }
   1.100 -            throw new IllegalStateException("Opn: ");
   1.101 -        }
   1.102          if (to < current) {
   1.103              if (canBack < to) {
   1.104                  out.append("{ gt = 0; continue X_" + to + "; }");
   1.105 @@ -2498,7 +2488,7 @@
   1.106          System.err.println(msg);
   1.107      }
   1.108  
   1.109 -    protected Flow checkFlow(MethodData byteCodes) {
   1.110 +    protected Flow checkFlow(MethodData byteCodes) throws IOException {
   1.111          return null;
   1.112      }
   1.113  }