SetConstructor.cs 3.0 KB

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