WeakMapConstructor.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using Jint.Native.Function;
  2. using Jint.Native.Iterator;
  3. using Jint.Native.Object;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Descriptors;
  6. namespace Jint.Native.WeakMap;
  7. internal sealed class WeakMapConstructor : Constructor
  8. {
  9. private static readonly JsString _functionName = new JsString("WeakMap");
  10. internal WeakMapConstructor(
  11. Engine engine,
  12. Realm realm,
  13. FunctionPrototype prototype,
  14. ObjectPrototype objectPrototype)
  15. : base(engine, realm, _functionName)
  16. {
  17. _prototype = prototype;
  18. PrototypeObject = new WeakMapPrototype(engine, realm, this, objectPrototype);
  19. _length = new PropertyDescriptor(0, PropertyFlag.Configurable);
  20. _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
  21. }
  22. public WeakMapPrototype PrototypeObject { get; }
  23. public override ObjectInstance Construct(JsCallArguments arguments, JsValue newTarget)
  24. {
  25. if (newTarget.IsUndefined())
  26. {
  27. Throw.TypeError(_realm);
  28. }
  29. var map = OrdinaryCreateFromConstructor(
  30. newTarget,
  31. static intrinsics => intrinsics.WeakMap.PrototypeObject,
  32. static (Engine engine, Realm _, object? _) => new JsWeakMap(engine));
  33. if (arguments.Length > 0 && !arguments[0].IsNullOrUndefined())
  34. {
  35. var adder = map.Get("set");
  36. var iterator = arguments.At(0).GetIterator(_realm);
  37. IteratorProtocol.AddEntriesFromIterable(map, iterator, adder);
  38. }
  39. return map;
  40. }
  41. }