/// <summary> /// Parse a script /// </summary> /// <param name="container">Script container</param> /// <returns>An array of errors, if no errors then empty</returns> public ScriptError[] Parse(ScriptContainer container) { ScriptErrorListener listener = new ScriptErrorListener(); try { foreach (AssemblyName name in container.ReferencedNames) { _engine.Runtime.LoadAssembly(Assembly.Load(name)); } if (container.EnableDebug) { string newFile = Path.GetTempFileName(); ScriptUtils.AddTempFile(newFile); File.WriteAllText(newFile, container.Script); _source = _engine.CreateScriptSourceFromFile(newFile); } else { _source = _engine.CreateScriptSourceFromString(container.Script); } _code = _source.Compile(listener); if (listener.Errors.Count == 0) { // Just create the global scope, don't execute it yet _scope = _engine.CreateScope(); _scope.SetVariable("CANAPEScriptContainer", new ScriptContainer(container)); } } catch (Exception ex) { listener.Errors.Add(new ScriptError(ex.Message, "FatalError", 0, 0)); } return(listener.Errors.ToArray()); }
/// <summary> /// /// </summary> /// <param name="container"></param> /// <returns></returns> public override ScriptError[] Parse(ScriptContainer container) { List <ScriptError> errors = new List <ScriptError>(); try { CompilerParameters compileParams = new CompilerParameters(); TempFileCollection tempFiles = new TempFileCollection(Path.GetTempPath(), container.EnableDebug); compileParams.GenerateExecutable = false; compileParams.GenerateInMemory = true; compileParams.IncludeDebugInformation = true; compileParams.TempFiles = tempFiles; compileParams.ReferencedAssemblies.Add("System.dll"); compileParams.ReferencedAssemblies.Add("Microsoft.CSharp.dll"); compileParams.ReferencedAssemblies.Add("System.Core.dll"); compileParams.ReferencedAssemblies.Add("System.Numerics.dll"); foreach (AssemblyName name in container.ReferencedNames) { Assembly asm = Assembly.Load(name); if (!String.IsNullOrWhiteSpace(asm.Location)) { compileParams.ReferencedAssemblies.Add(asm.Location); } else { byte[] assemblyData = GetDataForAssembly(name); if (assemblyData != null) { string fileName = Path.GetTempFileName(); File.WriteAllBytes(fileName, assemblyData); tempFiles.AddFile(fileName, false); compileParams.ReferencedAssemblies.Add(fileName); } else { errors.Add(new ScriptError(String.Format(Properties.Resources.DotNetScriptEngine_CannotGetAssemblyLocation, name), Properties.Resources.Scripting_Warning, 1, 1)); } } } if (!String.IsNullOrEmpty(_options)) { compileParams.CompilerOptions = _options; } List <string> scripts = new List <string>(); scripts.Add(container.Script); string src = CreateScriptContainerSource(container); if (!String.IsNullOrWhiteSpace(src)) { scripts.Add(src); } CompilerResults results = _provider.CompileAssemblyFromSource(compileParams, scripts.ToArray()); if (results.Errors.HasErrors) { foreach (CompilerError e in results.Errors) { errors.Add(new ScriptError(e.ErrorText, Properties.Resources.Scripting_Error, e.Line, e.Column)); } } else { _assembly = results.CompiledAssembly; } if (container.EnableDebug) { foreach (string file in tempFiles) { ScriptUtils.AddTempFile(file); } } } catch (Exception ex) { errors.Add(new ScriptError(String.Format(Properties.Resources.DotNetScriptEngine_CompileException, ex.Message), Properties.Resources.Scripting_FatalError, 0, 0)); } // Test Code foreach (ScriptError error in errors) { System.Diagnostics.Trace.WriteLine(error.Description); } return(errors.ToArray()); }