Repository: flex-falcon Updated Branches: refs/heads/develop 28398c9a6 -> bb8b743a0
Implemented the @enum tag to generate enumerated classes. - var object literals with @enum get created as a class and each property string gets converted to a class constant of the type expression, Number is the default, doesn't really matter what the values are right now since there is no inlining in this compiler. Project: http://git-wip-us.apache.org/repos/asf/flex-falcon/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-falcon/commit/bb8b743a Tree: http://git-wip-us.apache.org/repos/asf/flex-falcon/tree/bb8b743a Diff: http://git-wip-us.apache.org/repos/asf/flex-falcon/diff/bb8b743a Branch: refs/heads/develop Commit: bb8b743a08c4b9cea8c7d128af51d1f375d4ab80 Parents: 28398c9 Author: Michael Schmalle <[email protected]> Authored: Sun Jun 14 13:25:57 2015 -0400 Committer: Michael Schmalle <[email protected]> Committed: Sun Jun 14 13:25:57 2015 -0400 ---------------------------------------------------------------------- .../codegen/externals/TestAnnotationEnum.java | 64 ++++++++++++++++++++ .../externals_unit_tests/annotation_enum.js | 48 +++++++++++++++ .../externals/pass/NamespaceResolutionPass.java | 4 ++ .../externals/reference/ClassReference.java | 50 ++++++++++++++- .../externals/reference/FieldReference.java | 47 +++++++++++++- .../externals/reference/ReferenceModel.java | 13 ++++ 6 files changed, 223 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/bb8b743a/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/externals/TestAnnotationEnum.java ---------------------------------------------------------------------- diff --git a/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/externals/TestAnnotationEnum.java b/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/externals/TestAnnotationEnum.java new file mode 100644 index 0000000..c43cd8b --- /dev/null +++ b/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/externals/TestAnnotationEnum.java @@ -0,0 +1,64 @@ +/* + * + * 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.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 TestAnnotationEnum extends ExternalsTestBase +{ + @Test + public void test_class_creation() throws IOException + { + compile("annotation_enum.js"); + + ClassReference FontFaceLoadStatus = model.getClassReference("FontFaceLoadStatus"); + ClassReference FontFaceSetLoadStatus = model.getClassReference("FontFaceSetLoadStatus"); + assertNotNull(FontFaceLoadStatus); + assertNotNull(FontFaceSetLoadStatus); + + assertTrue(FontFaceLoadStatus.hasStaticField("ERROR")); + assertTrue(FontFaceLoadStatus.hasStaticField("LOADED")); + assertTrue(FontFaceLoadStatus.hasStaticField("LOADING")); + assertTrue(FontFaceLoadStatus.hasStaticField("UNLOADED")); + + assertTrue(FontFaceSetLoadStatus.hasStaticField("FOO_LOADED")); + assertTrue(FontFaceSetLoadStatus.hasStaticField("FOO_LOADING")); + + assertTrue(FontFaceLoadStatus.getField("ERROR").isStatic()); + assertTrue(FontFaceLoadStatus.getField("ERROR").isConst()); + + // TODO check values and value type IE String, Number + + //String emit1 = client.getEmitter().emit(FontFaceLoadStatus); + //String emit2 = client.getEmitter().emit(FontFaceSetLoadStatus); + } + + @Override + protected void configure(ExternCConfiguration config) throws IOException + { + } +} http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/bb8b743a/compiler.jx.tests/test-files/externals_unit_tests/annotation_enum.js ---------------------------------------------------------------------- diff --git a/compiler.jx.tests/test-files/externals_unit_tests/annotation_enum.js b/compiler.jx.tests/test-files/externals_unit_tests/annotation_enum.js new file mode 100644 index 0000000..bc9276c --- /dev/null +++ b/compiler.jx.tests/test-files/externals_unit_tests/annotation_enum.js @@ -0,0 +1,48 @@ +/* + * + * 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. + * + */ + +/** + * @enum {string} + * @see http://dev.w3.org/csswg/css-font-loading/#enumdef-fontfaceloadstatus + */ +var FontFaceLoadStatus = { + ERROR: 'error', + LOADED: 'loaded', + LOADING: 'loading', + UNLOADED: 'unloaded' +}; + +/** + * @enum + * @see http://dev.w3.org/csswg/css-font-loading/#enumdef-fontfacesetloadstatus + */ +var FontFaceSetLoadStatus = { + FOO_LOADED: 'loaded', + FOO_LOADING: 'loading' +}; + + + + + + + + + + http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/bb8b743a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/NamespaceResolutionPass.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/NamespaceResolutionPass.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/NamespaceResolutionPass.java index a0711ad..1ccd3c4 100644 --- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/NamespaceResolutionPass.java +++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/NamespaceResolutionPass.java @@ -66,6 +66,10 @@ public class NamespaceResolutionPass extends AbstractCompilerPass model.addClass(child, name.getQualifiedName()); } } + else if (comment != null && comment.hasEnumParameterType()) + { + model.addEnum(child, name.getQualifiedName()); + } } else if (child.isExprResult()) { http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/bb8b743a/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 b11c6a0..ff96fe6 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 @@ -29,6 +29,7 @@ import java.util.Map.Entry; import org.apache.flex.compiler.internal.codegen.externals.utils.DebugLogUtils; import org.apache.flex.compiler.internal.codegen.externals.utils.JSTypeUtils; +import org.apache.flex.compiler.internal.codegen.externals.utils.TypeUtils; import com.google.javascript.rhino.JSDocInfo; import com.google.javascript.rhino.JSDocInfoBuilder; @@ -39,6 +40,7 @@ import com.google.javascript.rhino.jstype.JSType; public class ClassReference extends BaseReference { private boolean isFinal; + private int enumConstantCounter = 0; private List<String> imports = new ArrayList<String>(); private MethodReference constructor; @@ -54,6 +56,16 @@ public class ClassReference extends BaseReference private boolean isNamespace; + public final int getEnumConstant() + { + return enumConstantCounter; + } + + public final void nextEnumConstant() + { + enumConstantCounter++; + } + public void setIsNamespace(boolean isNamespace) { this.isNamespace = isNamespace; @@ -122,7 +134,40 @@ public class ClassReference extends BaseReference functionNode = null; paramListNode = null; - if (comment.getTypedefType() != null) + if (comment.hasEnumParameterType()) + { + /* + VAR 35 [jsdoc_info: JSDocInfo] + NAME FontFaceSetLoadStatus + OBJECTLIT + STRING_KEY LOADED + STRING loaded + STRING_KEY LOADING + STRING loading + */ + JSTypeExpression enumParameterType = comment.getEnumParameterType(); + String overrideStringType = TypeUtils.transformType(getModel().evaluate( + enumParameterType).toAnnotationString()); + + Node objLit = node.getFirstChild().getFirstChild(); + for (Node stringKey : objLit.children()) + { + if (stringKey.isStringKey()) + { + Node valueNode = stringKey.getFirstChild(); + + JSDocInfoBuilder b = new JSDocInfoBuilder(true); + JSDocInfo fieldComment = b.build(); + String fieldName = stringKey.getString(); + FieldReference field = addField(stringKey, fieldName, + fieldComment, true); + field.setConst(true); + field.setOverrideStringType(overrideStringType); + field.setConstantValueNode(valueNode); + } + } + } + else if (comment.getTypedefType() != null) { //System.out.println(node.toStringTree()); /* @@ -204,6 +249,8 @@ public class ClassReference extends BaseReference @Override public void emit(StringBuilder sb) { + enumConstantCounter = 0; + String packageName = getPackageName(); sb.append("package "); @@ -647,6 +694,7 @@ public class ClassReference extends BaseReference { fieldSet.getValue().emit(sb); sb.append("\n"); + nextEnumConstant(); } } http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/bb8b743a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/FieldReference.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/FieldReference.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/FieldReference.java index 8965c13..618a13a 100644 --- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/FieldReference.java +++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/FieldReference.java @@ -32,7 +32,9 @@ public class FieldReference extends MemberReference { private boolean isStatic; + private boolean isConst; private String overrideStringType; + private Node constantValueNode; public boolean isStatic() { @@ -44,11 +46,26 @@ public class FieldReference extends MemberReference this.isStatic = isStatic; } + public boolean isConst() + { + return isConst; + } + + public void setConst(boolean isConst) + { + this.isConst = isConst; + } + public void setOverrideStringType(String overrideStringType) { this.overrideStringType = overrideStringType; } + public void setConstantValueNode(Node constantValueNode) + { + this.constantValueNode = constantValueNode; + } + public String toTypeAnnotationString() { JSType jsType = getModel().evaluate(getComment().getType()); @@ -109,14 +126,39 @@ public class FieldReference extends MemberReference private void emitVar(StringBuilder sb) { String staticValue = (isStatic) ? "static " : ""; + String constVarValue = (isConst) ? "const " : "var "; String type = toTypeString(); if (type.indexOf("|") != -1 || type.indexOf("?") != -1) type = "*"; sb.append(indent); - sb.append("public " + staticValue + "var " + getQualifiedName() + ":" - + type + ";\n"); + sb.append("public "); + sb.append(staticValue); + sb.append(constVarValue); + sb.append(getQualifiedName()); + sb.append(":"); + sb.append(type); + if (isConst) + { + emitConstValue(sb); + } + sb.append(";\n"); + } + + private void emitConstValue(StringBuilder sb) + { + sb.append(" = "); + sb.append(toConstValue(constantValueNode)); + } + + private String toConstValue(Node node) + { + if (toTypeString().equals("Number")) + return Integer.toString(getClassReference().getEnumConstant()); + if (node.isString()) + return "'" + node.getString() + "'"; + return "undefined /* TODO type not set */"; } private String toTypeString() @@ -162,4 +204,5 @@ public class FieldReference extends MemberReference sb.append("\n"); } } + } http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/bb8b743a/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 7f9a342..52b64d6 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 @@ -136,6 +136,19 @@ public class ReferenceModel classes.put(qualifiedName, reference); } + public void addEnum(Node node, String qualifiedName) + { + if (classes.containsKey(qualifiedName)) + { + err("Duplicate class, @enum conflict [" + qualifiedName + "]"); + return; + } + + log("Model.addEnum(" + qualifiedName + ")"); + ClassReference reference = new ClassReference(this, node, qualifiedName); + classes.put(qualifiedName, reference); + } + public void addTypeDef(Node node, String qualifiedName) { if (typedefs.containsKey(qualifiedName))
