This is an automated email from the ASF dual-hosted git repository.

joshtynjala pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-compiler.git

commit 2f3f77fd8d03acf042383535d73358c97e2127a7
Author: Josh Tynjala <[email protected]>
AuthorDate: Mon Nov 20 15:20:01 2023 -0800

    DynamicAccessEmitter, IdentifierEmitter: extract some of the branches into 
separate methods to help readability
---
 .../codegen/js/jx/DynamicAccessEmitter.java        |  99 +++++----
 .../internal/codegen/js/jx/IdentifierEmitter.java  | 227 ++++++++++++---------
 2 files changed, 187 insertions(+), 139 deletions(-)

diff --git 
a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/DynamicAccessEmitter.java
 
b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/DynamicAccessEmitter.java
index fb5b1cb70..47ddcee57 100644
--- 
a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/DynamicAccessEmitter.java
+++ 
b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/DynamicAccessEmitter.java
@@ -25,7 +25,6 @@ import org.apache.royale.compiler.codegen.js.IJSEmitter;
 import org.apache.royale.compiler.constants.IASLanguageConstants;
 import org.apache.royale.compiler.definitions.ITypeDefinition;
 import org.apache.royale.compiler.internal.codegen.as.ASEmitterTokens;
-import org.apache.royale.compiler.internal.codegen.js.JSEmitterTokens;
 import org.apache.royale.compiler.internal.codegen.js.JSSubEmitter;
 import 
org.apache.royale.compiler.internal.codegen.js.royale.JSRoyaleDocEmitter;
 import org.apache.royale.compiler.internal.codegen.js.royale.JSRoyaleEmitter;
@@ -33,17 +32,13 @@ import 
org.apache.royale.compiler.internal.codegen.js.royale.JSRoyaleEmitterToke
 import org.apache.royale.compiler.internal.definitions.AppliedVectorDefinition;
 import org.apache.royale.compiler.internal.projects.RoyaleJSProject;
 import 
org.apache.royale.compiler.internal.tree.as.BinaryOperatorAssignmentNode;
-import org.apache.royale.compiler.internal.tree.as.FunctionCallNode;
 import org.apache.royale.compiler.internal.tree.as.IdentifierNode;
 import org.apache.royale.compiler.internal.tree.as.MemberAccessExpressionNode;
-import org.apache.royale.compiler.parsing.IASToken;
 import org.apache.royale.compiler.tree.ASTNodeID;
 import org.apache.royale.compiler.tree.as.IDynamicAccessNode;
 import org.apache.royale.compiler.tree.as.IExpressionNode;
 import org.apache.royale.compiler.tree.as.ILiteralNode;
-import org.apache.royale.compiler.tree.as.IOperatorNode.OperatorType;
 import org.apache.royale.compiler.utils.ASNodeUtils;
-import org.apache.royale.compiler.utils.NativeUtils;
 
 public class DynamicAccessEmitter extends JSSubEmitter implements
         ISubEmitter<IDynamicAccessNode>
@@ -83,51 +78,14 @@ public class DynamicAccessEmitter extends JSSubEmitter 
implements
                        isProxy = fjs.isProxy((IExpressionNode)leftOperandNode);
                if (isXML)
                {
-                               if (type == null) {
-                               //this can happen if myThing is of type Object 
or AnyType (*)
-                                       //with example: 
myXml.somethingChild[myThing.id]
-                               //use Stringify with 'child' method, which has 
support for attributes vs elements
-                                       write(".child('' +");
-                                               
getWalker().walk(rightOperandNode);
-                                       write(")");
-                                       if (ASNodeUtils.hasParenClose(node))
-                                               
write(ASEmitterTokens.PAREN_CLOSE);
-                                       return;
-                               }
-                               if (type.isInstanceOf("String", getProject()))
-                               {
-                                       String field = 
fjs.stringifyNode(rightOperandNode);
-                                       if (field.startsWith("\"@"))
-                                       {
-                                               field = field.replace("@", "");
-                                               write(".attribute(" + field + 
")");
-                                       }
-                                       else
-                                               write(".child(" + field + ")");
-                                       if (ASNodeUtils.hasParenClose(node))
-                                               
write(ASEmitterTokens.PAREN_CLOSE);
-                                       return;
-                               }
-                               else if (type.isInstanceOf("QName", 
getProject()))
+                               if (emitXmlDynamicAccess(node, type))
                                {
-                                       String field = 
fjs.stringifyNode(rightOperandNode);                                     
-                                       write(".child(" + field + ")");
-                                       if (ASNodeUtils.hasParenClose(node))
-                                               
write(ASEmitterTokens.PAREN_CLOSE);
                                        return;
                                }
                }
                else if (isProxy)
                {
-                       boolean isNonStringLiteral = rightOperandNode 
instanceof ILiteralNode && ((ILiteralNode) rightOperandNode).getLiteralType() 
!= ILiteralNode.LiteralType.STRING;
-                       write(".getProperty(");
-                       if (isNonStringLiteral) write("'");
-                       String s = fjs.stringifyNode(rightOperandNode);
-                       write(s);
-                       if (isNonStringLiteral) write("'");
-                       write(")");
-                               if (ASNodeUtils.hasParenClose(node))
-                                       write(ASEmitterTokens.PAREN_CLOSE);
+                               emitProxyGetProperty(node);
                        return;
                }
        }
