FunctionPrototype.cs 8.2 KB

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