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;
}