MapConstructor.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using Jint.Collections;
  2. using Jint.Native.Function;
  3. using Jint.Native.Iterator;
  4. using Jint.Native.Object;
  5. using Jint.Native.Symbol;
  6. using Jint.Runtime;
  7. using Jint.Runtime.Descriptors;
  8. using Jint.Runtime.Descriptors.Specialized;
  9. using Jint.Runtime.Interop;
  10. namespace Jint.Native.Map
  11. {
  12. public sealed class MapConstructor : FunctionInstance, IConstructor
  13. {
  14. private static readonly JsString _functionName = new JsString("Map");
  15. private MapConstructor(Engine engine)
  16. : base(engine, _functionName, strict: false)
  17. {
  18. }
  19. public MapPrototype PrototypeObject { get; private set; }
  20. public static MapConstructor CreateMapConstructor(Engine engine)
  21. {
  22. var obj = new MapConstructor(engine)
  23. {
  24. _prototype = engine.Function.PrototypeObject
  25. };
  26. // The value of the [[Prototype]] internal property of the Map constructor is the Function prototype object
  27. obj.PrototypeObject = MapPrototype.CreatePrototypeObject(engine, obj);
  28. obj._length = new PropertyDescriptor(0, PropertyFlag.Configurable);
  29. // The initial value of Map.prototype is the Map prototype object
  30. obj._prototypeDescriptor = new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden);
  31. return obj;
  32. }
  33. protected override void Initialize()
  34. {
  35. _properties = new StringDictionarySlim<PropertyDescriptor>(2)
  36. {
  37. [GlobalSymbolRegistry.Species] = new GetSetPropertyDescriptor(get: new ClrFunctionInstance(_engine, "get [Symbol.species]", Species, 0, PropertyFlag.Configurable), set: Undefined, PropertyFlag.Configurable)
  38. };
  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 Map requires 'new'");
  49. }
  50. return Construct(arguments, thisObject);
  51. }
  52. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  53. {
  54. var instance = new MapInstance(Engine)
  55. {
  56. _prototype = PrototypeObject
  57. };
  58. if (arguments.Length > 0 && !arguments[0].IsNullOrUndefined())
  59. {
  60. var iterator = arguments.At(0).GetIterator(_engine);
  61. var mapProtocol = new MapProtocol(_engine, instance, iterator);
  62. mapProtocol.Execute();
  63. }
  64. return instance;
  65. }
  66. private sealed class MapProtocol : IteratorProtocol
  67. {
  68. private readonly MapInstance _instance;
  69. private readonly ICallable _setter;
  70. public MapProtocol(
  71. Engine engine,
  72. MapInstance instance,
  73. IIterator iterator) : base(engine, iterator, 2)
  74. {
  75. _instance = instance;
  76. var setterProperty = instance.GetProperty("set");
  77. if (setterProperty is null
  78. || !setterProperty.TryGetValue(instance, out var setterValue)
  79. || (_setter = setterValue as ICallable) is null)
  80. {
  81. ExceptionHelper.ThrowTypeError(_engine, "set must be callable");
  82. }
  83. }
  84. protected override void ProcessItem(JsValue[] args, JsValue currentValue)
  85. {
  86. if (!(currentValue is ObjectInstance oi))
  87. {
  88. ExceptionHelper.ThrowTypeError(_engine, "iterator's value must be an object");
  89. return;
  90. }
  91. oi.TryGetValue("0", out var key);
  92. oi.TryGetValue("1", out var value);
  93. args[0] = key;
  94. args[1] = value;
  95. _setter.Call(_instance, args);
  96. }
  97. }
  98. }
  99. }