incubator-flex-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From erikdebr...@apache.org
Subject svn commit: r1426183 - in /incubator/flex/whiteboard/mschmalle/falconjx: compiler.jx.tests/src/org/apache/flex/js/internal/js/codegen/ compiler.jx/src/org/apache/flex/compiler/internal/js/codgen/
Date Thu, 27 Dec 2012 12:53:34 GMT
Author: erikdebruin
Date: Thu Dec 27 12:53:33 2012
New Revision: 1426183

URL: http://svn.apache.org/viewvc?rev=1426183&view=rev
Log:
Implemented an alternate method to deal with default values for parameters in JS. 

In order to do this without 'breaking' any existing code, I've create a new 'setting' on JSSharedData:
OUTPUT_ALTERNATE. I used this setting to split the flow in 'emitFunctionBlockHeader' and call
to 'emitDefaultParameterCodeBlock_Alternate' instead of the regular method.

I've also added a test to 'TestGoogEmiter': 'testDefaultParameter_Alternate' to check the
functionality of the above mentioned addition.

Modified:
    incubator/flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/js/internal/js/codegen/TestGoogEmiter.java
    incubator/flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codgen/JSGoogEmitter.java
    incubator/flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codgen/JSSharedData.java

Modified: incubator/flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/js/internal/js/codegen/TestGoogEmiter.java
URL: http://svn.apache.org/viewvc/incubator/flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/js/internal/js/codegen/TestGoogEmiter.java?rev=1426183&r1=1426182&r2=1426183&view=diff
==============================================================================
--- incubator/flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/js/internal/js/codegen/TestGoogEmiter.java
(original)
+++ incubator/flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/js/internal/js/codegen/TestGoogEmiter.java
Thu Dec 27 12:53:33 2012
@@ -170,6 +170,30 @@ public class TestGoogEmiter extends Test
         JSSharedData.OUTPUT_JSDOC = true;
     }
 
+    @Test
+    public void testDefaultParameter_Alternate()
+    {
+        /*
+        foo.bar.A.method1 = function(p1, p2, p3, p4) {
+        	p3 = typeof p3 !== 'undefined' ? p3 : 3;
+        	p4 = typeof p4 !== 'undefined' ? p4 : 4;
+        		
+            return p1 + p2 + p3 + p4;
+        }
+        */
+        JSSharedData.OUTPUT_JSDOC = false;
+        JSSharedData.OUTPUT_ALTERNATE = true;
+        IFunctionNode node = getMethod("function method1(p1:int, p2:int, p3:int = 3, p4:int
= 4):int{return p1 + p2 + p3 + p4;}");
+        visitor.visitFunction(node);
+        assertOut("foo.bar.A.method1 = function(p1, p2, p3, p4) {\n" +
+        		"\tp3 = typeof p3 !== 'undefined' ? p3 : 3;\n" +
+        		"\tp4 = typeof p4 !== 'undefined' ? p4 : 4;\n" +
+        		"\n" +
+                "\treturn p1 + p2 + p3 + p4;\n}");
+        JSSharedData.OUTPUT_ALTERNATE = false;
+        JSSharedData.OUTPUT_JSDOC = true;
+    }
+
     protected IBackend createBackend()
     {
         return new GoogBackend();

Modified: incubator/flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codgen/JSGoogEmitter.java
URL: http://svn.apache.org/viewvc/incubator/flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codgen/JSGoogEmitter.java?rev=1426183&r1=1426182&r2=1426183&view=diff
==============================================================================
--- incubator/flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codgen/JSGoogEmitter.java
(original)
+++ incubator/flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codgen/JSGoogEmitter.java
Thu Dec 27 12:53:33 2012
@@ -210,7 +210,14 @@ public class JSGoogEmitter extends JSEmi
     @Override
     public void emitFunctionBlockHeader(IFunctionNode node)
     {
-        emitDefaultParameterCodeBlock(node);
+        if (JSSharedData.OUTPUT_ALTERNATE) 
+        {
+        	emitDefaultParameterCodeBlock_Alternate(node);
+        } 
+        else 
+        {
+        	emitDefaultParameterCodeBlock(node);
+        }
     }
 
     private void emitDefaultParameterCodeBlock(IFunctionNode node)
@@ -271,6 +278,46 @@ public class JSGoogEmitter extends JSEmi
         }
     }
 
+    private void emitDefaultParameterCodeBlock_Alternate(IFunctionNode node)
+    {
+        // (erikdebruin) implemented alternative approach to handling 
+    	//               default parameter values in JS
+
+    	IParameterNode[] pnodes = node.getParameterNodes();
+        
+        Map<Integer, IParameterNode> defaults = getDefaults(pnodes);
+        
+        final StringBuilder code = new StringBuilder();
+        
+        if (defaults != null)
+        {
+            List<IParameterNode> parameters = new ArrayList<IParameterNode>(
+                    defaults.values());
+            
+            int numDefaults = 0;
+            for (IParameterNode pnode : parameters)
+            {
+                if (pnode != null)
+                {
+                	if (numDefaults > 0)
+                		code.append(getIndent(getCurrentIndent()));
+                	
+                    code.append(
+                    		pnode.getName() + 
+                    		" = typeof " + pnode.getName() + " !== 'undefined' ? " + 
+                    		pnode.getName() + " : " + 
+                    		pnode.getDefaultValue() + ";\n");
+                    
+                    numDefaults++;
+                }
+            }
+
+            code.append("\n");
+            
+            write(code.toString());
+        }
+    }
+
     @Override
     public void emitParameter(IParameterNode node)
     {

Modified: incubator/flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codgen/JSSharedData.java
URL: http://svn.apache.org/viewvc/incubator/flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codgen/JSSharedData.java?rev=1426183&r1=1426182&r2=1426183&view=diff
==============================================================================
--- incubator/flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codgen/JSSharedData.java
(original)
+++ incubator/flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codgen/JSSharedData.java
Thu Dec 27 12:53:33 2012
@@ -39,6 +39,7 @@ public class JSSharedData
     
     // TODO (mschmalle) Temp until I figure out the correct place for configuration
     public static boolean OUTPUT_JSDOC = true;
+    public static boolean OUTPUT_ALTERNATE = false;
     
     public static IBackend backend;
     public static String OUTPUT_EXTENSION;



Mime
View raw message