SetConstructor.cs 3.4 KB

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