FunctionConstructor.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using Jint.Native.Object;
  4. using Jint.Parser.Ast;
  5. using Jint.Runtime;
  6. using Jint.Runtime.Descriptors;
  7. using Jint.Runtime.Environments;
  8. namespace Jint.Native.Function
  9. {
  10. /// <summary>
  11. ///
  12. /// </summary>
  13. public sealed class FunctionConstructor : FunctionInstance, IConstructor
  14. {
  15. private FunctionConstructor(Engine engine):base(engine, null, null, false)
  16. {
  17. }
  18. public static FunctionConstructor CreateFunctionConstructor(Engine engine)
  19. {
  20. var obj = new FunctionConstructor(engine);
  21. // The initial value of Function.prototype is the standard built-in Function prototype object
  22. obj.PrototypeObject = FunctionPrototype.CreatePrototypeObject(engine);
  23. // The value of the [[Prototype]] internal property of the Function constructor is the standard built-in Function prototype object
  24. obj.Prototype = obj.PrototypeObject;
  25. obj.FastAddProperty("prototype", obj.PrototypeObject, false, false, false);
  26. obj.FastAddProperty("length", 1, false, false, false);
  27. return obj;
  28. }
  29. public void Configure()
  30. {
  31. }
  32. public FunctionPrototype PrototypeObject { get; private set; }
  33. public override object Call(object thisObject, object[] arguments)
  34. {
  35. return Construct(arguments);
  36. }
  37. public ObjectInstance Construct(object[] arguments)
  38. {
  39. var instance = new FunctionShim(Engine, null, Engine.GlobalEnvironment);
  40. instance.Extensible = true;
  41. instance.DefineOwnProperty("constructor", new DataDescriptor(Prototype) { Writable = true, Enumerable = false, Configurable = false }, false);
  42. return instance;
  43. }
  44. /// <summary>
  45. /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2
  46. /// </summary>
  47. /// <param name="functionDeclaration"></param>
  48. /// <returns></returns>
  49. public FunctionInstance CreateFunctionObject(FunctionDeclaration functionDeclaration)
  50. {
  51. var functionObject = new ScriptFunctionInstance(
  52. Engine,
  53. functionDeclaration,
  54. LexicalEnvironment.NewDeclarativeEnvironment(Engine, Engine.ExecutionContext.LexicalEnvironment),
  55. functionDeclaration.Strict
  56. ) { Extensible = true };
  57. return functionObject;
  58. }
  59. private FunctionInstance _throwTypeError;
  60. public FunctionInstance ThrowTypeError
  61. {
  62. get
  63. {
  64. if (_throwTypeError != null)
  65. {
  66. return _throwTypeError;
  67. }
  68. _throwTypeError = new ThrowTypeError(Engine);
  69. return _throwTypeError;
  70. }
  71. }
  72. public object Apply(object thisObject, object[] arguments)
  73. {
  74. if (arguments.Length != 2)
  75. {
  76. throw new ArgumentException("Apply has to be called with two arguments.");
  77. }
  78. var func = thisObject as ICallable;
  79. var thisArg = arguments[0];
  80. var argArray = arguments[1];
  81. if (func == null)
  82. {
  83. throw new JavaScriptException(Engine.TypeError);
  84. }
  85. if (argArray == Null.Instance || argArray == Undefined.Instance)
  86. {
  87. return func.Call(thisArg, Arguments.Empty);
  88. }
  89. var argArrayObj = argArray as ObjectInstance;
  90. if (argArrayObj == null)
  91. {
  92. throw new JavaScriptException(Engine.TypeError);
  93. }
  94. var len = argArrayObj.Get("length");
  95. var n = TypeConverter.ToUint32(len);
  96. var argList = new List<object>();
  97. for (var index = 0; index < n; index++)
  98. {
  99. var indexName = index.ToString();
  100. var nextArg = argArrayObj.Get(indexName);
  101. argList.Add(nextArg);
  102. }
  103. return func.Call(thisArg, argList.ToArray());
  104. }
  105. }
  106. }