Return-Path: X-Original-To: apmail-flex-commits-archive@www.apache.org Delivered-To: apmail-flex-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5646CE91E for ; Tue, 8 Jan 2013 09:54:55 +0000 (UTC) Received: (qmail 62153 invoked by uid 500); 8 Jan 2013 09:54:55 -0000 Delivered-To: apmail-flex-commits-archive@flex.apache.org Received: (qmail 61968 invoked by uid 500); 8 Jan 2013 09:54:54 -0000 Mailing-List: contact commits-help@flex.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@flex.apache.org Delivered-To: mailing list commits@flex.apache.org Received: (qmail 61925 invoked by uid 99); 8 Jan 2013 09:54:53 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 08 Jan 2013 09:54:53 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 08 Jan 2013 09:54:50 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 774C123888E7; Tue, 8 Jan 2013 09:54:31 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1430195 - in /flex/whiteboard/mschmalle/falconjx: compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/amd/ compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/goog/ compiler.jx/src/org/apache/flex/compiler/int... Date: Tue, 08 Jan 2013 09:54:30 -0000 To: commits@flex.apache.org From: erikdebruin@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130108095431.774C123888E7@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: erikdebruin Date: Tue Jan 8 09:54:30 2013 New Revision: 1430195 URL: http://svn.apache.org/viewvc?rev=1430195&view=rev Log: - Created 'AMD' emitter to 'store' the alternate handling of default parameters (and also to see if I could ;-)) - Created tests for the new emitter - Removed alternate handling of default parameters (and it's tests) from the 'goog' emitter - Added a few missing Apache license headers Added: flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/amd/ flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/amd/TestAMDEmiter.java (with props) flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/ flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDDocEmitter.java (with props) flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDEmitter.java (with props) flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/driver/amd/ flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/driver/amd/AMDBackend.java (with props) flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/js/codegen/amd/ flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/js/codegen/amd/IJSAMDDocEmitter.java (with props) flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/js/codegen/amd/IJSAMDEmitter.java (with props) Modified: flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/goog/TestGoogEmiter.java flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/goog/TestGoogMethodMembers.java flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/goog/JSGoogDocEmitter.java Added: flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/amd/TestAMDEmiter.java URL: http://svn.apache.org/viewvc/flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/amd/TestAMDEmiter.java?rev=1430195&view=auto ============================================================================== --- flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/amd/TestAMDEmiter.java (added) +++ flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/amd/TestAMDEmiter.java Tue Jan 8 09:54:30 2013 @@ -0,0 +1,192 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.flex.compiler.internal.js.codegen.amd; + +import org.apache.flex.compiler.clients.IBackend; +import org.apache.flex.compiler.internal.as.codegen.TestWalkerBase; +import org.apache.flex.compiler.internal.js.codegen.JSSharedData; +import org.apache.flex.compiler.internal.js.driver.amd.AMDBackend; +import org.apache.flex.compiler.tree.as.IFileNode; +import org.apache.flex.compiler.tree.as.IFunctionNode; +import org.junit.Ignore; +import org.junit.Test; + +/** + * This class tests the production of 'AMD' JavaScript output. + *

