flex-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mschma...@apache.org
Subject svn commit: r1442200 - in /flex/falcon/trunk: compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/amd/TestAMDPackage.java compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDEmitter.java
Date Mon, 04 Feb 2013 17:49:49 GMT
Author: mschmalle
Date: Mon Feb  4 17:49:48 2013
New Revision: 1442200

URL: http://svn.apache.org/viewvc?rev=1442200&view=rev
Log:
Flex:FalconJx
- Initial AMD package and class implementation.

Modified:
    flex/falcon/trunk/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/amd/TestAMDPackage.java
    flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDEmitter.java

Modified: flex/falcon/trunk/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/amd/TestAMDPackage.java
URL: http://svn.apache.org/viewvc/flex/falcon/trunk/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/amd/TestAMDPackage.java?rev=1442200&r1=1442199&r2=1442200&view=diff
==============================================================================
--- flex/falcon/trunk/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/amd/TestAMDPackage.java
(original)
+++ flex/falcon/trunk/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/amd/TestAMDPackage.java
Mon Feb  4 17:49:48 2013
@@ -38,7 +38,7 @@ public class TestAMDPackage extends Test
      * $2 = I,       trace,       bind_
      * 
      * 
-define(["runtime/$0", $1],
+define(["exports", "runtime/AS3", $1],
     function($0,     $2) {
     "use strict";
     
@@ -102,7 +102,11 @@ L#      return this.msg + n; // add 'thi
     {
         IFileNode node = getFileNode("package {public class A{}}");
         visitor.visitFile(node);
-        //assertOut("");
+        String code = writer.toString();
+        assertOut("define([\"exports\", \"AS3\"], function($exports, AS3) {" +
+        		"\n\t\"use strict\"; AS3.class_($exports,\n\tfunction() {" +
+        		"\n\t\tvar Super=Object._;\n\t\tvar super$=Super.prototype;\n\t\t" +
+        		"return {\n\t\t\tclass_: \"A\",\n\t\t\textends_: Super\n\t\t};\n\t});\n});");
     }
 
     @Override

Modified: flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDEmitter.java
URL: http://svn.apache.org/viewvc/flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDEmitter.java?rev=1442200&r1=1442199&r2=1442200&view=diff
==============================================================================
--- flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDEmitter.java
(original)
+++ flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDEmitter.java
Mon Feb  4 17:49:48 2013
@@ -21,6 +21,7 @@ package org.apache.flex.compiler.interna
 
 import java.io.FilterWriter;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
@@ -32,6 +33,7 @@ import org.apache.flex.compiler.constant
 import org.apache.flex.compiler.definitions.IClassDefinition;
 import org.apache.flex.compiler.definitions.IPackageDefinition;
 import org.apache.flex.compiler.definitions.ITypeDefinition;
+import org.apache.flex.compiler.definitions.references.IReference;
 import org.apache.flex.compiler.internal.js.codegen.JSEmitter;
 import org.apache.flex.compiler.internal.tree.as.FunctionNode;
 import org.apache.flex.compiler.js.codegen.amd.IJSAMDDocEmitter;
@@ -51,6 +53,32 @@ import org.apache.flex.compiler.tree.as.
  */
 public class JSAMDEmitter extends JSEmitter implements IJSAMDEmitter
 {
+    private List<String> runtime = new ArrayList<String>();
+
+    private List<String> types = new ArrayList<String>();
+
+    /*
+    
+    define(["exports", "..."], function($exports, Type, ...) {
+        "use strict"; AS3.class_($exports, 
+        function() {
+    
+            var Super=Object._;
+            var super$=Super.prototype;
+            
+            return {
+                class_: "A",
+                extends_: Super,
+                members: {
+                    constructor: A,
+                    // public methods
+                    foo: {}
+                }
+            };
+        });
+    });
+    */
+
     IJSAMDDocEmitter getDoc()
     {
         return (IJSAMDDocEmitter) getDocEmitter();
@@ -66,14 +94,12 @@ public class JSAMDEmitter extends JSEmit
         if (type == null)
             return;
 
-        // runtime
-        writeRuntimeList(type);
+        addFrameworkDependencies(runtime);
+        addImports(type, types);
 
-    }
-
-    private void writeRuntimeList(ITypeDefinition type)
-    {
-        // [defineClass|Interface, deps
+        // runtime
+        writeExports(type, true);
+        write(", ");
 
     }
 
@@ -86,16 +112,102 @@ public class JSAMDEmitter extends JSEmit
     @Override
     public void emitPackageContents(IPackageDefinition definition)
     {
-        // walk IClassNode | IInterfaceNode
+        IASScope containedScope = definition.getContainedScope();
+        ITypeDefinition type = findType(containedScope.getAllLocalDefinitions());
+        if (type == null)
+            return;
+
+        write("function($exports");
+        writeExports(type, false);
+        write(") {");
+        indentPush();
+        writeNewline();
+        write("\"use strict\"; ");
+
+        //-----------------------------------------------------
+        ITypeNode tnode = findTypeNode(definition.getNode());
+        if (tnode != null)
+        {
+            getWalker().walk(tnode); // IClassNode | IInterfaceNode
+        }
+        //-----------------------------------------------------
+
+        indentPop();
+        writeNewline();
+        write("}"); // end returned function
     }
 
     @Override
     public void emitPackageFooter(IPackageDefinition definition)
     {
-        //write("}"); // end returned function
         write(");"); // end define()
     }
 
+    @Override
+    public void emitClass(IClassNode node)
+    {
+        IClassDefinition definition = node.getDefinition();
+        final String className = definition.getBaseName();
+
+        write("AS3.class_($exports,");
+        writeNewline();
+        write("function() {");
+
+        // walk IClassNode | IInterfaceNode
+
+        indentPush();
+        writeNewline();
+        //var Super=Object._;
+        //var super$=Super.prototype;
+        String baseName = toSuperBaseName(definition);
+        if (baseName != null)
+        {
+            write("var Super=" + baseName + "._;");
+            writeNewline();
+            write("var super$=Super.prototype;");
+        }
+
+        writeNewline();
+        write("return {");
+        indentPush();
+
+        // class
+        writeNewline();
+        write("class_: \"" + className + "\",");
+        writeNewline();
+        write("extends_: Super");
+
+        final IDefinitionNode[] members = node.getAllMemberNodes();
+
+        if (members.length > 0)
+        {
+            write(",");
+            writeNewline();
+            write("members: {");
+            indentPush();
+            writeNewline();
+
+            // constructor
+            write("constructor: " + className + ",");
+            writeNewline();
+
+            // end members
+            indentPop();
+            writeNewline();
+            write("};");
+        }
+
+        // end return
+        indentPop();
+        writeNewline();
+        write("};");
+
+        indentPop();
+
+        writeNewline();
+        write("});");
+    }
+
     //--------------------------------------------------------------------------
     // 
     //--------------------------------------------------------------------------
@@ -287,4 +399,90 @@ public class JSAMDEmitter extends JSEmit
         return tnode.getDefinition();
     }
 
+    private void writeExports(ITypeDefinition type, boolean outputString)
+    {
+        if (outputString)
+        {
+            write("[");
+            write("\"exports\"");
+        }
+
+        write(", ");
+
+        int i = 0;
+        int len = runtime.size();
+        for (String dependency : runtime)
+        {
+            if (outputString)
+            {
+                write("\"" + dependency.replaceAll("\\.", "/") + "\"");
+            }
+            else
+            {
+                write(dependency);
+            }
+
+            if (i < len - 1)
+                write(", ");
+            i++;
+        }
+
+        i = 0;
+        len = types.size();
+        if (len > 0)
+            write(", ");
+
+        for (String dependency : types)
+        {
+            if (outputString)
+            {
+                write("\"" + dependency.replaceAll("\\.", "/") + "\"");
+            }
+            else
+            {
+                write(dependency);
+            }
+            if (i < len - 1)
+                write(", ");
+            i++;
+        }
+
+        if (outputString)
+        {
+            write("]");
+        }
+    }
+
+    protected void addImports(ITypeDefinition type, List<String> dependencies)
+    {
+        Collection<String> imports = new ArrayList<String>();
+        type.getContainedScope().getScopeNode().getAllImports(imports);
+        for (String imp : imports)
+        {
+            if (!isExcludedImport(imp))
+                dependencies.add(imp);
+        }
+    }
+
+    protected void addFrameworkDependencies(List<String> dependencies)
+    {
+        dependencies.add("AS3");
+    }
+
+    protected boolean isExcludedImport(String imp)
+    {
+        return imp.startsWith(AS3);
+    }
+
+    private String toSuperBaseName(ITypeDefinition type)
+    {
+        if (type instanceof IClassDefinition)
+        {
+            IClassDefinition cdefintion = (IClassDefinition) type;
+            IReference reference = cdefintion.getBaseClassReference();
+            if (reference != null)
+                return reference.getName();
+        }
+        return null;
+    }
 }



Mime
View raw message