MapConstructor.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.Map
  10. {
  11. public sealed class MapConstructor : FunctionInstance, IConstructor
  12. {
  13. private MapConstructor(Engine engine, string name) : base(engine, name, null, null, false)
  14. {
  15. }
  16. public MapPrototype PrototypeObject { get; private set; }
  17. public static MapConstructor CreateMapConstructor(Engine engine)
  18. {
  19. MapConstructor CreateMapConstructorTemplate(string name)
  20. {
  21. var mapConstructor = new MapConstructor(engine, name);
  22. mapConstructor.Extensible = true;
  23. // The value of the [[Prototype]] internal property of the Map constructor is the Function prototype object
  24. mapConstructor.Prototype = engine.Function.PrototypeObject;
  25. mapConstructor.PrototypeObject = MapPrototype.CreatePrototypeObject(engine, mapConstructor);
  26. mapConstructor.SetOwnProperty("length", new PropertyDescriptor(0, PropertyFlag.Configurable));
  27. return mapConstructor;
  28. }
  29. var obj = CreateMapConstructorTemplate("Map");
  30. // The initial value of Map.prototype is the Map 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 Map requires 'new'");
  51. }
  52. return Construct(arguments);
  53. }
  54. public ObjectInstance Construct(JsValue[] arguments)
  55. {
  56. var instance = new MapInstance(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 mapProtocol = new MapProtocol(_engine, instance, iterator);
  65. mapProtocol.Execute();
  66. }
  67. return instance;
  68. }
  69. private sealed class MapProtocol : IteratorProtocol
  70. {
  71. private readonly MapInstance _instance;
  72. private readonly ICallable _setter;
  73. public MapProtocol(
  74. Engine engine,
  75. MapInstance instance,
  76. IIterator iterator) : base(engine, iterator, 2)
  77. {
  78. _instance = instance;
  79. var setterProperty = instance.GetProperty("set");
  80. if (setterProperty is null
  81. || !setterProperty.TryGetValue(instance, out var setterValue)
  82. || (_setter = setterValue as ICallable) is null)
  83. {
  84. ExceptionHelper.ThrowTypeError(_engine, "set must be callable");
  85. }
  86. }
  87. protected override void ProcessItem(JsValue[] args, JsValue currentValue)
  88. {
  89. if (!(currentValue is ObjectInstance oi))
  90. {
  91. ExceptionHelper.ThrowTypeError(_engine, "iterator's value must be an object");
  92. return;
  93. }
  94. oi.TryGetValue("0", out var key);
  95. oi.TryGetValue("1", out var value);
  96. args[0] = key;
  97. args[1] = value;
  98. _setter.Call(_instance, args);
  99. }
  100. }
  101. }
  102. }