Exemple #1
0
        internal JSArgumentField AddNewArgumentField(String name)
        {
            JSArgumentField result = new JSArgumentField(name, Missing.Value);

            AddField(result);
            return(result);
        }
Exemple #2
0
        internal CatchScope(ActivationObject parent, Context argContext, JSParser parser)
            : base(parent, argContext, parser)
        {
            // get the name of the catch variable
            m_name = Context.Code;

            // add it to the catch-scope's name table
            JSVariableField field = new JSArgumentField(m_name, null);

            NameTable[m_name] = field;
            FieldTable.Add(field);
        }
Exemple #3
0
        internal bool IsArgumentTrimmable(JSArgumentField targetArgumentField)
        {
            // walk backward until we either find the given argument field or the
            // first parameter that is referenced.
            // If we find the argument field, then we can trim it because there are no
            // referenced parameters after it.
            // if we find a referenced argument, then the parameter is not trimmable.
            JSArgumentField argumentField = null;

            for (int index = m_parameterDeclarations.Length - 1; index >= 0; --index)
            {
                argumentField = m_parameterDeclarations[index].Field;
                if (argumentField != null &&
                    (argumentField == targetArgumentField || argumentField.IsReferenced))
                {
                    break;
                }
            }
            // if the argument field we landed on is the same as the target argument field,
            // then we found the target argument BEFORE we found a referenced parameter. Therefore
            // the argument can be trimmed.
            return(argumentField == targetArgumentField);
        }
Exemple #4
0
        public override string ToCode(ToCodeFormat format)
        {
            StringBuilder sb = new StringBuilder();

            if (!Parser.Settings.MinifyCode || !HideFromOutput)
            {
                if (LeftHandFunctionExpression)
                {
                    sb.Append('(');
                }
                if (format != ToCodeFormat.NoFunction)
                {
                    sb.Append("function");
                    if (m_identifier != null)
                    {
                        // we don't want to show the name for named function expressions where the
                        // name is never referenced. Don't use IsReferenced because that will always
                        // return true for function expressions. Since we really only want to know if
                        // the field name is referenced, check the refcount directly on the field.
                        // also output the function expression name if this is debug mode
                        if (FunctionType != FunctionType.Expression ||
                            !(Parser.Settings.RemoveFunctionExpressionNames && Parser.Settings.IsModificationAllowed(TreeModifications.RemoveFunctionExpressionNames)) ||
                            m_variableField.RefCount > 0)
                        {
                            sb.Append(' ');
                            sb.Append(m_identifier.ToCode());
                        }
                    }
                }

                sb.Append('(');
                if (m_parameterDeclarations.Length > 0)
                {
                    // figure out the last referenced argument so we can skip
                    // any that aren't actually referenced
                    int lastRef = m_parameterDeclarations.Length - 1;

                    // if we're not known at compile time, then we can't leave off unreferenced parameters
                    // (also don't leave things off if we're not hypercrunching)
                    // (also check the kill flag for removing unused parameters)
                    if (Parser.Settings.RemoveUnneededCode &&
                        m_functionScope.IsKnownAtCompileTime &&
                        Parser.Settings.MinifyCode &&
                        Parser.Settings.IsModificationAllowed(TreeModifications.RemoveUnusedParameters))
                    {
                        while (lastRef >= 0)
                        {
                            // we want to loop backwards until we either find a parameter that is referenced.
                            // at that point, lastRef will be the index of the last referenced parameter so
                            // we can output from 0 to lastRef
                            JSArgumentField argumentField = m_parameterDeclarations[lastRef].Field;
                            if (argumentField != null && !argumentField.IsReferenced)
                            {
                                --lastRef;
                            }
                            else
                            {
                                // found a referenced parameter, or something weird -- stop looking
                                break;
                            }
                        }
                    }

                    for (int ndx = 0; ndx <= lastRef; ++ndx)
                    {
                        if (ndx > 0)
                        {
                            sb.Append(',');
                        }
                        sb.Append(m_parameterDeclarations[ndx].Name);
                    }
                }

                sb.Append(')');
                if (Body != null)
                {
                    if (Body.Count == 0)
                    {
                        sb.Append("{}");
                    }
                    else
                    {
                        sb.Append(Body.ToCode(ToCodeFormat.AlwaysBraces));
                    }
                }

                if (LeftHandFunctionExpression)
                {
                    sb.Append(')');
                }
            }
            return(sb.ToString());
        }
