MapConstructor.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. Extensible = true,
  25. Prototype = engine.Function.PrototypeObject
  26. };
  27. // The value of the [[Prototype]] internal property of the Map constructor is the Function prototype object
  28. obj.PrototypeObject = MapPrototype.CreatePrototypeObject(engine, obj);
  29. obj._length = new PropertyDescriptor(0, PropertyFlag.Configurable);
  30. // The initial value of Map.prototype is the Map prototype object
  31. obj._prototype = new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden);
  32. return obj;
  33. }
  34. protected override void Initialize()
  35. {
  36. _properties = new StringDictionarySlim<PropertyDescriptor>(2)
  37. {
  38. [GlobalSymbolRegistry.Species._value] = new GetSetPropertyDescriptor(get: new ClrFunctionInstance(_engine, "get [Symbol.species]", Species, 0, PropertyFlag.Configurable), set: Undefined, PropertyFlag.Configurable)
  39. };
  40. }
  41. private static JsValue Species(JsValue thisObject, JsValue[] arguments)
  42. {
  43. return thisObject;
  44. }
  45. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  46. {
  47. if (thisObject.IsUndefined())
  48. {
  49. ExceptionHelper.ThrowTypeError(_engine, "Constructor Map requires 'new'");
  50. }
  51. return Construct(arguments);
  52. }
  53. public ObjectInstance Construct(JsValue[] arguments)
  54. {
  55. var instance = new MapInstance(Engine)
  56. {
  57. Prototype = PrototypeObject,
  58. Extensible = true
  59. };
  60. if (arguments.Length > 0 && !arguments[0].IsNullOrUndefined())
  61. {
  62. var iterator = arguments.At(0).GetIterator(_engine);
  63. var mapProtocol = new MapProtocol(_engine, instance, iterator);
  64. mapProtocol.Execute();
  65. }
  66. return instance;
  67. }
  68. private sealed class MapProtocol : IteratorProtocol
  69. {
  70. private readonly MapInstance _instance;
  71. private readonly ICallable _setter;
  72. public MapProtocol(
  73. Engine engine,
  74. MapInstance instance,
  75. IIterator iterator) : base(engine, iterator, 2)
  76. {
  77. _instance = instance;
  78. var setterProperty = instance.GetProperty("set");
  79. if (setterProperty is null
  80. || !setterProperty.TryGetValue(instance, out var setterValue)
  81. || (_setter = setterValue as ICallable) is null)
  82. {
  83. ExceptionHelper.ThrowTypeError(_engine, "set must be callable");
  84. }
  85. }
  86. protected override void ProcessItem(JsValue[] args, JsValue currentValue)
  87. {
  88. if (!(currentValue is ObjectInstance oi))
  89. {
  90. ExceptionHelper.ThrowTypeError(_engine, "iterator's value must be an object");
  91. return;
  92. }
  93. oi.TryGetValue("0", out var key);
  94. oi.TryGetValue("1", out var value);
  95. args[0] = key;
  96. args[1] = value;
  97. _setter.Call(_instance, args);
  98. }
  99. }
  100. }
  101. }