SetConstructor.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using Jint.Native.Function;
  2. using Jint.Native.Iterator;
  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 SetConstructor(Engine engine, string name) : base(engine, name, null, null, false)
  14. {
  15. }
  16. public SetPrototype PrototypeObject { get; private set; }
  17. public static SetConstructor CreateSetConstructor(Engine engine)
  18. {
  19. SetConstructor CreateSetConstructorTemplate(string name)
  20. {
  21. var ctr = new SetConstructor(engine, name);
  22. ctr.Extensible = true;
  23. // The value of the [[Prototype]] internal property of the Set constructor is the Function prototype object
  24. ctr.Prototype = engine.Function.PrototypeObject;
  25. ctr.PrototypeObject = SetPrototype.CreatePrototypeObject(engine, ctr);
  26. ctr.SetOwnProperty("length", new PropertyDescriptor(0, PropertyFlag.Configurable));
  27. return ctr;
  28. }
  29. var obj = CreateSetConstructorTemplate("Set");
  30. // The initial value of Set.prototype is the Set prototype object
  31. obj.SetOwnProperty("prototype", new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden));
  32. obj.SetOwnProperty(GlobalSymbolRegistry.Species._value,
  33. new GetSetPropertyDescriptor(
  34. get: new ClrFunctionInstance(engine, "get [Symbol.species]", Species, 0, PropertyFlag.Configurable),
  35. set: Undefined,
  36. PropertyFlag.Configurable));
  37. return obj;
  38. }
  39. public void Configure()
  40. {
  41. }
  42. private static JsValue Species(JsValue thisObject, JsValue[] arguments)
  43. {
  44. return thisObject;
  45. }
  46. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  47. {
  48. if (thisObject.IsUndefined())
  49. {
  50. ExceptionHelper.ThrowTypeError(_engine, "Constructor Set requires 'new'");
  51. }
  52. return Construct(arguments);
  53. }
  54. public ObjectInstance Construct(JsValue[] arguments)
  55. {
  56. var instance = new SetInstance(Engine)
  57. {
  58. Prototype = PrototypeObject,
  59. Extensible = true
  60. };
  61. if (arguments.Length > 0 && !arguments[0].IsNullOrUndefined())
  62. {
  63. var iterator = arguments.At(0).GetIterator(_engine);
  64. var protocol = new SetProtocol(_engine, instance, iterator);
  65. protocol.Execute();
  66. }
  67. return instance;
  68. }
  69. private sealed class SetProtocol : IteratorProtocol
  70. {
  71. private readonly SetInstance _instance;
  72. private readonly ICallable _adder;
  73. public SetProtocol(
  74. Engine engine,
  75. SetInstance instance,
  76. IIterator iterator) : base(engine, iterator, 1)
  77. {
  78. _instance = instance;
  79. var setterProperty = instance.GetProperty("add");
  80. if (setterProperty is null
  81. || !setterProperty.TryGetValue(instance, out var setterValue)
  82. || (_adder = setterValue as ICallable) is null)
  83. {
  84. ExceptionHelper.ThrowTypeError(_engine, "add must be callable");
  85. }
  86. }
  87. protected override void ProcessItem(JsValue[] args, JsValue currentValue)
  88. {
  89. args[0] = ExtractValueFromIteratorInstance(currentValue);
  90. _adder.Call(_instance, args);
  91. }
  92. }
  93. }
  94. }