Author: [EMAIL PROTECTED]
Date: Mon Sep  8 13:49:22 2008
New Revision: 3629

Added:
     
changes/spoon/runAsync/dev/core/src/com/google/gwt/dev/jjs/impl/JavaAndJavaScript.java
    
(contents, props changed)
     
changes/spoon/runAsync/dev/core/src/com/google/gwt/dev/jjs/impl/JavaToJavaScriptMap.java
    
(contents, props changed)
Modified:
     
changes/spoon/runAsync/dev/core/src/com/google/gwt/dev/jjs/JavaToJavaScriptCompiler.java
     
changes/spoon/runAsync/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java
     
changes/spoon/runAsync/dev/core/src/com/google/gwt/dev/js/JsStringInterner.java
     
changes/spoon/runAsync/samples/showcase/src/com/google/gwt/sample/showcase/generator/ShowcaseGenerator.java

Log:
When compiling, create a map between Java and JavaScript AST
nodes.  This could be useful for runAsync.

Modified:  
changes/spoon/runAsync/dev/core/src/com/google/gwt/dev/jjs/JavaToJavaScriptCompiler.java
==============================================================================
---  
changes/spoon/runAsync/dev/core/src/com/google/gwt/dev/jjs/JavaToJavaScriptCompiler.java
         
(original)
+++  
changes/spoon/runAsync/dev/core/src/com/google/gwt/dev/jjs/JavaToJavaScriptCompiler.java
         
Mon Sep  8 13:49:22 2008
@@ -47,7 +47,9 @@
  import com.google.gwt.dev.jjs.impl.FragmentMapper;
  import com.google.gwt.dev.jjs.impl.GenerateJavaAST;
  import com.google.gwt.dev.jjs.impl.GenerateJavaScriptAST;
+import com.google.gwt.dev.jjs.impl.JavaAndJavaScript;
  import com.google.gwt.dev.jjs.impl.JavaScriptObjectNormalizer;
+import com.google.gwt.dev.jjs.impl.JavaToJavaScriptMap;
  import com.google.gwt.dev.jjs.impl.JsoDevirtualizer;
  import com.google.gwt.dev.jjs.impl.LongCastNormalizer;
  import com.google.gwt.dev.jjs.impl.LongEmulationNormalizer;
@@ -72,7 +74,9 @@
  import com.google.gwt.dev.js.JsSymbolResolver;
  import com.google.gwt.dev.js.JsUnusedFunctionRemover;
  import com.google.gwt.dev.js.JsVerboseNamer;
+import com.google.gwt.dev.js.ast.JsName;
  import com.google.gwt.dev.js.ast.JsProgram;
+import com.google.gwt.dev.js.ast.JsStatement;
  import com.google.gwt.dev.util.DefaultTextOutput;
  import com.google.gwt.dev.util.PerfLogger;
  import com.google.gwt.dev.util.Util;
@@ -88,8 +92,10 @@
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.util.ArrayList;
+import java.util.HashMap;
  import java.util.HashSet;
  import java.util.List;
+import java.util.Map;
  import java.util.Set;
  import java.util.TreeSet;

@@ -439,6 +445,12 @@
    public String[] compile(TreeLogger logger, RebindOracle rebindOracle)
        throws UnableToCompleteException {

+    return compileToJavaAndJavaScript(logger, rebindOracle).jscode;
+  }
+
+  public JavaAndJavaScript compileToJavaAndJavaScript(TreeLogger logger,
+      RebindOracle rebindOracle) throws IllegalStateException,
+      RuntimeException, UnableToCompleteException {
      JProgram jprogram = null;
      JsProgram jsProgram = null;

@@ -490,7 +502,7 @@
      return astMemoryUsage;
    }

