コード例 #1
0
ファイル: VariableOps.cs プロジェクト: rsumner31/powershell
        private static bool ThrowStrictModeUndefinedVariable(ExecutionContext executionContext, VariableExpressionAst varAst)
        {
            // In some limited cases, the compiler knows we don't want an error, like when we're backing up
            // $foreach and $switch, which might not be set.  In that case, the ast passed is null.
            if (varAst == null)
            {
                return(false);
            }

            if (executionContext.IsStrictVersion(2))
            {
                return(true);
            }

            if (executionContext.IsStrictVersion(1))
            {
                var parent = varAst.Parent;
                while (parent != null)
                {
                    if (parent is ExpandableStringExpressionAst)
                    {
                        return(false);
                    }
                    parent = parent.Parent;
                }
                return(true);
            }

            return(false);
        }
コード例 #2
0
        internal PSVariable GetVariable(ExecutionContext context)
        {
            if (this._useConstantValue)
            {
                return((PSVariable)null);
            }
            SessionStateInternal engineSessionState = context.EngineSessionState;
            CommandOrigin        scopeOrigin        = engineSessionState.currentScope.ScopeOrigin;

            if (!this._variablePath.IsScopedItem)
            {
                SessionStateScope scope = (SessionStateScope)null;
                return(engineSessionState.GetVariableItem(this._variablePath, out scope, scopeOrigin));
            }
            PSVariable variable = engineSessionState.CurrentActivationRecord.GetVariable(this._activationRecordSlot, scopeOrigin);

            if (variable == null)
            {
                SessionStateScope scope;
                variable = engineSessionState.GetVariableItem(this._variablePath, out scope, scopeOrigin);
                if (variable != null)
                {
                    engineSessionState.CurrentActivationRecord.SetVariable(variable, scope, this._activationRecordSlot);
                }
                else if ((context.IsStrictVersion(2) || !this._inExpandableString && context.IsStrictVersion(1)) && !this.IsInternalCode)
                {
                    throw InterpreterError.NewInterpreterException((object)this.NodeToken, typeof(RuntimeException), this.NodeToken, "VariableIsUndefined", (object)this.NodeToken);
                }
            }
            return(variable);
        }
コード例 #3
0
 internal static object GetMDArrayValue(Array array, int[] indexes, bool slicing)
 {
     if (array.Rank != indexes.Length)
     {
         ReportIndexingError(array, indexes, null);
     }
     for (int i = 0; i < indexes.Length; i++)
     {
         int upperBound = array.GetUpperBound(i);
         int lowerBound = array.GetLowerBound(i);
         if (indexes[i] < lowerBound)
         {
             indexes[i] = (indexes[i] + upperBound) + 1;
         }
         if ((indexes[i] < lowerBound) || (indexes[i] > upperBound))
         {
             ExecutionContext executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS();
             if ((executionContextFromTLS != null) && !executionContextFromTLS.IsStrictVersion(3))
             {
                 if (!slicing)
                 {
                     return(null);
                 }
                 return(AutomationNull.Value);
             }
         }
     }
     return(array.GetValue(indexes));
 }
コード例 #4
0
 internal static bool IsStrictVersion(ExecutionContext context, int majorVersion)
 {
     if (context == null)
     {
         context = LocalPipeline.GetExecutionContextFromTLS();
     }
     if (context == null)
     {
         return(false);
     }
     return(context.IsStrictVersion(majorVersion));
 }
コード例 #5
0
 private static bool ThrowStrictModeUndefinedVariable(ExecutionContext executionContext, VariableExpressionAst varAst)
 {
     if (varAst == null)
     {
         return(false);
     }
     if (!executionContext.IsStrictVersion(2))
     {
         if (!executionContext.IsStrictVersion(1))
         {
             return(false);
         }
         for (Ast ast = varAst.Parent; ast != null; ast = ast.Parent)
         {
             if (ast is ExpandableStringExpressionAst)
             {
                 return(false);
             }
         }
     }
     return(true);
 }
コード例 #6
0
 internal object GetValue(PSObject obj, object property, ExecutionContext context)
 {
     if (!LanguagePrimitives.IsNull((object)obj))
     {
         if (!LanguagePrimitives.IsNull(property))
         {
             try
             {
                 IDictionary dictionary = PSObject.Base((object)obj) as IDictionary;
                 try
                 {
                     if (dictionary != null)
                     {
                         if (dictionary.Contains(property))
                         {
                             return(dictionary[property]);
                         }
                     }
                 }
                 catch (InvalidOperationException ex)
                 {
                 }
                 PSMemberInfo memberInfo = this.GetMemberInfo(obj, property, context);
                 if (memberInfo != null)
                 {
                     return(memberInfo.Value);
                 }
             }
             catch (TerminateException ex)
             {
                 throw;
             }
             catch (Exception ex)
             {
                 CommandProcessorBase.CheckForSevereException(ex);
                 return((object)null);
             }
         }
     }
     if (context.IsStrictVersion(2))
     {
         throw InterpreterError.NewInterpreterException((object)this.NodeToken, typeof(RuntimeException), this.NodeToken, "PropertyNotFoundStrict", property);
     }
     return((object)null);
 }
