/// <summary>
 /// Creates a new FunctionContext instance.
 /// </summary>
 /// <param name="engine"> The script engine. </param>
 /// <param name="scope"> The function scope. </param>
 /// <param name="functionName"> The name of the function. </param>
 /// <param name="argumentNames"> The names of the arguments. </param>
 /// <param name="body"> The source code for the body of the function. </param>
 /// <param name="options"> Options that influence the compiler. </param>
 public FunctionMethodGenerator(ScriptEngine engine, DeclarativeScope scope, string functionName, IList <string> argumentNames, string body, CompilerOptions options)
     : base(engine, scope, new StringScriptSource(body), options)
 {
     this.Name          = functionName;
     this.ArgumentNames = argumentNames;
     this.BodyText      = body;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new declarative scope for use inside a function body.
        /// </summary>
        /// <param name="parentScope"> A reference to the parent scope.  Can not be <c>null</c>. </param>
        /// <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. </param>
        /// <returns> A new DeclarativeScope instance. </returns>
        internal static DeclarativeScope CreateFunctionScope(Scope parentScope, string functionName, IEnumerable <string> argumentNames)
        {
            if (parentScope == null)
            {
                throw new ArgumentNullException("parentScope", @"Function scopes must have a parent scope.");
            }
            if (functionName == null)
            {
                throw new ArgumentNullException("functionName");
            }
            if (argumentNames == null)
            {
                throw new ArgumentNullException("argumentNames");
            }
            var result = new DeclarativeScope(parentScope, 0);

            if (string.IsNullOrEmpty(functionName) == false)
            {
                result.DeclareVariable(functionName, null, true, false);
            }
            result.DeclareVariable("this", null, true, false);
            result.DeclareVariable("arguments", null, true, false);
            foreach (var argumentName in argumentNames)
            {
                result.DeclareVariable(argumentName, null, true, false);
            }
            return(result);
        }
 /// <summary>
 /// Creates a new FunctionMethodGenerator instance.
 /// </summary>
 /// <param name="engine"> The script engine. </param>
 /// <param name="scope"> The function scope. </param>
 /// <param name="functionName"> The name of the function. </param>
 /// <param name="includeNameInScope"> Indicates whether the name should be included in the function scope. </param>
 /// <param name="argumentNames"> The names of the arguments. </param>
 /// <param name="bodyText"> The source code of the function. </param>
 /// <param name="body"> The root of the abstract syntax tree for the body of the function. </param>
 /// <param name="scriptPath"> The URL or file system path that the script was sourced from. </param>
 /// <param name="options"> Options that influence the compiler. </param>
 public FunctionMethodGenerator(ScriptEngine engine, DeclarativeScope scope, string functionName, bool includeNameInScope, IList <string> argumentNames, string bodyText, Statement body, string scriptPath, CompilerOptions options)
     : base(engine, scope, new DummyScriptSource(scriptPath), options)
 {
     this.Name = functionName;
     this.IncludeNameInScope = includeNameInScope;
     this.ArgumentNames      = argumentNames;
     this.BodyRoot           = body;
     this.BodyText           = bodyText;
     Validate();
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a new declarative scope for use inside a catch statement.
        /// </summary>
        /// <param name="parentScope"> A reference to the parent scope.  Can not be <c>null</c>. </param>
        /// <param name="catchVariableName"> The name of the catch variable. </param>
        /// <returns> A new DeclarativeScope instance. </returns>
        internal static DeclarativeScope CreateCatchScope(Scope parentScope, string catchVariableName)
        {
            if (parentScope == null)
            {
                throw new ArgumentNullException("parentScope", @"Catch scopes must have a parent scope.");
            }
            if (catchVariableName == null)
            {
                throw new ArgumentNullException("catchVariableName");
            }
            var result = new DeclarativeScope(parentScope, 0);

            result.DeclareVariable(catchVariableName, null, true, false);
            result.CanDeclareVariables = false; // Only the catch variable can be declared in this scope.
            return(result);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a new declarative scope for use at runtime.
        /// </summary>
        /// <param name="parentScope"> A reference to the parent scope.  Can not be <c>null</c>. </param>
        /// <param name="declaredVariableNames"> The names of variables that were declared in this scope. </param>
        /// <returns> A new DeclarativeScope instance. </returns>
        public static DeclarativeScope CreateRuntimeScope(Scope parentScope, string[] declaredVariableNames)
        {
            if (parentScope == null)
            {
                throw new ArgumentNullException("parentScope", @"Function scopes must have a parent scope.");
            }
            if (declaredVariableNames == null)
            {
                throw new ArgumentNullException("declaredVariableNames");
            }
            var result = new DeclarativeScope(parentScope, declaredVariableNames.Length);

            foreach (string variableName in declaredVariableNames)
            {
                result.DeclareVariable(variableName, null, true, false);
            }
            result.m_values = new object[result.DeclaredVariableCount];
            return(result);
        }
        /// <summary>
        /// Parses the source text into an abstract syntax tree.
        /// </summary>
        public override void Parse()
        {
            using (var lexer = new Lexer(this.Engine, this.Source))
            {
                var parser = new Parser(this.Engine, lexer, this.InitialScope, this.Options, CodeContext.Eval)
                {
                    DirectivePrologueProcessedCallback = parser2 =>
                    {
                        if (parser2.StrictMode)
                        {
                            parser2.InitialScope = parser2.Scope = DeclarativeScope.CreateEvalScope(parser2.Scope);
                        }
                    }
                };

                // If the eval() is running strict mode, create a new scope.

                this.AbstractSyntaxTree = parser.Parse();

                this.StrictMode              = parser.StrictMode;
                this.InitialScope            = parser.Scope;
                this.MethodOptimizationHints = parser.MethodOptimizationHints;
            }
        }