public dynamic Execute(Script script) { if (script == null) throw new ArgumentNullException("script"); return script.Execute(this.executionContext); }
public void ThrowActualExceptionInsteadOfTargetInvocationException() { Script task = new Script(); task.BuildEngine = new MockBuild(); task.Code = @"public static void ScriptMain(){ throw new InvalidOperationException(""This is the actual exception.""); }"; Assert.IsTrue(task.Execute(), "Task should have succeeded."); }
public void Execute(Script script) { if (script == null) { throw new ArgumentNullException("script"); } script.Execute(); }
public void AttemptsToReturnANonStringValue() { Script task = new Script(); task.BuildEngine = new MockBuild(); task.Code = @"public static int ScriptMain(){ return 4; }"; Assert.IsTrue(task.Execute(), "Task should have succeeded."); Assert.IsNull(task.ReturnValue, "ReturnValue should only be set when it is a string."); }
public void SetsReturnValue() { Script task = new Script(); task.BuildEngine = new MockBuild(); task.Code = @"public static string ScriptMain(){ return ""Hello World""; }"; Assert.IsTrue(task.Execute(), "Task should have succeeded."); Assert.AreEqual("Hello World", task.ReturnValue, "ReturnValue should have been set."); }
public void Execute() { Script task = new Script(); task.BuildEngine = new MockBuild(); task.Code = @"public static void ScriptMain(){ int x = 1; System.Diagnostics.Debug.WriteLine(x); }"; Assert.IsTrue(task.Execute(), "Task should have succeeded."); }
public void ExecuteWithStrongNameReference() { Script task = new Script(); task.BuildEngine = new MockBuild(); task.Code = @"public static void ScriptMain(){ string x = System.Web.HttpUtility.HtmlEncode(""<tag>""); System.Diagnostics.Debug.WriteLine(x); }"; task.References = new ITaskItem[] { new TaskItem("System.Web") }; Assert.IsTrue(task.Execute(), "Task should have succeeded."); }
public void ExecuteWithImports() { Script task = new Script(); task.BuildEngine = new MockBuild(); task.Code = @"public static void ScriptMain(){ int x = 1; Debug.WriteLine(x); }"; task.Imports = new ITaskItem[] { new TaskItem("System.Diagnostics") }; Assert.IsTrue(task.Execute(), "Task should have succeeded."); }
public string InitScript() { Script = new Script(); if (Settings.Script.Length > 0) { try { Script.GlobalsAdd("MGO", MGO); Script.GlobalsAdd("World", this); Script.GlobalsAdd("Session", session); Script.GlobalsAdd("AiEventHandler", aiEventHandler); Script.GlobalsAdd("GetFrameFactor", new GameAI.GetFrameFactorDelegate(GameAI.GetFrameFactor)); Script.Execute("lr = littleRunner(MGO, World, Session, AiEventHandler, GetFrameFactor)"); Script.GlobalsAdd("lr"); foreach (GameObject go in this.AllElements) { if (go.Name != null && go.Name != "") { Script.GlobalsAdd(go.Name, go); Script.Execute("lr.Handler." + go.Name + " = EventAttrDict()"); } } Script.Execute("handler = lr.Handler"); // very important! because Script.cs needs access to Globals["handler"]. Script.GlobalsAdd("handler"); Script.Execute(Settings.Script); Script.Init = false; } catch (NullReferenceException) { return null; // Script closed game! } catch (Exception e) { return e.GetType().FullName + ":\n" + e.Message; } } return ""; }
private void ScriptInString() { Console.WriteLine("Running script from string..."); var code = @" class script2class: def Goodbye(self): message = 'Goodbye via the script!' return message def GoodbyeWithName(self, name): message = 'Goodbye ' + name return message def GoodbyeWithPerson(self, person): message = 'Goodbye ' + person.Name return message"; var compiled = new Script(); compiled.InitializeFromString(code); Console.WriteLine(compiled.Execute<string>("script2class", "Goodbye")); // single param call Console.WriteLine(compiled.Execute<string>("script2class", "GoodbyeWithName", "Paulo")); // pass instance of complex type var person = new Person { Name = "Paulo Mouat", Address = "Boston" }; Console.WriteLine(compiled.Execute<string>("script2class", "GoodbyeWithPerson", person)); // duck typing var hasNameLikePerson = new HasNameLikePerson { Name = "HAL", Model = 9000 }; Console.WriteLine(compiled.Execute<string>("script2class", "GoodbyeWithPerson", hasNameLikePerson)); Console.WriteLine(); }
private void ScriptInFile() { Console.WriteLine("Running script from file..."); var script = new Script(); script.InitializeFromFile(@"..\..\..\Scripts\script1.py"); // no params call Console.WriteLine(script.Execute<string>("script1class", "Hello")); // single param call Console.WriteLine(script.Execute<string>("script1class", "HelloWithName", "Paulo")); // pass instance of complex type var person = new Person { Name = "Paulo Mouat", Address = "Boston" }; Console.WriteLine(script.Execute<string>("script1class", "HelloWithPerson", person)); // duck typing var hasNameLikePerson = new HasNameLikePerson { Name = "HAL", Model = 9000 }; Console.WriteLine(script.Execute<string>("script1class", "HelloWithPerson", hasNameLikePerson)); // change field in passed type var changeState = new Person { Name = "Paulo Mouat", Address = "Boston" }; Console.WriteLine("Original state: " + changeState); script.Execute<string>("script1class", "ChangeAddress", changeState); Console.WriteLine("New state: " + changeState); Console.WriteLine(); }
private void RuntimeError() { Console.WriteLine("Running script with runtime error..."); var codeWithRuntimeError = @" class script4class: def Message(self): msg = 1/0 return msg"; var compiled = new Script(); try { compiled.InitializeFromString(codeWithRuntimeError); compiled.Execute<string>("script4class", "Message"); } catch (Exception e) { Console.WriteLine(e.GetType() + " - " + e.Message); } Console.WriteLine(); }
private void ExecutionPerf() { Console.WriteLine("Measuring execution performance for a large method..."); const int totalCount = 10000; var sw = new Stopwatch(); const int id = 300; var code = GetLargeMethod(id); var compiled = new Script(); compiled.InitializeFromString(code); for (var i = 0; i < totalCount; ++i) { sw.Start(); compiled.Execute<string>("script300class", "ManyIfStatements", i); sw.Stop(); } Console.WriteLine( string.Format("Executed script a total of {0} times, in {1} ms, averaging {2} ms per run.", totalCount, sw.ElapsedMilliseconds, 1.0 * sw.ElapsedMilliseconds / totalCount )); }