Beispiel #1
0
        // pushes all what's required to perform a clr-to-script function call. function can be null if it's already
        // at vstack top.
        private int PushClrToScriptStackFrame(CallStackItemFlags flags, DynValue function, IList <DynValue> args)
        {
            if (!function.IsValid)
            {
                function = m_ValueStack.Peek();
            }
            else
            {
                m_ValueStack.Push(function);  // func val
            }
            args = Internal_AdjustTuple(args);

            for (int i = 0; i < args.Count; i++)
            {
                m_ValueStack.Push(args[i]);
            }

            m_ValueStack.Push(DynValue.NewNumber(args.Count));  // func args count

            m_ExecutionStack.Push(new CallStackItem()
            {
                BasePointer      = m_ValueStack.Count,
                Debug_EntryPoint = function.Function.EntryPointByteCodeLocation,
                ReturnAddress    = -1,
                ClosureScope     = function.Function.ClosureContext,
                CallingSourceRef = SourceRef.GetClrLocation(),
                Flags            = flags
            });

            return(function.Function.EntryPointByteCodeLocation);
        }
Beispiel #2
0
 internal void PopIndent()
 {
     if (istack.Count > 0)
     {
         istack.Pop();
         expectIndent = istack.Count > 0 ? istack.Peek() : -1;
     }
     else
     {
         expectIndent = -1;
     }
 }
Beispiel #3
0
        public bool IsThreefoldRepetition()
        {
            if (NullMoves == 0 && _hashes.Count() >= 8)
            {
                var first  = _hashes.Peek(3);
                var second = _hashes.Peek(7);

                if (Hash == first && first == second)
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #4
0
		// pushes all what's required to perform a clr-to-script function call. function can be null if it's already
		// at vstack top.
		private int PushClrToScriptStackFrame(CallStackItemFlags flags, DynValue function, DynValue[] args)
		{
			if (function == null) 
				function = m_ValueStack.Peek();
			else
				m_ValueStack.Push(function);  // func val

			args = Internal_AdjustTuple(args);

			for (int i = 0; i < args.Length; i++)
				m_ValueStack.Push(args[i]);

			m_ValueStack.Push(DynValue.NewNumber(args.Length));  // func args count

			m_ExecutionStack.Push(new CallStackItem()
			{
				BasePointer = m_ValueStack.Count,
				Debug_EntryPoint = function.Function.EntryPointByteCodeLocation,
				ReturnAddress = -1,
				ClosureScope = function.Function.ClosureContext,
				CallingSourceRef = SourceRef.GetClrLocation(),
				Flags = flags
			});

			return function.Function.EntryPointByteCodeLocation;
		}
        /// <summary>
        /// Pops a previous output.
        /// </summary>
        public IScriptOutput PopOutput()
        {
            if (_outputs.Count == 1)
            {
                throw new InvalidOperationException("Unexpected PopOutput for top level writer");
            }

            var previous = _outputs.Pop();

            _output = _outputs.Peek();
            return(previous);
        }
Beispiel #6
0
        /// <summary>
        /// Sets the variable with the specified value.
        /// </summary>
        /// <param name="variable">The variable.</param>
        /// <param name="value">The value.</param>
        /// <exception cref="System.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, $"Cannot set value on the readonly variable `{variable}`"); // unit test: 105-assign-error2.txt
                }
            }
            else
            {
                // unit test: 215-for-special-var-error1.txt
                throw new ScriptRuntimeException(variable.Span, $"Invalid usage of the loop variable `{variable}` not inside a loop");
            }
        }
        internal bool StepLoop(ScriptLoopStatementBase loop)
        {
            Debug.Assert(_loops.Count > 0);

            _loopStep++;
            if (_loopStep > LoopLimit)
            {
                var currentLoopStatement = _loops.Peek();

                throw new ScriptRuntimeException(currentLoopStatement.Span, $"Exceeding number of iteration limit `{LoopLimit}` for loop statement."); // unit test: 215-for-statement-error1.txt
            }
            return(OnStepLoop(loop));
        }
Beispiel #8
0
        public static void WorksForValueTypes()
        {
            var c = new FastStack <int>();

            c.Push(1);
            Assert.Equal(1, c.Count);
            Assert.Equal(1, c.Peek());
            Assert.Equal(1, c.Count);
            Assert.Equal(1, c.Pop());
            Assert.Equal(0, c.Count);
            c.Push(1);
            c.Push(2);
            Assert.Equal(2, c.Count);
            Assert.Equal(2, c.Peek());
            Assert.Equal(2, c.Count);
            Assert.Equal(2, c.Pop());
            Assert.Equal(1, c.Count);
            Assert.Equal(1, c.Peek());
            Assert.Equal(1, c.Count);
            Assert.Equal(1, c.Pop());
            Assert.Equal(0, c.Count);
        }
