public void SetValue(object value, ExecutionContext context)
 {
     try
     {
         this.CheckTypeConstraintViolation();
         if (context.PSDebug > 1 && !this.IsInternalCode)
         {
             string str    = PSObject.ToStringParser(context, value);
             int    length = 60 - this._varName.Length;
             if (str.Length > length)
             {
                 str = str.Substring(0, length) + "...";
             }
             ScriptTrace.Trace(context, 1, "TraceVariableAssignment", (object)this._varName, (object)str);
         }
         this.PrivateSetValue(value, context);
     }
     catch (RuntimeException ex)
     {
         if (ex.ErrorRecord != null && ex.ErrorRecord.InvocationInfo == null)
         {
             ex.ErrorRecord.SetInvocationInfo(new InvocationInfo((CommandInfo)null, this.NodeToken, context));
         }
         throw;
     }
 }
Exemple #2
0
        internal static ExceptionHandlerNode GetHandler(
            ReadOnlyCollection <ExceptionHandlerNode> handlers,
            Exception exception,
            ExecutionContext context)
        {
            ExceptionTypeList.Comparer comparer1 = new ExceptionTypeList.Comparer(ExceptionTypeList.CompareEqual);
            Type matchedType = (Type)null;

            foreach (ExceptionHandlerNode handler in handlers)
            {
                if (handler.ExceptionTypes != null && handler.ExceptionTypes.Matches(comparer1, exception.GetType(), out matchedType))
                {
                    ScriptTrace.Trace(context, 1, "TrapOnExceptionMatch", (object)exception.GetType());
                    return(handler);
                }
            }
            ExceptionTypeList.Comparer comparer2 = new ExceptionTypeList.Comparer(ExceptionTypeList.CompareSubclass);
            foreach (ExceptionHandlerNode handler in handlers)
            {
                if (handler.ExceptionTypes != null && handler.ExceptionTypes.Matches(comparer2, exception.GetType(), out matchedType))
                {
                    ScriptTrace.Trace(context, 1, "TrapOnSubclassMatch", (object)matchedType, (object)exception.GetType());
                    return(handler);
                }
            }
            foreach (ExceptionHandlerNode handler in handlers)
            {
                if (handler.ExceptionTypes == null)
                {
                    ScriptTrace.Trace(context, 1, "TrapOnGenericException", (object)exception.GetType());
                    return(handler);
                }
            }
            return((ExceptionHandlerNode)null);
        }
Exemple #3
0
        internal static void Trace(int level, string messageId, params object[] args)
        {
            ExecutionContext executionContextFromTls = LocalPipeline.GetExecutionContextFromTLS();

            if (executionContextFromTls == null)
            {
                return;
            }
            ScriptTrace.Trace(executionContextFromTls, level, messageId, args);
        }
        private void ExecuteStatement(
            ParseTreeNode statement,
            Array input,
            Pipe outputPipe,
            ref ArrayList resultList,
            ExecutionContext context)
        {
            ScriptTrace.TraceLine(context, statement);
            Exception e = (Exception)null;

            try
            {
                try
                {
                    if (statement.IsVoidable)
                    {
                        statement.Execute(input, (Pipe)null, context);
                    }
                    else
                    {
                        statement.Execute(input, outputPipe, ref resultList, context);
                    }
                    ParseTreeNode parseTreeNode = statement;
                    while (parseTreeNode is AssignmentStatementNode assignmentStatementNode)
                    {
                        parseTreeNode = assignmentStatementNode.RightHandSide;
                    }
                    if (parseTreeNode.IsExpression)
                    {
                        context.QuestionMarkVariableValue = true;
                    }
                    else if (parseTreeNode is PipelineNode pipelineNode)
                    {
                        context.QuestionMarkVariableValue = !pipelineNode.ExecutionFailed(context);
                    }
                }
                catch (COMException ex)
                {
                    throw InterpreterError.NewInterpreterExceptionWithInnerException((object)null, typeof(RuntimeException), statement.NodeToken, "COMException", (Exception)ex, (object)ex.Message);
                }
                catch (InvalidComObjectException ex)
                {
                    throw InterpreterError.NewInterpreterExceptionWithInnerException((object)null, typeof(RuntimeException), statement.NodeToken, "InvalidComObjectException", (Exception)ex, (object)ex.Message);
                }
            }
            catch (ReturnException ex)
            {
                if (resultList == null || resultList.Count == 0)
                {
                    e = (Exception)ex;
                }
                else
                {
                    ParseTreeNode.AppendResult(context, ex.Argument, (Pipe)null, ref resultList);
                    ex.SetArgument((object)resultList.ToArray());
                    resultList = (ArrayList)null;
                    e          = (Exception)ex;
                }
            }
            catch (RuntimeException ex)
            {
                e = this.HandleException(statement.NodeToken, ex, outputPipe, ref resultList, context);
            }
            if (e != null)
            {
                RuntimeException.LockStackTrace(e);
                throw e;
            }
        }
