public override bool Render(RenderContext dest) { // Statement dest.Append("try"); Code.Render(dest); // Catch clauses foreach (var cc in CatchClauses) { cc.Render(dest); } // Finally clause if (FinallyClause != null) { dest.Append("finally"); FinallyClause.RenderIndented(dest); } return(false); }
public override bool Render(RenderContext dest) { // Get obfuscated name before we enter our own scope string strObfuscatedName = dest.Symbols.GetObfuscatedSymbol(Name); // Enter a new symbol scope and tell symbol allocator // about our local symbols dest.EnterScope(Scope); // `function` dest.Append("function"); // Function name not present for anonymous functions if (Name != null) { dest.Append(strObfuscatedName); } // Parameters dest.Append('('); for (int i = 0; i < Parameters.Count; i++) { if (i > 0) { dest.Append(','); } Parameters[i].Render(dest); } dest.Append(")"); // Body of the function Code.Render(dest); // Clean up scope and we're finished dest.LeaveScope(); return(false); }
public override bool Render(RenderContext dest) { // Enter a new symbol scope since the exception variable // can be obfuscated dest.EnterScope(Scope); // Catch clause dest.StartLine(); dest.Append("catch("); dest.Append(dest.Symbols.GetObfuscatedSymbol(ExceptionVariable)); if (Condition != null) { dest.Append("if"); Condition.Render(dest); } dest.Append(')'); // Associated code Code.Render(dest); // Done dest.LeaveScope(); return(false); }
// Compile all loaded script to a string public string CompileJavascriptToString() { // Create a symbol allocator SymbolAllocator SymbolAllocator = new SymbolAllocator(this); // Don't let the symbol allocator use any reserved words or common Javascript bits // We only go up to three letters - symbol allocation of more than 3 letters is // highly unlikely. // (based on list here: http://www.quackit.com/javascript/javascript_reserved_words.cfm) string[] words = new string[] { "if", "in", "do", "for", "new", "var", "int", "try", "NaN", "ref", "sun", "top" }; foreach (var s in words) { SymbolAllocator.ClaimSymbol(s); } // Create a member allocator SymbolAllocator MemberAllocator = new SymbolAllocator(this); // Render RenderContext r = new RenderContext(this, SymbolAllocator, MemberAllocator); // Process all files bool bNeedSemicolon = false; foreach (var file in m_files) { Console.WriteLine("Processing {0}...", System.IO.Path.GetFileName(file.filename)); // Create a tokenizer and parser Warnings = new List <Warning>(); WarningsEnabledStack = new Stack <bool>(); Tokenizer t = new Tokenizer(this, file.content, file.filename, file.warnings); Parser p = new Parser(t); // Create the global statement block var code = new ast.CodeBlock(null, TriState.No); // Parse the file into a namespace p.ParseStatements(code); // Ensure everything processed if (t.more) { throw new CompileError("Unexpected end of file", t); } // Dump the abstract syntax tree if (DumpAST) { code.Dump(0); } // Create the root symbol scope and build scopes for all // constained function scopes SymbolScope rootScope = new SymbolScope(this, null, Accessibility.Public); SymbolScope rootPseudoScope = new SymbolScope(this, null, Accessibility.Public); code.Visit(new VisitorScopeBuilder(rootScope, rootPseudoScope)); // Combine consecutive var declarations into a single one code.Visit(new VisitorCombineVarDecl(rootScope)); // Find all variable declarations code.Visit(new VisitorSymbolDeclaration(rootScope, rootPseudoScope)); // Do lint stuff code.Visit(new VisitorLint(rootScope, rootPseudoScope)); // Try to eliminate const declarations if (DetectConsts && !NoObfuscate) { code.Visit(new VisitorConstDetectorPass1(rootScope)); code.Visit(new VisitorConstDetectorPass2(rootScope)); code.Visit(new VisitorConstDetectorPass3(rootScope)); } // Simplify expressions code.Visit(new VisitorSimplifyExpressions()); // If obfuscation is allowed, find all in-scope symbols and then // count the frequency of their use. if (!NoObfuscate) { code.Visit(new VisitorSymbolUsage(rootScope)); } // Process all symbol scopes, applying default accessibility levels // and determining the "rank" of each symbol rootScope.Prepare(); // Dump scopes to stdout if (DumpScopes) { rootScope.Dump(0); } // Tell the global scope to claim all locally defined symbols // so they're not re-used (and therefore hidden) by the // symbol allocation rootScope.ClaimSymbols(SymbolAllocator); // Create a credit comment on the first file if (!NoCredit && file == m_files[0]) { int iInsertPos = 0; while (iInsertPos < code.Content.Count && code.Content[iInsertPos].GetType() == typeof(ast.StatementComment)) { iInsertPos++; } code.Content.Insert(iInsertPos, new ast.StatementComment(null, "// Minified by MiniME from toptensoftware.com")); } if (bNeedSemicolon) { r.Append(";"); } // Render it r.EnterScope(rootScope); bNeedSemicolon = code.Render(r); r.LeaveScope(); // Display warnings Warnings.Sort(delegate(Warning w1, Warning w2) { int Compare = w1.Order.file.FileName.CompareTo(w2.Order.file.FileName); if (Compare == 0) { Compare = w1.Order.position - w2.Order.position; } if (Compare == 0) { Compare = w1.OriginalOrder - w2.OriginalOrder; } return(Compare); }); foreach (var w in Warnings) { Console.WriteLine("{0}: {1}", w.Bookmark, w.Message); } } // return the final script string strResult = r.GetGeneratedOutput(); return(strResult); }
/// <summary> /// Parses all loaded scripts as javascript and compiles them to a string. /// </summary> /// <returns>A string containing the minified javascript.</returns> public string CompileJavascriptToString() { // Create a symbol allocator SymbolAllocator SymbolAllocator = new SymbolAllocator(this); // Don't let the symbol allocator use any reserved words or common Javascript bits // We only go up to three letters - symbol allocation of more than 3 letters is // highly unlikely. // (based on list here: http://www.quackit.com/javascript/javascript_reserved_words.cfm) string[] words = new string[] { "if", "in", "do", "for", "new", "var", "int", "try", "NaN", "ref", "sun", "top" }; foreach (var s in words) { SymbolAllocator.ClaimSymbol(s); } // Create a member allocator SymbolAllocator MemberAllocator = new SymbolAllocator(this); // Render RenderContext r = new RenderContext(this, SymbolAllocator, MemberAllocator); // Process all files bool bNeedSemicolon = false; foreach (var file in m_files) { Console.WriteLine("Processing {0}...", System.IO.Path.GetFileName(file.filename)); // Create a tokenizer and parser Warnings = new List<Warning>(); WarningsEnabledStack = new Stack<bool>(); Tokenizer t = new Tokenizer(this, file.content, file.filename, file.warnings); Parser p = new Parser(t); // Create the global statement block var code = new ast.CodeBlock(null, TriState.No); // Parse the file into a namespace p.ParseStatements(code); // Ensure everything processed if (t.more) { throw new CompileError("Unexpected end of file", t); } // Dump the abstract syntax tree if (DumpAST) code.Dump(0); // Create the root symbol scope and build scopes for all // constained function scopes SymbolScope rootScope = new SymbolScope(this, null, Accessibility.Public); SymbolScope rootPseudoScope = new SymbolScope(this, null, Accessibility.Public); code.Visit(new VisitorScopeBuilder(rootScope, rootPseudoScope)); // Combine consecutive var declarations into a single one code.Visit(new VisitorCombineVarDecl(rootScope)); // Find all variable declarations code.Visit(new VisitorSymbolDeclaration(rootScope, rootPseudoScope)); // Do lint stuff code.Visit(new VisitorLint(rootScope, rootPseudoScope)); // Try to eliminate const declarations if (DetectConsts && !NoObfuscate) { code.Visit(new VisitorConstDetectorPass1(rootScope)); code.Visit(new VisitorConstDetectorPass2(rootScope)); code.Visit(new VisitorConstDetectorPass3(rootScope)); } // Simplify expressions code.Visit(new VisitorSimplifyExpressions()); // If obfuscation is allowed, find all in-scope symbols and then // count the frequency of their use. if (!NoObfuscate) { code.Visit(new VisitorSymbolUsage(rootScope)); } // Process all symbol scopes, applying default accessibility levels // and determining the "rank" of each symbol rootScope.Prepare(); // Dump scopes to stdout if (DumpScopes) rootScope.Dump(0); // Tell the global scope to claim all locally defined symbols // so they're not re-used (and therefore hidden) by the // symbol allocation rootScope.ClaimSymbols(SymbolAllocator); // Create a credit comment on the first file if (!NoCredit && file==m_files[0]) { int iInsertPos = 0; while (iInsertPos < code.Content.Count && code.Content[iInsertPos].GetType() == typeof(ast.StatementComment)) iInsertPos++; code.Content.Insert(iInsertPos, new ast.StatementComment(null, "// Minified by MiniME from toptensoftware.com")); } if (bNeedSemicolon) { r.Append(";"); } // Render it r.EnterScope(rootScope); bNeedSemicolon=code.Render(r); r.LeaveScope(); // Display warnings Warnings.Sort(delegate(Warning w1, Warning w2) { int Compare = w1.Order.file.FileName.CompareTo(w2.Order.file.FileName); if (Compare == 0) Compare = w1.Order.position - w2.Order.position; if (Compare == 0) Compare = w1.OriginalOrder - w2.OriginalOrder; return Compare; }); foreach (var w in Warnings) { Console.WriteLine("{0}: {1}", w.Bookmark, w.Message); } } // return the final script string strResult = r.GetGeneratedOutput(); return strResult; }