/// <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]); }
// 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); }
// 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); }
/// <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); }
// 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); }