Repository: flex-falcon Updated Branches: refs/heads/develop 6ec489b34 -> 151b03d58
FLEX-35114 improve Date class handling Project: http://git-wip-us.apache.org/repos/asf/flex-falcon/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-falcon/commit/151b03d5 Tree: http://git-wip-us.apache.org/repos/asf/flex-falcon/tree/151b03d5 Diff: http://git-wip-us.apache.org/repos/asf/flex-falcon/diff/151b03d5 Branch: refs/heads/develop Commit: 151b03d589ea6570cb120ed67d4e28f97bf89279 Parents: 6ec489b Author: Alex Harui <aha...@apache.org> Authored: Wed Nov 16 09:05:50 2016 -0800 Committer: Alex Harui <aha...@apache.org> Committed: Wed Nov 16 09:05:50 2016 -0800 ---------------------------------------------------------------------- .../codegen/js/jx/BinaryOperatorEmitter.java | 144 +++++++++---------- .../codegen/js/jx/MemberAccessEmitter.java | 10 +- .../js/flexjs/TestFlexJSExpressions.java | 10 ++ .../js/flexjs/TestFlexJSGlobalClasses.java | 35 ++++- 4 files changed, 117 insertions(+), 82 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/151b03d5/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 98ada1a..4dab0ea 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 @@ -38,6 +38,7 @@ 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; import org.apache.flex.compiler.internal.tree.as.UnaryOperatorAtNode; +import org.apache.flex.compiler.problems.AssignToReadOnlyPropertyProblem; import org.apache.flex.compiler.tree.ASTNodeID; import org.apache.flex.compiler.tree.as.IASNode; import org.apache.flex.compiler.tree.as.IBinaryOperatorNode; @@ -512,8 +513,42 @@ public class BinaryOperatorEmitter extends JSSubEmitter implements write(ASEmitterTokens.PAREN_CLOSE); } - private enum DateProperties + public static enum DatePropertiesGetters { + TIME("time", "getTime"), + FULLYEAR("fullYear", "getFullYear"), + MONTH("month", "getMonth"), + DATE("date", "getDate"), + FULLYEARUTC("fullYearUTC", "getUTCFullYear"), + MONTHUTC("monthUTC", "getUTCMonth"), + DATEUTC("dateUTC", "getUTCDate"), + HOURS("hours", "getHours"), + MINUTES("minutes", "getMinutes"), + SECONDS("seconds", "getSeconds"), + MILLISECONDS("milliseconds", "getMilliseconds"), + HOURSUTC("hoursUTC", "getUTCHours"), + MINUTESUTC("minutesUTC", "getUTCMinutes"), + SECONDSUTC("secondsUTC", "getUTCSeconds"), + MILLISECONDSUTC("millisecondsUTC", "getUTCMilliseconds"); + + DatePropertiesGetters(String value, String functionName) + { + this.value = value; + this.functionName = functionName; + } + + private String value; + private String functionName; + + public String getFunctionName() + { + return functionName; + } + } + + public static enum DatePropertiesSetters + { + TIME("time", "setTime"), FULLYEAR("fullYear", "setFullYear"), MONTH("month", "setMonth"), DATE("date", "setDate"), @@ -529,7 +564,7 @@ public class BinaryOperatorEmitter extends JSSubEmitter implements SECONDSUTC("secondsUTC", "setUTCSeconds"), MILLISECONDSUTC("millisecondsUTC", "setUTCMilliseconds"); - DateProperties(String value, String functionName) + DatePropertiesSetters(String value, String functionName) { this.value = value; this.functionName = functionName; @@ -548,86 +583,45 @@ public class BinaryOperatorEmitter extends JSSubEmitter implements { MemberAccessExpressionNode dateNode = (MemberAccessExpressionNode)leftSide; IIdentifierNode rightSide = (IIdentifierNode)dateNode.getRightOperandNode(); + String op = node.getOperator().getOperatorText(); + boolean isAssignment = op.contains("=") + && !op.contains("==") + && !(op.startsWith("<") || op.startsWith(">") || op + .startsWith("!")); getWalker().walk(dateNode.getLeftOperandNode()); String rightName = rightSide.getName(); - DateProperties prop = DateProperties.valueOf(rightName.toUpperCase()); - write(ASEmitterTokens.MEMBER_ACCESS); - write(prop.getFunctionName()); - write(ASEmitterTokens.PAREN_OPEN); - getWalker().walk(node.getRightOperandNode()); - switch (prop) + if (isAssignment) { - case FULLYEAR: - write(ASEmitterTokens.COMMA); - write(ASEmitterTokens.SPACE); - getWalker().walk(dateNode.getLeftOperandNode()); - write(ASEmitterTokens.MEMBER_ACCESS); - write("getMonth()"); - // fall through - case MONTH: - write(ASEmitterTokens.COMMA); - write(ASEmitterTokens.SPACE); - getWalker().walk(dateNode.getLeftOperandNode()); - write(ASEmitterTokens.MEMBER_ACCESS); - write("getDate()"); - break; - case FULLYEARUTC: - write(ASEmitterTokens.COMMA); - write(ASEmitterTokens.SPACE); - getWalker().walk(dateNode.getLeftOperandNode()); - write(ASEmitterTokens.MEMBER_ACCESS); - write("getMonthUTC()"); - // fall through - case MONTHUTC: - write(ASEmitterTokens.COMMA); - write(ASEmitterTokens.SPACE); - getWalker().walk(dateNode.getLeftOperandNode()); - write(ASEmitterTokens.MEMBER_ACCESS); - write("getDateUTC()"); - break; - case HOURS: - write(ASEmitterTokens.COMMA); - write(ASEmitterTokens.SPACE); - getWalker().walk(dateNode.getLeftOperandNode()); + DatePropertiesSetters prop = DatePropertiesSetters.valueOf(rightName.toUpperCase()); write(ASEmitterTokens.MEMBER_ACCESS); - write("getMinutes()"); - // fall through - case MINUTES: - write(ASEmitterTokens.COMMA); - write(ASEmitterTokens.SPACE); - getWalker().walk(dateNode.getLeftOperandNode()); - write(ASEmitterTokens.MEMBER_ACCESS); - write("getSeconds()"); - // fall through - case SECONDS: - write(ASEmitterTokens.COMMA); - write(ASEmitterTokens.SPACE); - getWalker().walk(dateNode.getLeftOperandNode()); - write(ASEmitterTokens.MEMBER_ACCESS); - write("getMilliseconds()"); - break; - case HOURSUTC: - write(ASEmitterTokens.COMMA); - write(ASEmitterTokens.SPACE); - getWalker().walk(dateNode.getLeftOperandNode()); + write(prop.getFunctionName()); + write(ASEmitterTokens.PAREN_OPEN); + if (op.length() > 1) + { + DatePropertiesGetters propGetter = DatePropertiesGetters.valueOf(rightName.toUpperCase()); + getWalker().walk(dateNode.getLeftOperandNode()); + write(ASEmitterTokens.MEMBER_ACCESS); + write(propGetter.getFunctionName()); + write(ASEmitterTokens.PAREN_OPEN); + write(ASEmitterTokens.PAREN_CLOSE); + write(ASEmitterTokens.SPACE); + write(op.substring(0, 1)); + write(ASEmitterTokens.SPACE); + } + getWalker().walk(node.getRightOperandNode()); + write(ASEmitterTokens.PAREN_CLOSE); + } + else + { + DatePropertiesGetters propGetter = DatePropertiesGetters.valueOf(rightName.toUpperCase()); write(ASEmitterTokens.MEMBER_ACCESS); - write("getUTCMinutes()"); - // fall through - case MINUTESUTC: - write(ASEmitterTokens.COMMA); + write(propGetter.getFunctionName()); + write(ASEmitterTokens.PAREN_OPEN); + write(ASEmitterTokens.PAREN_CLOSE); write(ASEmitterTokens.SPACE); - getWalker().walk(dateNode.getLeftOperandNode()); - write(ASEmitterTokens.MEMBER_ACCESS); - write("getUTCSeconds()"); - // fall through - case SECONDSUTC: - write(ASEmitterTokens.COMMA); + write(op); write(ASEmitterTokens.SPACE); - getWalker().walk(dateNode.getLeftOperandNode()); - write(ASEmitterTokens.MEMBER_ACCESS); - write("getUTCMilliseconds()"); - break; + getWalker().walk(node.getRightOperandNode()); } - write(ASEmitterTokens.PAREN_CLOSE); } } http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/151b03d5/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/jx/MemberAccessEmitter.java ---------------------------------------------------------------------- diff --git a/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/jx/MemberAccessEmitter.java b/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/jx/MemberAccessEmitter.java index afe5d95..0c37490 100644 --- a/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/jx/MemberAccessEmitter.java +++ b/compiler-jx/src/main/java/org/apache/flex/compiler/internal/codegen/js/jx/MemberAccessEmitter.java @@ -29,6 +29,7 @@ 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.codegen.js.flexjs.JSFlexJSEmitterTokens; import org.apache.flex.compiler.internal.codegen.js.goog.JSGoogEmitterTokens; +import org.apache.flex.compiler.internal.codegen.js.jx.BinaryOperatorEmitter.DatePropertiesGetters; import org.apache.flex.compiler.internal.definitions.AccessorDefinition; import org.apache.flex.compiler.internal.definitions.FunctionDefinition; import org.apache.flex.compiler.internal.tree.as.FunctionCallNode; @@ -167,13 +168,10 @@ public class MemberAccessEmitter extends JSSubEmitter implements else if (fjs.isDateProperty(node)) { writeLeftSide(node, leftNode, rightNode); - write(".get"); String rightName = ((IIdentifierNode)rightNode).getName(); - String firstChar = rightName.substring(0, 1); - firstChar = firstChar.toUpperCase(); - rightName = rightName.substring(1); - write(firstChar); - write(rightName); + DatePropertiesGetters propGetter = DatePropertiesGetters.valueOf(rightName.toUpperCase()); + write(ASEmitterTokens.MEMBER_ACCESS); + write(propGetter.getFunctionName()); write(ASEmitterTokens.PAREN_OPEN); write(ASEmitterTokens.PAREN_CLOSE); return; http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/151b03d5/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 3240840..8873627 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 @@ -713,6 +713,16 @@ public class TestFlexJSExpressions extends TestGoogExpressions } @Test + public void testVisitBinaryOperatorNode_LotsOfBuiltinFunctions() + { + IBinaryOperatorNode node = (IBinaryOperatorNode) getNode( + "public class B {public function d() { var seed:Number; seed = new Date().time - Math.random() * int.MAX_VALUE; }}", + IBinaryOperatorNode.class, WRAP_LEVEL_PACKAGE); + asBlockWalker.visitBinaryOperator(node); + assertOut("seed = new Date().getTime() - Math.random() * 2147483648"); + } + + @Test public void testNamedFunctionAsArgument() { IFunctionNode node = (IFunctionNode) getNode( http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/151b03d5/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSGlobalClasses.java ---------------------------------------------------------------------- diff --git a/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSGlobalClasses.java b/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSGlobalClasses.java index 9d7f50b..58324bd 100644 --- a/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSGlobalClasses.java +++ b/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSGlobalClasses.java @@ -209,6 +209,31 @@ public class TestFlexJSGlobalClasses extends TestGoogGlobalClasses } @Test + public void testDateGetTimeInMilliseconds() + { + IVariableNode node = getVariable("var a:Date = new Date(); var b:Number = a.time"); + node = (IVariableNode)(node.getParent().getChild(1)); + asBlockWalker.visitVariable(node); + assertOut("var /** @type {number} */ b = a.getTime()"); + } + + @Test + public void testDateSetTimeInMilliseconds() + { + IBinaryOperatorNode node = getBinaryNode("var a:Date = new Date(); a.time = 10"); + asBlockWalker.visitBinaryOperator(node); + assertOut("a.setTime(10)"); + } + + @Test + public void testDateIncreaseTimeInMilliseconds() + { + IBinaryOperatorNode node = getBinaryNode("var a:Date = new Date(); a.time += 10"); + asBlockWalker.visitBinaryOperator(node); + assertOut("a.setTime(a.getTime() + 10)"); + } + + @Test public void testDateGetMinutes() { IVariableNode node = getVariable("var a:Date = new Date(); var b:Number = a.minutes"); @@ -222,7 +247,15 @@ public class TestFlexJSGlobalClasses extends TestGoogGlobalClasses { IBinaryOperatorNode node = getBinaryNode("var a:Date = new Date(); a.minutes = 10"); asBlockWalker.visitBinaryOperator(node); - assertOut("a.setMinutes(10, a.getSeconds(), a.getMilliseconds())"); + assertOut("a.setMinutes(10)"); + } + + @Test + public void testDateIncreaseMinutes() + { + IBinaryOperatorNode node = getBinaryNode("var a:Date = new Date(); a.minutes += 10"); + asBlockWalker.visitBinaryOperator(node); + assertOut("a.setMinutes(a.getMinutes() + 10)"); } @Test