Exemplo n.º 1
0
        /// <summary>
        /// Gets the value for the specified global variable from the current object context/scope.
        /// </summary>
        /// <param name="variable">The variable to retrieve the value</param>
        /// <returns>Value of the variable</returns>
        public object GetValue(ScriptVariableGlobal variable)
        {
            if (variable == null)
            {
                throw new ArgumentNullException(nameof(variable));
            }
            object value = null;
            var    count = _globalStores.Count;
            var    items = _globalStores.Items;

            for (int i = count - 1; i >= 0; i--)
            {
                if (items[i].TryGetValue(this, variable.Span, variable.Name, out value))
                {
                    return(value);
                }
            }

            bool found = false;

            if (TryGetVariable != null)
            {
                if (TryGetVariable(this, variable.Span, variable, out value))
                {
                    found = true;
                }
            }

            if (StrictVariables && !found)
            {
                throw new ScriptRuntimeException(variable.Span, $"The variable `{variable}` was not found");
            }
            return(value);
        }
        /// <summary>
        /// Builds a script expression to get a parent object
        /// </summary>
        /// <param name="callHierarchy">Call hierarchy</param>
        /// <returns>Script expression</returns>
        private ScriptNode BuildParentObjectValueScriptExpression(List <string> callHierarchy)
        {
            ScriptNode parentObjectExpression = null;

            if (callHierarchy.Count == 2)
            {
                parentObjectExpression = new ScriptVariableGlobal(callHierarchy[callHierarchy.Count - 2]);
            }
            else
            {
                ScriptExpressionStatement statement           = new ScriptExpressionStatement();
                ScriptMemberExpression    curMemberExpression = new ScriptMemberExpression();
                statement.Expression = curMemberExpression;
                for (int curHierarchEntry = callHierarchy.Count - 2; curHierarchEntry >= 0; --curHierarchEntry)
                {
                    curMemberExpression.Member = new ScriptVariableGlobal(callHierarchy[curHierarchEntry]);
                    if (curHierarchEntry > 1)
                    {
                        ScriptMemberExpression nextMemberExpression = new ScriptMemberExpression();
                        curMemberExpression.Target = nextMemberExpression;
                        curMemberExpression        = nextMemberExpression;
                    }
                    else
                    {
                        curMemberExpression.Target = new ScriptVariableGlobal(callHierarchy[0]);
                        break;
                    }
                }

                parentObjectExpression = statement;
            }

            return(parentObjectExpression);
        }
Exemplo n.º 3
0
        public object GetValue(ScriptVariableGlobal variable)
        {
            if (variable == null)
            {
                throw new ArgumentNullException(nameof(variable));
            }
            object value = null;

            {
                var count    = _globalContexts.Count;
                var items    = _globalContexts.Items;
                var isInLoop = IsInLoop;
                for (int i = count - 1; i >= 0; i--)
                {
                    var context = items[i];
                    // Check loop variable first
                    if (isInLoop)
                    {
                        var loopCount = context.Loops.Count;
                        if (loopCount > 0)
                        {
                            var loopItems = context.Loops.Items;
                            for (int j = loopCount - 1; j >= 0; j--)
                            {
                                if (loopItems[j].TryGetValue(this, variable.Span, variable.Name, out value))
                                {
                                    return(value);
                                }
                            }
                        }
                    }

                    if (items[i].LocalObject.TryGetValue(this, variable.Span, variable.Name, out value))
                    {
                        return(value);
                    }
                }
            }

            bool found = false;

            if (TryGetVariable != null)
            {
                if (TryGetVariable(this, variable.Span, variable, out value))
                {
                    found = true;
                }
            }

            CheckVariableFound(variable, found);
            return(value);
        }
Exemplo n.º 4
0
        public object GetValue(ScriptVariableGlobal variable)
        {
            if (variable == null)
            {
                throw new ArgumentNullException(nameof(variable));
            }
            object value = null;

            if (IsInLoop)
            {
                var count = _currentLocalContext.Loops.Count;
                var items = _currentLocalContext.Loops.Items;
                for (int i = count - 1; i >= 0; i--)
                {
                    if (items[i].TryGetValue(this, variable.Span, variable.Name, out value))
                    {
                        return(value);
                    }
                }
            }

            {
                var count = _globalStores.Count;
                var items = _globalStores.Items;
                for (int i = count - 1; i >= 0; i--)
                {
                    if (items[i].TryGetValue(this, variable.Span, variable.Name, out value))
                    {
                        return(value);
                    }
                }
            }

            bool found = false;

            if (TryGetVariable != null)
            {
                if (TryGetVariable(this, variable.Span, variable, out value))
                {
                    found = true;
                }
            }

            CheckVariableFound(variable, found);
            return(value);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets the value for the specified global variable from the current object context/scope.
        /// </summary>
        /// <param name="variable">The variable to retrieve the value</param>
        /// <returns>Value of the variable</returns>
        public object GetValue(ScriptVariableGlobal variable)
        {
            if (variable == null)
            {
                throw new ArgumentNullException(nameof(variable));
            }

            object value = null;
            int    count = _globalStores.Count;

            IScriptObject[] items = _globalStores.Items;
            for (int i = count - 1; i >= 0; i--)
            {
                if (items[i].TryGetValue(this, variable.Span, variable.Name, out value))
                {
                    return(value);
                }
            }

            bool found = false;

            if (TryGetVariable != null)
            {
                if (TryGetVariable(this, variable.Span, variable, out value))
                {
                    found = true;
                }
            }

            if (StrictVariables && !found)
            {
                throw new ScriptRuntimeException(variable.Span, string.Format(RS.VariableNotFound, variable));
            }

            return(value);
        }
Exemplo n.º 6
0
 public ValueTask <object> GetValueAsync(ScriptVariableGlobal variable)
 {
     return(new ValueTask <object>(GetValue(variable)));
 }