Beispiel #9
0
        public static void WorksForReferenceTypes()
        {
            var c = new FastStack <string>();

            c.Push("1");
            Assert.Equal(1, c.Count);
            Assert.Equal("1", c.Peek());
            Assert.Equal(1, c.Count);
            Assert.Equal("1", c.Pop());
            Assert.Equal(0, c.Count);
            c.Push("1");
            c.Push("2");
            Assert.Equal(2, c.Count);
            Assert.Equal("2", c.Peek());
            Assert.Equal(2, c.Count);
            Assert.Equal("2", c.Pop());
            Assert.Equal(1, c.Count);
            Assert.Equal("1", c.Peek());
            Assert.Equal(1, c.Count);
            Assert.Equal("1", c.Pop());
            Assert.Equal(0, c.Count);
        }
        internal void PopPipeArguments()
        {
            if (_pipeArguments.Count == 0)
            {
                throw new InvalidOperationException("Cannot PopPipeArguments more than PushPipeArguments");
            }

            var pipeFrom = _pipeArguments.Pop();

            pipeFrom.Clear();
            _currentPipeArguments = _pipeArguments.Count > 0 ? _pipeArguments.Peek() : null;
            _availablePipeArguments.Push(pipeFrom);
        }
Beispiel #11
0
        internal void PopPipeArguments()
        {
            if (_pipeArguments.Count == 0)
            {
                throw new InvalidOperationException("Cannot PopPipeArguments more than PushPipeArguments");
            }

            var pipeArguments = _pipeArguments.Pop();

            // Might be not null in case of an exception
            pipeArguments.Clear();
            _availablePipeArguments.Push(pipeArguments);
            _currentPipeArguments = _pipeArguments.Count > 0 ? _pipeArguments.Peek() : null;
        }
Beispiel #12
0
        internal void Emit(Op op, int data)
        {
            ops.Add(op);
            var size = OpStackHelper.Op[(Int32)op];

            var ss = locals.Peek();

            ss.Counter += size;

            if (ss.Counter > ss.Max)
            {
                ss.Max = ss.Counter;
            }

            opData.Add(data);
        }
Beispiel #13
0
    public void ComputeSubTreeSize(int root)
    {
        const long X     = 1000000000;
        var        stack = new FastStack <long>(N + 1);

        stack.Push(root * X);
        par[root] = -1;
        d[root]   = 0;

        while (stack.Any())
        {
            var val = stack.Peek();
            var u   = (int)(val / X);
            var it  = (int)(val % X);

            if (it == G[u].Count)
            {
                stack.Pop();
                subTreeSize[u]++;
                if (par[u] >= 0)
                {
                    subTreeSize[par[u]] += subTreeSize[u];
                }
            }
            else
            {
                var to = G[u][it].to;
                stack.Last++;
                if (to == par[u])
                {
                    continue;
                }

                par[to] = u;
                d[to]   = d[u] + 1;
                stack.Push(to * X);
            }
        }
    }
Beispiel #14
0
        public bool IsThreefoldRepetition()
        {
            var positionsToCheck = Math.Min(_hashes.Count(), IrreversibleMovesCount + 1);

            if (NullMoves == 0 && positionsToCheck >= 8)
            {
                var repetitionsCount = 1;
                for (var positionIndex = 1; positionIndex < positionsToCheck; positionIndex += 2)
                {
                    if (_hashes.Peek(positionIndex) == Hash)
                    {
                        repetitionsCount++;
                        if (repetitionsCount >= 3)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Beispiel #15
0
        internal void StartScope(int offset, int line, int col, bool runtimeScope)
        {
            var index = ++scopeCount;

            scopes.Push(new ScopeSym(index, scopes.Peek().Index, offset, line, col, runtimeScope));
        }
 internal object PeekCase()
 {
     return(_caseValues.Peek());
 }
Beispiel #17
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="System.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--)
                {
                    var 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))
                    {
                        var variableType = store == BuiltinObject ? "builtin " : string.Empty;
                        throw new ScriptRuntimeException(variable.Span, $"Cannot set the {variableType}readonly variable `{variable}`");
                    }
                }
                break;

            case ScriptVariableScope.Local:
                if (_localStores.Count > 0)
                {
                    firstStore = _localStores.Peek();
                }
                else
                {
                    throw new ScriptRuntimeException(variable.Span, $"Invalid usage of the local variable `{variable}` in the current context");
                }
                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, $"Invalid usage of the loop variable `{variable}` not inside a loop");
                }
                break;

            default:
                throw new NotImplementedException($"Variable scope `{scope}` is not implemented");
            }

            // Try to set the variable
            if (!firstStore.TrySetValue(variable.Name, value, asReadOnly))
            {
                throw new ScriptRuntimeException(variable.Span, $"Cannot set value on the readonly variable `{variable}`"); // unit test: 105-assign-error2.txt
            }
        }