SetConstructor.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using Jint.Native.Array;
  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 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
  62. && !arguments[0].IsUndefined()
  63. && !arguments[0].IsNull())
  64. {
  65. var iterator = arguments.At(0).GetIterator(_engine);
  66. if (iterator != null)
  67. {
  68. var setterProperty = instance.GetProperty("add");
  69. ICallable adder;
  70. if (setterProperty == null
  71. || !setterProperty.TryGetValue(instance, out var setterValue)
  72. || (adder = setterValue as ICallable) == null)
  73. {
  74. ExceptionHelper.ThrowTypeError(_engine, "add must be callable");
  75. return null;
  76. }
  77. var args = _engine._jsValueArrayPool.RentArray(1);
  78. try
  79. {
  80. do
  81. {
  82. var item = iterator.Next();
  83. if (item.TryGetValue("done", out var done) && done.AsBoolean())
  84. {
  85. break;
  86. }
  87. if (!item.TryGetValue("value", out var currentValue))
  88. {
  89. break;
  90. }
  91. args[0] = ExtractValueFromIteratorInstance(currentValue);
  92. adder.Call(instance, args);
  93. } while (true);
  94. }
  95. catch
  96. {
  97. iterator.Return();
  98. throw;
  99. }
  100. finally
  101. {
  102. _engine._jsValueArrayPool.ReturnArray(args);
  103. }
  104. }
  105. }
  106. return instance;
  107. }
  108. private static JsValue ExtractValueFromIteratorInstance(JsValue jsValue)
  109. {
  110. if (jsValue is ArrayInstance ai)
  111. {
  112. uint index = 0;
  113. if (ai.GetLength() > 1)
  114. {
  115. index = 1;
  116. }
  117. ai.TryGetValue(index, out var value);
  118. return value;
  119. }
  120. return jsValue;
  121. }
  122. }
  123. }