Esempio n. 1
0
        private CommandResult Inspect(ScriptResult result)
        {
            if (result == null)
            {
                return CommandResult.Error;
            }

            if (result.CompileExceptionInfo != null)
            {
                _logger.Error(result.CompileExceptionInfo.SourceException.Message);
                _logger.Debug(result.CompileExceptionInfo.SourceException);
                return CommandResult.Error;
            }

            if (result.ExecuteExceptionInfo != null)
            {
                _logger.Error(result.ExecuteExceptionInfo.SourceException);
                return CommandResult.Error;
            }

            if (!result.IsCompleteSubmission)
            {
                _logger.Error("The script is incomplete.");
                return CommandResult.Error;
            }

            return CommandResult.Success;
        }
        private CommandResult Inspect(ScriptResult result)
        {
            if (result == null)
            {
                return CommandResult.Error;
            }

            if (result.CompileExceptionInfo != null)
            {
                var ex = result.CompileExceptionInfo.SourceException;
                _logger.ErrorException("Script compilation failed.", ex);
                return CommandResult.Error;
            }

            if (result.ExecuteExceptionInfo != null)
            {
                var ex = result.ExecuteExceptionInfo.SourceException;
                _logger.ErrorException("Script execution failed.", ex);
                return CommandResult.Error;
            }

            if (!result.IsCompleteSubmission)
            {
                _logger.Error("The script is incomplete.");
                return CommandResult.Error;
            }

            return CommandResult.Success;
        }
        private void CompileAndExecute(string code, Session session, ScriptResult scriptResult)
        {
            Submission<object> submission = null;

            Logger.Debug("Compiling submission");
            try
            {
                submission = session.CompileSubmission<object>(code);

                var exeBytes = new byte[0];
                var pdbBytes = new byte[0];
                var compileSuccess = false;

                using (var exeStream = new MemoryStream())
                using (var pdbStream = new MemoryStream())
                {
                    var result = submission.Compilation.Emit(exeStream, pdbStream: pdbStream);
                    compileSuccess = result.Success;

                    if (result.Success)
                    {
                        Logger.Debug("Compilation was successful.");
                        exeBytes = exeStream.ToArray();
                        pdbBytes = pdbStream.ToArray();
                    }
                    else
                    {
                        var errors = string.Join(Environment.NewLine, result.Diagnostics.Select(x => x.ToString()));
                        Logger.ErrorFormat("Error occurred when compiling: {0})", errors);
                    }
                }

                if (compileSuccess)
                {
                    var assembly = LoadAssembly(exeBytes, pdbBytes);

                    InvokeEntryPointMethod(session, assembly, scriptResult);
                }
            }
            catch (Exception compileException)
            {
                //we catch Exception rather than CompilationErrorException because there might be issues with assembly loading too
                scriptResult.CompileExceptionInfo =
                    ExceptionDispatchInfo.Capture(new ScriptCompilationException(compileException.Message, compileException));
            }
        }
        protected override ScriptResult Execute(string code, Session session)
        {
            Guard.AgainstNullArgument("session", session);

            var scriptResult = new ScriptResult();

            if (ShouldCompile())
            {
                CompileAndExecute(code, session, scriptResult);
            }
            else
            {
                var assembly = LoadAssemblyFromCache();
                InvokeEntryPointMethod(session, assembly, scriptResult);
            }

            return scriptResult;
        }
        private void InvokeEntryPointMethod(Session session, Assembly assembly, ScriptResult scriptResult)
        {
            Logger.Debug("Retrieving compiled script class (reflection).");

            // the following line can throw NullReferenceException, if that happens it's useful to notify that an error ocurred
            var type = assembly.GetType(CompiledScriptClass);
            Logger.Debug("Retrieving compiled script method (reflection).");
            var method = type.GetMethod(CompiledScriptMethod, BindingFlags.Static | BindingFlags.Public);

            try
            {
                Logger.Debug("Invoking method.");
                scriptResult.ReturnValue = method.Invoke(null, new[] { session });
            }
            catch (Exception executeException)
            {
                var ex = executeException.InnerException ?? executeException;
                scriptResult.ExecuteExceptionInfo = ExceptionDispatchInfo.Capture(ex);
                Logger.Error("An error occurred when executing the scripts.");
            }
        }
Esempio n. 6
0
            public void ShouldReturnCommandsScriptResultfCommandHasReturnValueThatAlreadyIsScriptResult()
            {
                var returnValue = new ScriptResult("hello world");

                var helloCommand = new Mock<IReplCommand>();
                helloCommand.SetupGet(x => x.CommandName).Returns("hello");
                helloCommand.Setup(x => x.Execute(It.IsAny<IRepl>(), It.IsAny<object[]>()))
                    .Returns(returnValue);

                var mocks = new Mocks { ReplCommands = new[] { helloCommand } };
                mocks.ObjectSerializer.Setup(x => x.Serialize(returnValue.ReturnValue)).Returns("hello world");

                _repl = GetRepl(mocks);

                var result = _repl.Execute(":hello", null);

                mocks.ObjectSerializer.Verify(x => x.Serialize(returnValue.ReturnValue), Times.Once);
                mocks.Console.Verify(x => x.WriteLine("hello world"), Times.Once);
                result.ShouldBeSameAs(returnValue);
            }
