Repository: flex-falcon
Updated Branches:
  refs/heads/feature/maven-migration-test 28f5f13c9 -> 584093001


look for RequireExtern metadata on external classes to see if require() is 
needed for an external class (such as in Node.js). in the future, if different 
module output types are allowed, the emitter may decide to output it 
differently than in the current CommonJS format


Project: http://git-wip-us.apache.org/repos/asf/flex-falcon/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-falcon/commit/53e6204e
Tree: http://git-wip-us.apache.org/repos/asf/flex-falcon/tree/53e6204e
Diff: http://git-wip-us.apache.org/repos/asf/flex-falcon/diff/53e6204e

Branch: refs/heads/feature/maven-migration-test
Commit: 53e6204e466a76f458cc52ee35693eb44502da6a
Parents: fb2dc7a
Author: Josh Tynjala <joshtynj...@apache.org>
Authored: Thu Apr 21 11:05:59 2016 -0700
Committer: Josh Tynjala <joshtynj...@apache.org>
Committed: Thu Apr 21 11:05:59 2016 -0700

----------------------------------------------------------------------
 .../codegen/js/jx/PackageHeaderEmitter.java     | 119 +++++++++++++------
 .../codegen/js/node/NodeEmitterTokens.java      |  21 ++++
 .../internal/projects/FlexJSProject.java        |  50 ++++++++
 3 files changed, 155 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/53e6204e/compiler.jx/src/org/apache/flex/compiler/internal/codegen/js/jx/PackageHeaderEmitter.java
----------------------------------------------------------------------
diff --git 
a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/js/jx/PackageHeaderEmitter.java
 
b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/js/jx/PackageHeaderEmitter.java
index d47c6dc..6ce6328 100644
--- 
a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/js/jx/PackageHeaderEmitter.java
+++ 
b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/js/jx/PackageHeaderEmitter.java
@@ -38,6 +38,7 @@ import 
org.apache.flex.compiler.internal.codegen.js.JSSubEmitter;
 import org.apache.flex.compiler.internal.codegen.js.flexjs.JSFlexJSEmitter;
 import 
org.apache.flex.compiler.internal.codegen.js.flexjs.JSFlexJSEmitterTokens;
 import org.apache.flex.compiler.internal.codegen.js.goog.JSGoogEmitterTokens;
+import org.apache.flex.compiler.internal.codegen.js.node.NodeEmitterTokens;
 import org.apache.flex.compiler.internal.codegen.js.utils.EmitterUtils;
 import org.apache.flex.compiler.internal.projects.FlexJSProject;
 import org.apache.flex.compiler.internal.scopes.ASProjectScope;
@@ -187,10 +188,53 @@ public class PackageHeaderEmitter extends JSSubEmitter 
implements
                 .getCompilationUnitForDefinition(type != null ? type : 
otherMainDefinition);
         ArrayList<String> requiresList = flexProject.getRequires(cu);
         ArrayList<String> interfacesList = flexProject.getInterfaces(cu);
+        ArrayList<String> externalRequiresList = 
flexProject.getExternalRequires(cu);
         
         String cname = (type != null) ? type.getQualifiedName() : 
otherMainDefinition.getQualifiedName();
         writtenRequires.add(cname); // make sure we don't add ourselves
 
