コード例 #1
0
        /// <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);
            }
        }
コード例 #2
0
        public ScriptOutputStream(ScriptOutput gui, ScriptEngine engine)
        {
            _gui = gui;
            _engine = engine;
            _gui.txtStdOut.KeyPress += KeyPressEventHandler;
            _gui.txtStdOut.KeyDown += KeyDownEventHandler;
            //_gui.Closing += ClosingEventHandler;
            //_gui.Closed += ClosedEventHandler;

            _gui.txtStdOut.Focus();

            _completedLines = new Queue<MemoryStream>();
            _inputBuffer = new MemoryStream();

            _bomCharsLeft = 3; //0xef, 0xbb, 0xbf for UTF-8 (see http://en.wikipedia.org/wiki/Byte_order_mark#Representations_of_byte_order_marks_by_encoding)
        }
コード例 #3
0
        public ScriptOutputStream(ScriptOutput gui, ScriptEngine engine)
        {
            _gui    = gui;
            _engine = engine;
            _gui.txtStdOut.KeyPress += KeyPressEventHandler;
            _gui.txtStdOut.KeyDown  += KeyDownEventHandler;
            //_gui.Closing += ClosingEventHandler;
            //_gui.Closed += ClosedEventHandler;

            _gui.txtStdOut.Focus();

            _completedLines = new Queue <MemoryStream>();
            _inputBuffer    = new MemoryStream();

            _bomCharsLeft = 3; //0xef, 0xbb, 0xbf for UTF-8 (see http://en.wikipedia.org/wiki/Byte_order_mark#Representations_of_byte_order_marks_by_encoding)
        }
コード例 #4
0
        /// <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);
            }
        }
コード例 #5
0
        /// <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;
            }
        }