FunctionPrototype.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #pragma warning disable CA1859 // Use concrete types when possible for improved performance -- most of prototype methods return JsValue
  2. using Jint.Collections;
  3. using Jint.Native.Array;
  4. using Jint.Native.Object;
  5. using Jint.Native.Symbol;
  6. using Jint.Runtime;
  7. using Jint.Runtime.Descriptors;
  8. using Jint.Runtime.Descriptors.Specialized;
  9. using Jint.Runtime.Interop;
  10. namespace Jint.Native.Function;
  11. /// <summary>
  12. /// https://tc39.es/ecma262/#sec-properties-of-the-function-prototype-object
  13. /// </summary>
  14. internal sealed class FunctionPrototype : Function
  15. {
  16. internal FunctionPrototype(
  17. Engine engine,
  18. Realm realm,
  19. ObjectPrototype objectPrototype)
  20. : base(engine, realm, JsString.Empty)
  21. {
  22. _prototype = objectPrototype;
  23. _length = new PropertyDescriptor(JsNumber.PositiveZero, PropertyFlag.Configurable);
  24. }
  25. protected override void Initialize()
  26. {
  27. const PropertyFlag PropertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
  28. const PropertyFlag LengthFlags = PropertyFlag.Configurable;
  29. var properties = new PropertyDictionary(7, checkExistingKeys: false)
  30. {
  31. ["constructor"] = new LazyPropertyDescriptor<FunctionPrototype>(this, static prototype => prototype._realm.Intrinsics.Function, PropertyFlag.NonEnumerable),
  32. ["toString"] = new LazyPropertyDescriptor<FunctionPrototype>(this, static prototype => new ClrFunction(prototype._engine, "toString", prototype.ToString, 0, LengthFlags), PropertyFlags),
  33. ["apply"] = new LazyPropertyDescriptor<FunctionPrototype>(this, static prototype => new ClrFunction(prototype._engine, "apply", prototype.Apply, 2, LengthFlags), PropertyFlags),
  34. ["call"] = new LazyPropertyDescriptor<FunctionPrototype>(this, static prototype => new ClrFunction(prototype._engine, "call", prototype.CallImpl, 1, LengthFlags), PropertyFlags),
  35. ["bind"] = new LazyPropertyDescriptor<FunctionPrototype>(this, static prototype => new ClrFunction(prototype._engine, "bind", prototype.Bind, 1, LengthFlags), PropertyFlags),
  36. ["arguments"] = new GetSetPropertyDescriptor.ThrowerPropertyDescriptor(_engine, PropertyFlag.Configurable),
  37. ["caller"] = new GetSetPropertyDescriptor.ThrowerPropertyDescriptor(_engine, PropertyFlag.Configurable)
  38. };
  39. SetProperties(properties);
  40. var symbols = new SymbolDictionary(1)
  41. {
  42. [GlobalSymbolRegistry.HasInstance] = new PropertyDescriptor(new ClrFunction(_engine, "[Symbol.hasInstance]", HasInstance, 1, PropertyFlag.Configurable), PropertyFlag.AllForbidden)
  43. };
  44. SetSymbols(symbols);
  45. }
  46. /// <summary>
  47. /// https://tc39.es/ecma262/#sec-function.prototype-@@hasinstance
  48. /// </summary>
  49. private static JsValue HasInstance(JsValue thisObject, JsValue[] arguments)
  50. {
  51. return thisObject.OrdinaryHasInstance(arguments.At(0));
  52. }
  53. /// <summary>
  54. /// https://tc39.es/ecma262/#sec-function.prototype.bind
  55. /// </summary>
  56. private JsValue Bind(JsValue thisObject, JsValue[] arguments)
  57. {
  58. if (thisObject is not (ICallable and ObjectInstance oi))
  59. {
  60. ExceptionHelper.ThrowTypeError(_realm, "Bind must be called on a function");
  61. return default;
  62. }
  63. var thisArg = arguments.At(0);
  64. var f = BoundFunctionCreate(oi, thisArg, arguments.Skip(1));
  65. JsNumber l;
  66. var targetHasLength = oi.HasOwnProperty(CommonProperties.Length) == true;
  67. if (targetHasLength)
  68. {
  69. var targetLen = oi.Get(CommonProperties.Length);
  70. if (targetLen is not JsNumber number)
  71. {
  72. l = JsNumber.PositiveZero;
  73. }
  74. else
  75. {
  76. if (number.IsPositiveInfinity())
  77. {
  78. l = number;
  79. }
  80. else if (number.IsNegativeInfinity())
  81. {
  82. l = JsNumber.PositiveZero;
  83. }
  84. else
  85. {
  86. var targetLenAsInt = (long) TypeConverter.ToIntegerOrInfinity(targetLen);
  87. // first argument is target
  88. var argumentsLength = System.Math.Max(0, arguments.Length - 1);
  89. l = JsNumber.Create((ulong) System.Math.Max(targetLenAsInt - argumentsLength, 0));
  90. }
  91. }
  92. }
  93. else
  94. {
  95. l = JsNumber.PositiveZero;
  96. }
  97. f.DefinePropertyOrThrow(CommonProperties.Length, new PropertyDescriptor(l, PropertyFlag.Configurable));
  98. var targetName = oi.Get(CommonProperties.Name);
  99. if (!targetName.IsString())
  100. {
  101. targetName = JsString.Empty;
  102. }
  103. f.SetFunctionName(targetName, "bound");
  104. return f;
  105. }
  106. /// <summary>
  107. /// https://tc39.es/ecma262/#sec-boundfunctioncreate
  108. /// </summary>
  109. private BindFunction BoundFunctionCreate(ObjectInstance targetFunction, JsValue boundThis, JsValue[] boundArgs)
  110. {
  111. var proto = targetFunction.GetPrototypeOf();
  112. var obj = new BindFunction(_engine, _realm, proto, targetFunction, boundThis, boundArgs);
  113. return obj;
  114. }
  115. /// <summary>
  116. /// https://tc39.es/ecma262/#sec-function.prototype.tostring
  117. /// </summary>
  118. private JsValue ToString(JsValue thisObject, JsValue[] arguments)
  119. {
  120. if (thisObject.IsObject() && thisObject.IsCallable)
  121. {
  122. return thisObject.ToString();
  123. }
  124. ExceptionHelper.ThrowTypeError(_realm, "Function.prototype.toString requires that 'this' be a Function");
  125. return null;
  126. }
  127. /// <summary>
  128. /// https://tc39.es/ecma262/#sec-function.prototype.apply
  129. /// </summary>
  130. private JsValue Apply(JsValue thisObject, JsValue[] arguments)
  131. {
  132. var func = thisObject as ICallable;
  133. if (func is null)
  134. {
  135. ExceptionHelper.ThrowTypeError(_realm);
  136. }
  137. var thisArg = arguments.At(0);
  138. var argArray = arguments.At(1);
  139. if (argArray.IsNullOrUndefined())
  140. {
  141. return func.Call(thisArg, Arguments.Empty);
  142. }
  143. var argList = CreateListFromArrayLike(_realm, argArray);
  144. var result = func.Call(thisArg, argList);
  145. return result;
  146. }
  147. /// <summary>
  148. /// https://tc39.es/ecma262/#sec-createlistfromarraylike
  149. /// </summary>
  150. internal static JsValue[] CreateListFromArrayLike(Realm realm, JsValue argArray, Types? elementTypes = null)
  151. {
  152. var argArrayObj = argArray as ObjectInstance;
  153. if (argArrayObj is null)
  154. {
  155. ExceptionHelper.ThrowTypeError(realm);
  156. }
  157. var operations = ArrayOperations.For(argArrayObj, forWrite: false);
  158. var argList = elementTypes is null ? operations.GetAll() : operations.GetAll(elementTypes.Value);
  159. return argList;
  160. }
  161. /// <summary>
  162. /// https://tc39.es/ecma262/#sec-function.prototype.call
  163. /// </summary>
  164. private JsValue CallImpl(JsValue thisObject, JsValue[] arguments)
  165. {
  166. var func = thisObject as ICallable;
  167. if (func is null)
  168. {
  169. ExceptionHelper.ThrowTypeError(_realm);
  170. }
  171. JsValue[] values = System.Array.Empty<JsValue>();
  172. if (arguments.Length > 1)
  173. {
  174. values = new JsValue[arguments.Length - 1];
  175. System.Array.Copy(arguments, 1, values, 0, arguments.Length - 1);
  176. }
  177. var result = func.Call(arguments.At(0), values);
  178. return result;
  179. }
  180. protected internal override JsValue Call(JsValue thisObject, JsValue[] arguments)
  181. {
  182. return Undefined;
  183. }
  184. }