/// <summary>
        /// Initializes a user-defined function.
        /// </summary>
        /// <param name="name"> The name of the function. </param>
        /// <param name="args"> The names of the arguments. </param>
        /// <param name="bodyText"> The source code for the function body. </param>
        /// <param name="generatedMethod"> A delegate which represents the body of the function. </param>
        /// <param name="strictMode"> <c>true</c> if the function body is strict mode; <c>false</c> otherwise. </param>
        /// <param name="hasInstancePrototype"> <c>true</c> if the function should have a valid
        /// "prototype" property; <c>false</c> if the "prototype" property should be <c>null</c>. </param>
        private void Init(string name, IList <ArgVariable> args, FunctionMethodGenerator gen, bool strictMode, bool hasInstancePrototype)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (args == null)
            {
                args = new ArgVariable[0];
            }
            this.Arguments  = args;
            this.Generator  = gen;
            this.StrictMode = strictMode;

            Name   = name;
            Length = args.Count;

            /*
             * // The empty function doesn't have an instance prototype.
             * if (hasInstancePrototype == true)
             * {
             *      this.FastSetProperty("prototype",ObjectInstance.OnConstruct(Engine), PropertyAttributes.Writable);
             #warning this is incorrect; instanceProto is actually the (globally shared) Object proto.
             *      // this.InstancePrototype.AddProperty("constructor", this, PropertyAttributes.NonEnumerable);
             * }
             */
        }
        /// <summary>
        /// Creates a new instance of a user-defined function.
        /// </summary>
        /// <param name="prototype"> The next object in the prototype chain. </param>
        /// <param name="name"> The name of the function. </param>
        /// <param name="args"> The names of the arguments. MUST include 'this'.</param>
        /// <param name="bodyText"> The source code for the body of the function. </param>
        internal static UserDefinedFunction Create(ScriptEngine engine, string name, List <ArgVariable> args, string bodyText)
        {
            if (args == null)
            {
                args = new List <ArgVariable>();
                args.Add(new ArgVariable("this"));
            }

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }
            if (bodyText == null)
            {
                throw new ArgumentNullException("bodyText");
            }

            // Set up a new function scope.
            var scope = DeclarativeScope.CreateFunctionScope(engine.GlobalScope, name, args);

            // Compile the code.
            var context = new FunctionMethodGenerator(engine, scope, name, args, bodyText, new CompilerOptions());

            context.GenerateCode(new OptimizationInfo(engine));

            return(context.GeneratedMethods[0]);
        }
Esempio n. 3
0
        //     INITIALIZATION
        //_________________________________________________________________________________________

        /// <summary>
        /// Creates a new instance of a user-defined function.
        /// </summary>
        /// <param name="prototype"> The next object in the prototype chain. </param>
        /// <param name="name"> The name of the function. </param>
        /// <param name="argumentsText"> A comma-separated list of arguments. </param>
        /// <param name="bodyText"> The source code for the body of the function. </param>
        /// <remarks> This is used by <c>new Function()</c>. </remarks>
        internal UserDefinedFunction(ObjectInstance prototype, string name, string argumentsText, string bodyText)
            : base(prototype)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (argumentsText == null)
            {
                throw new ArgumentNullException("argumentsText");
            }
            if (bodyText == null)
            {
                throw new ArgumentNullException("bodyText");
            }

            // Set up a new function scope.
            var scope = DeclarativeScope.CreateFunctionScope(this.Engine.CreateGlobalScope(), name, null);

            // Compile the code.
            var context = new FunctionMethodGenerator(this.Engine, scope, name, argumentsText, bodyText, new CompilerOptions());

            context.GenerateCode();

            this.ArgumentsText   = argumentsText;
            this.ArgumentNames   = context.Arguments.Select(a => a.Name).ToList();
            this.BodyText        = bodyText;
            this.generatedMethod = context.GeneratedMethod;
            this.body            = (FunctionDelegate)this.generatedMethod.GeneratedDelegate;
            this.ParentScope     = this.Engine.CreateGlobalScope();
            this.StrictMode      = context.StrictMode;
            InitProperties(name, context.Arguments.Count);
        }
Esempio n. 4
0
        //     INITIALIZATION
        //_________________________________________________________________________________________

        /// <summary>
        /// Creates a new instance of a user-defined function.
        /// </summary>
        /// <param name="prototype"> The next object in the prototype chain. </param>
        /// <param name="name"> The name of the function. </param>
        /// <param name="argumentNames"> The names of the arguments. </param>
        /// <param name="bodyText"> The source code for the body of the function. </param>
        internal UserDefinedFunction(ObjectInstance prototype, string name, IList <string> argumentNames, string bodyText)
            : base(prototype)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (argumentNames == null)
            {
                throw new ArgumentNullException("argumentNames");
            }
            if (bodyText == null)
            {
                throw new ArgumentNullException("bodyText");
            }

            // Set up a new function scope.
            var scope = DeclarativeScope.CreateFunctionScope(this.Engine.CreateGlobalScope(), name, argumentNames);

            // Compile the code.
            var context = new FunctionMethodGenerator(this.Engine, scope, name, argumentNames, bodyText, new CompilerOptions());

            context.GenerateCode();

            // Create a new user defined function.
            Init(name, argumentNames, this.Engine.CreateGlobalScope(), bodyText, context.GeneratedMethod, context.StrictMode, true);
        }
