Пример #1
0
        private static void GenerateDelegateExpression(ScriptGenerator generator, MemberSymbol symbol, DelegateExpression expression)
        {
            ScriptTextWriter writer = generator.Writer;

            bool createDelegate = false;

            if ((expression.Method.Visibility & MemberVisibility.Static) == 0) {
                createDelegate = true;

                writer.Write("ss.Delegate.create(");
                ExpressionGenerator.GenerateExpression(generator, symbol, expression.ObjectReference);
                writer.WriteTrimmed(", ");
            }

            AnonymousMethodSymbol anonymousMethod = expression.Method as AnonymousMethodSymbol;
            if (anonymousMethod == null) {
                // TODO: This probably needs to handle global method roots...

                if (expression.Method.IsGlobalMethod == false) {
                    ExpressionGenerator.GenerateExpression(generator, symbol, expression.ObjectReference);
                    writer.Write(".");
                }
                writer.Write(expression.Method.GeneratedName);
            }
            else {
                writer.Write("function(");
                if ((anonymousMethod.Parameters != null) && (anonymousMethod.Parameters.Count != 0)) {
                    bool obfuscateParams = generator.Options.Minimize;
                    string obfuscationPrefix = null;

                    int paramIndex = 0;
                    foreach (ParameterSymbol parameterSymbol in anonymousMethod.Parameters) {
                        if (paramIndex > 0) {
                            writer.WriteTrimmed(", ");
                        }
                        if (obfuscateParams) {
                            if (paramIndex == 0) {
                                obfuscationPrefix = "$p" + anonymousMethod.Depth.ToString(CultureInfo.InvariantCulture) + "_";
                            }
                            parameterSymbol.SetTransformedName(obfuscationPrefix + paramIndex);
                        }
                        writer.Write(parameterSymbol.GeneratedName);

                        paramIndex++;
                    }
                }
                writer.Write(")");
                writer.WriteTrimmed(" {");
                writer.WriteLine();
                writer.Indent++;
                CodeGenerator.GenerateScript(generator, anonymousMethod);
                writer.Indent--;
                writer.Write("}");
            }

            if (createDelegate) {
                writer.Write(")");
            }
        }
        private static void GenerateDelegateExpression(ScriptGenerator generator, MemberSymbol symbol, DelegateExpression expression)
        {
            ScriptTextWriter writer = generator.Writer;

            AnonymousMethodSymbol anonymousMethod = expression.Method as AnonymousMethodSymbol;
            if (anonymousMethod != null) {
                writer.Write("function(");
                if ((anonymousMethod.Parameters != null) && (anonymousMethod.Parameters.Count != 0)) {
                    int paramIndex = 0;
                    foreach (ParameterSymbol parameterSymbol in anonymousMethod.Parameters) {
                        if (paramIndex > 0) {
                            writer.Write(", ");
                        }
                        writer.Write(parameterSymbol.GeneratedName);

                        paramIndex++;
                    }
                }
                writer.WriteLine(") {");
                writer.Indent++;
                CodeGenerator.GenerateScript(generator, anonymousMethod);
                writer.Indent--;
                writer.Write("}");
            }
            else if ((expression.Method.Visibility & MemberVisibility.Static) != 0) {
                if (expression.Method.IsExtension) {
                    Debug.Assert(expression.Method.Parent.Type == SymbolType.Class);

                    ClassSymbol classSymbol = (ClassSymbol)expression.Method.Parent;
                    Debug.Assert(classSymbol.IsExtenderClass);

                    writer.Write(classSymbol.Extendee);
                    writer.Write(".");
                    writer.Write(expression.Method.GeneratedName);
                }
                else {
                    ExpressionGenerator.GenerateExpression(generator, symbol, expression.ObjectReference);
                    writer.Write(".");
                    writer.Write(expression.Method.GeneratedName);
                }
            }
            else {
                writer.Write("ss.bind('");
                writer.Write(expression.Method.GeneratedName);
                writer.Write("', ");
                ExpressionGenerator.GenerateExpression(generator, symbol, expression.ObjectReference);
                writer.Write(")");
            }
        }