Example #1
0
        /// <summary>
        /// Pops a previous output.
        /// </summary>
        public IScriptOutput PopOutput()
        {
            if (_outputs.Count == 1)
            {
                throw new InvalidOperationException(RS.PopOutputError);
            }

            var previous = _outputs.Pop();

            _output = _outputs.Peek();
            return(previous);
        }
Example #2
0
        /// <summary>
        /// Sets the variable with the specified value.
        /// </summary>
        /// <param name="variable">The variable.</param>
        /// <param name="value">The value.</param>
        /// <exception cref="ArgumentNullException">If variable is null</exception>
        /// <exception cref="ScriptRuntimeException">If an existing variable is already read-only</exception>
        public void SetValue(ScriptVariableLoop variable, object value)
        {
            if (variable == null)
            {
                throw new ArgumentNullException(nameof(variable));
            }

            if (_loopStores.Count > 0)
            {
                // Try to set the variable
                var store = _loopStores.Peek();
                if (!store.TrySetValue(variable.Name, value, false))
                {
                    throw new ScriptRuntimeException(variable.Span, string.Format(RS.VariableReadOnly, variable)); // unit test: 105-assign-error2.txt
                }
            }
            else
            {
                // unit test: 215-for-special-var-error1.txt
                throw new ScriptRuntimeException(variable.Span, string.Format(RS.InvalidLoopVariableOutsideLoop, variable));
            }
        }
Example #3
0
        internal bool StepLoop(ScriptLoopStatementBase loop)
        {
#if DEBUG
            Debug.Assert(_loops.Count > 0);
#endif
            _loopStep++;
            if (_loopStep > LoopLimit)
            {
                ScriptLoopStatementBase currentLoopStatement = _loops.Peek();

                throw new ScriptRuntimeException(currentLoopStatement.Span, string.Format(RS.IterationDepthLimitReached, LoopLimit, currentLoopStatement)); // unit test: 215-for-statement-error1.txt
            }
            return(OnStepLoop(loop));
        }
Example #4
0
        internal void PopPipeArguments()
        {
            if (_pipeArguments.Count == 1)
            {
                throw new InvalidOperationException(RS.PopPipeArgOverflow);
            }

            ScriptPipeArguments pipeArguments = _pipeArguments.Pop();

            // Might be not null in case of an exception
            pipeArguments.Clear();
            _availablePipeArguments.Push(pipeArguments);
            _currentPipeArguments = _pipeArguments.Peek();
        }
Example #5
0
 internal object PeekCase()
 {
     return(_caseValues.Peek());
 }
Example #6
0
        /// <summary>
        /// Sets the variable with the specified value.
        /// </summary>
        /// <param name="variable">The variable.</param>
        /// <param name="value">The value.</param>
        /// <param name="asReadOnly">if set to <c>true</c> the variable set will be read-only.</param>
        /// <exception cref="ArgumentNullException">If variable is null</exception>
        /// <exception cref="ScriptRuntimeException">If an existing variable is already read-only</exception>
        public void SetValue(ScriptVariable variable, object value, bool asReadOnly = false)
        {
            if (variable == null)
            {
                throw new ArgumentNullException(nameof(variable));
            }


            var           scope      = variable.Scope;
            IScriptObject firstStore = null;

            switch (scope)
            {
            case ScriptVariableScope.Global:
                for (int i = _globalStores.Count - 1; i >= 0; i--)
                {
                    IScriptObject store = _globalStores.Items[i];
                    if (firstStore == null)
                    {
                        firstStore = store;
                    }

                    // We check that for upper store, we actually can write a variable with this name
                    // otherwise we don't allow to create a variable with the same name as a readonly variable
                    if (!store.CanWrite(variable.Name))
                    {
                        if (store == BuiltinObject)
                        {
                            throw new ScriptRuntimeException(variable.Span, string.Format(RS.BuiltinObjectReadOnly));
                        }
                        else
                        {
                            throw new ScriptRuntimeException(variable.Span, string.Format(RS.VariableReadOnly, variable));
                        }
                    }
                }
                break;

            case ScriptVariableScope.Local:
                if (_localStores.Count > 0)
                {
                    firstStore = _localStores.Peek();
                }
                else
                {
                    throw new ScriptRuntimeException(variable.Span, string.Format(RS.InvalidLocalVariableInContext, variable));
                }

                break;

            case ScriptVariableScope.Loop:
                if (_loopStores.Count > 0)
                {
                    firstStore = _loopStores.Peek();
                }
                else
                {
                    // unit test: 215-for-special-var-error1.txt
                    throw new ScriptRuntimeException(variable.Span, string.Format(RS.InvalidLoopVariableOutsideLoop, variable));
                }
                break;

            default:
                throw new NotImplementedException(string.Format(RS.InvalidVariableScope, scope));
            }

            // Try to set the variable
            if (!firstStore.TrySetValue(variable.Name, value, asReadOnly))
            {
                throw new ScriptRuntimeException(variable.Span, string.Format(RS.VariableReadOnly, variable)); // unit test: 105-assign-error2.txt
            }
        }