Look for [ArrayElementType] on class (not property) when a class is accessed via numeric dynamic access such as [i] and resolve the results to that type. Allows dynamic array access to Float32Array and things like that
Project: http://git-wip-us.apache.org/repos/asf/flex-falcon/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-falcon/commit/ed2c72a6 Tree: http://git-wip-us.apache.org/repos/asf/flex-falcon/tree/ed2c72a6 Diff: http://git-wip-us.apache.org/repos/asf/flex-falcon/diff/ed2c72a6 Branch: refs/heads/feature-autobuild/maven-archetypes Commit: ed2c72a69113344d561471d93848d18ee654c275 Parents: 7a44e38 Author: Alex Harui <aha...@apache.org> Authored: Wed Sep 28 07:52:50 2016 -0700 Committer: Alex Harui <aha...@apache.org> Committed: Thu Sep 29 20:56:44 2016 -0700 ---------------------------------------------------------------------- .../codegen/js/jx/BinaryOperatorEmitter.java | 51 +++++++++++++++----- .../codegen/js/jx/VarDeclarationEmitter.java | 51 +++++++++++++++----- .../js/flexjs/TestFlexJSExpressions.java | 10 ++++ .../js/flexjs/TestFlexJSFieldMembers.java | 9 ++++ 4 files changed, 99 insertions(+), 22 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/ed2c72a6/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/jx/BinaryOperatorEmitter.java ---------------------------------------------------------------------- diff --git a/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/jx/BinaryOperatorEmitter.java b/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/jx/BinaryOperatorEmitter.java index 38d13fe..141949b 100644 --- a/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/jx/BinaryOperatorEmitter.java +++ b/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/jx/BinaryOperatorEmitter.java @@ -23,6 +23,8 @@ import org.apache.flex.compiler.codegen.ISubEmitter; import org.apache.flex.compiler.codegen.js.IJSEmitter; import org.apache.flex.compiler.constants.IASLanguageConstants; import org.apache.flex.compiler.definitions.IDefinition; +import org.apache.flex.compiler.definitions.metadata.IMetaTag; +import org.apache.flex.compiler.definitions.metadata.IMetaTagAttribute; import org.apache.flex.compiler.internal.codegen.as.ASEmitterTokens; import org.apache.flex.compiler.internal.codegen.js.JSSubEmitter; import org.apache.flex.compiler.internal.codegen.js.flexjs.JSFlexJSDocEmitter; @@ -31,6 +33,7 @@ import org.apache.flex.compiler.internal.codegen.js.flexjs.JSFlexJSEmitterTokens import org.apache.flex.compiler.internal.codegen.js.goog.JSGoogEmitterTokens; import org.apache.flex.compiler.internal.definitions.AccessorDefinition; import org.apache.flex.compiler.internal.projects.FlexJSProject; +import org.apache.flex.compiler.internal.tree.as.DynamicAccessNode; import org.apache.flex.compiler.internal.tree.as.FunctionCallNode; import org.apache.flex.compiler.internal.tree.as.IdentifierNode; import org.apache.flex.compiler.internal.tree.as.MemberAccessExpressionNode; @@ -270,18 +273,44 @@ public class BinaryOperatorEmitter extends JSSubEmitter implements boolean rightIsNumber = (rightDef != null && (rightDef.getQualifiedName().equals(IASLanguageConstants.Number) || rightDef.getQualifiedName().equals(IASLanguageConstants._int) || rightDef.getQualifiedName().equals(IASLanguageConstants.uint))); - if (leftIsNumber && !rightIsNumber && (rightDef == null || rightDef.getQualifiedName().equals(IASLanguageConstants.ANY_TYPE)) - && rNode.getNodeID() == ASTNodeID.FunctionCallID) + if (leftIsNumber && !rightIsNumber && (rightDef == null || rightDef.getQualifiedName().equals(IASLanguageConstants.ANY_TYPE))) { - IExpressionNode fnNameNode = ((FunctionCallNode)rNode).getNameNode(); - if (fnNameNode.getNodeID() == ASTNodeID.MemberAccessExpressionID) - { - MemberAccessExpressionNode mae = (MemberAccessExpressionNode)fnNameNode; - IExpressionNode rightNode = mae.getRightOperandNode(); - rightIsNumber = rightNode.getNodeID() == ASTNodeID.IdentifierID && - ((IdentifierNode)rightNode).getName().equals("length") && - fjs.isXMLList(mae); - } + if (rNode.getNodeID() == ASTNodeID.FunctionCallID) + { + IExpressionNode fnNameNode = ((FunctionCallNode)rNode).getNameNode(); + if (fnNameNode.getNodeID() == ASTNodeID.MemberAccessExpressionID) + { + MemberAccessExpressionNode mae = (MemberAccessExpressionNode)fnNameNode; + IExpressionNode rightNode = mae.getRightOperandNode(); + rightIsNumber = rightNode.getNodeID() == ASTNodeID.IdentifierID && + ((IdentifierNode)rightNode).getName().equals("length") && + fjs.isXMLList(mae); + } + } + else if (rNode.getNodeID() == ASTNodeID.ArrayIndexExpressionID) + { + DynamicAccessNode dyn = (DynamicAccessNode)rNode; + IDefinition lDef = dyn.getLeftOperandNode().resolveType(getProject()); + IDefinition rDef = dyn.getRightOperandNode().resolveType(getProject()); + // numeric indexing? + if (rDef.getQualifiedName().equals(IASLanguageConstants.Number)) + { + IMetaTag[] metas = lDef.getAllMetaTags(); + for (IMetaTag meta : metas) + { + if (meta.getTagName().equals("ArrayElementType")) + { + IMetaTagAttribute[] attrs = meta.getAllAttributes(); + for (IMetaTagAttribute attr : attrs) + { + String t = attr.getValue(); + if (t.equals(IASLanguageConstants.Number)) + rightIsNumber = true; + } + } + } + } + } } String coercion = (leftIsNumber && !rightIsNumber && isAssignment) ? "Number(" : ""; if (isAssignment && leftDef != null && leftDef.getQualifiedName().equals(IASLanguageConstants.String)) http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/ed2c72a6/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/jx/VarDeclarationEmitter.java ---------------------------------------------------------------------- diff --git a/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/jx/VarDeclarationEmitter.java b/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/jx/VarDeclarationEmitter.java index 3d66f97..4e35e39 100644 --- a/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/jx/VarDeclarationEmitter.java +++ b/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/jx/VarDeclarationEmitter.java @@ -23,10 +23,13 @@ import org.apache.flex.compiler.codegen.ISubEmitter; import org.apache.flex.compiler.codegen.js.IJSEmitter; import org.apache.flex.compiler.constants.IASLanguageConstants; import org.apache.flex.compiler.definitions.IDefinition; +import org.apache.flex.compiler.definitions.metadata.IMetaTag; +import org.apache.flex.compiler.definitions.metadata.IMetaTagAttribute; import org.apache.flex.compiler.internal.codegen.as.ASEmitterTokens; import org.apache.flex.compiler.internal.codegen.js.JSSubEmitter; import org.apache.flex.compiler.internal.codegen.js.flexjs.JSFlexJSEmitter; import org.apache.flex.compiler.internal.tree.as.ChainedVariableNode; +import org.apache.flex.compiler.internal.tree.as.DynamicAccessNode; import org.apache.flex.compiler.internal.tree.as.FunctionCallNode; import org.apache.flex.compiler.internal.tree.as.IdentifierNode; import org.apache.flex.compiler.internal.tree.as.MemberAccessExpressionNode; @@ -119,18 +122,44 @@ public class VarDeclarationEmitter extends JSSubEmitter implements boolean valIsNumber = (avdef != null && (avdef.getQualifiedName().equals(IASLanguageConstants.Number) || avdef.getQualifiedName().equals(IASLanguageConstants._int) || avdef.getQualifiedName().equals(IASLanguageConstants.uint))); - if (varIsNumber && !valIsNumber && (avdef == null || avdef.getQualifiedName().equals(IASLanguageConstants.ANY_TYPE)) - && avnode.getNodeID() == ASTNodeID.FunctionCallID) + if (varIsNumber && !valIsNumber && (avdef == null || avdef.getQualifiedName().equals(IASLanguageConstants.ANY_TYPE))) { - IExpressionNode fnNameNode = ((FunctionCallNode)avnode).getNameNode(); - if (fnNameNode.getNodeID() == ASTNodeID.MemberAccessExpressionID) - { - MemberAccessExpressionNode mae = (MemberAccessExpressionNode)fnNameNode; - IExpressionNode rightNode = mae.getRightOperandNode(); - valIsNumber = rightNode.getNodeID() == ASTNodeID.IdentifierID && - ((IdentifierNode)rightNode).getName().equals("length") && - fjs.isXMLList(mae); - } + if (avnode.getNodeID() == ASTNodeID.FunctionCallID) + { + IExpressionNode fnNameNode = ((FunctionCallNode)avnode).getNameNode(); + if (fnNameNode.getNodeID() == ASTNodeID.MemberAccessExpressionID) + { + MemberAccessExpressionNode mae = (MemberAccessExpressionNode)fnNameNode; + IExpressionNode rightNode = mae.getRightOperandNode(); + valIsNumber = rightNode.getNodeID() == ASTNodeID.IdentifierID && + ((IdentifierNode)rightNode).getName().equals("length") && + fjs.isXMLList(mae); + } + } + else if (avnode.getNodeID() == ASTNodeID.ArrayIndexExpressionID) + { + DynamicAccessNode dyn = (DynamicAccessNode)avnode; + IDefinition leftDef = dyn.getLeftOperandNode().resolveType(getProject()); + IDefinition rightDef = dyn.getRightOperandNode().resolveType(getProject()); + // numeric indexing? + if (rightDef.getQualifiedName().equals(IASLanguageConstants.Number)) + { + IMetaTag[] metas = leftDef.getAllMetaTags(); + for (IMetaTag meta : metas) + { + if (meta.getTagName().equals("ArrayElementType")) + { + IMetaTagAttribute[] attrs = meta.getAllAttributes(); + for (IMetaTagAttribute attr : attrs) + { + String t = attr.getValue(); + if (t.equals(IASLanguageConstants.Number)) + valIsNumber = true; + } + } + } + } + } } String coercion = ""; if (varIsNumber && !valIsNumber) http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/ed2c72a6/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSExpressions.java ---------------------------------------------------------------------- diff --git a/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSExpressions.java b/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSExpressions.java index c9594c5..e46d9ca 100644 --- a/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSExpressions.java +++ b/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSExpressions.java @@ -653,6 +653,16 @@ public class TestFlexJSExpressions extends TestGoogExpressions } @Test + public void testVisitBinaryOperatorNode_ArrayElementType() + { + IBinaryOperatorNode node = (IBinaryOperatorNode) getNode( + "public class B {public function d() { var b:Number; b = c[0]; var c:C;}}\n[ArrayElementType(\"Number\")]\nclass C {}", + IBinaryOperatorNode.class, WRAP_LEVEL_PACKAGE); + asBlockWalker.visitBinaryOperator(node); + assertOut("b = c[0]"); + } + + @Test public void testNamedFunctionAsArgument() { IFunctionNode node = (IFunctionNode) getNode( http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/ed2c72a6/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSFieldMembers.java ---------------------------------------------------------------------- diff --git a/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSFieldMembers.java b/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSFieldMembers.java index ef54f29..4725485 100644 --- a/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSFieldMembers.java +++ b/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSFieldMembers.java @@ -81,6 +81,15 @@ public class TestFlexJSFieldMembers extends TestGoogFieldMembers assertOut("/**\n * @export\n * @type {number}\n */\nFalconTest_A.prototype.foo = -420"); } + @Test + public void testVariable_withTypeValue_ArrayElementType() + { + IVariableNode node = (IVariableNode)getNode("public class A { function foobar():void {var foo:Number = bar[0];var bar:B;}}\n[ArrayElementType(\"Number\")]\nclass B {}", + IVariableNode.class, WRAP_LEVEL_PACKAGE); + asBlockWalker.visitVariable(node); + assertOut("var /** @type {number} */ foo = bar[0]"); + } + @Override @Test public void testField_withNamespaceTypeValue()