Exemple #5
0
        private void RunClause(ParseTreeNode clause, object dollarUnderbar, object inputToProcess)
        {
            if (clause == null)
            {
                return;
            }
            if (this.Context.Debugger.IsOn)
            {
                this.Context.Debugger.CheckCommand(this.Command.MyInvocation);
            }
            bool flag = false;

            try
            {
                this.Context.Debugger.PushMethodCall(this.Command.MyInvocation, this._scriptBlock);
                if (this.FromScriptFile)
                {
                    if (this.CommandInfo is ExternalScriptInfo commandInfo)
                    {
                        this.Context.Debugger.PushRunning(commandInfo.Path, this._scriptBlock, true);
                    }
                    else
                    {
                        this.Context.Debugger.PushRunning(this.CommandInfo.Name, this._scriptBlock, true);
                    }
                    flag = true;
                }
                else if (this._scriptBlock.File != null && this._scriptBlock.File.Length > 0)
                {
                    this.Context.Debugger.PushRunning(this._scriptBlock.File, this._scriptBlock, false);
                    flag = true;
                }
                if (this.Context.PSDebug > 1)
                {
                    if (string.IsNullOrEmpty(this.CommandInfo.Name))
                    {
                        ScriptTrace.Trace(this.context, 1, "TraceEnteringScriptBlock");
                    }
                    else if (string.IsNullOrEmpty(this._scriptBlock.File))
                    {
                        ScriptTrace.Trace(this.context, 1, "TraceEnteringFunction", (object)this.CommandInfo.Name);
                    }
                    else
                    {
                        ScriptTrace.Trace(this.context, 1, "TraceEnteringFunctionDefinedInFile", (object)this.CommandInfo.Name, (object)this._scriptBlock.File);
                    }
                }
                this.EnterScope();
                if (!this._scriptBlock.IsSynthesized)
                {
                    if (inputToProcess != AutomationNull.Value)
                    {
                        if (inputToProcess == null)
                        {
                            this.Context.InputVariable = (object)MshCommandRuntime.StaticEmptyArray.GetEnumerator();
                        }
                        else
                        {
                            IList list = inputToProcess as IList;
                            this.Context.InputVariable = list != null ? (object)list.GetEnumerator() : (object)LanguagePrimitives.GetEnumerator(inputToProcess);
                        }
                    }
                    if (dollarUnderbar != AutomationNull.Value)
                    {
                        this.Context.UnderbarVariable = dollarUnderbar;
                    }
                }
                object sendToPipeline          = (object)AutomationNull.Value;
                Pipe   functionErrorOutputPipe = this.Context.ShellFunctionErrorOutputPipe;
                try
                {
                    if (this.commandRuntime.MergeMyErrorOutputWithSuccess)
                    {
                        this.Context.RedirectErrorPipe(this.commandRuntime.OutputPipe);
                    }
                    else if (this.commandRuntime.ErrorOutputPipe.IsRedirected)
                    {
                        this.Context.RedirectErrorPipe(this.commandRuntime.ErrorOutputPipe);
                    }
                    sendToPipeline = this.ExecuteWithCatch(clause, (Array)null);
                }
                finally
                {
                    this.Context.RestoreErrorPipe(functionErrorOutputPipe);
                    this.ExitScope();
                }
                if (sendToPipeline == AutomationNull.Value)
                {
                    return;
                }
                IEnumerator enumerator = LanguagePrimitives.GetEnumerator(sendToPipeline);
                if (enumerator != null)
                {
                    while (enumerator.MoveNext())
                    {
                        this.commandRuntime._WriteObjectSkipAllowCheck(enumerator.Current);
                    }
                }
                else
                {
                    this.commandRuntime._WriteObjectSkipAllowCheck(sendToPipeline);
                }
            }
            finally
            {
                this.Context.Debugger.PopMethodCall();
                if (flag)
                {
                    this.Context.Debugger.PopRunning();
                }
            }
        }