コード例 #7
0
        internal static object GetNonIndexable(object target, object[] indices)
        {
            if (indices.Length == 1)
            {
                object second = indices[0];
                if ((second != null) && (LanguagePrimitives.Equals(0, second) || LanguagePrimitives.Equals(-1, second)))
                {
                    return(target);
                }
            }
            ExecutionContext executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS();

            if ((executionContextFromTLS != null) && executionContextFromTLS.IsStrictVersion(2))
            {
                throw InterpreterError.NewInterpreterException(target, typeof(RuntimeException), null, "CannotIndex", ParserStrings.CannotIndex, new object[] { target.GetType() });
            }
            return(AutomationNull.Value);
        }
コード例 #8
0
ファイル: EnumerableOps.cs プロジェクト: modulexcite/pash-1
        internal static object PropertyGetter(PSGetMemberBinder binder, IEnumerator enumerator)
        {
            CallSite <Func <CallSite, object, object> > getMemberBinderSite = CallSite <Func <CallSite, object, object> > .Create(binder);

            ArrayList        result = new ArrayList();
            ExecutionContext executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS();

            PropertyGetterWorker(getMemberBinderSite, enumerator, executionContextFromTLS, result);
            if (result.Count == 1)
            {
                return(result[0]);
            }
            if (result.Count != 0)
            {
                return(result.ToArray());
            }
            if (executionContextFromTLS.IsStrictVersion(2))
            {
                throw InterpreterError.NewInterpreterException(null, typeof(RuntimeException), null, "PropertyNotFoundStrict", ParserStrings.PropertyNotFoundStrict, new object[] { binder.Name });
            }
            return(null);
        }
コード例 #9
0
ファイル: CommandNode.cs プロジェクト: mmoenfly/GitCook2021
        private CommandProcessorBase CreateCommandProcessor(
            out int index,
            ExecutionContext context)
        {
            index = 0;
            CommandProcessorBase commandProcessorBase = (CommandProcessorBase)null;

            if (this._expression != null)
            {
                return((CommandProcessorBase) new ScriptCommandProcessor(ScriptBlock.CreateSynthesized(this._expression, this.NodeToken), context, false));
            }
            bool createScope;
            SessionStateInternal commandSessionState;
            bool   forceSessionState;
            object commandName = this.GetCommandName(out createScope, out index, out commandSessionState, out forceSessionState, context);

            switch (commandName)
            {
            case ScriptBlock scriptblock:
                commandProcessorBase = CommandDiscovery.CreateCommandProcessorForScript(scriptblock, context, createScope);
                if (commandProcessorBase.CommandSessionState == null || forceSessionState)
                {
                    commandProcessorBase.CommandSessionState = commandSessionState;
                    break;
                }
                break;

            case CommandInfo commandInfo:
                commandProcessorBase = context.CommandDiscovery.LookupCommandProcessor(commandInfo, context.EngineSessionState.currentScope.ScopeOrigin, new bool?(false));
                break;

            case string stringParser:
label_7:
                if (string.IsNullOrEmpty(stringParser))
                {
                    throw InterpreterError.NewInterpreterException(commandName, typeof(RuntimeException), (Token)this._elements[0], "BadExpression", (object)((Token)this._elements[0]).TokenText);
                }
                try
                {
                    if (commandSessionState != context.EngineSessionState)
                    {
                        SessionStateInternal engineSessionState = context.EngineSessionState;
                        try
                        {
                            context.EngineSessionState = commandSessionState;
                            commandProcessorBase       = context.CreateCommand(stringParser);
                            break;
                        }
                        finally
                        {
                            context.EngineSessionState = engineSessionState;
                        }
                    }
                    else
                    {
                        commandProcessorBase = context.CreateCommand(stringParser);
                        break;
                    }
                }
                catch (ParseException ex)
                {
                    throw;
                }
                catch (RuntimeException ex)
                {
                    if (ex.ErrorRecord.InvocationInfo == null)
                    {
                        ex.ErrorRecord.SetInvocationInfo(new InvocationInfo((CommandInfo)null, (Token)this._elements[0], context));
                    }
                    throw;
                }

            default:
                stringParser = PSObject.ToStringParser(context, commandName);
                goto label_7;
            }
            if (commandProcessorBase.CommandSessionState == null || forceSessionState)
            {
                commandProcessorBase.CommandSessionState = commandSessionState;
            }
            InternalCommand command = commandProcessorBase.Command;

            command.MyInvocation.ScriptToken = (Token)this._elements[0];
            if (context.IsStrictVersion(2) && this._elements.Count == index + 1 && (this._elements[index - 1] is Token && this._elements[index] is ArrayLiteralNode))
            {
                Token element = (Token)this._elements[index - 1];
                if (element.Script[element.End] == '(')
                {
                    throw InterpreterError.NewInterpreterException((object)null, typeof(RuntimeException), (Token)this._elements[index - 1], "StrictModeFunctionCallWithParens");
                }
            }
            if (createScope)
            {
                switch (command)
                {
                case ScriptCommand _:
                case PSScriptCmdlet _:
                    commandProcessorBase.UseLocalScope = true;
                    commandProcessorBase.CommandScope  = commandProcessorBase.CommandSessionState == null?context.EngineSessionState.NewScope(commandProcessorBase.FromScriptFile) : commandProcessorBase.CommandSessionState.NewScope(commandProcessorBase.FromScriptFile);

                    goto label_27;
                }
            }
            commandProcessorBase.UseLocalScope = false;
            commandProcessorBase.CommandScope  = commandProcessorBase.CommandSessionState.CurrentScope;
label_27:
            commandProcessorBase.Command.CallingToken = (Token)this._elements[0];
            return(commandProcessorBase);
        }