Esempio n. 7
0
        protected virtual ScriptResult Execute(string code, Session session)
        {
            Guard.AgainstNullArgument("session", session);

            var result = new ScriptResult();
            try
            {
                var submission = session.CompileSubmission<object>(code);
                try
                {
                    result.ReturnValue = submission.Execute();
                }
                catch (Exception ex)
                {
                    result.ExecuteExceptionInfo = ExceptionDispatchInfo.Capture(ex);
                }
            }
            catch (Exception ex)
            {
                 result.UpdateClosingExpectation(ex);
                if (!result.IsPendingClosingChar)
                {
                    result.CompileExceptionInfo = ExceptionDispatchInfo.Capture(ex);
                }
            }

            return result;
        }
Esempio n. 8
0
 public void EvaluateCompleted(ScriptResult result)
 {
     Execute.OnUIThread(() => internalRepl.EvaluateCompleted(result));
 }
        protected override ScriptResult Execute(string code, Session session)
        {
            Guard.AgainstNullArgument("session", session);

            var scriptResult = new ScriptResult();
            Submission<object> submission = null;

            this.Logger.Debug("Compiling submission");
            try
            {
                submission = session.CompileSubmission<object>(code);

                var exeBytes = new byte[0];
                var pdbBytes = new byte[0];
                var compileSuccess = false;

                using (var exeStream = new MemoryStream())
                using (var pdbStream = new MemoryStream())
                {
                    var result = submission.Compilation.Emit(exeStream, pdbStream: pdbStream);
                    compileSuccess = result.Success;

                    if (result.Success)
                    {
                        this.Logger.Debug("Compilation was successful.");
                        exeBytes = exeStream.ToArray();
                        pdbBytes = pdbStream.ToArray();
                    }
                    else
                    {
                        var errors = String.Join(Environment.NewLine, result.Diagnostics.Select(x => x.ToString()));
                        this.Logger.ErrorFormat("Error occurred when compiling: {0})", errors);
                    }
                }

                if (compileSuccess)
                {
                    var assembly = this.LoadAssembly(exeBytes, pdbBytes);

                    Logger.Debug("Retrieving compiled script class (reflection).");

                    // the following line can throw NullReferenceException, if that happens it's useful to notify that an error ocurred
                    var type = assembly.GetType(CompiledScriptClass);
                    Logger.Debug("Retrieving compiled script method (reflection).");
                    var method = type.GetMethod(CompiledScriptMethod, BindingFlags.Static | BindingFlags.Public);

                    try
                    {
                        this.Logger.Debug("Invoking method.");
                        scriptResult.ReturnValue = method.Invoke(null, new[] {session});
                    }
                    catch (Exception executeException)
                    {
                        var ex = executeException.InnerException ?? executeException;
                        scriptResult.ExecuteExceptionInfo = ExceptionDispatchInfo.Capture(ex);
                        this.Logger.Error("An error occurred when executing the scripts.");
                    }
                }
            }
            catch (Exception compileException)
            {
                //we catch Exception rather than CompilationErrorException because there might be issues with assembly loading too
                scriptResult.CompileExceptionInfo = ExceptionDispatchInfo.Capture(new ScriptCompilationException(compileException.Message, compileException));
            }

            return scriptResult;
        }
        protected override ScriptResult Execute(string code, Session session)
        {
            Guard.AgainstNullArgument("session", session);

            var scriptResult = new ScriptResult();
            Submission<object> submission = null;

            _logger.Debug("Compiling submission");
            try
            {
                submission = session.CompileSubmission<object>(code);
            }
            catch (Exception compileException)
            {
                scriptResult.CompileExceptionInfo = ExceptionDispatchInfo.Capture(compileException);
            }

            var exeBytes = new byte[0];
            var pdbBytes = new byte[0];
            var compileSuccess = false;

            using (var exeStream = new MemoryStream())
            using (var pdbStream = new MemoryStream())
            {
                var result = submission.Compilation.Emit(exeStream, pdbStream: pdbStream);
                compileSuccess = result.Success;

                if (result.Success)
                {
                    _logger.Debug("Compilation was successful.");
                    exeBytes = exeStream.ToArray();
                    pdbBytes = pdbStream.ToArray();
                }
                else
                {
                    var errors = String.Join(Environment.NewLine, result.Diagnostics.Select(x => x.ToString()));
                    _logger.ErrorFormat("Error occurred when compiling: {0})", errors);
                }
            }

            if (compileSuccess)
            {
                _logger.Debug("Loading assembly into appdomain.");
                var assembly = AppDomain.CurrentDomain.Load(exeBytes, pdbBytes);
                _logger.Debug("Retrieving compiled script class (reflection).");
                var type = assembly.GetType(CompiledScriptClass);
                _logger.Debug("Retrieving compiled script method (reflection).");
                var method = type.GetMethod(CompiledScriptMethod, BindingFlags.Static | BindingFlags.Public);

                try
                {
                    _logger.Debug("Invoking method.");
                    scriptResult.ReturnValue = method.Invoke(null, new[] { session });
                }
                catch (Exception executeException)
                {
                    scriptResult.ExecuteExceptionInfo = ExceptionDispatchInfo.Capture(executeException);
                    _logger.Error("An error occurred when executing the scripts.");
                    var message =
                        string.Format(
                        "Exception Message: {0} {1}Stack Trace:{2}",
                        executeException.InnerException.Message,
                        Environment.NewLine,
                        executeException.InnerException.StackTrace);
                    throw new ScriptExecutionException(message, executeException);
                }
            }

            return scriptResult;
        }
 public ScriptResultEventArgs(ScriptResult result)
 {
     Result = result;
 }