Repository: flex-falcon Updated Branches: refs/heads/develop c83fc99af -> 2a23f7bb9
Added Class contructor test and emitter now emits constructors with params. Project: http://git-wip-us.apache.org/repos/asf/flex-falcon/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-falcon/commit/2a23f7bb Tree: http://git-wip-us.apache.org/repos/asf/flex-falcon/tree/2a23f7bb Diff: http://git-wip-us.apache.org/repos/asf/flex-falcon/diff/2a23f7bb Branch: refs/heads/develop Commit: 2a23f7bb95866d14867f84a0886adf10cfa72038 Parents: c83fc99 Author: Michael Schmalle <[email protected]> Authored: Thu Jun 11 16:54:38 2015 -0400 Committer: Michael Schmalle <[email protected]> Committed: Thu Jun 11 16:54:38 2015 -0400 ---------------------------------------------------------------------- .../codegen/externals/ExternalsTestBase.java | 9 ++ .../codegen/externals/TestConstructor.java | 88 ++++++++++++++++ .../externals_unit_tests/constructor_params.js | 67 ++++++++++++ .../apache/flex/compiler/clients/EXTERNC.java | 10 ++ .../externals/pass/ReferenceCompiler.java | 19 ++-- .../externals/reference/ClassReference.java | 103 ++++++++++++++++++- .../externals/reference/ConstantReference.java | 20 +++- .../externals/reference/MethodReference.java | 39 ++++++- .../externals/reference/ReferenceModel.java | 12 +-- .../codegen/externals/utils/JSTypeUtils.java | 4 + 10 files changed, 348 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/2a23f7bb/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/externals/ExternalsTestBase.java ---------------------------------------------------------------------- diff --git a/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/externals/ExternalsTestBase.java b/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/externals/ExternalsTestBase.java index 1ec1727..1e1ff73 100644 --- a/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/externals/ExternalsTestBase.java +++ b/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/externals/ExternalsTestBase.java @@ -24,6 +24,7 @@ import java.io.IOException; import org.apache.flex.compiler.clients.EXTERNC; import org.apache.flex.compiler.clients.ExternCConfiguration; +import org.apache.flex.compiler.internal.codegen.externals.reference.MethodReference; import org.apache.flex.compiler.internal.codegen.externals.reference.ReferenceModel; import org.apache.flex.utils.FilenameNormalization; import org.junit.After; @@ -31,6 +32,7 @@ import org.junit.Assert; import org.junit.Before; import com.google.javascript.jscomp.Result; +import com.google.javascript.rhino.jstype.JSType; public abstract class ExternalsTestBase { @@ -73,4 +75,11 @@ public abstract class ExternalsTestBase return result; } + protected JSType evaluateParam(MethodReference method, String paramName) + { + JSType jsType = method.getComment().getParameterType(paramName).evaluate( + null, client.getCompiler().getJSCompiler().getTypeRegistry()); + return jsType; + } + } http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/2a23f7bb/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/externals/TestConstructor.java ---------------------------------------------------------------------- diff --git a/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/externals/TestConstructor.java b/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/externals/TestConstructor.java new file mode 100644 index 0000000..168fee5 --- /dev/null +++ b/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/externals/TestConstructor.java @@ -0,0 +1,88 @@ +/* + * + * 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.codegen.externals; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; + +import org.apache.flex.compiler.clients.ExternCConfiguration; +import org.apache.flex.compiler.internal.codegen.externals.reference.ClassReference; +import org.junit.Test; + +public class TestConstructor extends ExternalsTestBase +{ + + @Test + public void test_constructor_args() throws IOException + { + compile("constructor_params.js"); + + ClassReference FooNoArgs = model.getClassReference("FooNoArgs"); + ClassReference FooOptArgs = model.getClassReference("FooOptArgs"); + ClassReference FooVarArgs = model.getClassReference("FooVarArgs"); + ClassReference FooOptVarArgs = model.getClassReference("FooOptVarArgs"); + + assertNotNull(FooNoArgs.getConstructor()); + assertNotNull(FooOptArgs.getConstructor()); + assertNotNull(FooVarArgs.getConstructor()); + assertNotNull(FooOptVarArgs.getConstructor()); + + assertEquals(0, FooNoArgs.getConstructor().getParameterNames().size()); + assertEquals(2, FooOptArgs.getConstructor().getParameterNames().size()); + assertEquals(2, FooVarArgs.getConstructor().getParameterNames().size()); + assertEquals(3, + FooOptVarArgs.getConstructor().getParameterNames().size()); + + assertFalse(FooOptArgs.getConstructor().getComment().getParameterType( + "arg1").isOptionalArg()); + assertTrue(FooOptArgs.getConstructor().getComment().getParameterType( + "opt_arg2").isOptionalArg()); + + assertFalse(FooVarArgs.getConstructor().getComment().getParameterType( + "arg1").isVarArgs()); + assertTrue(FooVarArgs.getConstructor().getComment().getParameterType( + "var_args").isVarArgs()); + + assertTrue(FooOptVarArgs.getConstructor().getComment().getParameterType( + "opt_arg2").isOptionalArg()); + assertTrue(FooOptVarArgs.getConstructor().getComment().getParameterType( + "var_args").isVarArgs()); + + assertEquals( + "number", + evaluateParam(FooOptVarArgs.getConstructor(), "arg1").toAnnotationString()); + assertEquals( + "*", + evaluateParam(FooOptVarArgs.getConstructor(), "opt_arg2").toAnnotationString()); + assertEquals( + "*", + evaluateParam(FooOptVarArgs.getConstructor(), "var_args").toAnnotationString()); + } + + @Override + protected void configure(ExternCConfiguration config) throws IOException + { + } + +} http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/2a23f7bb/compiler.jx.tests/test-files/externals_unit_tests/constructor_params.js ---------------------------------------------------------------------- diff --git a/compiler.jx.tests/test-files/externals_unit_tests/constructor_params.js b/compiler.jx.tests/test-files/externals_unit_tests/constructor_params.js new file mode 100644 index 0000000..fb0fad6 --- /dev/null +++ b/compiler.jx.tests/test-files/externals_unit_tests/constructor_params.js @@ -0,0 +1,67 @@ +/* + * + * 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. + * + */ + +/** + * A constructor with no args. + * + * @constructor + */ +function FooNoArgs() {} + +/** + * A constructor with arg and opt arg. + * + * @constructor + * @param {number} arg1 + * @param {*=} opt_arg2 + */ +function FooOptArgs(arg1, opt_arg2) {} + +/** + * A constructor with arg and var args. + * + * @constructor + * @param {number} arg1 + * @param {...*} var_args + */ +function FooVarArgs(arg1, var_args) {} + +/** + * A constructor with arg, opt arg and var args. + * + * @constructor + * @param {number} arg1 + * @param {*=} opt_arg2 + * @param {...*} var_args + */ +function FooOptVarArgs(arg1, opt_arg2, var_args) {} + +/** + * A constructor with no args. + * + * @constructor + */ +AssignFooNoArgs = function () {}; + +/** + * A constructor with no args. + * + * @constructor + */ +var VarAssignFooNoArgs = function () {}; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/2a23f7bb/compiler.jx/src/org/apache/flex/compiler/clients/EXTERNC.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/clients/EXTERNC.java b/compiler.jx/src/org/apache/flex/compiler/clients/EXTERNC.java index 0ff9a3e..7bb9b42 100644 --- a/compiler.jx/src/org/apache/flex/compiler/clients/EXTERNC.java +++ b/compiler.jx/src/org/apache/flex/compiler/clients/EXTERNC.java @@ -63,6 +63,16 @@ public class EXTERNC return model; } + public ReferenceCompiler getCompiler() + { + return compiler; + } + + public ReferenceEmitter getEmitter() + { + return emitter; + } + public EXTERNC(ExternCConfiguration configuration) { this.configuration = configuration; http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/2a23f7bb/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/ReferenceCompiler.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/ReferenceCompiler.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/ReferenceCompiler.java index 769d429..28e36fe 100644 --- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/ReferenceCompiler.java +++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/ReferenceCompiler.java @@ -42,9 +42,14 @@ public class ReferenceCompiler private ReferenceModel model; - private Compiler compiler; + private Compiler jscompiler; private JXCompilerOptions options; + public Compiler getJSCompiler() + { + return jscompiler; + } + public ReferenceCompiler(ReferenceModel model) { this.model = model; @@ -54,7 +59,7 @@ public class ReferenceCompiler private void initializeCompiler() { - compiler = new Compiler(); + jscompiler = new Compiler(); options = new JXCompilerOptions(); //options.setLanguageIn(LanguageMode.ECMASCRIPT6_TYPED); @@ -68,14 +73,14 @@ public class ReferenceCompiler options.setExternExports(false); options.addCustomPass(CustomPassExecutionTime.BEFORE_OPTIMIZATIONS, - new CollectTypesPass(model, compiler)); + new CollectTypesPass(model, jscompiler)); options.addCustomPass(CustomPassExecutionTime.BEFORE_OPTIMIZATIONS, - new AddMemberPass(model, compiler)); + new AddMemberPass(model, jscompiler)); //compiler.setErrorManager(testErrorManager); - compiler.initOptions(options); + jscompiler.initOptions(options); - model.setCompiler(compiler); + model.setCompiler(jscompiler); } public Result compile() throws IOException @@ -90,7 +95,7 @@ public class ReferenceCompiler sources.add(SourceFile.fromCode("[" + name + "]", source)); } - Result result = compiler.compile(EMPTY_EXTERNS, sources, options); + Result result = jscompiler.compile(EMPTY_EXTERNS, sources, options); if (!result.success) { http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/2a23f7bb/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ClassReference.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ClassReference.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ClassReference.java index 631202b..b733af4 100644 --- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ClassReference.java +++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ClassReference.java @@ -38,9 +38,22 @@ public class ClassReference extends BaseReference private boolean isFinal; + private MethodReference constructor; private Map<String, FieldReference> fields = new HashMap<String, FieldReference>(); private Map<String, MethodReference> methods = new HashMap<String, MethodReference>(); + private Node nameNode; + + private Node functionNode; + + @SuppressWarnings("unused") + private Node paramListNode; + + public MethodReference getConstructor() + { + return constructor; + } + public Map<String, FieldReference> getFields() { return fields; @@ -71,10 +84,89 @@ public class ClassReference extends BaseReference return getComment().isInterface(); } - public ClassReference(ReferenceModel model, Node node, String qualfiedName, - JSDocInfo comment) + /** + * + * @param model + * @param node (FUNCTION [NAME, PARAM_LIST, BLOCK]), or (ASSIGN [FUNCTION + * [NAME, PARAM_LIST, BLOCK]]) + * @param qualfiedName + * @param comment + */ + public ClassReference(ReferenceModel model, Node node, String qualfiedName) { - super(model, node, qualfiedName, comment); + super(model, node, qualfiedName, node.getJSDocInfo()); + + nameNode = null; + functionNode = null; + paramListNode = null; + + if (comment.getTypedefType() != null) + { + //System.out.println(node.toStringTree()); + /* + VAR 727 [jsdoc_info: JSDocInfo] [source_file: [w3c_rtc]] [length: 21] + NAME MediaConstraints 727 [source_file: [w3c_rtc]] [length: 16] + */ + } + else if (node.isFunction()) + { + /* + FUNCTION FooVarArgs 43 [jsdoc_info: JSDocInfo] + NAME FooVarArgs + PARAM_LIST + NAME arg1 + NAME var_args + BLOCK 43 + */ + nameNode = node.getChildAtIndex(0); + functionNode = node; + paramListNode = functionNode.getChildAtIndex(1); + } + else if (node.isVar()) + { + /* + VAR 67 [jsdoc_info: JSDocInfo] + NAME VarAssignFooNoArgs + FUNCTION + NAME + PARAM_LIST + BLOCK + */ + nameNode = node.getChildAtIndex(0); + functionNode = nameNode.getChildAtIndex(0); + try + { + paramListNode = functionNode.getChildAtIndex(1); + } + catch (Exception e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + else if (node.isAssign()) + { + /* + ASSIGN 60 [jsdoc_info: JSDocInfo] + NAME AssignFooNoArgs + FUNCTION + NAME + PARAM_LIST + BLOCK + */ + nameNode = node.getFirstChild(); + functionNode = node.getLastChild(); + // this is an anonymous function assignment, no name + //functionNameNode = functionNode.getChildAtIndex(0); + paramListNode = functionNode.getChildAtIndex(1); + } + + if (functionNode != null) + { + constructor = new MethodReference(model, this, functionNode, + getBaseName(), comment, false); + } + } public FieldReference addField(Node node, String fieldName, @@ -278,7 +370,10 @@ public class ClassReference extends BaseReference private void printConstructor(StringBuilder sb) { - sb.append(" native public function " + getQualifiedName() + "();\n"); + if (constructor != null) + { + constructor.emit(sb); + } } private void printImports() http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/2a23f7bb/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ConstantReference.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ConstantReference.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ConstantReference.java index 62a3fab..bc364c6 100644 --- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ConstantReference.java +++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ConstantReference.java @@ -19,6 +19,7 @@ package org.apache.flex.compiler.internal.codegen.externals.reference; +import java.io.File; import java.util.HashMap; import org.apache.flex.compiler.internal.codegen.externals.utils.JSTypeUtils; @@ -27,7 +28,7 @@ import com.google.javascript.rhino.JSDocInfo; import com.google.javascript.rhino.Node; import com.google.javascript.rhino.jstype.JSType; -public class ConstantReference extends ClassReference +public class ConstantReference extends BaseReference { @SuppressWarnings("unused") @@ -36,16 +37,29 @@ public class ConstantReference extends ClassReference public ConstantReference(ReferenceModel model, Node node, String qualifiedName, JSDocInfo comment) { - super(model, node, qualifiedName, comment); + super(model, node, qualifiedName, node.getJSDocInfo()); + + /* + VAR 70 [jsdoc_info: JSDocInfo] + NAME self [is_constant_var: 1] + */ } public ConstantReference(ReferenceModel model, Node node, String qualifiedName, JSDocInfo comment, JSType type) { - super(model, node, qualifiedName, comment); + super(model, node, qualifiedName, node.getJSDocInfo()); this.type = type; } + public File getFile(File asSourceRoot) + { + String packageName = ""; + + return new File(asSourceRoot, packageName + File.separator + + getQualifiedName() + ".as"); + } + @Override public void emit(StringBuilder sb) { http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/2a23f7bb/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/MethodReference.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/MethodReference.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/MethodReference.java index c70e3d6..e194f64 100644 --- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/MethodReference.java +++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/MethodReference.java @@ -19,6 +19,8 @@ package org.apache.flex.compiler.internal.codegen.externals.reference; +import java.util.Set; + import org.apache.flex.compiler.clients.ExternCConfiguration.ExcludedMemeber; import org.apache.flex.compiler.internal.codegen.externals.utils.FunctionUtils; @@ -42,6 +44,11 @@ public class MethodReference extends MemberReference return isStatic; } + public Set<String> getParameterNames() + { + return getComment().getParameterNames(); + } + public void setStatic(boolean isStatic) { this.isStatic = isStatic; @@ -52,12 +59,26 @@ public class MethodReference extends MemberReference { super(model, classReference, node, name, comment); this.isStatic = isStatic; - this.paramNode = node.getLastChild().getChildAtIndex(1); + + if (node.isFunction()) + { + this.paramNode = node.getChildAtIndex(1); + } + else + { + this.paramNode = node.getLastChild().getChildAtIndex(1); + } } @Override public void emit(StringBuilder sb) { + if (isConstructor()) + { + printConstructor(sb); + return; + } + if (isOverride()) return; @@ -119,6 +140,22 @@ public class MethodReference extends MemberReference override = null; } + private void printConstructor(StringBuilder sb) + { + printComment(sb); + + sb.append(" native public function "); + sb.append(getQualifiedName()); + sb.append(toPrameterString()); + sb.append(";"); + sb.append("\n"); + } + + public boolean isConstructor() + { + return getComment().isConstructor(); + } + private String transformReturnString() { return FunctionUtils.transformReturnString(getContext(), getComment()); http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/2a23f7bb/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ReferenceModel.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ReferenceModel.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ReferenceModel.java index 1ca540d..31900fd 100644 --- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ReferenceModel.java +++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ReferenceModel.java @@ -117,8 +117,7 @@ public class ReferenceModel System.out.println("Model.addClass(" + qName + ")"); - ClassReference reference = new ClassReference(this, node, qName, - node.getJSDocInfo()); + ClassReference reference = new ClassReference(this, node, qName); classes.put(qName, reference); } @@ -132,8 +131,7 @@ public class ReferenceModel System.out.println("Model.addTypeDef(" + qName + ")"); - ClassReference reference = new ClassReference(this, node, qName, - node.getJSDocInfo()); + ClassReference reference = new ClassReference(this, node, qName); typedefs.put(qName, reference); } @@ -147,8 +145,7 @@ public class ReferenceModel System.out.println("Model.addInterface(" + qName + ")"); - ClassReference reference = new ClassReference(this, node, qName, - node.getJSDocInfo()); + ClassReference reference = new ClassReference(this, node, qName); classes.put(qName, reference); } @@ -162,8 +159,7 @@ public class ReferenceModel System.out.println("Model.addFinalClass(" + qName + ")"); - ClassReference reference = new ClassReference(this, node, qName, - node.getJSDocInfo()); + ClassReference reference = new ClassReference(this, node, qName); reference.setFinal(true); classes.put(qName, reference); } http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/2a23f7bb/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/utils/JSTypeUtils.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/utils/JSTypeUtils.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/utils/JSTypeUtils.java index 815d9ee..0f2288f 100644 --- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/utils/JSTypeUtils.java +++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/utils/JSTypeUtils.java @@ -51,6 +51,10 @@ public class JSTypeUtils { return "Function /* " + type + " */"; } + else if (jsType.isRecordType()) + { + return "Object /* " + type + " */"; + } else { if (type.indexOf("Array<") == 0)