+        boolean emitsRequires = emitRequires(requiresList, writtenRequires, 
cname);
+        boolean emitsInterfaces = emitInterfaces(interfacesList, 
writtenRequires);
+
+        // erikdebruin: Add missing language feature support, with e.g. 'is' 
and 
+        //              'as' operators. We don't need to worry about requiring
+        //              this in every project: ADVANCED_OPTIMISATIONS will NOT
+        //              include any of the code if it is not used in the 
project.
+        boolean makingSWC = flexProject.getSWFTarget() != null && 
+                                               
flexProject.getSWFTarget().getTargetType() == TargetType.SWC;
+        boolean isMainCU = flexProject.mainCU != null
+                && cu.getName().equals(flexProject.mainCU.getName());
+        if (isMainCU || makingSWC)
+        {
+            ICompilerProject project = this.getProject();
+            if (project instanceof FlexJSProject)
+            {
+               if (((FlexJSProject)project).needLanguage)
+               {
+                           write(JSGoogEmitterTokens.GOOG_REQUIRE);
+                           write(ASEmitterTokens.PAREN_OPEN);
+                           write(ASEmitterTokens.SINGLE_QUOTE);
+                           write(JSFlexJSEmitterTokens.LANGUAGE_QNAME);
+                           write(ASEmitterTokens.SINGLE_QUOTE);
+                           write(ASEmitterTokens.PAREN_CLOSE);
+                           writeNewline(ASEmitterTokens.SEMICOLON);
+               }
+            }
+        }
+
+        boolean emitsExternalRequires = 
emitExternalRequires(externalRequiresList, writtenRequires);
+
+        if (emitsRequires || emitsInterfaces || emitsExternalRequires || 
isMainCU)
+        {
+            writeNewline();
+        }
+
+        writeNewline();
+        writeNewline();
+    }
+    
+    private boolean emitRequires(List<String> requiresList, List<String> 
writtenRequires, String cname)
+    {
         boolean emitsRequires = false;
         if (requiresList != null)
         {
@@ -208,8 +252,8 @@ public class PackageHeaderEmitter extends JSSubEmitter 
implements
 
                 if (NativeUtils.isNative(imp))
                 {
-                       if (!(imp.equals("QName") || imp.equals("Namespace") || 
imp.equals("XML") || imp.equals("XMLList")))
-                               continue;                       
+                    if (!(imp.equals("QName") || imp.equals("Namespace") || 
imp.equals("XML") || imp.equals("XMLList")))
+                        continue;
                 }
 
                 if (writtenRequires.indexOf(imp) == -1)
@@ -219,7 +263,7 @@ public class PackageHeaderEmitter extends JSSubEmitter 
implements
                     write(JSGoogEmitterTokens.GOOG_REQUIRE);
                     write(ASEmitterTokens.PAREN_OPEN);
                     write(ASEmitterTokens.SINGLE_QUOTE);
-                    write(fjs.formatQualifiedName(imp));
+                    write(getEmitter().formatQualifiedName(imp));
                     write(ASEmitterTokens.SINGLE_QUOTE);
                     write(ASEmitterTokens.PAREN_CLOSE);
                     writeNewline(ASEmitterTokens.SEMICOLON);
@@ -230,7 +274,11 @@ public class PackageHeaderEmitter extends JSSubEmitter 
implements
                 }
             }
         }
-
+        return emitsRequires;
+    }
+    
+    private boolean emitInterfaces(List<String> interfacesList, List<String> 
writtenRequires)
+    {
         boolean emitsInterfaces = false;
         if (interfacesList != null)
         {
@@ -242,7 +290,7 @@ public class PackageHeaderEmitter extends JSSubEmitter 
implements
                     write(JSGoogEmitterTokens.GOOG_REQUIRE);
                     write(ASEmitterTokens.PAREN_OPEN);
                     write(ASEmitterTokens.SINGLE_QUOTE);
-                    write(fjs.formatQualifiedName(imp));
+                    write(getEmitter().formatQualifiedName(imp));
                     write(ASEmitterTokens.SINGLE_QUOTE);
                     write(ASEmitterTokens.PAREN_CLOSE);
                     writeNewline(ASEmitterTokens.SEMICOLON);
@@ -251,40 +299,41 @@ public class PackageHeaderEmitter extends JSSubEmitter 
implements
                 }
             }
         }
-
-        // erikdebruin: Add missing language feature support, with e.g. 'is' 
and 
-        //              'as' operators. We don't need to worry about requiring
-        //              this in every project: ADVANCED_OPTIMISATIONS will NOT
-        //              include any of the code if it is not used in the 
project.
-        boolean makingSWC = flexProject.getSWFTarget() != null && 
-                                               
flexProject.getSWFTarget().getTargetType() == TargetType.SWC;
-        boolean isMainCU = flexProject.mainCU != null
-                && cu.getName().equals(flexProject.mainCU.getName());
-        if (isMainCU || makingSWC)
+        return emitsInterfaces;
+    }
+    
+    private boolean emitExternalRequires(List<String> externalRequiresList, 
List<String> writtenRequires)
+    {
+        boolean emitsExternalRequires = false;
+        if (externalRequiresList != null)
         {
-            ICompilerProject project = this.getProject();
-            if (project instanceof FlexJSProject)
+            Collections.sort(externalRequiresList);
+            for (String imp : externalRequiresList)
             {
-               if (((FlexJSProject)project).needLanguage)
-               {
-                           write(JSGoogEmitterTokens.GOOG_REQUIRE);
-                           write(ASEmitterTokens.PAREN_OPEN);
-                           write(ASEmitterTokens.SINGLE_QUOTE);
-                           write(JSFlexJSEmitterTokens.LANGUAGE_QNAME);
-                           write(ASEmitterTokens.SINGLE_QUOTE);
-                           write(ASEmitterTokens.PAREN_CLOSE);
-                           writeNewline(ASEmitterTokens.SEMICOLON);
-               }
-            }
-        }
+                if (writtenRequires.indexOf(imp) == -1)
+                {
+                    /* var x = require('x');\n */
+                    write(ASEmitterTokens.VAR);
+                    write(ASEmitterTokens.SPACE);
+                    write(imp);
+                    write(ASEmitterTokens.SPACE);
+                    write(ASEmitterTokens.EQUAL);
+                    write(ASEmitterTokens.SPACE);
+                    write(NodeEmitterTokens.REQUIRE);
+                    write(ASEmitterTokens.PAREN_OPEN);
+                    write(ASEmitterTokens.SINGLE_QUOTE);
+                    write(imp);
+                    write(ASEmitterTokens.SINGLE_QUOTE);
+                    write(ASEmitterTokens.PAREN_CLOSE);
+                    writeNewline(ASEmitterTokens.SEMICOLON);
 
-        if (emitsRequires || emitsInterfaces || isMainCU)
-        {
-            writeNewline();
-        }
+                    writtenRequires.add(imp);
 
-        writeNewline();
-        writeNewline();
+                    emitsExternalRequires = true;
+                }
+            }
+        }
+        return emitsExternalRequires;
     }
 
 }

