2
0

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