FunctionPrototype.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4
  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. private static JsValue HasInstance(JsValue thisObj, JsValue[] arguments)
  46. {
  47. if (thisObj is not FunctionInstance f)
  48. {
  49. return false;
  50. }
  51. return f.OrdinaryHasInstance(arguments.At(0));
  52. }
  53. private JsValue Bind(JsValue thisObj, JsValue[] arguments)
  54. {
  55. if (thisObj is not ICallable)
  56. {
  57. ExceptionHelper.ThrowTypeError(_realm, "Bind must be called on a function");
  58. }
  59. var thisArg = arguments.At(0);
  60. var f = BoundFunctionCreate((ObjectInstance) thisObj, thisArg, arguments.Skip(1));
  61. JsNumber l;
  62. var targetHasLength = thisObj.HasOwnProperty(CommonProperties.Length);
  63. if (targetHasLength)
  64. {
  65. var targetLen = thisObj.Get(CommonProperties.Length);
  66. if (!targetLen.IsNumber())
  67. {
  68. l = JsNumber.PositiveZero;
  69. }
  70. else
  71. {
  72. targetLen = TypeConverter.ToInteger(targetLen);
  73. // first argument is target
  74. var argumentsLength = System.Math.Max(0, arguments.Length - 1);
  75. l = JsNumber.Create((uint) System.Math.Max(((JsNumber) targetLen)._value - argumentsLength, 0));
  76. }
  77. }
  78. else
  79. {
  80. l = JsNumber.PositiveZero;
  81. }
  82. f._length = new PropertyDescriptor(l, PropertyFlag.Configurable);
  83. var targetName = thisObj.Get(CommonProperties.Name);
  84. if (!targetName.IsString())
  85. {
  86. targetName = JsString.Empty;
  87. }
  88. f.SetFunctionName(targetName, "bound");
  89. return f;
  90. }
  91. /// <summary>
  92. /// https://tc39.es/ecma262/#sec-boundfunctioncreate
  93. /// </summary>
  94. private FunctionInstance BoundFunctionCreate(ObjectInstance targetFunction, JsValue boundThis, JsValue[] boundArgs)
  95. {
  96. var proto = targetFunction.GetPrototypeOf();
  97. var obj = new BindFunctionInstance(_engine, _realm)
  98. {
  99. _prototype = proto,
  100. TargetFunction = targetFunction,
  101. BoundThis = boundThis,
  102. BoundArgs = boundArgs
  103. };
  104. return obj;
  105. }
  106. private JsValue ToString(JsValue thisObj, JsValue[] arguments)
  107. {
  108. if (thisObj.IsObject() && thisObj.IsCallable)
  109. {
  110. return thisObj.ToString();
  111. }
  112. ExceptionHelper.ThrowTypeError(_realm, "Function.prototype.toString requires that 'this' be a Function");
  113. return null;
  114. }
  115. internal JsValue Apply(JsValue thisObject, JsValue[] arguments)
  116. {
  117. var func = thisObject as ICallable;
  118. if (func is null)
  119. {
  120. ExceptionHelper.ThrowTypeError(_realm);
  121. }
  122. var thisArg = arguments.At(0);
  123. var argArray = arguments.At(1);
  124. if (argArray.IsNullOrUndefined())
  125. {
  126. return func.Call(thisArg, Arguments.Empty);
  127. }
  128. var argList = CreateListFromArrayLike(_realm, argArray);
  129. var result = func.Call(thisArg, argList);
  130. return result;
  131. }
  132. /// <summary>
  133. /// https://tc39.es/ecma262/#sec-createlistfromarraylike
  134. /// </summary>
  135. internal static JsValue[] CreateListFromArrayLike(Realm realm, JsValue argArray, Types? elementTypes = null)
  136. {
  137. var argArrayObj = argArray as ObjectInstance;
  138. if (argArrayObj is null)
  139. {
  140. ExceptionHelper.ThrowTypeError(realm);
  141. }
  142. var operations = ArrayOperations.For(argArrayObj);
  143. var allowedTypes = elementTypes ??
  144. Types.Undefined | Types.Null | Types.Boolean | Types.String | Types.Symbol | Types.Number | Types.Object;
  145. var argList = operations.GetAll(allowedTypes);
  146. return argList;
  147. }
  148. private JsValue CallImpl(JsValue thisObject, JsValue[] arguments)
  149. {
  150. var func = thisObject as ICallable;
  151. if (func is null)
  152. {
  153. ExceptionHelper.ThrowTypeError(_realm);
  154. }
  155. JsValue[] values = System.Array.Empty<JsValue>();
  156. if (arguments.Length > 1)
  157. {
  158. values = new JsValue[arguments.Length - 1];
  159. System.Array.Copy(arguments, 1, values, 0, arguments.Length - 1);
  160. }
  161. var result = func.Call(arguments.At(0), values);
  162. return result;
  163. }
  164. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  165. {
  166. return Undefined;
  167. }
  168. }
  169. }