Jurassic.Compiler.Scope.DeclareVariable C# (CSharp) Method

DeclareVariable() private method

Declares a variable or function in this scope. This will be initialized with the value of the given expression.
private DeclareVariable ( string name, Jurassic.Compiler.Expression valueAtTopOfScope = null, bool writable = true, bool deletable = false ) : DeclaredVariable
name string The name of the variable.
valueAtTopOfScope Jurassic.Compiler.Expression The value of the variable at the top of the scope. /// Can be null to indicate the variable does not need initializing.
writable bool true if the variable can be modified; false /// otherwise. Defaults to true.
deletable bool true if the variable can be deleted; false /// otherwise. Defaults to true.
return DeclaredVariable
        internal virtual DeclaredVariable DeclareVariable(string name, Expression valueAtTopOfScope = null, bool writable = true, bool deletable = false)
        {
            if (name == null)
                throw new ArgumentNullException("name");

            // If variables cannot be declared in the scope, try the parent scope instead.
            if (this.CanDeclareVariables == false)
            {
                if (this.ParentScope == null)
                    throw new InvalidOperationException("Invalid scope chain.");
                return this.ParentScope.DeclareVariable(name, valueAtTopOfScope, writable, deletable);
            }

            DeclaredVariable variable;
            this.variables.TryGetValue(name, out variable);
            if (variable == null)
            {
                // This is a local variable that has not been declared before.
                variable = new DeclaredVariable() { Scope = this, Index = this.variables.Count, Name = name, Writable = writable, Deletable = deletable };
                this.variables.Add(name, variable);
            }

            // Set the initial value, if one was provided.
            if (valueAtTopOfScope != null)
            {
                // Function expressions override literals.
                if ((valueAtTopOfScope is LiteralExpression && variable.ValueAtTopOfScope is FunctionExpression) == false)
                    variable.ValueAtTopOfScope = valueAtTopOfScope;
            }

            return variable;
        }

Usage Example

Example #1
0
        /// <summary>
        /// Creates a new declarative scope for use inside a function body (and within function
        /// argument default values).
        /// </summary>
        /// <param name="functionName"> The name of the function.  Can be empty for an anonymous function. </param>
        /// <param name="argumentNames"> The names of each of the function arguments.  Can be <c>null</c>. </param>
        /// <returns> A new DeclarativeScope instance. </returns>
        internal static Scope CreateFunctionScope(string functionName, IEnumerable <string> argumentNames)
        {
            var result = new Scope(null, (argumentNames != null ? argumentNames.Count() : 0) + 3);

            result.Type = ScopeType.TopLevelFunction;
            if (functionName != null)
            {
                result.DeclareVariable(KeywordToken.Var, functionName);
            }
            result.DeclareVariable(KeywordToken.Var, "arguments");
            if (argumentNames != null)
            {
                foreach (var argumentName in argumentNames)
                {
                    result.DeclareVariable(KeywordToken.Var, argumentName);
                }
            }
            return(result);
        }