示例#1
0
        public override AstNode Clone()
        {
            Lookup clone = new Lookup(m_name, (Context == null ? null : Context.Clone()), Parser);

            clone.IsGenerated = m_isGenerated;
            return(clone);
        }
示例#2
0
 internal JScriptException(JSError errorNumber, Context context)
 {
     m_valueObject = Missing.Value;
     m_context     = (context == null ? null : context.Clone());
     m_fileContext = (context == null ? null : context.FileContext);
     m_code        = HResult = unchecked ((int)(0x800A0000 + (int)errorNumber));
 }
示例#3
0
 internal JScriptException(JSError errorNumber, Context context)
 {
     m_valueObject = Missing.Value;
     m_context = (context == null ? null : context.Clone());
     m_fileContext = (context == null ? null : context.Document.FileContext);
     m_code = HResult = unchecked((int)(0x800A0000 + (int)errorNumber));
 }
示例#4
0
 public override AstNode Clone()
 {
     return(new EvaluateNode(
                (Context == null ? null : Context.Clone()),
                Parser,
                (m_operand == null ? null : m_operand.Clone())
                ));
 }
示例#5
0
 public override AstNode Clone()
 {
     return(new ArrayLiteral(
                (Context == null ? null : Context.Clone()),
                Parser,
                (m_elements == null ? null : (AstNodeList)m_elements.Clone())
                ));
 }
示例#6
0
 public override AstNode Clone()
 {
     return(new ContinueNode(
                (Context == null ? null : Context.Clone()),
                Parser,
                m_nestLevel,
                m_label));
 }
示例#7
0
文件: member.cs 项目: formist/LinkMe
 public override AstNode Clone()
 {
     return(new Member(
                (Context == null ? null : Context.Clone()),
                Parser,
                (Root == null ? null : Root.Clone()),
                Name
                ));
 }
示例#8
0
文件: dowhile.cs 项目: formist/LinkMe
 public override AstNode Clone()
 {
     return(new DoWhile(
                (Context == null ? null : Context.Clone()),
                Parser,
                (Body == null ? null : Body.Clone()),
                (Condition == null ? null : Condition.Clone())
                ));
 }
示例#9
0
 public override AstNode Clone()
 {
     return(new NumericUnary(
                (Context == null ? null : Context.Clone()),
                Parser,
                (Operand == null ? null : Operand.Clone()),
                OperatorToken
                ));
 }
示例#10
0
文件: switch.cs 项目: formist/LinkMe
 public override AstNode Clone()
 {
     return new Switch(
       (Context == null ? null : Context.Clone()),
       Parser,
       (m_expression == null ? null : m_expression.Clone()),
       (m_cases == null ? null : (AstNodeList)m_cases.Clone())
       );
 }
示例#11
0
 public override AstNode Clone()
 {
     return(new ConstantWrapperPP(
                m_varName,
                m_forceComments,
                (Context == null ? null : Context.Clone()),
                Parser
                ));
 }
示例#12
0
 public override AstNode Clone()
 {
     return(new RegExpLiteral(
                m_pattern,
                m_patternSwitches,
                (Context == null ? null : Context.Clone()),
                Parser
                ));
 }
示例#13
0
 public override AstNode Clone()
 {
     return(new ConstantWrapper(
                Value,
                PrimitiveType,
                (Context == null ? null : Context.Clone()),
                Parser
                ));
 }
示例#14
0
 public override AstNode Clone()
 {
     return(new SwitchCase(
                (Context == null ? null : Context.Clone()),
                Parser,
                (m_caseValue == null ? null : m_caseValue.Clone()),
                (m_statements == null ? null : (Block)m_statements.Clone())
                ));
 }
示例#15
0
 public override AstNode Clone()
 {
     return(new Conditional(
                (Context == null ? null : Context.Clone()),
                Parser,
                (m_condition == null ? null : m_condition.Clone()),
                (m_trueExpression == null ? null : m_trueExpression.Clone()),
                (m_falseExpression == null ? null : m_falseExpression.Clone())
                ));
 }
