FunctionPrototype.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using Jint.Native.Object;
  2. using Jint.Runtime;
  3. using Jint.Runtime.Descriptors;
  4. using Jint.Runtime.Descriptors.Specialized;
  5. using Jint.Runtime.Interop;
  6. namespace Jint.Native.Function
  7. {
  8. /// <summary>
  9. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4
  10. /// </summary>
  11. public sealed class FunctionPrototype : FunctionInstance
  12. {
  13. private FunctionPrototype(Engine engine)
  14. : base(engine, "Function", null, null, false)
  15. {
  16. }
  17. public static FunctionPrototype CreatePrototypeObject(Engine engine)
  18. {
  19. var obj = new FunctionPrototype(engine);
  20. obj.Extensible = true;
  21. // The value of the [[Prototype]] internal property of the Function prototype object is the standard built-in Object prototype object
  22. obj.Prototype = engine.Object.PrototypeObject;
  23. obj.SetOwnProperty("length", new PropertyDescriptor(0, PropertyFlag.AllForbidden));
  24. return obj;
  25. }
  26. public void Configure()
  27. {
  28. SetOwnProperty("constructor", new PropertyDescriptor(Engine.Function, PropertyFlag.NonEnumerable));
  29. FastAddProperty("toString", new ClrFunctionInstance(Engine, "toString", ToString), true, false, true);
  30. FastAddProperty("apply", new ClrFunctionInstance(Engine, "apply", Apply, 2), true, false, true);
  31. FastAddProperty("call", new ClrFunctionInstance(Engine, "call", CallImpl, 1), true, false, true);
  32. FastAddProperty("bind", new ClrFunctionInstance(Engine, "bind", Bind, 1), true, false, true);
  33. }
  34. private JsValue Bind(JsValue thisObj, JsValue[] arguments)
  35. {
  36. var target = thisObj.TryCast<ICallable>(x =>
  37. {
  38. ExceptionHelper.ThrowTypeError(Engine);
  39. });
  40. var thisArg = arguments.At(0);
  41. var f = new BindFunctionInstance(Engine) {Extensible = true};
  42. f.TargetFunction = thisObj;
  43. f.BoundThis = thisArg;
  44. f.BoundArgs = arguments.Skip(1);
  45. f.Prototype = Engine.Function.PrototypeObject;
  46. var o = target as FunctionInstance;
  47. if (!ReferenceEquals(o, null))
  48. {
  49. var l = TypeConverter.ToNumber(o.Get("length")) - (arguments.Length - 1);
  50. f.SetOwnProperty("length", new PropertyDescriptor(System.Math.Max(l, 0), PropertyFlag.AllForbidden));
  51. }
  52. else
  53. {
  54. f.SetOwnProperty("length", new PropertyDescriptor(0, PropertyFlag.AllForbidden));
  55. }
  56. var thrower = Engine.Function.ThrowTypeError;
  57. const PropertyFlag flags = PropertyFlag.EnumerableSet | PropertyFlag.ConfigurableSet;
  58. f.DefineOwnProperty("caller", new GetSetPropertyDescriptor(thrower, thrower, flags), false);
  59. f.DefineOwnProperty("arguments", new GetSetPropertyDescriptor(thrower, thrower, flags), false);
  60. return f;
  61. }
  62. private JsValue ToString(JsValue thisObj, JsValue[] arguments)
  63. {
  64. var func = thisObj.TryCast<FunctionInstance>();
  65. if (ReferenceEquals(func, null))
  66. {
  67. ExceptionHelper.ThrowTypeError(_engine, "Function object expected.");
  68. }
  69. return "function() {{ ... }}";
  70. }
  71. public JsValue Apply(JsValue thisObject, JsValue[] arguments)
  72. {
  73. var func = thisObject.TryCast<ICallable>();
  74. var thisArg = arguments.At(0);
  75. var argArray = arguments.At(1);
  76. if (func is null)
  77. {
  78. return ExceptionHelper.ThrowTypeError<JsValue>(Engine);
  79. }
  80. if (argArray.IsNullOrUndefined())
  81. {
  82. return func.Call(thisArg, Arguments.Empty);
  83. }
  84. var argArrayObj = argArray.TryCast<ObjectInstance>();
  85. if (argArrayObj is null)
  86. {
  87. return ExceptionHelper.ThrowTypeError<JsValue>(Engine);
  88. }
  89. var len = ((JsNumber) argArrayObj.Get("length"))._value;
  90. uint n = TypeConverter.ToUint32(len);
  91. var argList = _engine._jsValueArrayPool.RentArray((int) n);
  92. for (int index = 0; index < n; index++)
  93. {
  94. string indexName = TypeConverter.ToString(index);
  95. var nextArg = argArrayObj.Get(indexName);
  96. argList[index] = nextArg;
  97. }
  98. var result = func.Call(thisArg, argList);
  99. _engine._jsValueArrayPool.ReturnArray(argList);
  100. return result;
  101. }
  102. public JsValue CallImpl(JsValue thisObject, JsValue[] arguments)
  103. {
  104. var func = thisObject.TryCast<ICallable>();
  105. if (func is null)
  106. {
  107. return ExceptionHelper.ThrowTypeError<JsValue>(Engine);
  108. }
  109. return func.Call(arguments.At(0), arguments.Length == 0 ? arguments : arguments.Skip(1));
  110. }
  111. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  112. {
  113. return Undefined;
  114. }
  115. }
  116. }