+ * Note; this is a complete prototype more used in figuring out where + * abstraction and indirection is needed concerning the AS -> JS translations. + * + * @author Michael Schmalle + */ +public class TestAMDEmiter extends TestWalkerBase +{ + // emitPackageHeader() + // emitImports() + // emitClass() + + @Test + public void testSimple() + { + String code = "package com.example.components {" + + "import org.apache.flex.html.staticControls.TextButton;" + + "public class MyTextButton extends TextButton {" + + "public function MyTextButton() {if (foo() != 42) { bar(); } }" + + "private var _privateVar:String = \"do \";" + + "public var publicProperty:Number = 100;" + + "public function myFunction(value: String): String{" + + "return \"Don't \" + _privateVar + value; }"; + IFileNode node = getFileNode(code); + visitor.visitFile(node); + //assertOut(""); + } + + @Test + public void testSimpleMethod() + { + JSSharedData.OUTPUT_JSDOC = false; + IFunctionNode node = getMethodSimple("function method1():void{\n}"); + visitor.visitFunction(node); + assertOut("A.prototype.method1 = function() {\n}"); + JSSharedData.OUTPUT_JSDOC = true; + } + + @Test + public void testSimpleParameterReturnType() + { + JSSharedData.OUTPUT_JSDOC = false; + IFunctionNode node = getMethod("function method1(bar:int):int{\n}"); + visitor.visitFunction(node); + assertOut("foo.bar.A.prototype.method1 = function(bar) {\n}"); + JSSharedData.OUTPUT_JSDOC = true; + } + + @Test + public void testSimpleMultipleParameter() + { + JSSharedData.OUTPUT_JSDOC = false; + IFunctionNode node = getMethod("function method1(bar:int, baz:String, goo:A):void{\n}"); + visitor.visitFunction(node); + assertOut("foo.bar.A.prototype.method1 = function(bar, baz, goo) {\n}"); + JSSharedData.OUTPUT_JSDOC = true; + } + + @Ignore + @Test + public void testSimpleMultipleParameter_JSDoc() + { + // jsdoc still needs to be sorted out before tests are executing + IFunctionNode node = getMethod("function method1(bar:int, baz:String, goo:A):void{\n}"); + visitor.visitFunction(node); + assertOut("/**\n * @this {foo.bar.A}\n * @param {int} bar\n * @param {String} baz\n" + + " * @param {A} goo\n * @return {void}\n */\nfoo.bar.A.prototype.method1 = " + + "function(bar, baz, goo) {\n}"); + } + + @Test + public void testDefaultParameter() + { + /* + foo.bar.A.method1 = function(p1, p2, p3, p4) { + if (arguments.length < 4) { + if (arguments.length < 3) { + p3 = 3; + } + p4 = 4; + } + return p1 + p2 + p3 + p4; + } + */ + JSSharedData.OUTPUT_JSDOC = false; + 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.prototype.method1 = function(p1, p2, p3, p4) {\n\tif (arguments.length < 4) " + + "{\n\t\tif (arguments.length < 3) {\n\t\t\tp3 = 3;\n\t\t}\n\t\tp4 = 4;\n\t}" + + "\n\treturn p1 + p2 + p3 + p4;\n}"); + JSSharedData.OUTPUT_JSDOC = true; + } + + @Test + public void testDefaultParameter_Body() + { + /* + foo.bar.A.method1 = function(bar, bax) { + if (arguments.length < 2) { + if (arguments.length < 1) { + bar = 42; + } + bax = 4; + } + } + */ + JSSharedData.OUTPUT_JSDOC = false; + IFunctionNode node = getMethod("function method1(bar:int = 42, bax:int = 4):void{if (a) foo();}"); + visitor.visitFunction(node); + assertOut("foo.bar.A.prototype.method1 = function(bar, bax) {\n\tif (arguments.length < 2) {\n\t\t" + + "if (arguments.length < 1) {\n\t\t\tbar = 42;\n\t\t}\n\t\tbax = 4;\n\t}\n\t" + + "if (a)\n\t\tfoo();\n}"); + JSSharedData.OUTPUT_JSDOC = true; + } + + @Test + public void testDefaultParameter_NoBody_Alternate() + { + /* + foo.bar.A.method1 = function(bar, bax) { + if (arguments.length < 2) { + if (arguments.length < 1) { + bar = 42; + } + bax = 4; + } + } + */ + JSSharedData.OUTPUT_JSDOC = false; + IFunctionNode node = getMethod("function method1(bar:int = 42, bax:int = 4):void{\n}"); + visitor.visitFunction(node); + assertOut("foo.bar.A.prototype.method1 = function(bar, bax) {\n\tif (arguments.length < 2) {\n\t\t" + + "if (arguments.length < 1) {\n\t\t\tbar = 42;\n\t\t}\n\t\tbax = 4;\n\t}\n}"); + JSSharedData.OUTPUT_JSDOC = true; + } + + @Override + protected IBackend createBackend() + { + return new AMDBackend(); + } + + protected IFunctionNode getMethodSimple(String code) + { + String source = "package {public class A {" + code + "}}"; + IFileNode node = getFileNode(source); + IFunctionNode child = (IFunctionNode) findFirstDescendantOfType(node, + IFunctionNode.class); + return child; + } + + protected IFunctionNode getMethod(String code) + { + String source = "package foo.bar {public class A {" + code + "}}"; + IFileNode node = getFileNode(source); + IFunctionNode child = (IFunctionNode) findFirstDescendantOfType(node, + IFunctionNode.class); + return child; + } +} Propchange: flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/amd/TestAMDEmiter.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/goog/TestGoogEmiter.java URL: http://svn.apache.org/viewvc/flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/goog/TestGoogEmiter.java?rev=1430195&r1=1430194&r2=1430195&view=diff ============================================================================== --- flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/goog/TestGoogEmiter.java (original) +++ flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/goog/TestGoogEmiter.java Tue Jan 8 09:54:30 2013 @@ -121,31 +121,6 @@ public class TestGoogEmiter extends Test JSSharedData.OUTPUT_JSDOC = true; } - @Ignore - @Test - public void testDefaultParameter_Alternate() - { - // (erikdebruin) this tests the 'alternate' handling of default values - /* - foo.bar.A.method1 = function(p1, p2, p3, p4) { - if (arguments.length < 4) { - if (arguments.length < 3) { - p3 = 3; - } - p4 = 4; - } - return p1 + p2 + p3 + p4; - } - */ - JSSharedData.OUTPUT_JSDOC = false; - 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.prototype.method1 = function(p1, p2, p3, p4) {\n\tif (arguments.length < 4) " - + "{\n\t\tif (arguments.length < 3) {\n\t\t\tp3 = 3;\n\t\t}\n\t\tp4 = 4;\n\t}" - + "\n\treturn p1 + p2 + p3 + p4;\n}"); - JSSharedData.OUTPUT_JSDOC = true; - } - @Test public void testDefaultParameter_Body() { @@ -165,30 +140,6 @@ public class TestGoogEmiter extends Test JSSharedData.OUTPUT_JSDOC = true; } - @Ignore - @Test - public void testDefaultParameter_Body_Alternate() - { - // (erikdebruin) this tests the 'alternate' handling of default values - /* - foo.bar.A.method1 = function(bar, bax) { - if (arguments.length < 2) { - if (arguments.length < 1) { - bar = 42; - } - bax = 4; - } - } - */ - JSSharedData.OUTPUT_JSDOC = false; - IFunctionNode node = getMethod("function method1(bar:int = 42, bax:int = 4):void{if (a) foo();}"); - visitor.visitFunction(node); - assertOut("foo.bar.A.prototype.method1 = function(bar, bax) {\n\tif (arguments.length < 2) {\n\t\t" - + "if (arguments.length < 1) {\n\t\t\tbar = 42;\n\t\t}\n\t\tbax = 4;\n\t}\n\t" - + "if (a)\n\t\tfoo();\n}"); - JSSharedData.OUTPUT_JSDOC = true; - } - @Test public void testDefaultParameter_NoBody() { @@ -207,29 +158,6 @@ public class TestGoogEmiter extends Test JSSharedData.OUTPUT_JSDOC = true; } - @Ignore - @Test - public void testDefaultParameter_NoBody_Alternate() - { - // (erikdebruin) this tests the 'alternate' handling of default values - /* - foo.bar.A.method1 = function(bar, bax) { - if (arguments.length < 2) { - if (arguments.length < 1) { - bar = 42; - } - bax = 4; - } - } - */ - JSSharedData.OUTPUT_JSDOC = false; - IFunctionNode node = getMethod("function method1(bar:int = 42, bax:int = 4):void{\n}"); - visitor.visitFunction(node); - assertOut("foo.bar.A.prototype.method1 = function(bar, bax) {\n\tif (arguments.length < 2) {\n\t\t" - + "if (arguments.length < 1) {\n\t\t\tbar = 42;\n\t\t}\n\t\tbax = 4;\n\t}\n}"); - JSSharedData.OUTPUT_JSDOC = true; - } - @Override protected IBackend createBackend() { Modified: flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/goog/TestGoogMethodMembers.java URL: http://svn.apache.org/viewvc/flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/goog/TestGoogMethodMembers.java?rev=1430195&r1=1430194&r2=1430195&view=diff ============================================================================== --- flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/goog/TestGoogMethodMembers.java (original) +++ flex/whiteboard/mschmalle/falconjx/compiler.jx.tests/src/org/apache/flex/compiler/internal/js/codegen/goog/TestGoogMethodMembers.java Tue Jan 8 09:54:30 2013 @@ -21,10 +21,8 @@ package org.apache.flex.compiler.interna import org.apache.flex.compiler.clients.IBackend; import org.apache.flex.compiler.internal.as.codegen.TestMethodMembers; -import org.apache.flex.compiler.internal.js.codegen.JSSharedData; import org.apache.flex.compiler.internal.js.driver.goog.GoogBackend; import org.apache.flex.compiler.tree.as.IFunctionNode; -import org.junit.Ignore; import org.junit.Test; /** @@ -85,16 +83,6 @@ public class TestGoogMethodMembers exten assertOut("/**\n * @param {string=} bar\n * @return {number}\n */\nA.prototype.foo = function(bar) {\n\tbar = typeof bar !== 'undefined' ? bar : \"baz\";\n\treturn -1;\n}"); } - @Ignore - @Test - public void testMethod_withDefaultParameterTypeReturnType_Alternate() - { - // (erikdebruin) this tests the 'alternate' handling of default values - IFunctionNode node = getMethod("function foo(bar:String = \"baz\"):int{\treturn -1;}"); - visitor.visitFunction(node); - assertOut("/**\n * @param {string=} bar\n * @return {number}\n */\nA.prototype.foo = function(bar) {\n\tif (arguments.length < 1) {\n\t\tbar = \"baz\";\n\t}\n\treturn -1;\n}"); - } - @Override @Test public void testMethod_withMultipleDefaultParameterTypeReturnType() @@ -104,16 +92,6 @@ public class TestGoogMethodMembers exten assertOut("/**\n * @param {string} bar\n * @param {number=} baz\n * @return {number}\n */\nA.prototype.foo = function(bar, baz) {\n\tbaz = typeof baz !== 'undefined' ? baz : null;\n\treturn -1;\n}"); } - @Ignore - @Test - public void testMethod_withMultipleDefaultParameterTypeReturnType_Alternate() - { - // (erikdebruin) this tests the 'alternate' handling of default values - IFunctionNode node = getMethod("function foo(bar:String, baz:int = null):int{\treturn -1;}"); - visitor.visitFunction(node); - assertOut("/**\n * @param {string} bar\n * @param {number=} baz\n * @return {number}\n */\nA.prototype.foo = function(bar, baz) {\n\tif (arguments.length < 2) {\n\t\tbaz = null;\n\t}\n\treturn -1;\n}"); - } - @Override @Test public void testMethod_withRestParameterTypeReturnType() Added: flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDDocEmitter.java URL: http://svn.apache.org/viewvc/flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDDocEmitter.java?rev=1430195&view=auto ============================================================================== --- flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDDocEmitter.java (added) +++ flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDDocEmitter.java Tue Jan 8 09:54:30 2013 @@ -0,0 +1,35 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.flex.compiler.internal.js.codegen.amd; + +import org.apache.flex.compiler.internal.js.codegen.JSDocEmitter; +import org.apache.flex.compiler.js.codegen.IJSEmitter; +import org.apache.flex.compiler.js.codegen.amd.IJSAMDDocEmitter; + +public class JSAMDDocEmitter extends JSDocEmitter implements IJSAMDDocEmitter +{ + + public JSAMDDocEmitter(IJSEmitter emitter) + { + super(emitter); + // TODO Auto-generated constructor stub + } + +} Propchange: flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDDocEmitter.java ------------------------------------------------------------------------------ svn:eol-style = native Added: flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDEmitter.java URL: http://svn.apache.org/viewvc/flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDEmitter.java?rev=1430195&view=auto ============================================================================== --- flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDEmitter.java (added) +++ flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDEmitter.java Tue Jan 8 09:54:30 2013 @@ -0,0 +1,240 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.flex.compiler.internal.js.codegen.amd; + +import java.io.FilterWriter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.flex.compiler.common.ASModifier; +import org.apache.flex.compiler.definitions.IClassDefinition; +import org.apache.flex.compiler.definitions.ITypeDefinition; +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; +import org.apache.flex.compiler.js.codegen.amd.IJSAMDEmitter; +import org.apache.flex.compiler.problems.ICompilerProblem; +import org.apache.flex.compiler.tree.as.IClassNode; +import org.apache.flex.compiler.tree.as.IDefinitionNode; +import org.apache.flex.compiler.tree.as.IFunctionNode; +import org.apache.flex.compiler.tree.as.IPackageNode; +import org.apache.flex.compiler.tree.as.IParameterNode; +import org.apache.flex.compiler.tree.as.IScopedNode; +import org.apache.flex.compiler.tree.as.ITypeNode; + +/** + * Concrete implementation of the 'AMD' JavaScript production. + * + * @author Michael Schmalle + */ +public class JSAMDEmitter extends JSEmitter implements IJSAMDEmitter +{ + IJSAMDDocEmitter getDoc() + { + return (IJSAMDDocEmitter) getDocEmitter(); + } + + //-------------------------------------------------------------------------- + // + //-------------------------------------------------------------------------- + + public void emitJSDocPackgeHeader(IPackageNode node) + { + // getDocEmitter().emmitPackageHeader(node); + } + + //-------------------------------------------------------------------------- + // + //-------------------------------------------------------------------------- + + @Override + public void emitConstructor(IFunctionNode node) + { + IClassDefinition definition = getClassDefinition(node); + + FunctionNode fn = (FunctionNode) node; + fn.parseFunctionBody(new ArrayList()); + + emitJSDocConstructor(node, getWalker().getProject()); + + String qname = definition.getQualifiedName(); + write(qname); + write(" "); + write("="); + write(" "); + write("function"); + emitParamters(node.getParameterNodes()); + emitMethodScope(node.getScopedNode()); + } + + @Override + public void emitMethod(IFunctionNode node) + { + if (node.isConstructor()) + { + emitConstructor(node); + return; + } + + IClassDefinition definition = getClassDefinition(node); + + emitJSDoc(node, getWalker().getProject(), false, definition); + FunctionNode fn = (FunctionNode) node; + fn.parseFunctionBody(new ArrayList()); + + String qname = getTypeDefinition(node).getQualifiedName(); + if (qname != null && !qname.equals("")) + { + write(qname); + write("."); + if (!fn.hasModifier(ASModifier.STATIC)) + write("prototype."); + } + + emitMemberName(node); + write(" "); + write("="); + write(" "); + write("function"); + emitParamters(node.getParameterNodes()); + emitMethodScope(node.getScopedNode()); + } + + @Override + public void emitFunctionBlockHeader(IFunctionNode node) + { + emitDefaultParameterCodeBlock(node); + } + + private void emitDefaultParameterCodeBlock(IFunctionNode node) + { + // TODO (mschmalle) test for ... rest + // if default parameters exist, produce the init code + IParameterNode[] pnodes = node.getParameterNodes(); + Map defaults = getDefaults(pnodes); + if (pnodes.length == 0) + return; + + final StringBuilder code = new StringBuilder(); + if (defaults != null) + { + List parameters = new ArrayList( + defaults.values()); + Collections.reverse(parameters); + + int len = defaults.size(); + int numDefaults = 0; + // make the header in reverse order + for (IParameterNode pnode : parameters) + { + if (pnode != null) + { + code.append(getIndent(numDefaults)); + code.append("if (arguments.length < " + len + ") {\n"); + numDefaults++; + } + len--; + } + + Collections.reverse(parameters); + for (IParameterNode pnode : parameters) + { + if (pnode != null) + { + code.append(getIndent(numDefaults)); + code.append(pnode.getName()); + code.append(" = "); + code.append(pnode.getDefaultValue()); + code.append(";\n"); + code.append(getIndent(numDefaults - 1)); + code.append("}"); + if (numDefaults > 1) + code.append("\n"); + numDefaults--; + } + } + IScopedNode sbn = node.getScopedNode(); + boolean hasBody = sbn.getChildCount() > 0; + // adds the current block indent to the generated code + String indent = getIndent(getCurrentIndent() + (!hasBody ? 1 : 0)); + String result = code.toString().replaceAll("\n", "\n" + indent); + // if the block dosn't have a body (children), need to add indent to head + if (!hasBody) + result = indent + result; + // have to add newline after the replace or we get an extra indent + result += "\n"; + write(result); + } + } + + @Override + public void emitParameter(IParameterNode node) + { + // only the name gets output in JS + getWalker().walk(node.getNameExpressionNode()); + } + + public JSAMDEmitter(FilterWriter out) + { + super(out); + } + + private Map getDefaults(IParameterNode[] nodes) + { + Map result = new HashMap(); + int i = 0; + boolean hasDefaults = false; + for (IParameterNode node : nodes) + { + if (node.hasDefaultValue()) + { + hasDefaults = true; + result.put(i, node); + } + else + { + result.put(i, null); + } + i++; + } + + if (!hasDefaults) + return null; + + return result; + } + + private static ITypeDefinition getTypeDefinition(IDefinitionNode node) + { + ITypeNode tnode = (ITypeNode) node.getAncestorOfType(ITypeNode.class); + return (ITypeDefinition) tnode.getDefinition(); + } + + private static IClassDefinition getClassDefinition(IDefinitionNode node) + { + IClassNode tnode = (IClassNode) node + .getAncestorOfType(IClassNode.class); + return tnode.getDefinition(); + } + +} Propchange: flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDEmitter.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/goog/JSGoogDocEmitter.java URL: http://svn.apache.org/viewvc/flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/goog/JSGoogDocEmitter.java?rev=1430195&r1=1430194&r2=1430195&view=diff ============================================================================== --- flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/goog/JSGoogDocEmitter.java (original) +++ flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/goog/JSGoogDocEmitter.java Tue Jan 8 09:54:30 2013 @@ -1,3 +1,22 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.flex.compiler.internal.js.codegen.goog; import org.apache.flex.compiler.definitions.IClassDefinition; Added: flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/driver/amd/AMDBackend.java URL: http://svn.apache.org/viewvc/flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/driver/amd/AMDBackend.java?rev=1430195&view=auto ============================================================================== --- flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/driver/amd/AMDBackend.java (added) +++ flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/driver/amd/AMDBackend.java Tue Jan 8 09:54:30 2013 @@ -0,0 +1,52 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.flex.compiler.internal.js.driver.amd; + +import java.io.FilterWriter; + +import org.apache.flex.compiler.as.codegen.IASEmitter; +import org.apache.flex.compiler.as.codegen.IDocEmitter; +import org.apache.flex.compiler.clients.IBackend; +import org.apache.flex.compiler.internal.js.codegen.amd.JSAMDDocEmitter; +import org.apache.flex.compiler.internal.js.codegen.amd.JSAMDEmitter; +import org.apache.flex.compiler.internal.js.driver.JSBackend; +import org.apache.flex.compiler.js.codegen.IJSEmitter; + +/** + * A concrete implementation of the {@link IBackend} API for 'AMD' code production. + * + * @author Michael Schmalle + */ +public class AMDBackend extends JSBackend +{ + @Override + public IDocEmitter createDocEmitter(IASEmitter emitter) + { + return new JSAMDDocEmitter((IJSEmitter) emitter); + } + + @Override + protected IJSEmitter createEmitter(FilterWriter out) + { + IJSEmitter emitter = new JSAMDEmitter(out); + emitter.setDocEmitter(createDocEmitter(emitter)); + return emitter; + } +} Propchange: flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/internal/js/driver/amd/AMDBackend.java ------------------------------------------------------------------------------ svn:eol-style = native Added: flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/js/codegen/amd/IJSAMDDocEmitter.java URL: http://svn.apache.org/viewvc/flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/js/codegen/amd/IJSAMDDocEmitter.java?rev=1430195&view=auto ============================================================================== --- flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/js/codegen/amd/IJSAMDDocEmitter.java (added) +++ flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/js/codegen/amd/IJSAMDDocEmitter.java Tue Jan 8 09:54:30 2013 @@ -0,0 +1,40 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.flex.compiler.js.codegen.amd; + +import org.apache.flex.compiler.js.codegen.IJSDocEmitter; + +/** + * The {@link IJSAMDDocEmitter} interface allows the abstraction of JavaScript + * document comments to be emitted per tag. + *

+ * The purpose of the API is to clamp emitted output to JavaScript doc tags. The + * output can be multiline but is specific to one tag. This allows a full + * comment to be created without worrying about how to assemble the tags. + *

+ * TODO (mschmalle) Might make a comment API and tag API so comments are not + * dependent on tag creation IE IJSDocEmitter and IJSDocTagEmitter + * + * @author Michael Schmalle + */ +public interface IJSAMDDocEmitter extends IJSDocEmitter +{ + +} Propchange: flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/js/codegen/amd/IJSAMDDocEmitter.java ------------------------------------------------------------------------------ svn:eol-style = native Added: flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/js/codegen/amd/IJSAMDEmitter.java URL: http://svn.apache.org/viewvc/flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/js/codegen/amd/IJSAMDEmitter.java?rev=1430195&view=auto ============================================================================== --- flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/js/codegen/amd/IJSAMDEmitter.java (added) +++ flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/js/codegen/amd/IJSAMDEmitter.java Tue Jan 8 09:54:30 2013 @@ -0,0 +1,33 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.flex.compiler.js.codegen.amd; + +import org.apache.flex.compiler.js.codegen.IJSEmitter; + +/** + * The {@link IJSAMDEmitter} interface allows abstraction between the base + * JavaScript and the AMD specific IJSAMDEmitter. + * + * @author Michael Schmalle + */ +public interface IJSAMDEmitter extends IJSEmitter +{ + +} Propchange: flex/whiteboard/mschmalle/falconjx/compiler.jx/src/org/apache/flex/compiler/js/codegen/amd/IJSAMDEmitter.java ------------------------------------------------------------------------------ svn:eol-style = native