示例#16
0
文件: forin.cs 项目: formist/LinkMe
 public override AstNode Clone()
 {
     return(new ForIn(
                (Context == null ? null : Context.Clone()),
                Parser,
                (m_var == null ? null : m_var.Clone()),
                (m_collection == null ? null : m_collection.Clone()),
                (m_body == null ? null : m_body.Clone())
                ));
 }
示例#17
0
 public override AstNode Clone()
 {
     return(new LabeledStatement(
                (Context == null ? null : Context.Clone()),
                Parser,
                m_label,
                m_nestCount,
                (m_statement == null ? null : m_statement.Clone())
                ));
 }
示例#18
0
文件: if.cs 项目: formist/LinkMe
 public override AstNode Clone()
 {
     return(new IfNode(
                (Context == null ? null : Context.Clone()),
                Parser,
                (Condition == null ? null : Condition.Clone()),
                (TrueBlock == null ? null : TrueBlock.Clone()),
                (FalseBlock == null ? null : FalseBlock.Clone())
                ));
 }
示例#19
0
 public override AstNode Clone()
 {
     return(new TryNode(
                (Context == null ? null : Context.Clone()),
                Parser,
                (TryBlock == null ? null : TryBlock.Clone()),
                m_catchVarName,
                (CatchBlock == null ? null : CatchBlock.Clone()),
                (FinallyBlock == null ? null : FinallyBlock.Clone())
                ));
 }
示例#20
0
文件: for.cs 项目: formist/LinkMe
 public override AstNode Clone()
 {
     return(new ForNode(
                (Context == null ? null : Context.Clone()),
                Parser,
                (Initializer == null ? null : Initializer.Clone()),
                (Condition == null ? null : Condition.Clone()),
                (Incrementer == null ? null : Incrementer.Clone()),
                (Body == null ? null : Body.Clone())
                ));
 }
示例#21
0
文件: call.cs 项目: formist/LinkMe
        public override AstNode Clone()
        {
            CallNode newCallNode = new CallNode(
                (Context == null ? null : Context.Clone()),
                Parser,
                (m_func == null ? null : m_func.Clone()),
                (m_args == null ? null : (AstNodeList)m_args.Clone()),
                m_inBrackets
                );

            newCallNode.m_isConstructor = m_isConstructor;
            return(newCallNode);
        }
示例#22
0
        public override AstNode Clone()
        {
            VariableDeclaration varDecl = new VariableDeclaration(
                (Context == null ? null : Context.Clone()),
                Parser,
                m_identifier,
                m_field.OriginalContext,
                (m_initializer == null ? null : m_initializer.Clone()),
                m_field.Attributes
                );

            varDecl.m_isGenerated = m_isGenerated;
            return(varDecl);
        }
示例#23
0
文件: astlist.cs 项目: formist/LinkMe
        public override AstNode Clone()
        {
            // create a new empty list
            AstNodeList newList = new AstNodeList((Context == null ? null : Context.Clone()), Parser);

            // and clone all the items into it (skipping nulls)
            for (int ndx = 0; ndx < m_list.Count; ++ndx)
            {
                if (m_list[ndx] != null)
                {
                    newList.Append(m_list[ndx].Clone());
                }
            }
            return(newList);
        }
示例#24
0
        internal override void AnalyzeNode()
        {
            // recurse first, then check to see if the unary is still needed
            base.AnalyzeNode();

            // if the operand is a numeric literal
            ConstantWrapper constantWrapper = Operand as ConstantWrapper;

            if (constantWrapper != null &&
                constantWrapper.IsNumericLiteral)
            {
                // get the value of the constant. We've already screened it for numeric, so
                // we don't have to worry about catching any errors
                double doubleValue = constantWrapper.ToNumber();

                // if this is a unary minus...
                if (OperatorToken == JSToken.Minus &&
                    Parser.Settings.IsModificationAllowed(TreeModifications.ApplyUnaryMinusToNumericLiteral))
                {
                    // negate the value
                    constantWrapper.Value = -doubleValue;

                    // replace us with the negated constant
                    if (Parent.ReplaceChild(this, constantWrapper))
                    {
                        // the context for the minus will include the number (its operand),
                        // but the constant will just be the number. Update the context on
                        // the constant to be a copy of the context on the operator
                        constantWrapper.Context = Context.Clone();
                        return;
                    }
                }
                else if (OperatorToken == JSToken.Plus &&
                         Parser.Settings.IsModificationAllowed(TreeModifications.RemoveUnaryPlusOnNumericLiteral))
                {
                    // +NEG is still negative, +POS is still positive, and +0 is still 0.
                    // so just get rid of the unary operator altogether
                    if (Parent.ReplaceChild(this, constantWrapper))
                    {
                        // the context for the unary will include the number (its operand),
                        // but the constant will just be the number. Update the context on
                        // the constant to be a copy of the context on the operator
                        constantWrapper.Context = Context.Clone();
                        return;
                    }
                }
            }
        }
