FunctionConstructor.cs 3.8 KB

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