Microsoft.JScript.JSInProcCompiler.Compile C# (CSharp) Method

Compile() private method

private Compile ( CompilerParameters options, string partialCmdLine, string sourceFiles, string outputFile ) : int
options System.CodeDom.Compiler.CompilerParameters
partialCmdLine string
sourceFiles string
outputFile string
return int
      internal int Compile(CompilerParameters options, string partialCmdLine, string[] sourceFiles, string outputFile){
        // write compiler output to a stream with outputFile as the name
        StreamWriter output = null;
        int nativeReturnValue = 0;
        try{
          // Compiler output must be UTF-8 encoded
          output = new StreamWriter(outputFile);
          output.AutoFlush = true;
          if (options.IncludeDebugInformation){
            Debug.Assert(partialCmdLine != null);
            this.PrintOptions(output, options);
            this.debugCommandLine = partialCmdLine;
          }
          VsaEngine engine = null;
          try{
            engine = this.CreateAndInitEngine(options, sourceFiles, outputFile, output);
          }catch(CmdLineException e){
            output.WriteLine(e.Message);
            nativeReturnValue = 10;
          }catch(Exception e){
            output.WriteLine("fatal error JS2999: " + e);
            nativeReturnValue = 10;
          }catch{
            output.WriteLine("fatal error JS2999");
            nativeReturnValue = 10;
          }
          if (engine == null)
            return nativeReturnValue;
          if (options.IncludeDebugInformation){
            // this.debugCommandLine was completed during CreateAndInitEngine (except for filenames)
            StringBuilder fullCmdLine = new StringBuilder(this.debugCommandLine);
            foreach (string sourceFile in sourceFiles){
              fullCmdLine.Append(" \"");
              fullCmdLine.Append(sourceFile);
              fullCmdLine.Append("\"");
            }
            this.debugCommandLine = fullCmdLine.ToString();
            // write the full command line to a response file for debugging
            string responseFileName = options.TempFiles.AddExtension("cmdline");
            StreamWriter responseFile = null;
            try{
              responseFile = new StreamWriter(responseFileName);
              responseFile.WriteLine(this.debugCommandLine);
              responseFile.Flush();
            }finally{
              if (responseFile != null)
                responseFile.Close();
            }
            // show full debug command line in compiler output
            StringBuilder sb = new StringBuilder();
            sb.Append(Environment.NewLine);
            sb.Append(JScriptException.Localize("CmdLine helper", CultureInfo.CurrentUICulture));
            sb.Append(":");
            sb.Append(Environment.NewLine);
            sb.Append("    ");
            sb.Append(options.TempFiles.TempDir);
            sb.Append("> jsc.exe @\"");
            sb.Append(responseFileName);
            sb.Append("\"");
            sb.Append(Environment.NewLine);
            output.WriteLine(sb.ToString());
            this.PrintBanner(engine, output);
          }
          try{
            if (!engine.Compile())
              nativeReturnValue = 10;
            else
              nativeReturnValue = 0;
          }catch(VsaException e){
            // check for expected errors
            if (e.ErrorCode == VsaError.AssemblyExpected){
              if (e.InnerException != null && e.InnerException is System.BadImageFormatException){
                // the reference was not for an assembly
                CmdLineException cmdLineError = new CmdLineException(CmdLineError.InvalidAssembly, e.Message, engine.ErrorCultureInfo);
                output.WriteLine(cmdLineError.Message);
              }else if (e.InnerException != null && e.InnerException is System.IO.FileNotFoundException){
                // the referenced file not valid
                CmdLineException cmdLineError = new CmdLineException(CmdLineError.AssemblyNotFound, e.Message, engine.ErrorCultureInfo);
                output.WriteLine(cmdLineError.Message);
              }else{
                CmdLineException cmdLineError = new CmdLineException(CmdLineError.InvalidAssembly, engine.ErrorCultureInfo);
                output.WriteLine(cmdLineError.Message);
              }
            }else if (e.ErrorCode == VsaError.SaveCompiledStateFailed){
              CmdLineException cmdLineError = new CmdLineException(CmdLineError.ErrorSavingCompiledState, e.Message, engine.ErrorCultureInfo);
              output.WriteLine(cmdLineError.Message);
            }else{
              output.WriteLine(JScriptException.Localize("INTERNAL COMPILER ERROR", engine.ErrorCultureInfo));
              output.WriteLine(e);
            }
            nativeReturnValue = 10;
          }catch(Exception e){
            output.WriteLine(JScriptException.Localize("INTERNAL COMPILER ERROR", engine.ErrorCultureInfo));
            output.WriteLine(e);
            nativeReturnValue = 10;
          }catch{
            output.WriteLine(JScriptException.Localize("INTERNAL COMPILER ERROR", engine.ErrorCultureInfo));
            nativeReturnValue = 10;
          }
        }finally{
          if (output != null)
            output.Close();
        }
        return nativeReturnValue;
      }

Usage Example

      protected override CompilerResults FromFileBatch(CompilerParameters options, string[] fileNames){
        string outputFile = options.TempFiles.AddExtension("out");
        CompilerResults results = new CompilerResults(options.TempFiles);

        if (options.OutputAssembly == null || options.OutputAssembly.Length == 0) {
          options.OutputAssembly = results.TempFiles.AddExtension("dll", !options.GenerateInMemory);
        }

        // For debug mode, we construct a command line for debugging with JSC.  The command line is
        // only partially formed at this stage; the rest of it will be added and written to a response
        // file by JSInProcCompiler.

        string partialCmdLine = null;
        if (options.IncludeDebugInformation){
          results.TempFiles.AddExtension("ildb");
          partialCmdLine = this.CmdArgsFromParameters(options);
        }

        results.NativeCompilerReturnValue = 0;
        try {
          JSInProcCompiler jsc = new JSInProcCompiler();
          results.NativeCompilerReturnValue = jsc.Compile(options, partialCmdLine, fileNames, outputFile);
        } catch {
          // internal compiler error
          results.NativeCompilerReturnValue = 10;
        }

        // parse the output looking for errors and warnings
        try {
          StreamReader compilerOutput = new StreamReader(outputFile);
          try {
            string line = compilerOutput.ReadLine();
            while (line != null) {
              results.Output.Add(line);
              ProcessCompilerOutputLine(results, line);
              line = compilerOutput.ReadLine();
            }
          } finally {
            compilerOutput.Close();
          }
        }catch(Exception e){
          // could not open output file -- provide some other error information
          results.Output.Add(JScriptException.Localize("No error output", CultureInfo.CurrentUICulture));
          results.Output.Add(e.ToString());
        }catch{
          // could not open output file -- provide some other error information
          results.Output.Add(JScriptException.Localize("No error output", CultureInfo.CurrentUICulture));
        }
        if ((results.NativeCompilerReturnValue == 0) && options.GenerateInMemory) {
          FileStream fs = new FileStream(options.OutputAssembly, FileMode.Open, FileAccess.Read, FileShare.Read);
          try {
            int fileLen = (int)fs.Length;
            byte[] b = new byte[fileLen];
            fs.Read(b, 0, fileLen);
            results.CompiledAssembly = Assembly.Load(b, null, options.Evidence);
          }finally {
            fs.Close();
          }
        }else{
          results.PathToAssembly = Path.GetFullPath(options.OutputAssembly);
        }
        results.Evidence = options.Evidence;
        return results;
      }