The Labs \ Source Viewer \ SSCLI \ Microsoft.JScript \ DocumentContext

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. namespace Microsoft.JScript
  16. {
  17.    
  18.     using Microsoft.JScript.Vsa;
  19.     using System;
  20.     using Microsoft.Vsa;
  21.     using System.Reflection;
  22.     using System.Reflection.Emit;
  23.     using System.Collections;
  24.     using System.Diagnostics.SymbolStore;
  25.    
  26.     public class DocumentContext
  27.     {
  28.         internal string documentName;
  29.         internal ISymbolDocumentWriter documentWriter;
  30.         internal int startLine;
  31.         internal int startCol;
  32.         internal int lastLineInSource;
  33.         internal VsaItem sourceItem;
  34.         internal VsaEngine engine;
  35.         // This may be null
  36.         internal bool debugOn;
  37.         private CompilerGlobals _compilerGlobals;
  38.         private SimpleHashtable reportedVariables;
  39.        
  40.         private bool checkForFirst;
  41.         private int firstStartLine;
  42.         private int firstStartCol;
  43.         private int firstEndLine;
  44.         private int firstEndCol;
  45.        
  46.         static internal readonly Guid language = new Guid("3a12d0b6-c26c-11d0-b442-00a0244a1dd2");
  47.         static internal readonly Guid vendor = new Guid("994b45c4-e6e9-11d2-903f-00c04fa302a1");
  48.        
  49.         internal DocumentContext(string name, VsaEngine engine)
  50.         {
  51.             this.documentName = name;
  52.             this.documentWriter = null;
  53.             this.startLine = 0;
  54.             this.startCol = 0;
  55.             this.lastLineInSource = 0;
  56.             this.sourceItem = null;
  57.             this.engine = engine;
  58.             this.debugOn = null == engine ? false : engine.GenerateDebugInfo;
  59.             this._compilerGlobals = null;
  60.             this.reportedVariables = null;
  61.             this.checkForFirst = false;
  62.         }
  63.        
  64.         internal DocumentContext(VsaItem sourceItem)
  65.         {
  66.             if (sourceItem.codebase != null)
  67.                 this.documentName = sourceItem.codebase;
  68.             else {
  69.                 string rootMoniker = sourceItem.engine.RootMoniker;
  70.                 this.documentName = rootMoniker + (rootMoniker.EndsWith("/", StringComparison.Ordinal) ? "" : "/") + sourceItem.Name;
  71.             }
  72.             this.documentWriter = null;
  73.             this.startLine = 0;
  74.             this.startCol = 0;
  75.             this.lastLineInSource = 0;
  76.             this.sourceItem = sourceItem;
  77.             this.engine = sourceItem.engine;
  78.             this.debugOn = null == engine ? false : engine.GenerateDebugInfo;
  79.             this._compilerGlobals = null;
  80.             this.checkForFirst = false;
  81.         }
  82.        
  83.         internal DocumentContext(string documentName, int startLine, int startCol, int lastLineInSource, VsaItem sourceItem)
  84.         {
  85.             this.documentName = documentName;
  86.             this.documentWriter = null;
  87.             this.startLine = startLine;
  88.             this.startCol = startCol;
  89.             this.lastLineInSource = lastLineInSource;
  90.             this.sourceItem = sourceItem;
  91.             this.engine = sourceItem.engine;
  92.             this.debugOn = null == engine ? false : engine.GenerateDebugInfo;
  93.             this._compilerGlobals = null;
  94.             this.checkForFirst = false;
  95.         }
  96.        
  97.         internal CompilerGlobals compilerGlobals {
  98.             get {
  99.                 if (this._compilerGlobals == null)
  100.                     this._compilerGlobals = this.engine.CompilerGlobals;
  101.                 return this._compilerGlobals;
  102.             }
  103.         }
  104.        
  105.         internal void EmitLineInfo(ILGenerator ilgen, int line, int column, int endLine, int endColumn)
  106.         {
  107.             if (debugOn) {
  108.                 if (checkForFirst && line == this.firstStartLine && column == this.firstStartCol && endLine == this.firstEndLine && endColumn == this.firstEndCol)
  109.                     checkForFirst = false;
  110.                 else {
  111.                     if (this.documentWriter == null)
  112.                         this.documentWriter = this.GetSymDocument(documentName);
  113.                     ilgen.MarkSequencePoint(this.documentWriter, this.startLine + line - this.lastLineInSource, this.startCol + column + 1, this.startLine - this.lastLineInSource + endLine, this.startCol + endColumn + 1);
  114.                 }
  115.             }
  116.         }
  117.        
  118.         internal void EmitFirstLineInfo(ILGenerator ilgen, int line, int column, int endLine, int endColumn)
  119.         {
  120.             this.EmitLineInfo(ilgen, line, column, endLine, endColumn);
  121.             this.checkForFirst = true;
  122.             this.firstStartLine = line;
  123.             this.firstStartCol = column;
  124.             this.firstEndLine = endLine;
  125.             this.firstEndCol = endColumn;
  126.         }
  127.        
  128.        
  129.         private ISymbolDocumentWriter GetSymDocument(string documentName)
  130.         {
  131.             SimpleHashtable documents = this.compilerGlobals.documents;
  132.             object document = documents[documentName];
  133.             if (null == document) {
  134.                 document = _compilerGlobals.module.DefineDocument(this.documentName, DocumentContext.language, DocumentContext.vendor, Guid.Empty);
  135.                 documents[documentName] = document;
  136.             }
  137.             return (ISymbolDocumentWriter)document;
  138.         }
  139.        
  140.         //---------------------------------------------------------------------------------------
  141.         // HandleError
  142.         //
  143.         // Handle an error. There are two actions that can be taken when an error occurs:
  144.         // - throwing an exception with no recovering action (eval case)
  145.         // - notify the host that an error occurred and let the host decide whether or not
  146.         // parsing has to continue (the host returns true when parsing has to continue)
  147.         //---------------------------------------------------------------------------------------
  148.        
  149.         internal void HandleError(JScriptException error)
  150.         {
  151.             if (this.sourceItem == null)
  152.                 if (error.Severity == 0)
  153.                     throw error;
  154.                 else
  155.                 //Called from eval
  156.                     return;
  157.             if (!this.sourceItem.engine.OnCompilerError(error))
  158.                 throw new EndOfFile();
  159.             // this exception terminates the parser
  160.         }
  161.        
  162.         internal bool HasAlreadySeenErrorFor(string varName)
  163.         {
  164.             if (this.reportedVariables == null)
  165.                 this.reportedVariables = new SimpleHashtable(8);
  166.             else if (this.reportedVariables[varName] != null)
  167.                 return true;
  168.             this.reportedVariables[varName] = varName;
  169.             return false;
  170.         }
  171.        
  172.     }
  173.    
  174. }

Developer Fusion