@@ -190,4 +148,57 @@ public class DynamicAccessEmitter extends JSSubEmitter 
implements
                if (ASNodeUtils.hasParenClose(node))
                        write(ASEmitterTokens.PAREN_CLOSE);
     }
+
+       private boolean emitXmlDynamicAccess(IDynamicAccessNode node, 
ITypeDefinition type) {
+        JSRoyaleEmitter fjs = (JSRoyaleEmitter) getEmitter();
+        IExpressionNode rightOperandNode = node.getRightOperandNode();
+               if (type == null) {
+                       //this can happen if myThing is of type Object or 
AnyType (*)
+                       //with example: myXml.somethingChild[myThing.id]
+                       //use Stringify with 'child' method, which has support 
for attributes vs elements
+                       write(".child('' +");
+                               getWalker().walk(rightOperandNode);
+                       write(")");
+                       if (ASNodeUtils.hasParenClose(node))
+                               write(ASEmitterTokens.PAREN_CLOSE);
+                       return true;
+               }
+               if (type.isInstanceOf("String", getProject()))
+               {
+                       String field = fjs.stringifyNode(rightOperandNode);
+                       if (field.startsWith("\"@"))
+                       {
+                               field = field.replace("@", "");
+                               write(".attribute(" + field + ")");
+                       }
+                       else
+                               write(".child(" + field + ")");
+                       if (ASNodeUtils.hasParenClose(node))
+                               write(ASEmitterTokens.PAREN_CLOSE);
+                       return true;
+               }
+               else if (type.isInstanceOf("QName", getProject()))
+               {
+                       String field = fjs.stringifyNode(rightOperandNode);     
                                
+                       write(".child(" + field + ")");
+                       if (ASNodeUtils.hasParenClose(node))
+                               write(ASEmitterTokens.PAREN_CLOSE);
+                       return true;
+               }
+               return false;
+       }
+
+       private void emitProxyGetProperty(IDynamicAccessNode node) {
+        JSRoyaleEmitter fjs = (JSRoyaleEmitter) getEmitter();
+        IExpressionNode rightOperandNode = node.getRightOperandNode();
+               boolean isNonStringLiteral = rightOperandNode instanceof 
ILiteralNode && ((ILiteralNode) rightOperandNode).getLiteralType() != 
ILiteralNode.LiteralType.STRING;
+               write(".getProperty(");
+               if (isNonStringLiteral) write("'");
+               String s = fjs.stringifyNode(rightOperandNode);
+               write(s);
+               if (isNonStringLiteral) write("'");
+               write(")");
+               if (ASNodeUtils.hasParenClose(node))
+                       write(ASEmitterTokens.PAREN_CLOSE);
+       }
 }
diff --git 
a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/IdentifierEmitter.java
 