示例#25
0
文件: var.cs 项目: formist/LinkMe
        public override AstNode Clone()
        {
            // creates a new EMPTY statement
            Var newVar = new Var((Context == null ? null : Context.Clone()), Parser);

            // now go through and clone all the actual declarations
            for (int ndx = 0; ndx < m_list.Count; ++ndx)
            {
                if (m_list[ndx] != null)
                {
                    // cloning the declaration will add a field to the current scope
                    // (better already be in the proper scope chain)
                    newVar.Append(m_list[ndx].Clone());
                }
            }
            return(newVar);
        }
示例#26
0
        public override AstNode Clone()
        {
            // we need to manually clone the keys and values, so determine
            // the minimum length, allocate the arrays, and clone each item
            int count = (m_keys.Length < m_values.Length ? m_keys.Length : m_values.Length);

            ObjectLiteralField[] clonedKeys   = new ObjectLiteralField[count];
            AstNode[]            clonedValues = new AstNode[count];
            for (int ndx = 0; ndx < count; ++ndx)
            {
                clonedKeys[ndx]   = (ObjectLiteralField)m_keys[ndx].Clone();
                clonedValues[ndx] = m_values[ndx].Clone();
            }

            return(new ObjectLiteral(
                       (Context == null ? null : Context.Clone()),
                       Parser,
                       clonedKeys,
                       clonedValues
                       ));
        }
示例#27
0
 public override AstNode Clone()
 {
     return(new AspNetBlockNode((Context == null ? null : Context.Clone()), Parser,
                                aspNetBlockText, blockTerminatedByExplicitSemicolon));
 }
示例#28
0
文件: if.cs 项目: formist/LinkMe
        internal Conditional CanBeReturnOperand(AstNode ultimateOperand, bool isFunctionLevel)
        {
            Conditional conditional = null;

            try
            {
                if (TrueBlock != null && TrueBlock.Count == 1)
                {
                    ReturnNode returnNode = TrueBlock[0] as ReturnNode;
                    if (returnNode != null)
                    {
                        AstNode expr1 = returnNode.Operand;
                        if (FalseBlock == null || FalseBlock.Count == 0)
                        {
                            // no false branch to speak of. Convert to conditional.
                            // if there is an ultimate expression, use it.
                            // if we are not at the function body level, we can't
                            // combine these. But if we are we can
                            // use a false expression of "void 0" (undefined)
                            if (ultimateOperand != null || isFunctionLevel)
                            {
                                conditional = new Conditional(
                                    (Context == null ? null : Context.Clone()),
                                    Parser,
                                    Condition,
                                    expr1 ?? CreateVoidNode(),
                                    ultimateOperand ?? CreateVoidNode());
                            }
                        }
                        else if (FalseBlock.Count == 1)
                        {
                            // there is a false branch with only a single statement
                            // see if it is a return statement
                            returnNode = FalseBlock[0] as ReturnNode;
                            if (returnNode != null)
                            {
                                // it is. so we have if(cond)return expr1;else return expr2
                                // return cond?expr1:expr2
                                AstNode expr2 = returnNode.Operand;
                                conditional = new Conditional(
                                    (Context == null ? null : Context.Clone()),
                                    Parser,
                                    Condition,
                                    expr1 ?? CreateVoidNode(),
                                    expr2 ?? CreateVoidNode());
                            }
                            else
                            {
                                // see if it's another if-statement
                                IfNode elseIf = FalseBlock[0] as IfNode;
                                if (elseIf != null)
                                {
                                    // it's a nested if-statement. See if IT can be a return argument.
                                    Conditional expr2 = elseIf.CanBeReturnOperand(ultimateOperand, isFunctionLevel);
                                    if (expr2 != null)
                                    {
                                        // it can, so we can just nest the conditionals
                                        conditional = new Conditional(
                                            (Context == null ? null : Context.Clone()),
                                            Parser,
                                            Condition,
                                            expr1,
                                            expr2);
                                    }
                                }
                                // else neither return- nor if-statement
                            }
                        }
                        // else false branch has more than one statement
                    }
                    // else the single statement is not a return-statement
                }
                // else no true branch, or not a single statement in the branch
            }
            catch (NotImplementedException)
            {
                // one of the clone calls probably failed.
                // don't say this can be a return argument.
            }
            return(conditional);
        }