Esempio n. 5
0
 /// <summary>
 /// Compiles the function (if it hasn't already been compiled) and returns a delegate
 /// representing the compiled function.
 /// </summary>
 private FunctionDelegate Compile()
 {
     if (this.m_body == null)
     {
         // Compile the function.
         var scope             = DeclarativeScope.CreateFunctionScope(this.Engine.CreateGlobalScope(), this.Name, this.ArgumentNames);
         var functionGenerator = new FunctionMethodGenerator(this.Engine, scope, this.Name, this.ArgumentNames, this.BodyText, new CompilerOptions());
         functionGenerator.GenerateCode();
         this.m_generatedMethod = functionGenerator.GeneratedMethod;
         this.m_body            = (FunctionDelegate)this.m_generatedMethod.GeneratedDelegate;
     }
     return(this.m_body);
 }
Esempio n. 6
0
        //     INITIALIZATION
        //_________________________________________________________________________________________

        /// <summary>
        /// Creates a new instance of a user-defined function.
        /// </summary>
        /// <param name="prototype"> The next object in the prototype chain. </param>
        /// <param name="name"> The name of the function. </param>
        /// <param name="argumentsText"> A comma-separated list of arguments. </param>
        /// <param name="bodyText"> The source code for the body of the function. </param>
        /// <remarks> This is used by <c>new Function()</c>. </remarks>
        internal UserDefinedFunction(ObjectInstance prototype, string name, string argumentsText, string bodyText)
            : base(prototype)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (argumentsText == null)
            {
                throw new ArgumentNullException(nameof(argumentsText));
            }
            if (bodyText == null)
            {
                throw new ArgumentNullException(nameof(bodyText));
            }

            // Set up a new function scope.
            this.Scope = DeclarativeScope.CreateFunctionScope(ObjectScope.CreateGlobalScope(this.Engine.Global), name, null);

            // Compile the code.
            var context = new FunctionMethodGenerator(this.Scope, name, argumentsText, bodyText, new CompilerOptions()
            {
#if ENABLE_DEBUGGING
                EnableDebugging = this.Engine.EnableDebugging,
#endif
                ForceStrictMode   = this.Engine.ForceStrictMode,
                EnableILAnalysis  = this.Engine.EnableILAnalysis,
                CompatibilityMode = this.Engine.CompatibilityMode
            });

            try
            {
                context.GenerateCode();
            }
            catch (SyntaxErrorException ex)
            {
                throw new JavaScriptException(this.Engine, ErrorType.SyntaxError, ex.Message, ex.LineNumber, ex.SourcePath);
            }

            this.ArgumentsText   = argumentsText;
            this.ArgumentNames   = context.Arguments.Select(a => a.Name).ToList();
            this.BodyText        = bodyText;
            this.generatedMethod = context.GeneratedMethod;
            this.body            = (FunctionDelegate)this.generatedMethod.GeneratedDelegate;
            this.ParentScope     = ObjectScope.CreateGlobalScope(this.Engine.Global);
            this.StrictMode      = context.StrictMode;
            InitProperties(name, context.Arguments.Count);
        }
 /// <summary>
 /// Creates a new instance of a user-defined function.
 /// </summary>
 /// <param name="prototype"> The next object in the prototype chain. </param>
 /// <param name="name"> The name of the function. </param>
 /// <param name="args"> The names of the arguments. </param>
 /// <param name="bodyText"> The source code for the function body. </param>
 /// <param name="generatedMethod"> A delegate which represents the body of the function. </param>
 /// <param name="strictMode"> <c>true</c> if the function body is strict mode; <c>false</c> otherwise. </param>
 internal UserDefinedFunction(string name, IList <ArgVariable> args, FunctionMethodGenerator gen, bool strictMode)
 {
     Init(name, args, gen, strictMode, true);
 }
Esempio n. 8
0
 /// <summary>
 /// Creates a new instance of a user-defined function.
 /// </summary>
 /// <param name="prototype"> The next object in the prototype chain. </param>
 /// <param name="name"> The name of the function. </param>
 /// <param name="args"> The names of the arguments. </param>
 /// <param name="bodyText"> The source code for the function body. </param>
 /// <param name="body"> A delegate which represents the body of the function. </param>
 /// <param name="strictMode"> <c>true</c> if the function body is strict mode; <c>false</c> otherwise. </param>
 public HoistFunctionReference(ScriptEngine engine, long methodID, object[] hoistValues)
 {
     Host        = MethodLookup.LoadGenerator(methodID);
     HoistValues = hoistValues;
 }