/// <summary> /// Initializes a new instance of the <see cref="Dynamic.Compiler"/> class. /// </summary> /// <param name="project">Project.</param> public Compiler(Project project) { Project = project; Settings = new CompilerSettings() { OutputFile = "DynamicCodeTests-Scene.dll" }; // Settings.AssemblyReferences.Remove("system.dll"); Trace.Log(TraceEventType.Information, "Project=\"{0}\"\nSettings:\n{1}", project.Name, Settings.ToString()); }
/// <summary> /// Initializes a new instance of the <see cref="DynamicCodeTests.UI.ProjectPreferencesDialog"/> class. /// </summary> public ProjectDialog(Window parent, Project project) { Project = project; XML gxml = new XML("CodeWindow.glade", "ProjectDialog", null); gxml.Autoconnect(this); GtkDialog = nbPreferences.Toplevel as Dialog; // GtkDialog.Reparent(parent as Widget); GtkDialog.Modal = GtkDialog.DestroyWithParent = true; storeReferencePaths = new NodeStore(typeof(Project.ReferencePathTreeNode)); nvReferencePaths = new NodeView(storeReferencePaths); swReferencePaths.Add(nvReferencePaths); nvReferencePaths.AppendColumn("Reference Path", new CellRendererText(), "text", 0); Update(); }
/// <summary> /// Executes the code. /// </summary> /// <returns><c>null</c> if successful, otherwise, a <see cref="string"/></returns> /// <param name="code">Code to execute</param> /// <exception cref="InvalidProgramException"> /// Is thrown when a program contains invalid CIL instructions or metadata. /// </exception> public string ExecuteCode(string source, Project project) { Trace.Log(TraceEventType.Information, "source=\"{0}\", project.Name=\"{0}\"", source.Length < 48 ? source : string.Format("string({0})", source.Length), project == null ? "(null)" : project.Name); Project.CodeUsings = CSEvaluator.GetUsing(); Settings.AssemblyReferences = new System.Collections.Generic.List<string>(Project.ReferencePaths); CompilerOutput = new StringBuilder(); Context = new CompilerContext(Settings, new StreamReportPrinter(new StringWriter(CompilerOutput))); CSEvaluator = new Evaluator(Context); CSEvaluator.Run(project.CodeUsings); string r = string.Empty; try { string prefix = "EntityContext RootContext = EntityContext.Root; EntityContext CurrentContext = EntityContext.Current; ICollection<Scene> _scenes = RootContext.OfType<Scene>(); Scene[] Scenes = new Scene[_scenes.Count]; _scenes.CopyTo(Scenes, 0);"; string code = prefix + source; if (code.Contains(";") && !CSEvaluator.Run(code)) { string error = string.Format("Evaluator.Run() == false (source = \"{0}\")", source.Length < 48 ? source : string.Format("string({0})", source.Length)); Trace.Log(TraceEventType.Error, error); throw new InvalidProgramException(error); } else { object result; bool result_set; string input; source += ";"; if (!CSEvaluator.Run(prefix)) { string error = string.Format("Evaluator.Run() == false (prefix = \"{0}\")", prefix.Replace("\n", " ")); Trace.Log(TraceEventType.Error, error); throw new InvalidProgramException(error); } input = CSEvaluator.Evaluate(source, out result, out result_set); if (input != null) { string error = string.Format("Evaluator.Evaluate() != null (code = \"{0}\") = \"{1}\"", source.Replace("\n", " "), input.Replace("\n", " ")); Trace.Log(TraceEventType.Error, error); throw new InvalidProgramException(error); } if (result_set) r += result != null ? result.ToString() : "(null)"; } } catch (Exception ex) { string error = string.Format("Caught exception: " + ex.GetType().Name + ": " + ex.Message); Trace.Log(TraceEventType.Error, error); Console.Error.WriteLine((char)27 + "[31m" + ex.ToString()); } return r.Length > 0 ? string.Concat(r, "\n") : string.Empty; }