示例#29
0
 // not instantiated directly, only through derived classes
 protected BlockScope(ActivationObject parent, Context context, JSParser parser)
     : base(parent, parser)
 {
     m_context = (context == null ? new Context(parser) : context.Clone());
 }
示例#30
0
        //---------------------------------------------------------------------------------------
        // JSParser
        //
        // create a parser with a context. The context is the code that has to be compiled.
        // Typically used by the runtime
        //---------------------------------------------------------------------------------------
        public JSParser(string source)
        {
            Context context = new Context(new DocumentContext(this, source));

            m_sourceContext = context;
            m_currentToken = context.Clone();
            m_scanner = new JSScanner(m_currentToken);
            m_noSkipTokenSet = new NoSkipTokenSet();

            m_blockType = new List<BlockType>(16);
            m_labelTable = new Dictionary<string, LabelInfo>();
            m_severity = 5;
        }
示例#31
0
        internal override void AnalyzeNode()
        {
            // javascript doesn't have block scope, so there really is no point
            // in nesting blocks. Unnest any now, before we start combining var statements
            UnnestBlocks();

            // if we want to remove debug statements...
            if (Parser.Settings.StripDebugStatements && Parser.Settings.IsModificationAllowed(TreeModifications.StripDebugStatements))
            {
                // do it now before we try doing other things
                StripDebugStatements();
            }

            // these variables are used to check for combining a particular type of
            // for-statement with preceding var-statements.
            ForNode targetForNode = null;
            string  targetName    = null;

            // check to see if we want to combine adjacent var statements
            bool combineVarStatements = Parser.Settings.IsModificationAllowed(TreeModifications.CombineVarStatements);

            // check to see if we want to combine a preceding var with a for-statement
            bool moveVarIntoFor = Parser.Settings.IsModificationAllowed(TreeModifications.MoveVarIntoFor);

            // look at the statements in the block.
            // if there are multiple var statements adjacent to each other, combine them.
            // walk BACKWARDS down the list because we'll be removing items when we encounter
            // multiple vars.
            // we also don't need to check the first one, since there is nothing before it.
            for (int ndx = m_list.Count - 1; ndx > 0; --ndx)
            {
                // if the previous node is not a Var, then we don't need to try and combine
                // it withthe current node
                Var previousVar = m_list[ndx - 1] as Var;
                if (previousVar != null)
                {
                    // see if THIS item is also a Var...
                    if (m_list[ndx] is Var && combineVarStatements)
                    {
                        // add the items in this VAR to the end of the previous
                        previousVar.Append(m_list[ndx]);

                        // delete this item from the block
                        m_list.RemoveAt(ndx);

                        // if we have a target for-node waiting for another comparison....
                        if (targetForNode != null)
                        {
                            // check to see if the variable we are looking for is in the new list
                            if (previousVar.Contains(targetName))
                            {
                                // IT DOES! we can combine the var statement with the initializer in the for-statement
                                // we already know it's a binaryop, or it wouldn't be a target for-statement
                                BinaryOperator binaryOp = targetForNode.Initializer as BinaryOperator;

                                // create a vardecl that matches our assignment initializer
                                // ignore duplicates because this scope will already have the variable defined.
                                VariableDeclaration varDecl = new VariableDeclaration(
                                    binaryOp.Context.Clone(),
                                    Parser,
                                    targetName,
                                    binaryOp.Operand1.Context.Clone(),
                                    binaryOp.Operand2,
                                    0,
                                    true
                                    );
                                // append it to the preceding var-statement
                                previousVar.Append(varDecl);

                                // move the previous vardecl to our initializer
                                targetForNode.ReplaceChild(targetForNode.Initializer, previousVar);

                                // and remove the previous var from the list.
                                m_list.RemoveAt(ndx - 1);
                                // this will bump the for node up one position in the list, so the next iteration
                                // will be right back on this node, but the initializer will not be null

                                // but now we no longer need the target mechanism -- the for-statement is
                                // not the current node again
                                targetForNode = null;
                            }
                        }
                    }
                    else if (moveVarIntoFor)
                    {
                        // see if this item is a ForNode
                        ForNode forNode = m_list[ndx] as ForNode;
                        if (forNode != null)
                        {
                            // and see if the forNode's initializer is empty
                            if (forNode.Initializer != null)
                            {
                                // not empty -- see if it is a Var node
                                Var varInitializer = forNode.Initializer as Var;
                                if (varInitializer != null)
                                {
                                    // we want to PREPEND the initializers in the previous var statement
                                    // to our for-statement's initializer list
                                    varInitializer.InsertAt(0, previousVar);

                                    // then remove the previous var statement
                                    m_list.RemoveAt(ndx - 1);
                                    // this will bump the for node up one position in the list, so the next iteration
                                    // will be right back on this node in case there are other var statements we need
                                    // to combine
                                }
                                else
                                {
                                    // see if the initializer is a simple assignment
                                    BinaryOperator binaryOp = forNode.Initializer as BinaryOperator;
                                    if (binaryOp != null && binaryOp.OperatorToken == JSToken.Assign)
                                    {
                                        // it is. See if it's a simple lookup
                                        Lookup lookup = binaryOp.Operand1 as Lookup;
                                        if (lookup != null)
                                        {
                                            // it is. see if that variable is in the previous var statement
                                            if (previousVar.Contains(lookup.Name))
                                            {
                                                // create a vardecl that matches our assignment initializer
                                                // ignore duplicates because this scope will already have the variable defined.
                                                VariableDeclaration varDecl = new VariableDeclaration(
                                                    binaryOp.Context.Clone(),
                                                    Parser,
                                                    lookup.Name,
                                                    lookup.Context.Clone(),
                                                    binaryOp.Operand2,
                                                    0,
                                                    true
                                                    );
                                                // append it to the var statement before us
                                                previousVar.Append(varDecl);

                                                // move the previous vardecl to our initializer
                                                forNode.ReplaceChild(forNode.Initializer, previousVar);

                                                // and remove the previous var from the list.
                                                m_list.RemoveAt(ndx - 1);
                                                // this will bump the for node up one position in the list, so the next iteration
                                                // will be right back on this node, but the initializer will not be null
                                            }
                                            else
                                            {
                                                // it's not in the immediately preceding var-statement, but that doesn't mean it won't be in
                                                // a var-statement immediately preceding that one -- in which case they'll get combined and
                                                // then it WILL be in the immediately preceding var-statement. So hold on to this
                                                // for statement and we'll check after we do a combine.
                                                targetForNode = forNode;
                                                targetName    = lookup.Name;
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                // if it's empty, then we're free to add the previous var statement
                                // to this for statement's initializer. remove it from it's current
                                // position and add it as the initializer
                                m_list.RemoveAt(ndx - 1);
                                forNode.ReplaceChild(forNode.Initializer, previousVar);
                                // this will bump the for node up one position in the list, so the next iteration
                                // will be right back on this node, but the initializer will not be null
                            }
                        }
                    }
                }
                else
                {
                    // not a var statement. make sure the target for-node is cleared.
                    targetForNode = null;

                    ConditionalCompilationComment previousComment = m_list[ndx - 1] as ConditionalCompilationComment;
                    if (previousComment != null)
                    {
                        ConditionalCompilationComment thisComment = m_list[ndx] as ConditionalCompilationComment;
                        if (thisComment != null)
                        {
                            // two adjacent conditional comments -- combine them into the first.
                            // this will actually make the second block a nested block within the first block,
                            // but they'll be flattened when the comment's block gets recursed.
                            previousComment.Statements.Append(thisComment.Statements);

                            // and remove the second one (which is now a duplicate)
                            m_list.RemoveAt(ndx);
                        }
                    }
                }
            }

            if (m_blockScope != null)
            {
                ScopeStack.Push(m_blockScope);
            }
            try
            {
                // call the base class to recurse
                base.AnalyzeNode();
            }
            finally
            {
                if (m_blockScope != null)
                {
                    ScopeStack.Pop();
                }
            }

            // NOW that we've recursively analyzed all the child nodes in this block, let's see
            // if we can further reduce the statements by checking for a couple good opportunities
            if (Parser.Settings.RemoveUnneededCode)
            {
                // Transform: {var foo=expression;return foo;} to: {return expression;}
                if (m_list.Count == 2 && Parser.Settings.IsModificationAllowed(TreeModifications.VarInitializeReturnToReturnInitializer))
                {
                    Var        varStatement    = m_list[0] as Var;
                    ReturnNode returnStatement = m_list[1] as ReturnNode;

                    // see if we have two statements in our block: a var with a single declaration, and a return
                    if (returnStatement != null && varStatement != null &&
                        varStatement.Count == 1 && varStatement[0].Initializer != null)
                    {
                        // now see if the return is returning a lookup for the same var we are declaring in the
                        // previous statement
                        Lookup lookup = returnStatement.Operand as Lookup;
                        if (lookup != null &&
                            string.Compare(lookup.Name, varStatement[0].Identifier, StringComparison.Ordinal) == 0)
                        {
                            // it's a match!
                            // create a combined context starting with the var and adding in the return
                            Context context = varStatement.Context.Clone();
                            context.UpdateWith(returnStatement.Context);

                            // create a new return statement
                            ReturnNode newReturn = new ReturnNode(context, Parser, varStatement[0].Initializer);

                            // clear out the existing statements
                            m_list.Clear();

                            // and add our new one
                            Append(newReturn);
                        }
                    }
                }

                // we do things differently if these statements are the last in a function
                // because we can assume the implicit return
                bool isFunctionLevel = (Parent is FunctionObject);

                // see if we want to change if-statement that forces a return to a return conditional
                if (Parser.Settings.IsModificationAllowed(TreeModifications.IfElseReturnToReturnConditional))
                {
                    // transform: {...; if(cond1)return;} to {...;cond;}
                    // transform: {...; if(cond1)return exp1;else return exp2;} to {...;return cond1?exp1:exp2;}
                    if (m_list.Count >= 1)
                    {
                        // see if the last statement is an if-statement with a true-block containing only one statement
                        IfNode ifStatement = m_list[m_list.Count - 1] as IfNode;
                        if (ifStatement != null &&
                            ifStatement.TrueBlock != null)
                        {
                            // see if this if-statement is structured such that we can convert it to a
                            // Conditional node that is the operand of a return statement
                            Conditional returnOperand = ifStatement.CanBeReturnOperand(null, isFunctionLevel);
                            if (returnOperand != null)
                            {
                                // it can! change it.
                                ReturnNode returnNode = new ReturnNode(
                                    (Context == null ? null : Context.Clone()),
                                    Parser,
                                    returnOperand);

                                // replace the if-statement with the return statement
                                ReplaceChild(ifStatement, returnNode);
                            }
                        }
                        // else last statement is not an if-statement, or true block is not a single statement
                    }

                    // transform: {...; if(cond1)return exp1;return exp2;} to {...; return cond1?exp1:exp2;}
                    // my cascade! changing the two statements to a return may cause us to run this again if the
                    // third statement up becomes the penultimate and is an if-statement
                    while (m_list.Count > 1)
                    {
                        int lastIndex = m_list.Count - 1;
                        // end in a return statement?
                        ReturnNode finalReturn = m_list[lastIndex] as ReturnNode;
                        if (finalReturn != null)
                        {
                            // it does -- see if the penultimate statement is an if-block
                            IfNode ifNode = m_list[lastIndex - 1] as IfNode;
                            if (ifNode != null)
                            {
                                // if followed by return. See if the if statement can be changed to a
                                // return of a conditional, using the operand of the following return
                                // as the ultimate expression
                                Conditional returnConditional = ifNode.CanBeReturnOperand(finalReturn.Operand, isFunctionLevel);
                                if (returnConditional != null)
                                {
                                    // it can! so create the new return statement.
                                    // the context of this new return statement should start with a clone of
                                    // the if-statement and updated with the return statement
                                    Context context = ifNode.Context.Clone();
                                    context.UpdateWith(finalReturn.Context);

                                    // create the new return node
                                    ReturnNode newReturn = new ReturnNode(
                                        context,
                                        Parser,
                                        returnConditional);

                                    // remove the last node (the old return)
                                    m_list.RemoveAt(lastIndex--);

                                    // and replace the if-statement with the new return
                                    m_list[lastIndex] = newReturn;
                                    newReturn.Parent  = this;

                                    // we collapsed the last two statements, and we KNOW the last one is a
                                    // return -- go back up to the top of the loop to see if we can keep going.
                                    continue;
                                }
                            }
                        }

                        // if we get here, then something went wrong, we didn't collapse the last
                        // two statements, so break out of the loop
                        break;
                    }

                    // now we may have converted the last functional statement
                    // from if(cond)return expr to return cond?expr:void 0, which is four
                    // extra bytes. So let's check to see if the last statement in the function
                    // now fits this pattern, and if so, change it back.
                    // We didn't just NOT change it in the first place because changing it could've
                    // enabled even more changes that would save a lot more space. But apparently
                    // those subsequent changes didn't pan out.
                    if (m_list.Count >= 1)
                    {
                        int        lastIndex  = m_list.Count - 1;
                        ReturnNode returnNode = m_list[lastIndex] as ReturnNode;
                        if (returnNode != null)
                        {
                            Conditional conditional = returnNode.Operand as Conditional;
                            if (conditional != null)
                            {
                                VoidNode falseVoid = conditional.FalseExpression as VoidNode;
                                if (falseVoid != null && falseVoid.Operand is ConstantWrapper)
                                {
                                    // we have the required pattern: "return cond?expr:void 0"
                                    // (well, the object of the void is a constant, at least).
                                    // undo it back to "if(cond)return expr" because that takes fewer bytes.

                                    // by default, the operand of the return operator will be the
                                    // true branch of the conditional
                                    AstNode returnOperand = conditional.TrueExpression;

                                    VoidNode trueVoid = conditional.TrueExpression as VoidNode;
                                    if (trueVoid != null && trueVoid.Operand is ConstantWrapper)
                                    {
                                        // the true branch of the conditional is a void operator acting
                                        // on a constant! So really, there is no operand to the return statement
                                        returnOperand = null;

                                        if (Parser.Settings.IsModificationAllowed(TreeModifications.IfConditionReturnToCondition))
                                        {
                                            // actually, we have return cond?void 0:void 0,
                                            // which would get changed back to function{...;if(cond)return}
                                            // BUT we can just shorten it to function{...;cond}
                                            m_list[lastIndex]            = conditional.Condition;
                                            conditional.Condition.Parent = this;
                                            return;
                                        }
                                    }

                                    IfNode ifNode = new IfNode(
                                        returnNode.Context.Clone(),
                                        Parser,
                                        conditional.Condition,
                                        new ReturnNode(returnNode.Context.Clone(), Parser, returnOperand),
                                        null);
                                    m_list[lastIndex] = ifNode;
                                    ifNode.Parent     = this;
                                }
                            }
                        }
                    }
                }
            }
        }
示例#32
0
        private Context m_context; // = null;

        #endregion Fields

        #region Constructors

        // not instantiated directly, only through derived classes
        protected BlockScope(ActivationObject parent, Context context, JSParser parser)
            : base(parent, parser)
        {
            m_context = (context == null ? new Context(parser) : context.Clone());
        }