SetConstructor.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using Jint.Collections;
  2. using Jint.Native.Function;
  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.Set
  9. {
  10. public sealed class SetConstructor : FunctionInstance, IConstructor
  11. {
  12. private static readonly JsString _functionName = new JsString("Set");
  13. private SetConstructor(Engine engine)
  14. : base(engine, _functionName, FunctionThisMode.Global)
  15. {
  16. }
  17. public SetPrototype PrototypeObject { get; private set; }
  18. public static SetConstructor CreateSetConstructor(Engine engine)
  19. {
  20. var obj = new SetConstructor(engine)
  21. {
  22. _prototype = engine.Function.PrototypeObject
  23. };
  24. // The value of the [[Prototype]] internal property of the Set constructor is the Function prototype object
  25. obj.PrototypeObject = SetPrototype.CreatePrototypeObject(engine, obj);
  26. obj._length = new PropertyDescriptor(0, PropertyFlag.Configurable);
  27. // The initial value of Set.prototype is the Set prototype object
  28. obj._prototypeDescriptor = new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden);
  29. return obj;
  30. }
  31. protected override void Initialize()
  32. {
  33. var symbols = new SymbolDictionary(1)
  34. {
  35. [GlobalSymbolRegistry.Species] = new GetSetPropertyDescriptor(get: new ClrFunctionInstance(_engine, "get [Symbol.species]", Species, 0, PropertyFlag.Configurable), set: Undefined, PropertyFlag.Configurable)
  36. };
  37. SetSymbols(symbols);
  38. }
  39. private static JsValue Species(JsValue thisObject, JsValue[] arguments)
  40. {
  41. return thisObject;
  42. }
  43. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  44. {
  45. ExceptionHelper.ThrowTypeError(_engine, "Constructor Set requires 'new'");
  46. return null;
  47. }
  48. /// <summary>
  49. /// https://tc39.es/ecma262/#sec-set-iterable
  50. /// </summary>
  51. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  52. {
  53. if (newTarget.IsUndefined())
  54. {
  55. ExceptionHelper.ThrowTypeError(_engine);
  56. }
  57. var set = OrdinaryCreateFromConstructor(newTarget, PrototypeObject, static (engine, _) => new SetInstance(engine));
  58. if (arguments.Length > 0 && !arguments[0].IsNullOrUndefined())
  59. {
  60. var adderValue = set.Get("add");
  61. if (!(adderValue is ICallable adder))
  62. {
  63. return ExceptionHelper.ThrowTypeError<ObjectInstance>(_engine, "add must be callable");
  64. }
  65. var iterable = arguments.At(0).GetIterator(_engine);
  66. try
  67. {
  68. var args = new JsValue[1];
  69. do
  70. {
  71. if (!iterable.TryIteratorStep(out var next))
  72. {
  73. return set;
  74. }
  75. next.TryGetValue(CommonProperties.Value, out var nextValue);
  76. args[0] = nextValue;
  77. adder.Call(set, args);
  78. } while (true);
  79. }
  80. catch
  81. {
  82. iterable.Close(CompletionType.Throw);
  83. throw;
  84. }
  85. }
  86. return set;
  87. }
  88. }
  89. }