Exemple #5
0
        internal virtual void AnalyzeScope()
        {
            // check for unused local fields or arguments
            foreach (JSVariableField variableField in m_nameTable.Values)
            {
                JSLocalField locField = variableField as JSLocalField;
                if (locField != null && !locField.IsReferenced && locField.OriginalContext != null)
                {
                    if (locField.FieldValue is FunctionObject)
                    {
                        Context ctx = ((FunctionObject)locField.FieldValue).IdContext;
                        if (ctx == null)
                        {
                            ctx = locField.OriginalContext;
                        }
                        ctx.HandleError(JSError.FunctionNotReferenced, false);
                    }
                    else if (!locField.IsGenerated)
                    {
                        JSArgumentField argumentField = locField as JSArgumentField;
                        if (argumentField != null)
                        {
                            // we only want to throw this error if it's possible to remove it
                            // from the argument list. And that will only happen if there are
                            // no REFERENCED arguments after this one in the formal parameter list.
                            // Assertion: because this is a JSArgumentField, this should be a function scope,
                            // let's walk up to the first function scope we find, just in case.
                            FunctionScope functionScope = this as FunctionScope;
                            if (functionScope == null)
                            {
                                ActivationObject scope = this.Parent;
                                while (scope != null)
                                {
                                    functionScope = scope as FunctionScope;
                                    if (scope != null)
                                    {
                                        break;
                                    }
                                }
                            }
                            if (functionScope == null || functionScope.IsArgumentTrimmable(argumentField))
                            {
                                locField.OriginalContext.HandleError(
                                    JSError.ArgumentNotReferenced,
                                    false
                                    );
                            }
                        }
                        else if (locField.OuterField == null || !locField.OuterField.IsReferenced)
                        {
                            locField.OriginalContext.HandleError(
                                JSError.VariableDefinedNotReferenced,
                                false
                                );
                        }
                    }
                }
            }

            // rename fields if we need to
            RenameFields();

            // recurse
            foreach (ActivationObject activationObject in m_childScopes)
            {
                try
                {
                    Parser.ScopeStack.Push(activationObject);
                    activationObject.AnalyzeScope();
                }
                finally
                {
                    Parser.ScopeStack.Pop();
                }
            }
        }
Exemple #6
0
        internal bool IsArgumentTrimmable(JSArgumentField targetArgumentField)
        {
            // walk backward until we either find the given argument field or the
            // first parameter that is referenced.
            // If we find the argument field, then we can trim it because there are no
            // referenced parameters after it.
            // if we find a referenced argument, then the parameter is not trimmable.
            JSArgumentField argumentField = null;
            if (m_parameterDeclarations != null)
            {
                for (int index = m_parameterDeclarations.Length - 1; index >= 0; --index)
                {
                    argumentField = m_parameterDeclarations[index].Field;
                    if (argumentField != null
                        && (argumentField == targetArgumentField || argumentField.IsReferenced))
                    {
                        break;
                    }
                }
            }

            // if the argument field we landed on is the same as the target argument field,
            // then we found the target argument BEFORE we found a referenced parameter. Therefore
            // the argument can be trimmed.
            return (argumentField == targetArgumentField);
        }
Exemple #7
0
 internal bool IsArgumentTrimmable(JSArgumentField argumentField)
 {
     return m_owningFunctionObject.IsArgumentTrimmable(argumentField);
 }
Exemple #8
0
 /*
 internal void Remove(JSLocalField localField)
 {
     NameTable.Remove(localField.Name);
     FieldTable.Remove(localField);
 }
 */
 internal JSArgumentField AddNewArgumentField(String name)
 {
     JSArgumentField result = new JSArgumentField(name, Missing.Value);
     AddField(result);
     return result;
 }
Exemple #9
0
 internal bool IsArgumentTrimmable(JSArgumentField argumentField)
 {
     return(m_owningFunctionObject.IsArgumentTrimmable(argumentField));
 }