FunctionConstructor.cs 4.0 KB

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