-  protected String[] doCompile(TreeLogger logger, JProgram jprogram,
+  protected JavaAndJavaScript doCompile(TreeLogger logger, JProgram  
jprogram,
        JsProgram jsProgram, RebindOracle rebindOracle)
        throws InterruptedException {
      if (JProgram.isTracingEnabled()) {
@@ -525,10 +537,8 @@

      // (7) Generate a JavaScript code DOM from the Java type declarations
      jprogram.typeOracle.recomputeClinits();
-    GenerateJavaScriptAST.exec(jprogram, jsProgram, options.getOutput());
-
-    // Allow GC.
-    jprogram = null;
+    final JavaToJavaScriptMap map = GenerateJavaScriptAST.exec(jprogram,
+        jsProgram, options.getOutput());

      // (8) Normalize the JS AST.
      // Fix invalid constructs created during JS AST gen.
@@ -555,23 +565,39 @@
      }

      // (10) Obfuscate
+    final Map<JsName, String> stringLiteralMap;
      switch (options.getOutput()) {
        case OBFUSCATED:
-        JsStringInterner.exec(jsProgram);
+        stringLiteralMap = JsStringInterner.exec(jsProgram);
          JsObfuscateNamer.exec(jsProgram);
          break;
        case PRETTY:
          // We don't intern strings in pretty mode to improve readability
+        stringLiteralMap = new HashMap<JsName, String>();
          JsPrettyNamer.exec(jsProgram);
          break;
        case DETAILED:
-        JsStringInterner.exec(jsProgram);
+        stringLiteralMap = JsStringInterner.exec(jsProgram);
          JsVerboseNamer.exec(jsProgram);
          break;
        default:
          throw new InternalCompilerException("Unknown output mode");
      }

+    JavaToJavaScriptMap postStringInterningMap = new JavaToJavaScriptMap()  
{
+      public JMethod nameToMethod(JsName name) {
+        return map.nameToMethod(name);
+      }
+
+      public String stringLiteralForName(JsName name) {
+        return stringLiteralMap.get(name);
+      }
+
+      public JReferenceType typeForStat(JsStatement stat) {
+        return map.typeForStat(stat);
+      }
+    };
+
      // (11) Perform any post-obfuscation normalizations.

      // Work around an IE7 bug,
@@ -587,7 +613,9 @@
        v.accept(jsProgram.getFragmentBlock(i));
        js[i] = out.toString();
      }
-    return js;
+
+    return new JavaAndJavaScript(jprogram, jsProgram, js,
+        postStringInterningMap);
    }

    protected void optimize(JProgram jprogram) throws InterruptedException {

Modified:  
changes/spoon/runAsync/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java
==============================================================================
---  
changes/spoon/runAsync/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java
       
(original)
+++  
changes/spoon/runAsync/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java
       
Mon Sep  8 13:49:22 2008
@@ -516,7 +516,9 @@
            } else {
              assert globalStmts != null : x.getName()
                  + " does belong in a fragment";
-            globalStmts.add(func.makeStmt());
+            JsExprStmt funcStmt = func.makeStmt();
+            globalStmts.add(funcStmt);
+            typeForStatMap.put(funcStmt, x);
            }
          }
        }
@@ -554,6 +556,7 @@
        // setup instance fields
        for (JsStatement stmt : instanceFields) {
          globalStmts.add(stmt);
+        typeForStatMap.put(stmt, x);
        }
      }

@@ -1405,7 +1408,9 @@
          JsFunction seedFunc = new JsFunction(topScope, seedFuncName, true);
          JsBlock body = new JsBlock();
          seedFunc.setBody(body);
-        globalStmts.add(seedFunc.makeStmt());
+        JsExprStmt seedFuncStmt = seedFunc.makeStmt();
+        globalStmts.add(seedFuncStmt);
+        typeForStatMap.put(seedFuncStmt, x);

          // setup prototype, assign to temp
          // _ = com_example_foo_Foo.prototype = new  
