// 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); }
/// <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.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.generatedMethod = functionGenerator.GeneratedMethod; this.body = (FunctionDelegate)this.generatedMethod.GeneratedDelegate; } return this.body; }