예제 #1
0
 public override void Visit(ConstantWrapper node)
 {
     if (node != null)
     {
         // measure
         if (node.PrimitiveType == PrimitiveType.Boolean)
         {
             if (m_measure)
             {
                 // if we are converting true/false literals to !0/!1, then
                 // a logical-not doesn't add or subtract anything. But if we aren't,
                 // we need to add/subtract the difference in the length between the
                 // "true" and "false" strings
                 if (!m_parser.Settings.MinifyCode ||
                     !m_parser.Settings.IsModificationAllowed(TreeModifications.BooleanLiteralsToNotOperators))
                 {
                     // converting true to false adds a character, false to true subtracts
                     m_delta += node.ToBoolean() ? 1 : -1;
                 }
             }
             else
             {
                 // convert - just flip the boolean value
                 node.Value = !node.ToBoolean();
             }
         }
         else
         {
             // just the same typical operation as most other nodes for other types
             TypicalHandler(node);
         }
     }
 }
예제 #2
0
 public override void Visit(ConstantWrapper node)
 {
     if (node != null)
     {
         // no children, so don't bother calling the base.
         if (node.PrimitiveType == PrimitiveType.Boolean &&
             m_parser.Settings.IsModificationAllowed(TreeModifications.BooleanLiteralsToNotOperators))
         {
             node.Parent.ReplaceChild(node, new UnaryOperator(node.Context, m_parser)
             {
                 Operand       = new ConstantWrapper(node.ToBoolean() ? 0 : 1, PrimitiveType.Number, node.Context, m_parser),
                 OperatorToken = JSToken.LogicalNot
             });
         }
     }
 }
 public override void Visit(ConstantWrapper node)
 {
     if (node != null)
     {
         // no children, so don't bother calling the base.
         if (node.PrimitiveType == PrimitiveType.Boolean
             && m_parser.Settings.IsModificationAllowed(TreeModifications.BooleanLiteralsToNotOperators))
         {
             node.Parent.ReplaceChild(node, new UnaryOperator(node.Context, m_parser)
                 {
                     Operand = new ConstantWrapper(node.ToBoolean() ? 0 : 1, PrimitiveType.Number, node.Context, m_parser),
                     OperatorToken = JSToken.LogicalNot
                 });
         }
     }
 }
        private ConstantWrapper LogicalOr(ConstantWrapper left, ConstantWrapper right)
        {
            ConstantWrapper newLiteral = null;
            if (m_parser.Settings.IsModificationAllowed(TreeModifications.EvaluateNumericExpressions))
            {
                try
                {
                    // if the left-hand side evaluates to true, return the left-hand side.
                    // if the left-hand side is false, return the right-hand side.
                    newLiteral = left.ToBoolean() ? left : right;
                }
                catch (InvalidCastException)
                {
                    // if we couldn't cast to bool, ignore
                }
            }

            return newLiteral;
        }
        private ConstantWrapper StrictNotEqual(ConstantWrapper left, ConstantWrapper right)
        {
            ConstantWrapper newLiteral = null;

            if (m_parser.Settings.IsModificationAllowed(TreeModifications.EvaluateNumericExpressions))
            {
                PrimitiveType leftType = left.PrimitiveType;
                if (leftType == right.PrimitiveType)
                {
                    // the values are the same type
                    switch (leftType)
                    {
                        case PrimitiveType.Null:
                            // null !== null is false
                            newLiteral = new ConstantWrapper(false, PrimitiveType.Boolean, null, m_parser);
                            break;

                        case PrimitiveType.Boolean:
                            // compare boolean values
                            newLiteral = new ConstantWrapper(left.ToBoolean() != right.ToBoolean(), PrimitiveType.Boolean, null, m_parser);
                            break;

                        case PrimitiveType.String:
                            // compare string ordinally
                            if (left.IsOkayToCombine && right.IsOkayToCombine)
                            {
                                newLiteral = new ConstantWrapper(string.CompareOrdinal(left.ToString(), right.ToString()) != 0, PrimitiveType.Boolean, null, m_parser);
                            }
                            break;

                        case PrimitiveType.Number:
                            try
                            {
                                // compare the values
                                // +0 and -0 are treated as "equal" in C#, so we don't need to test them separately.
                                // and NaN is always unequal to everything else, including itself.
                                if (left.IsOkayToCombine && right.IsOkayToCombine)
                                {
                                    newLiteral = new ConstantWrapper(left.ToNumber() != right.ToNumber(), PrimitiveType.Boolean, null, m_parser);
                                }
                            }
                            catch (InvalidCastException)
                            {
                                // some kind of casting in ToNumber caused a situation where we don't want
                                // to perform the combination on these operands
                            }
                            break;
                    }
                }
                else
                {
                    // if they aren't the same type, they are not equal
                    newLiteral = new ConstantWrapper(true, PrimitiveType.Boolean, null, m_parser);
                }
            }

            return newLiteral;
        }
 public override void Visit(ConstantWrapper node)
 {
     if (node != null)
     {
         // measure
         if (node.PrimitiveType == PrimitiveType.Boolean)
         {
             if (m_measure)
             {
                 // if we are converting true/false literals to !0/!1, then
                 // a logical-not doesn't add or subtract anything. But if we aren't,
                 // we need to add/subtract the difference in the length between the
                 // "true" and "false" strings
                 if (!m_parser.Settings.MinifyCode
                     || !m_parser.Settings.IsModificationAllowed(TreeModifications.BooleanLiteralsToNotOperators))
                 {
                     // converting true to false adds a character, false to true subtracts
                     m_delta += node.ToBoolean() ? 1 : -1;
                 }
             }
             else
             {
                 // convert - just flip the boolean value
                 node.Value = !node.ToBoolean();
             }
         }
         else
         {
             // just the same typical operation as most other nodes for other types
             TypicalHandler(node);
         }
     }
 }