b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/IdentifierEmitter.java
index b97aa7f4e..bf41d0648 100644
--- 
a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/IdentifierEmitter.java
+++ 
b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/IdentifierEmitter.java
@@ -89,109 +89,19 @@ public class IdentifierEmitter extends JSSubEmitter 
implements
             Object initialValue = constDef.resolveInitialValue(project);
             if (initialValue != null)
             {
-                startMapping(parentNode);
-                if(initialValue instanceof String)
-                {
-                    write(ASEmitterTokens.DOUBLE_QUOTE);
-                    write((String) initialValue);
-                    write(ASEmitterTokens.DOUBLE_QUOTE);
-                }
-                else if(initialValue == ABCConstants.UNDEFINED_VALUE)
-                {
-                    write(IASLanguageConstants.UNDEFINED);
-                }
-                else if(initialValue == ABCConstants.NULL_VALUE)
-                {
-                    write(IASLanguageConstants.NULL);
-                }
-                else
-                {
-                    write(initialValue.toString());
-                }
-                endMapping(parentNode);
+                emitInitialValue(parentNode, initialValue);
                 return;
             }
         }
         if (isStatic)
         {
             String sname = nodeDef.getParent().getQualifiedName();
-            if (sname.equals("Array"))
-            {
-               String baseName = nodeDef.getBaseName();
-               if (baseName.equals("CASEINSENSITIVE"))
-               {
-                    startMapping(parentNode);
-                       write("1");
-                    endMapping(parentNode);
-                       return;
-               }
-               else if (baseName.equals("DESCENDING"))
-               {
-                    startMapping(parentNode);
-                       write("2");
-                    endMapping(parentNode);
-                       return;
-               }
-               else if (baseName.equals("UNIQUESORT"))
-               {
-                    startMapping(parentNode);
-                       write("4");
-                    endMapping(parentNode);
-                       return;
-               }
-               else if (baseName.equals("RETURNINDEXEDARRAY"))
-               {
-                    startMapping(parentNode);
-                       write("8");
-                    endMapping(parentNode);
-                       return;
-               }
-               else if (baseName.equals("NUMERIC"))
-               {
-                    startMapping(parentNode);
-                       write("16");
-                    endMapping(parentNode);
-                       return;
-               }
-            }
-            else if (sname.equals("int"))
-            {
-               String baseName = nodeDef.getBaseName();
-               if (baseName.equals("MAX_VALUE"))
-               {
-                    startMapping(parentNode);
-                       write("2147483647");
-                    endMapping(parentNode);
-                       return;
-               }
-               else if (baseName.equals("MIN_VALUE"))
-               {
-                    startMapping(parentNode);
-                       write("-2147483648");
-                    endMapping(parentNode);
-                       return;
-               }
-            }
-            else if (sname.equals("uint"))
-            {
-               String baseName = nodeDef.getBaseName();
-               if (baseName.equals("MAX_VALUE"))
-               {
-                    startMapping(parentNode);
-                       write("4294967295");
-                    endMapping(parentNode);
-                       return;
-               }
-               else if (baseName.equals("MIN_VALUE"))
-               {
-                    startMapping(parentNode);
-                       write("0");
-                    endMapping(parentNode);
-                       return;
-               }
-            }
             if (sname.length() > 0)
             {
+                if (emitStaticConstants(parentNode, nodeDef))
+                {
+                    return;
+                }
                 IASNode prevSibling = parentNode.getChild(0);
                 if(prevSibling == node)
                 {
@@ -416,6 +326,133 @@ public class IdentifierEmitter extends JSSubEmitter 
implements
         }
     }
     
+    private void emitInitialValue(IASNode parentNode, Object initialValue) {
+        startMapping(parentNode);
+        if(initialValue instanceof String)
+        {
+            write(ASEmitterTokens.DOUBLE_QUOTE);
+            write((String) initialValue);
+            write(ASEmitterTokens.DOUBLE_QUOTE);
+        }
+        else if(initialValue == ABCConstants.UNDEFINED_VALUE)
+        {
+            write(IASLanguageConstants.UNDEFINED);
+        }
+        else if(initialValue == ABCConstants.NULL_VALUE)
+        {
+            write(IASLanguageConstants.NULL);
+        }
+        else
+        {
+            write(initialValue.toString());
+        }
+        endMapping(parentNode);
+    }
     
+    private boolean emitStaticConstants(IASNode parentNode, IDefinition 
nodeDef)
+    {
+        String sname = nodeDef.getParent().getQualifiedName();
+        if (sname.equals("Array"))
+        {
+            String baseName = nodeDef.getBaseName();
+            if (emitArrayConstant(parentNode, baseName))
+            {
+                return true;
+            }
+        }
+        else if (sname.equals("int"))
+        {
+            String baseName = nodeDef.getBaseName();
+            if (emitIntConstant(parentNode, baseName))
+            {
+                return true;
+            }
+        }
+        else if (sname.equals("uint"))
+        {
+            String baseName = nodeDef.getBaseName();
+            if (emitUintConstant(parentNode, baseName))
+            {
+                return true;
+            }
+        }
+        return false;
+    }
 
+    private boolean emitArrayConstant(IASNode parentNode, String baseName) {
+        if (baseName.equals("CASEINSENSITIVE"))
+        {
+            startMapping(parentNode);
+            write("1");
+            endMapping(parentNode);
+            return true;
+        }
+        else if (baseName.equals("DESCENDING"))
+        {
+            startMapping(parentNode);
+            write("2");
+            endMapping(parentNode);
+            return true;
+        }
+        else if (baseName.equals("UNIQUESORT"))
+        {
+            startMapping(parentNode);
+            write("4");
+            endMapping(parentNode);
+            return true;
+        }
+        else if (baseName.equals("RETURNINDEXEDARRAY"))
+        {
+            startMapping(parentNode);
+            write("8");
+            endMapping(parentNode);
+            return true;
+        }
+        else if (baseName.equals("NUMERIC"))
+        {
+            startMapping(parentNode);
+            write("16");
+            endMapping(parentNode);
+            return true;
+        }
+        return false;
+    }
+
+    private boolean emitIntConstant(IASNode parentNode, String baseName)
+    {
+        if (baseName.equals("MAX_VALUE"))
+        {
+            startMapping(parentNode);
+            write("2147483647");
+            endMapping(parentNode);
+            return true;
+        }
+        else if (baseName.equals("MIN_VALUE"))
+        {
+            startMapping(parentNode);
+            write("-2147483648");
+            endMapping(parentNode);
+            return true;
+        }
+        return false;
+    }
+
+    private boolean emitUintConstant(IASNode parentNode, String baseName)
+    {
+        if (baseName.equals("MAX_VALUE"))
+        {
+            startMapping(parentNode);
+            write("4294967295");
+            endMapping(parentNode);
+            return true;
+        }
+        else if (baseName.equals("MIN_VALUE"))
+        {
+            startMapping(parentNode);
+            write("0");
+            endMapping(parentNode);
+            return true;
+        }
+        return false;
+    }
 }

Reply via email to