com_example_foo_FooSuper();
@@ -1421,7 +1426,9 @@
          }
          JsExpression protoAsg = createAssignment(lhs, rhs);
          JsExpression tmpAsg = createAssignment(globalTemp.makeRef(),  
protoAsg);
-        globalStmts.add(tmpAsg.makeStmt());
+        JsExprStmt tmpAsgStmt = tmpAsg.makeStmt();
+        globalStmts.add(tmpAsgStmt);
+        typeForStatMap.put(tmpAsgStmt, x);
        } else {
          /*
           * MAGIC: java.lang.String is implemented as a JavaScript String
@@ -1430,7 +1437,9 @@
          JsNameRef rhs = prototype.makeRef();
           
rhs.setQualifier(jsProgram.getRootScope().declareName("String").makeRef());
          JsExpression tmpAsg = createAssignment(globalTemp.makeRef(), rhs);
-        globalStmts.add(tmpAsg.makeStmt());
+        JsExprStmt tmpAsgStmt = tmpAsg.makeStmt();
+        globalStmts.add(tmpAsgStmt);
+        typeForStatMap.put(tmpAsgStmt, x);
        }
      }

@@ -1508,7 +1517,9 @@
          fieldRef.setQualifier(globalTemp.makeRef());
          JsNumberLiteral typeIdLit = jsProgram.getNumberLiteral(typeId);
          JsExpression asg = createAssignment(fieldRef, typeIdLit);
-        globalStmts.add(new JsExprStmt(asg));
+        JsExprStmt asgStmt = new JsExprStmt(asg);
+        globalStmts.add(asgStmt);
+        typeForStatMap.put(asgStmt, x);
        }
      }

@@ -1543,7 +1554,9 @@
            lhs.setQualifier(globalTemp.makeRef());
            JsNameRef rhs = names.get(method).makeRef();
            JsExpression asg = createAssignment(lhs, rhs);
-          globalStmts.add(new JsExprStmt(asg));
+          JsExprStmt asgStat = new JsExprStmt(asg);
+          globalStmts.add(asgStat);
+          typeForStatMap.put(asgStat, x);
          }
        }
      }
@@ -1720,11 +1733,11 @@
      }
    }

-  public static void exec(JProgram program, JsProgram jsProgram,
+  public static JavaToJavaScriptMap exec(JProgram program, JsProgram  
jsProgram,
        JsOutputOption output) {
      GenerateJavaScriptAST generateJavaScriptAST = new  
GenerateJavaScriptAST(
          program, jsProgram, output);
-    generateJavaScriptAST.execImpl();
+    return generateJavaScriptAST.execImpl();
    }

    private final Map<JBlock, JsCatch> catchMap = new  
IdentityHashMap<JBlock, JsCatch>();
@@ -1785,6 +1798,8 @@
     * Contains JsNames for all globals, such as static fields and methods.
     */
    private final JsScope topScope;
+  private final Map<JsStatement, JReferenceType> typeForStatMap = new  
HashMap<JsStatement, JReferenceType>();
+
    private final JTypeOracle typeOracle;

    private GenerateJavaScriptAST(JProgram program, JsProgram jsProgram,
@@ -1914,7 +1929,7 @@
      throw new InternalCompilerException("Unknown output mode");
    }

-  private void execImpl() {
+  private JavaToJavaScriptMap execImpl() {
      SortVisitor sorter = new SortVisitor();
      sorter.accept(program);
      RecordCrossClassCalls recorder = new RecordCrossClassCalls();
@@ -1923,6 +1938,34 @@
      creator.accept(program);
      GenerateJavaScriptVisitor generator = new GenerateJavaScriptVisitor();
      generator.accept(program);
-  }
+    final Map<JsName, JMethod> nameToMethodMap = new HashMap<JsName,  
JMethod>();
+    for (JAbstractMethodBody body : methodBodyMap.keySet()) {
+      nameToMethodMap.put(methodBodyMap.get(body).getName(),  
body.getMethod());
+    }
+    final HashMap<JsName, JReferenceType> constructorNameToTypeMap = new  
HashMap<JsName, JReferenceType>();
+    for (JReferenceType type : program.getDeclaredTypes()) {
+      JsName name = names.get(type);
+      if (name != null) {
+        constructorNameToTypeMap.put(name, type);
+      }
+    }
+
+    return new JavaToJavaScriptMap() {
+      public JMethod nameToMethod(JsName name) {
+        return nameToMethodMap.get(name);
+      }

+      public String stringLiteralForName(JsName var) {
+        /*
+         * This method will be supplied non-trivially elsewhere.
+         * GenerateJavaScriptAST doesn't have the information to implement  
it.
+         */
+        return null;
+      }
+
+      public JReferenceType typeForStat(JsStatement stat) {
+        return typeForStatMap.get(stat);
+      }
+    };
+  }
  }

Added:  
changes/spoon/runAsync/dev/core/src/com/google/gwt/dev/jjs/impl/JavaAndJavaScript.java
==============================================================================
--- (empty file)
+++  
changes/spoon/runAsync/dev/core/src/com/google/gwt/dev/jjs/impl/JavaAndJavaScript.java
   
