FunctionPrototype.cs 7.4 KB

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