SetConstructor.cs 3.0 KB

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