/// <summary> /// Run the script and print the output to a new output window. /// </summary> public int ExecuteScript(string source, string sourcePath) { try { var engine = CreateEngine(); var scope = SetupEnvironment(engine); var scriptOutput = new ScriptOutput(); scriptOutput.Show(); var outputStream = new ScriptOutputStream(scriptOutput, engine); scope.SetVariable("__window__", scriptOutput); scope.SetVariable("__file__", sourcePath); // Add script directory address to sys search paths var path = engine.GetSearchPaths(); path.Add(System.IO.Path.GetDirectoryName(sourcePath)); engine.SetSearchPaths(path); engine.Runtime.IO.SetOutput(outputStream, Encoding.UTF8); engine.Runtime.IO.SetErrorOutput(outputStream, Encoding.UTF8); engine.Runtime.IO.SetInput(outputStream, Encoding.UTF8); var script = engine.CreateScriptSourceFromString(source, SourceCodeKind.Statements); var errors = new ErrorReporter(); var command = script.Compile(errors); if (command == null) { // compilation failed _message = string.Join("\n", errors.Errors); return(-1); } try { script.Execute(scope); _message = (scope.GetVariable("__message__") ?? "").ToString(); return((int)(scope.GetVariable("__result__") ?? 0)); } catch (SystemExitException) { // ok, so the system exited. That was bound to happen... return(0); } catch (Exception exception) { // show (power) user everything! _message = exception.ToString(); return(-1); } } catch (Exception ex) { _message = ex.ToString(); return(-1); } }
/// <summary> /// Run the script and print the output to a new output window. /// </summary> public int ExecuteScript(string source) { try { var engine = CreateEngine(); var scope = SetupEnvironment(engine); var scriptOutput = new ScriptOutput(); scriptOutput.Show(); var outputStream = new ScriptOutputStream(scriptOutput, engine); scope.SetVariable("__window__", scriptOutput); engine.Runtime.IO.SetOutput(outputStream, Encoding.UTF8); engine.Runtime.IO.SetErrorOutput(outputStream, Encoding.UTF8); engine.Runtime.IO.SetInput(outputStream, Encoding.UTF8); var script = engine.CreateScriptSourceFromString(source, SourceCodeKind.Statements); try { script.Execute(scope); _message = (scope.GetVariable("__message__") ?? "").ToString(); return((int)(scope.GetVariable("__result__") ?? Result.Succeeded)); } catch (SystemExitException) { // ok, so the system exited. That was bound to happen... return((int)Result.Succeeded); } catch (Exception exception) { // show (power) user everything! _message = exception.ToString(); return((int)Result.Failed); } } catch (Exception ex) { _message = ex.ToString(); return((int)Result.Failed); } }
/// <summary> /// Run the script and print the output to a new output window. /// </summary> public int ExecuteScript(string source, string sourcePath) { try { var engine = CreateEngine(); var scope = SetupEnvironment(engine); var scriptOutput = new ScriptOutput(); scriptOutput.Show(); var outputStream = new ScriptOutputStream(scriptOutput, engine); scope.SetVariable("__window__", scriptOutput); scope.SetVariable("__file__", sourcePath); engine.Runtime.IO.SetOutput(outputStream, Encoding.UTF8); engine.Runtime.IO.SetErrorOutput(outputStream, Encoding.UTF8); engine.Runtime.IO.SetInput(outputStream, Encoding.UTF8); var script = engine.CreateScriptSourceFromString(source, SourceCodeKind.Statements); var errors = new ErrorReporter(); var command = script.Compile(errors); if (command == null) { // compilation failed _message = string.Join("\n", errors.Errors); return (int)Result.Failed; } try { script.Execute(scope); _message = (scope.GetVariable("__message__") ?? "").ToString(); return (int)(scope.GetVariable("__result__") ?? Result.Succeeded); } catch (SystemExitException) { // ok, so the system exited. That was bound to happen... return (int)Result.Succeeded; } catch (Exception exception) { // show (power) user everything! _message = exception.ToString(); return (int)Result.Failed; } } catch (Exception ex) { _message = ex.ToString(); return (int)Result.Failed; } }