FunctionPrototype.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using Jint.Collections;
  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.Interop;
  8. namespace Jint.Native.Function
  9. {
  10. /// <summary>
  11. /// https://tc39.es/ecma262/#sec-properties-of-the-function-prototype-object
  12. /// </summary>
  13. public sealed class FunctionPrototype : FunctionInstance
  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 PropertyDescriptor(_realm.Intrinsics.Function, PropertyFlag.NonEnumerable),
  31. ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToString, 0, lengthFlags), propertyFlags),
  32. ["apply"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "apply", Apply, 2, lengthFlags), propertyFlags),
  33. ["call"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "call", CallImpl, 1, lengthFlags), propertyFlags),
  34. ["bind"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "bind", Bind, 1, lengthFlags), propertyFlags),
  35. ["arguments"] = _engine._callerCalleeArgumentsThrowerConfigurable,
  36. ["caller"] = _engine._callerCalleeArgumentsThrowerConfigurable
  37. };
  38. SetProperties(properties);
  39. var symbols = new SymbolDictionary(1)
  40. {
  41. [GlobalSymbolRegistry.HasInstance] = new PropertyDescriptor(new ClrFunctionInstance(_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 thisObj, JsValue[] arguments)
  49. {
  50. return thisObj.OrdinaryHasInstance(arguments.At(0));
  51. }
  52. /// <summary>
  53. /// https://tc39.es/ecma262/#sec-function.prototype.bind
  54. /// </summary>
  55. private JsValue Bind(JsValue thisObj, JsValue[] arguments)
  56. {
  57. if (thisObj is not ICallable)
  58. {
  59. ExceptionHelper.ThrowTypeError(_realm, "Bind must be called on a function");
  60. }
  61. var thisArg = arguments.At(0);
  62. var f = BoundFunctionCreate((ObjectInstance) thisObj, thisArg, arguments.Skip(1));
  63. JsNumber l;
  64. var targetHasLength = thisObj.HasOwnProperty(CommonProperties.Length);
  65. if (targetHasLength)
  66. {
  67. var targetLen = thisObj.Get(CommonProperties.Length);
  68. if (targetLen is not JsNumber number)
  69. {
  70. l = JsNumber.PositiveZero;
  71. }
  72. else
  73. {
  74. if (number.IsPositiveInfinity())
  75. {
  76. l = number;
  77. }
  78. else if (number.IsNegativeInfinity())
  79. {
  80. l = JsNumber.PositiveZero;
  81. }
  82. else
  83. {
  84. var targetLenAsInt = (long) TypeConverter.ToIntegerOrInfinity(targetLen);
  85. // first argument is target
  86. var argumentsLength = System.Math.Max(0, arguments.Length - 1);
  87. l = JsNumber.Create((ulong) System.Math.Max(targetLenAsInt - argumentsLength, 0));
  88. }
  89. }
  90. }
  91. else
  92. {
  93. l = JsNumber.PositiveZero;
  94. }
  95. f.DefinePropertyOrThrow(CommonProperties.Length, new PropertyDescriptor(l, PropertyFlag.Configurable));
  96. var targetName = thisObj.Get(CommonProperties.Name);
  97. if (!targetName.IsString())
  98. {
  99. targetName = JsString.Empty;
  100. }
  101. f.SetFunctionName(targetName, "bound");
  102. return f;
  103. }
  104. /// <summary>
  105. /// https://tc39.es/ecma262/#sec-boundfunctioncreate
  106. /// </summary>
  107. private BindFunctionInstance BoundFunctionCreate(ObjectInstance targetFunction, JsValue boundThis, JsValue[] boundArgs)
  108. {
  109. var proto = targetFunction.GetPrototypeOf();
  110. var obj = new BindFunctionInstance(_engine, _realm, proto, targetFunction, boundThis, boundArgs);
  111. return obj;
  112. }
  113. /// <summary>
  114. /// https://tc39.es/ecma262/#sec-function.prototype.tostring
  115. /// </summary>
  116. private JsValue ToString(JsValue thisObj, JsValue[] arguments)
  117. {
  118. if (thisObj.IsObject() && thisObj.IsCallable)
  119. {
  120. return thisObj.ToString();
  121. }
  122. ExceptionHelper.ThrowTypeError(_realm, "Function.prototype.toString requires that 'this' be a Function");
  123. return null;
  124. }
  125. /// <summary>
  126. /// https://tc39.es/ecma262/#sec-function.prototype.apply
  127. /// </summary>
  128. private JsValue Apply(JsValue thisObject, JsValue[] arguments)
  129. {
  130. var func = thisObject as ICallable;
  131. if (func is null)
  132. {
  133. ExceptionHelper.ThrowTypeError(_realm);
  134. }
  135. var thisArg = arguments.At(0);
  136. var argArray = arguments.At(1);
  137. if (argArray.IsNullOrUndefined())
  138. {
  139. return func.Call(thisArg, Arguments.Empty);
  140. }
  141. var argList = CreateListFromArrayLike(_realm, argArray);
  142. var result = func.Call(thisArg, argList);
  143. return result;
  144. }
  145. /// <summary>
  146. /// https://tc39.es/ecma262/#sec-createlistfromarraylike
  147. /// </summary>
  148. internal static JsValue[] CreateListFromArrayLike(Realm realm, JsValue argArray, Types? elementTypes = null)
  149. {
  150. var argArrayObj = argArray as ObjectInstance;
  151. if (argArrayObj is null)
  152. {
  153. ExceptionHelper.ThrowTypeError(realm);
  154. }
  155. var operations = ArrayOperations.For(argArrayObj);
  156. var allowedTypes = elementTypes ??
  157. Types.Undefined | Types.Null | Types.Boolean | Types.String | Types.Symbol | Types.Number | Types.Object;
  158. var argList = operations.GetAll(allowedTypes);
  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. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  181. {
  182. return Undefined;
  183. }
  184. }
  185. }