FunctionPrototype.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. private FunctionPrototype(Engine engine)
  16. : base(engine, JsString.Empty)
  17. {
  18. }
  19. public static FunctionPrototype CreatePrototypeObject(Engine engine)
  20. {
  21. var obj = new FunctionPrototype(engine)
  22. {
  23. // The value of the [[Prototype]] internal property of the Function prototype object is the standard built-in Object prototype object
  24. _prototype = engine.Object.PrototypeObject,
  25. _length = PropertyDescriptor.AllForbiddenDescriptor.NumberZero
  26. };
  27. return obj;
  28. }
  29. protected override void Initialize()
  30. {
  31. const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
  32. const PropertyFlag lengthFlags = PropertyFlag.Configurable;
  33. var properties = new PropertyDictionary(7, checkExistingKeys: false)
  34. {
  35. ["constructor"] = new PropertyDescriptor(Engine.Function, PropertyFlag.NonEnumerable),
  36. ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToString, 0, lengthFlags), propertyFlags),
  37. ["apply"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "apply", Apply, 2, lengthFlags), propertyFlags),
  38. ["call"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "call", CallImpl, 1, lengthFlags), propertyFlags),
  39. ["bind"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "bind", Bind, 1, PropertyFlag.AllForbidden), propertyFlags),
  40. ["arguments"] = _engine._callerCalleeArgumentsThrowerConfigurable,
  41. ["caller"] = _engine._callerCalleeArgumentsThrowerConfigurable
  42. };
  43. SetProperties(properties);
  44. var symbols = new SymbolDictionary(1)
  45. {
  46. [GlobalSymbolRegistry.HasInstance] = new PropertyDescriptor(new ClrFunctionInstance(_engine, "[Symbol.hasInstance]", HasInstance, 1, PropertyFlag.Configurable), PropertyFlag.AllForbidden)
  47. };
  48. SetSymbols(symbols);
  49. }
  50. private static JsValue HasInstance(JsValue thisObj, JsValue[] arguments)
  51. {
  52. if (!(thisObj is FunctionInstance f))
  53. {
  54. return false;
  55. }
  56. return f.HasInstance(arguments.At(0));
  57. }
  58. private JsValue Bind(JsValue thisObj, JsValue[] arguments)
  59. {
  60. if (thisObj is not ICallable)
  61. {
  62. ExceptionHelper.ThrowTypeError(Engine, "Bind must be called on a function");
  63. }
  64. var thisArg = arguments.At(0);
  65. var f = BoundFunctionCreate((ObjectInstance) thisObj, thisArg, arguments.Skip(1));
  66. JsNumber l;
  67. var targetHasLength = thisObj.HasOwnProperty(CommonProperties.Length);
  68. if (targetHasLength)
  69. {
  70. var targetLen = thisObj.Get(CommonProperties.Length);
  71. if (!targetLen.IsNumber())
  72. {
  73. l = JsNumber.PositiveZero;
  74. }
  75. else
  76. {
  77. targetLen = TypeConverter.ToInteger(targetLen);
  78. // first argument is target
  79. var argumentsLength = System.Math.Max(0, arguments.Length - 1);
  80. l = JsNumber.Create((uint) System.Math.Max(((JsNumber) targetLen)._value - argumentsLength, 0));
  81. }
  82. }
  83. else
  84. {
  85. l = JsNumber.PositiveZero;
  86. }
  87. f._length = new PropertyDescriptor(l, PropertyFlag.Configurable);
  88. var targetName = thisObj.Get(CommonProperties.Name);
  89. if (!targetName.IsString())
  90. {
  91. targetName = JsString.Empty;
  92. }
  93. f.SetFunctionName(targetName, "bound");
  94. return f;
  95. }
  96. /// <summary>
  97. /// https://tc39.es/ecma262/#sec-boundfunctioncreate
  98. /// </summary>
  99. private FunctionInstance BoundFunctionCreate(ObjectInstance targetFunction, JsValue boundThis, JsValue[] boundArgs)
  100. {
  101. var proto = targetFunction.GetPrototypeOf();
  102. var obj = new BindFunctionInstance(Engine)
  103. {
  104. _prototype = proto,
  105. TargetFunction = targetFunction,
  106. BoundThis = boundThis,
  107. BoundArgs = boundArgs
  108. };
  109. return obj;
  110. }
  111. private JsValue ToString(JsValue thisObj, JsValue[] arguments)
  112. {
  113. if (thisObj.IsObject() && thisObj.IsCallable)
  114. {
  115. return thisObj.ToString();
  116. }
  117. return ExceptionHelper.ThrowTypeError<FunctionInstance>(_engine, "Function.prototype.toString requires that 'this' be a Function");
  118. }
  119. internal JsValue Apply(JsValue thisObject, JsValue[] arguments)
  120. {
  121. var func = thisObject as ICallable ?? ExceptionHelper.ThrowTypeError<ICallable>(Engine);
  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(argArray);
  129. var result = func.Call(thisArg, argList);
  130. return result;
  131. }
  132. internal JsValue[] CreateListFromArrayLike(JsValue argArray, Types? elementTypes = null)
  133. {
  134. var argArrayObj = argArray as ObjectInstance ?? ExceptionHelper.ThrowTypeError<ObjectInstance>(_engine);
  135. var operations = ArrayOperations.For(argArrayObj);
  136. var allowedTypes = elementTypes ??
  137. Types.Undefined | Types.Null | Types.Boolean | Types.String | Types.Symbol | Types.Number | Types.Object;
  138. var argList = operations.GetAll(allowedTypes);
  139. return argList;
  140. }
  141. private JsValue CallImpl(JsValue thisObject, JsValue[] arguments)
  142. {
  143. var func = thisObject as ICallable ?? ExceptionHelper.ThrowTypeError<ICallable>(Engine);
  144. JsValue[] values = System.Array.Empty<JsValue>();
  145. if (arguments.Length > 1)
  146. {
  147. values = new JsValue[arguments.Length - 1];
  148. System.Array.Copy(arguments, 1, values, 0, arguments.Length - 1);
  149. }
  150. var result = func.Call(arguments.At(0), values);
  151. return result;
  152. }
  153. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  154. {
  155. return Undefined;
  156. }
  157. }
  158. }