http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/53e6204e/compiler.jx/src/org/apache/flex/compiler/internal/codegen/js/node/NodeEmitterTokens.java
----------------------------------------------------------------------
diff --git 
a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/js/node/NodeEmitterTokens.java
 
b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/js/node/NodeEmitterTokens.java
new file mode 100644
index 0000000..dddcf90
--- /dev/null
+++ 
b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/js/node/NodeEmitterTokens.java
@@ -0,0 +1,21 @@
+package org.apache.flex.compiler.internal.codegen.js.node;
+
+import org.apache.flex.compiler.codegen.IEmitterTokens;
+
+public enum NodeEmitterTokens implements IEmitterTokens
+{
+    REQUIRE("require"),
+    EXPORTS("exports");
+
+    private String token;
+
+    private NodeEmitterTokens(String value)
+    {
+        token = value;
+    }
+
+    public String getToken()
+    {
+        return token;
+    }
+}

http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/53e6204e/compiler.jx/src/org/apache/flex/compiler/internal/projects/FlexJSProject.java
----------------------------------------------------------------------
diff --git 
a/compiler.jx/src/org/apache/flex/compiler/internal/projects/FlexJSProject.java 
b/compiler.jx/src/org/apache/flex/compiler/internal/projects/FlexJSProject.java
index 67869ab..5796c91 100644
--- 
a/compiler.jx/src/org/apache/flex/compiler/internal/projects/FlexJSProject.java
+++ 
b/compiler.jx/src/org/apache/flex/compiler/internal/projects/FlexJSProject.java
@@ -21,6 +21,7 @@ package org.apache.flex.compiler.internal.projects;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
@@ -60,6 +61,7 @@ public class FlexJSProject extends FlexProject
 
     private HashMap<ICompilationUnit, HashMap<String, String>> interfaces = 
new HashMap<ICompilationUnit, HashMap<String, String>>();
     private HashMap<ICompilationUnit, HashMap<String, DependencyType>> 
requires = new HashMap<ICompilationUnit, HashMap<String, DependencyType>>();
+    private HashMap<ICompilationUnit, HashMap<String, DependencyType>> 
externalRequires = new HashMap<ICompilationUnit, HashMap<String, 
DependencyType>>();
 
     public JSGoogConfiguration config;
     
@@ -106,6 +108,19 @@ public class FlexJSProject extends FlexProject
                                needXML = true;
                     reqs.put(qname, dt);
                 }
+                if (externalRequires.containsKey(from))
+                {
+                    reqs = externalRequires.get(from);
+                }
+                else
+                {
+                    reqs = new HashMap<String, DependencyType>();
+                    externalRequires.put(from, reqs);
+                }
+                if (hasRequireExternMetadata(to))
+                {
+                    reqs.put(qname, dt);
+                }
             }
         }
         else
@@ -146,6 +161,27 @@ public class FlexJSProject extends FlexProject
     // definitions that should be considered external linkage
     public Collection<String> unitTestExterns;
 
+    private boolean hasRequireExternMetadata(ICompilationUnit cu)
+    {
+        try
+        {
+            Iterator<IDefinition> iterator = 
cu.getFileScopeRequest().get().getExternallyVisibleDefinitions().iterator();
+            while(iterator.hasNext())
+            {
+                IDefinition def = iterator.next();
+                if (def.hasMetaTagByName("RequireExtern"))
+                {
+                    return true;
+                }
+            }
+        }
+        catch (Exception ex)
+        {
+            //it's safe to ignore an exception here
+        }
+        return false;
+    }
+
     private boolean isExternalLinkage(ICompilationUnit cu)
     {
         if (linkageChecker == null)
@@ -220,6 +256,20 @@ public class FlexJSProject extends FlexProject
         return null;
     }
 
+    public ArrayList<String> getExternalRequires(ICompilationUnit from)
+    {
+        if (externalRequires.containsKey(from))
+        {
+            HashMap<String, DependencyType> map = externalRequires.get(from);
+            ArrayList<String> arr = new ArrayList<String>();
+            Set<String> cus = map.keySet();
+            for (String s : cus)
+                arr.add(s);
+            return arr;
+        }
+        return null;
+    }
+
     JSCSSCompilationSession cssSession = new JSCSSCompilationSession();
 
     @Override

Reply via email to