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.Descriptors.Specialized;
  8. using Jint.Runtime.Interop;
  9. namespace Jint.Native.Set
  10. {
  11. public sealed class SetConstructor : FunctionInstance, IConstructor
  12. {
  13. private static readonly JsString _functionName = new JsString("Set");
  14. private SetConstructor(Engine engine)
  15. : base(engine, _functionName, FunctionThisMode.Global)
  16. {
  17. }
  18. public SetPrototype PrototypeObject { get; private set; }
  19. public static SetConstructor CreateSetConstructor(Engine engine)
  20. {
  21. var obj = new SetConstructor(engine)
  22. {
  23. _prototype = engine.Function.PrototypeObject
  24. };
  25. // The value of the [[Prototype]] internal property of the Set constructor is the Function prototype object
  26. obj.PrototypeObject = SetPrototype.CreatePrototypeObject(engine, obj);
  27. obj._length = new PropertyDescriptor(0, PropertyFlag.Configurable);
  28. // The initial value of Set.prototype is the Set prototype object
  29. obj._prototypeDescriptor = new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden);
  30. return obj;
  31. }
  32. protected override void Initialize()
  33. {
  34. var symbols = new SymbolDictionary(1)
  35. {
  36. [GlobalSymbolRegistry.Species] = new GetSetPropertyDescriptor(get: new ClrFunctionInstance(_engine, "get [Symbol.species]", Species, 0, PropertyFlag.Configurable), set: Undefined, PropertyFlag.Configurable)
  37. };
  38. SetSymbols(symbols);
  39. }
  40. private static JsValue Species(JsValue thisObject, JsValue[] arguments)
  41. {
  42. return thisObject;
  43. }
  44. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  45. {
  46. if (thisObject.IsUndefined())
  47. {
  48. ExceptionHelper.ThrowTypeError(_engine, "Constructor Set requires 'new'");
  49. }
  50. return Construct(arguments, thisObject);
  51. }
  52. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  53. {
  54. var set = new SetInstance(Engine)
  55. {
  56. _prototype = PrototypeObject
  57. };
  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. }