You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by ah...@apache.org on 2019/02/04 06:01:04 UTC

[royale-compiler] branch develop updated: added comments documenting changes

This is an automated email from the ASF dual-hosted git repository.

aharui pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-compiler.git


The following commit(s) were added to refs/heads/develop by this push:
     new b7017f0  added comments documenting changes
b7017f0 is described below

commit b7017f03329ef1e6ba82d9a15216774a3fc1e525
Author: Alex Harui <ah...@apache.org>
AuthorDate: Sun Feb 3 21:55:51 2019 -0800

    added comments documenting changes
---
 .../CollapsePropertiesWithModuleSupport.java       | 118 ++++++++++++---------
 .../ProcessClosurePrimitivesWithModuleSupport.java |  27 +++--
 .../jscomp/RenamePropertiesWithModuleSupport.java  |  17 +++
 .../jscomp/RenameVarsWithModuleSupport.java        |  18 ++++
 .../jscomp/ShadowVariablesWithModuleSupport.java   |   4 +
 5 files changed, 128 insertions(+), 56 deletions(-)

diff --git a/compiler-jx/src/main/java/com/google/javascript/jscomp/CollapsePropertiesWithModuleSupport.java b/compiler-jx/src/main/java/com/google/javascript/jscomp/CollapsePropertiesWithModuleSupport.java
index b567757..2593e82 100644
--- a/compiler-jx/src/main/java/com/google/javascript/jscomp/CollapsePropertiesWithModuleSupport.java
+++ b/compiler-jx/src/main/java/com/google/javascript/jscomp/CollapsePropertiesWithModuleSupport.java
@@ -111,10 +111,13 @@ class CollapsePropertiesWithModuleSupport implements CompilerPass {
   /** list of renamed variables */
   private ArrayList<String> renamedVars = null;
   
+  /** list of aliases that are also in externs */
   private List<String> externAliases;
 
+  /** list of aliases that came from goog.provides */
   private List<String> providedAliases = new ArrayList<String>();
   
+  /** list of namespaces that came from goog.provides */
   private List<String> providedNamespaces = new ArrayList<String>();
   
   CollapsePropertiesWithModuleSupport(AbstractCompiler compiler, PropertyCollapseLevel propertyCollapseLevel, String sourceFileName, File varRenameMapFile) {
@@ -126,6 +129,8 @@ class CollapsePropertiesWithModuleSupport implements CompilerPass {
 
   @Override
   public void process(Node externs, Node root) {
+	// ProcessClosurePrimitives runs first and builds up an initial list of
+	// namespaces from goog.provides that were also in externs.
 	externAliases = ProcessClosurePrimitivesWithModuleSupport.externedAliases.get(externs);
 	int n = externAliases.size();
 	for (int i = 0; i < n; i++)
@@ -134,6 +139,8 @@ class CollapsePropertiesWithModuleSupport implements CompilerPass {
 		String t = s.replace(".", "$");
 		externAliases.set(i, t);
 	}
+	// ProcessClosurePrimitives runs first and builds up an initial list of
+	// namespaces from goog.provides.
 	providedAliases = ProcessClosurePrimitivesWithModuleSupport.providedsMap.get(externs);
 	providedNamespaces.addAll(providedAliases);
 	n = providedAliases.size();
@@ -142,7 +149,18 @@ class CollapsePropertiesWithModuleSupport implements CompilerPass {
 		String s = providedAliases.get(i);
 		String t = s.replace(".", "$");
 		providedAliases.set(i, t);
-	}
+	}	
+	/*
+	 * The goal is to use knowledge of the aliases and which are externed
+	 * to eliminate warnings and allow aliases in more places than
+	 * closure compiler would normally.  Closure compiler doesn't want to
+	 * collapse things found in externs, but we want to collapse
+	 * them to save size and need to collapse them when there are
+	 * overrides in the module of a collapsed property in code
+	 * in the main app.  Or when the code in the main app is calling
+	 * via an interface and code in the module implements that
+	 * interface.
+	 */
 	
     GlobalNamespace namespace = new GlobalNamespace(compiler, externs, root);
     nameMap = namespace.getNameIndex();
@@ -150,7 +168,7 @@ class CollapsePropertiesWithModuleSupport implements CompilerPass {
     checkNamespaces();
 
     for (Name name : globalNames) {
-        flattenReferencesToCollapsibleDescendantNames(name, name.getBaseName());
+      flattenReferencesToCollapsibleDescendantNames(name, name.getBaseName());
     }
 
     // We collapse property definitions after collapsing property references
@@ -300,10 +318,6 @@ class CollapsePropertiesWithModuleSupport implements CompilerPass {
       boolean isAllowedToCollapse =
           propertyCollapseLevel != PropertyCollapseLevel.MODULE_EXPORT || p.isModuleExport();
 
-      //if (isAllowedToCollapse)
-      //    if (externStrings.contains(p.getName()))
-      //      isAllowedToCollapse = false; // don't collapse if used in extern
-
       if (isAllowedToCollapse && (p.canCollapse() || wasCollapsed(propAlias) || shouldCollapse(propAlias))) {
         flattenReferencesTo(p, propAlias);
       } else if (isAllowedToCollapse
@@ -316,6 +330,12 @@ class CollapsePropertiesWithModuleSupport implements CompilerPass {
     }
   }
   
+  /**
+   * we should collapse aliases in the module no matter what.
+   * 
+   * @param propAlias The alias being considered.
+   * @return true If alias is for a namespace provided in the module.
+   */
   private boolean shouldCollapse(String propAlias) {
 	if (providedAliases.contains(propAlias))
 	{
@@ -333,38 +353,38 @@ class CollapsePropertiesWithModuleSupport implements CompilerPass {
 	return false;
 }
 
-/**
-   * See if this property was collapse by the loading app
-   * @param propAlias The alias that might have been collapsed
-   * @return
+  /**
+   * See if this property was collapse by the loading app.
+   * @param propAlias The alias that might have been collapsed.
+   * @return true If the alias was collapsed in the loading app.
    */
   private boolean wasCollapsed(String propAlias) {
-	if (varRenameMapFile == null)
-		return false;
-	
-	if (renamedVars == null)
-	{
-		List<String> fileLines;
-		
-		renamedVars = new ArrayList<String>();
-		try {
-			fileLines = Files.readLines(varRenameMapFile, Charset.defaultCharset());
-			for (String line : fileLines)
-			{
-				int c = line.indexOf(":");
-				if (c > 0)
-				{
-					renamedVars.add(line.substring(0, c));
-				}
-			}
-		} catch (IOException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		}
-	}
-	if (renamedVars.contains(propAlias) || externAliases.contains(propAlias)) {
-	  // if it was collapsed, add a var for it in our source for now.
-	  // RemoveUnusedNames will move the var before it gets code-genned.
+    if (varRenameMapFile == null)
+      return false;
+
+    if (renamedVars == null)
+    {
+      List<String> fileLines;
+
+      renamedVars = new ArrayList<String>();
+      try {
+        fileLines = Files.readLines(varRenameMapFile, Charset.defaultCharset());
+        for (String line : fileLines)
+        {
+          int c = line.indexOf(":");
+          if (c > 0)
+          {
+            renamedVars.add(line.substring(0, c));
+          }
+        }
+      } catch (IOException e) {
+        // TODO Auto-generated catch block
+        e.printStackTrace();
+      }
+    }
+    if (renamedVars.contains(propAlias) || externAliases.contains(propAlias)) {
+      // if it was collapsed, add a var for it in our source for now.
+      // RemoveUnusedNames will move the var before it gets code-genned.
       InputId inputId = new InputId(
           sourceFileName);
       CompilerInput compilerInput = compiler.getInput(inputId);
@@ -372,8 +392,10 @@ class CollapsePropertiesWithModuleSupport implements CompilerPass {
       Node child = IR.var(nameNode);
       compilerInput.getAstRoot(compiler).addChildToBack(child);
       compiler.reportChangeToEnclosingScope(child);
+      // add it to the list of aliases from the externs so
+      // rename vars can know what variables we created here.
       if (!externAliases.contains(propAlias))
-    	externAliases.add(propAlias);
+        externAliases.add(propAlias);
       return true;
     }
     return false;
@@ -428,7 +450,7 @@ class CollapsePropertiesWithModuleSupport implements CompilerPass {
     // replaced with "a$b" in all occurrences of "a.b.c", "a.b.c.d", etc.
     if (n.props != null) {
       for (Name p : n.props) {
-    	flattenPrefixes(alias, p, 1);
+        flattenPrefixes(alias, p, 1);
       }
     }
   }
@@ -536,19 +558,19 @@ class CollapsePropertiesWithModuleSupport implements CompilerPass {
 
     if (parent == null)
     {
-    	// in some cases parent is null (not sure why)
-    	// but ref is a StringNode and a child of n
-    	// is a StringNode so replace that StringNode
-    	parent = n;
-    	n = n.getFirstChild();
-	    parent.replaceChild(n, ref);
+      // in some cases parent is null (not sure why)
+      // but ref is a StringNode and a child of n
+      // is a StringNode so replace that StringNode
+      parent = n;
+      n = n.getFirstChild();
+	  parent.replaceChild(n, ref);
     }
     else
     {
-	    parent.replaceChild(n, ref);
-	    Node enclosingScopeNode = NodeUtil.getEnclosingChangeScopeRoot(n.getParent());
-	    if (enclosingScopeNode != null)
-	    	compiler.reportChangeToEnclosingScope(ref);
+	  parent.replaceChild(n, ref);
+	  Node enclosingScopeNode = NodeUtil.getEnclosingChangeScopeRoot(n.getParent());
+	  if (enclosingScopeNode != null)
+	    compiler.reportChangeToEnclosingScope(ref);
     }
   }
 
diff --git a/compiler-jx/src/main/java/com/google/javascript/jscomp/ProcessClosurePrimitivesWithModuleSupport.java b/compiler-jx/src/main/java/com/google/javascript/jscomp/ProcessClosurePrimitivesWithModuleSupport.java
index 0f3e7bc..68ee660 100644
--- a/compiler-jx/src/main/java/com/google/javascript/jscomp/ProcessClosurePrimitivesWithModuleSupport.java
+++ b/compiler-jx/src/main/java/com/google/javascript/jscomp/ProcessClosurePrimitivesWithModuleSupport.java
@@ -45,6 +45,9 @@ import java.util.WeakHashMap;
 import javax.annotation.Nullable;
 
 /**
+ * Apache Royale copied ProcessClosurePrimitives and modified it to handle
+ * Royale modules.
+ * 
  * Replaces goog.provide calls, removes goog.{require,requireType} calls, verifies that each
  * goog.{require,requireType} has a corresponding goog.provide, and performs some Closure-pecific
  * simplifications.
@@ -1128,7 +1131,7 @@ class ProcessClosurePrimitivesWithModuleSupport extends AbstractPostOrderCallbac
       }
 
       @SuppressWarnings("serial")
-	CssRenamingMap cssRenamingMap = new CssRenamingMap() {
+      CssRenamingMap cssRenamingMap = new CssRenamingMap() {
         @Override
         public String get(String value) {
           if (cssNames.containsKey(value)) {
@@ -1342,6 +1345,12 @@ class ProcessClosurePrimitivesWithModuleSupport extends AbstractPostOrderCallbac
     return true;
   }
 
+  /**
+   * Build up the list of strings in the externs.
+   * 
+   * @param en The extern name.
+   * @param externStrings The list of strings to add to.
+   */
   private void addExternNameAndDescendants(Name en, ArrayList<String> externStrings) {
 	    externStrings.add(en.getName());
 
@@ -1372,13 +1381,15 @@ class ProcessClosurePrimitivesWithModuleSupport extends AbstractPostOrderCallbac
         providedNames.get(prefixNs).addProvide(
             node, module, false /* implicit */);
       } else {
-              providedNames.put(prefixNs,
-                  new ProvidedName(prefixNs, node, module, false /* implicit */));
-              if (externStrings.contains(prefixNs))
-            	  if (!externAliases.contains(prefixNs))
-            		  externAliases.add(prefixNs);
-        	  if (!provideds.contains(prefixNs))
-        		  provideds.add(prefixNs);
+        providedNames.put(prefixNs,
+            new ProvidedName(prefixNs, node, module, false /* implicit */));
+        // if it was in externs, added to externAliases
+        if (externStrings.contains(prefixNs))
+          if (!externAliases.contains(prefixNs))
+            externAliases.add(prefixNs);
+        // if the namespace of any provided classes
+        if (!provideds.contains(prefixNs))
+          provideds.add(prefixNs);
       }
     }
   }
diff --git a/compiler-jx/src/main/java/com/google/javascript/jscomp/RenamePropertiesWithModuleSupport.java b/compiler-jx/src/main/java/com/google/javascript/jscomp/RenamePropertiesWithModuleSupport.java
index 544479a..89ba74a 100644
--- a/compiler-jx/src/main/java/com/google/javascript/jscomp/RenamePropertiesWithModuleSupport.java
+++ b/compiler-jx/src/main/java/com/google/javascript/jscomp/RenamePropertiesWithModuleSupport.java
@@ -39,6 +39,9 @@ import java.util.TreeSet;
 import javax.annotation.Nullable;
 
 /**
+ * Apache Royale copied RenameProperties and modified it to handle
+ * Royale modules.
+ * 
  * RenameProperties renames properties (including methods) of all JavaScript
  * objects. This includes prototypes, functions, object literals, etc.
  *
@@ -72,6 +75,8 @@ class RenamePropertiesWithModuleSupport implements CompilerPass {
 
   /** Property renaming map from a previous compilation. */
   private final VariableMap prevUsedPropertyMap;
+  
+  /** Original names of properties that were renamed */
   private final List<String> prevUsedPropertyOldNames = new ArrayList<String>();
 
   private final List<Node> toRemove = new ArrayList<Node>();
@@ -207,6 +212,18 @@ class RenamePropertiesWithModuleSupport implements CompilerPass {
       reusePropertyNames(reservedNames, propsByFreq);
     }
 
+    /* add externed and quoted names after previously renamed
+     * properties are told to re-use their renames from the main
+     * app.  The original code added these names before
+     * trying to reuse previous renames because those
+     * externs are typically from 3rd-party libraries, but
+     * in modules, externs are from the main loading app
+     * and we want to reuse previous renames as much as possible
+     * because classes in the main loading app will make calls
+     * using the renames instead of the exported name so any
+     * override or implementations of interface methods must
+     * also have the same rename.
+     */
     reservedNames.addAll(externedNames);
     reservedNames.addAll(quotedNames);
     
diff --git a/compiler-jx/src/main/java/com/google/javascript/jscomp/RenameVarsWithModuleSupport.java b/compiler-jx/src/main/java/com/google/javascript/jscomp/RenameVarsWithModuleSupport.java
index 397df56..c7b104b 100644
--- a/compiler-jx/src/main/java/com/google/javascript/jscomp/RenameVarsWithModuleSupport.java
+++ b/compiler-jx/src/main/java/com/google/javascript/jscomp/RenameVarsWithModuleSupport.java
@@ -38,6 +38,9 @@ import java.util.TreeSet;
 import javax.annotation.Nullable;
 
 /**
+ * Apache Royale copied RenameVars and modified it to handle
+ * Royale modules.
+ * 
  * RenameVars renames all the variables names into short names, to reduce code
  * size and also to obfuscate the code.
  *
@@ -145,6 +148,7 @@ final class RenameVarsWithModuleSupport implements CompilerPass {
   // Shared name generator
   private final NameGenerator nameGenerator;
 
+  /* list of aliases from the externs */
   private List<String> externAliases;
   
   /*
@@ -370,14 +374,28 @@ final class RenameVarsWithModuleSupport implements CompilerPass {
       reusePreviouslyUsedVariableMap(varsByFrequency);
     }
 
+    // now that previous names have been assigned, 
+    // add those previous renames to reserved names
+    // so those variable names don't get re-used in assignNames.
     if (prevUsedRenameMap != null) {
     	reservedNames.addAll(prevUsedRenameMap.getNewNameToOriginalNameMap().keySet());
     }
+    
     // Assign names, sorted by descending frequency to minimize code size.
     assignNames(varsByFrequency);
 
     // Rename the globals!
     for (Node n : globalNameNodes) {
+      /* A module may have a reference to a class
+       * but no variable defining it since the class
+       * is defined in the main loading app.  Closure 
+       * expects every reference to have a var so in
+       * CollapseProperties, some vars are added so that
+       * they can be collapsed.  However, we don't want those
+       * added vars in the output so here we move them
+       * to externs (if we added them to the externs in 
+       * CollapseProperties they wouldn't get renamed).
+       */ 
       boolean moveToExternsAfterRename = false;
       String renamedVar = n.getString();
       if (prevUsedRenameMap != null && 
diff --git a/compiler-jx/src/main/java/com/google/javascript/jscomp/ShadowVariablesWithModuleSupport.java b/compiler-jx/src/main/java/com/google/javascript/jscomp/ShadowVariablesWithModuleSupport.java
index f729b83..83d74ed 100644
--- a/compiler-jx/src/main/java/com/google/javascript/jscomp/ShadowVariablesWithModuleSupport.java
+++ b/compiler-jx/src/main/java/com/google/javascript/jscomp/ShadowVariablesWithModuleSupport.java
@@ -28,6 +28,10 @@ import java.util.Map;
 import java.util.SortedSet;
 
 /**
+ * Apache Royale copied ShadowVariables and modified it to handle
+ * Royale modules.  The only change is to uses classes from
+ * RenameVarsWithModuleSupport instead of RenameVars.
+ * 
  * Tries to compute a list of variables that can shadow a variable in the
  * outer scope.
  *