MapConstructor.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.Interop;
  9. namespace Jint.Native.Map
  10. {
  11. public sealed class MapConstructor : FunctionInstance, IConstructor
  12. {
  13. private static readonly JsString _functionName = new JsString("Map");
  14. private MapConstructor(Engine engine)
  15. : base(engine, _functionName)
  16. {
  17. }
  18. public MapPrototype PrototypeObject { get; private set; }
  19. public static MapConstructor CreateMapConstructor(Engine engine)
  20. {
  21. var obj = new MapConstructor(engine)
  22. {
  23. _prototype = engine.Function.PrototypeObject
  24. };
  25. // The value of the [[Prototype]] internal property of the Map constructor is the Function prototype object
  26. obj.PrototypeObject = MapPrototype.CreatePrototypeObject(engine, obj);
  27. obj._length = new PropertyDescriptor(0, PropertyFlag.Configurable);
  28. // The initial value of Map.prototype is the Map prototype object
  29. obj._prototypeDescriptor = new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden);
  30. return obj;
  31. }
  32. protected override void Initialize()
  33. {
  34. var symbols = new SymbolDictionary(1)
  35. {
  36. [GlobalSymbolRegistry.Species] = new GetSetPropertyDescriptor(get: new ClrFunctionInstance(_engine, "get [Symbol.species]", Species, 0, PropertyFlag.Configurable), set: Undefined, PropertyFlag.Configurable)
  37. };
  38. SetSymbols(symbols);
  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 map = new MapInstance(Engine)
  55. {
  56. _prototype = PrototypeObject
  57. };
  58. if (arguments.Length > 0 && !arguments[0].IsNullOrUndefined())
  59. {
  60. var adder = map.Get("set");
  61. var iterator = arguments.At(0).GetIterator(_engine);
  62. IteratorProtocol.AddEntriesFromIterable(map, iterator, adder);
  63. }
  64. return map;
  65. }
  66. }
  67. }