Mon Sep  8 13:49:22 2008
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2008 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may  
not
+ * use this file except in compliance with the License. You may obtain a  
copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,  
WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations  
under
+ * the License.
+ */
+package com.google.gwt.dev.jjs.impl;
+
+import com.google.gwt.dev.jjs.ast.JProgram;
+import com.google.gwt.dev.js.ast.JsProgram;
+
+/**
+ * A Java program, a JavaScript program, and a map between them.
+ */
+public class JavaAndJavaScript {
+  public final JProgram jprogram;
+  public final JsProgram jsprogram;
+  public final String[] jscode;
+  public final JavaToJavaScriptMap map;
+
+  public JavaAndJavaScript(JProgram jprogram, JsProgram jsprogram,  
String[] jscode, JavaToJavaScriptMap map) {
+    this.jprogram = jprogram;
+    this.jsprogram = jsprogram;
+    this.jscode = jscode;
+    this.map = map;
+  }
+}

Added:  
changes/spoon/runAsync/dev/core/src/com/google/gwt/dev/jjs/impl/JavaToJavaScriptMap.java
==============================================================================
--- (empty file)
+++  
changes/spoon/runAsync/dev/core/src/com/google/gwt/dev/jjs/impl/JavaToJavaScriptMap.java
         
Mon Sep  8 13:49:22 2008
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2008 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may  
not
+ * use this file except in compliance with the License. You may obtain a  
copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,  
WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations  
under
+ * the License.
+ */
+package com.google.gwt.dev.jjs.impl;
+
+import com.google.gwt.dev.jjs.ast.JMethod;
+import com.google.gwt.dev.jjs.ast.JReferenceType;
+import com.google.gwt.dev.js.ast.JsName;
+import com.google.gwt.dev.js.ast.JsStatement;
+
+/**
+ * A map from chunks of Java to chunks of JavaScript.
+ */
+public interface JavaToJavaScriptMap {
+  JMethod nameToMethod(JsName name);
+
+  String stringLiteralForName(JsName var);
+
+  JReferenceType typeForStat(JsStatement stat);
+}

Modified:  
changes/spoon/runAsync/dev/core/src/com/google/gwt/dev/js/JsStringInterner.java
==============================================================================
---  
changes/spoon/runAsync/dev/core/src/com/google/gwt/dev/js/JsStringInterner.java 
 
(original)
+++  
changes/spoon/runAsync/dev/core/src/com/google/gwt/dev/js/JsStringInterner.java 
 
Mon Sep  8 13:49:22 2008
@@ -31,6 +31,7 @@
  import com.google.gwt.dev.js.ast.JsVars.JsVar;

  import java.util.Comparator;
+import java.util.HashMap;
  import java.util.Map;
  import java.util.SortedMap;
  import java.util.TreeMap;
@@ -174,9 +175,9 @@
     * global block.
     *
     * @param program the JsProgram
-   * @return <code>true</code> if any changes were made to the program
+   * @return a map from each created JsName to the string it is a literal  
for.
     */
-  public static boolean exec(JsProgram program) {
+  public static Map<JsName, String> exec(JsProgram program) {
      StringVisitor v = new StringVisitor(program.getScope());
      for (int i = 0; i < program.getFragmentCount(); i++) {
        v.accept(program.getFragmentBlock(i));
@@ -184,7 +185,14 @@

      createVars(program.getGlobalBlock(), v.toCreate);

-    return v.didChange();
+    Map<JsName, String> map = new HashMap<JsName, String>();
+    for (JsStringLiteral stringLit : v.toCreate.keySet()) {
+      map.put(v.toCreate.get(stringLit), stringLit.getValue());
+    }
+
+    createVars(program.getGlobalBlock(), v.toCreate);
+
+    return map;
    }

    private static void createVars(JsBlock block,

Modified:  
changes/spoon/runAsync/samples/showcase/src/com/google/gwt/sample/showcase/generator/ShowcaseGenerator.java
==============================================================================
---  
changes/spoon/runAsync/samples/showcase/src/com/google/gwt/sample/showcase/generator/ShowcaseGenerator.java
      
(original)
+++  
changes/spoon/runAsync/samples/showcase/src/com/google/gwt/sample/showcase/generator/ShowcaseGenerator.java
      
Mon Sep  8 13:49:22 2008
@@ -71,7 +71,7 @@
        String typeName) throws UnableToCompleteException {
      this.logger = logger;
      this.context = context;
-    this.classLoader = getClass().getClassLoader();
+    this.classLoader = Thread.currentThread().getContextClassLoader();

      // Only generate files on the first permutation
      if (!isFirstPass()) {

--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/Google-Web-Toolkit-Contributors
-~----------~----~----~----~------~----~------~--~---

Reply via email to