FunctionPrototype.cs 6.8 KB

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