The Labs \ Source Viewer \ SSCLI \ Microsoft.JScript \ FunctionConstructor

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. namespace Microsoft.JScript
  16. {
  17.    
  18.     using Microsoft.JScript.Vsa;
  19.     using System;
  20.     using System.Reflection;
  21.     using System.Text;
  22.     using System.Diagnostics;
  23.    
  24.     public sealed class FunctionConstructor : ScriptFunction
  25.     {
  26.         static internal readonly FunctionConstructor ob = new FunctionConstructor();
  27.        
  28.         internal FunctionPrototype originalPrototype;
  29.        
  30.         internal FunctionConstructor() : base(FunctionPrototype.ob, "Function", 1)
  31.         {
  32.             this.originalPrototype = FunctionPrototype.ob;
  33.             FunctionPrototype._constructor = this;
  34.             this.proto = FunctionPrototype.ob;
  35.         }
  36.        
  37.         internal FunctionConstructor(LenientFunctionPrototype prototypeProp) : base(prototypeProp, "Function", 1)
  38.         {
  39.             this.originalPrototype = prototypeProp;
  40.             prototypeProp.constructor = this;
  41.             this.proto = prototypeProp;
  42.             this.noExpando = false;
  43.         }
  44.        
  45.         internal override object Call(object[] args, object thisob)
  46.         {
  47.             return this.Construct(args, this.engine);
  48.         }
  49.        
  50.         internal override object Construct(object[] args)
  51.         {
  52.             return this.Construct(args, this.engine);
  53.         }
  54.        
  55.         internal ScriptFunction Construct(object[] args, VsaEngine engine)
  56.         {
  57.             StringBuilder func_string = new StringBuilder("function anonymous(");
  58.             for (int i = 0int n = args.Length - 2; i < n; i++) {
  59.                 func_string.Append(Convert.ToString(args[i]));
  60.                 func_string.Append(", ");
  61.             }
  62.             if (args.Length > 1)
  63.                 func_string.Append(Convert.ToString(args[args.Length - 2]));
  64.             func_string.Append(") {\n");
  65.             if (args.Length > 0)
  66.                 func_string.Append(Convert.ToString(args[args.Length - 1]));
  67.             func_string.Append("\n}");
  68.             Context context = new Context(new DocumentContext("anonymous", engine), func_string.ToString());
  69.             JSParser p = new JSParser(context);
  70.             engine.PushScriptObject(((IActivationObject)engine.ScriptObjectStackTop()).GetGlobalScope());
  71.             try {
  72.                 return (ScriptFunction)p.ParseFunctionExpression().PartiallyEvaluate().Evaluate();
  73.             }
  74.             finally {
  75.                 engine.PopScriptObject();
  76.             }
  77.         }
  78.        
  79.         [JSFunctionAttribute(JSFunctionAttributeEnum.HasVarArgs)]
  80.         public new ScriptFunction CreateInstance(params object[] args)
  81.         {
  82.             return this.Construct(args, this.engine);
  83.         }
  84.        
  85.         [JSFunctionAttribute(JSFunctionAttributeEnum.HasVarArgs)]
  86.         public ScriptFunction Invoke(params object[] args)
  87.         {
  88.             return this.Construct(args, this.engine);
  89.         }
  90.     }
  91. }

Developer Fusion