WeakMapConstructor.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. {
  8. internal sealed class WeakMapConstructor : Constructor
  9. {
  10. private static readonly JsString _functionName = new JsString("WeakMap");
  11. internal WeakMapConstructor(
  12. Engine engine,
  13. Realm realm,
  14. FunctionPrototype prototype,
  15. ObjectPrototype objectPrototype)
  16. : base(engine, realm, _functionName)
  17. {
  18. _prototype = prototype;
  19. PrototypeObject = new WeakMapPrototype(engine, realm, this, objectPrototype);
  20. _length = new PropertyDescriptor(0, PropertyFlag.Configurable);
  21. _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
  22. }
  23. public WeakMapPrototype PrototypeObject { get; }
  24. public override ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  25. {
  26. if (newTarget.IsUndefined())
  27. {
  28. ExceptionHelper.ThrowTypeError(_realm);
  29. }
  30. var map = OrdinaryCreateFromConstructor(
  31. newTarget,
  32. static intrinsics => intrinsics.WeakMap.PrototypeObject,
  33. static (Engine engine, Realm _, object? _) => new JsWeakMap(engine));
  34. if (arguments.Length > 0 && !arguments[0].IsNullOrUndefined())
  35. {
  36. var adder = map.Get("set");
  37. var iterator = arguments.At(0).GetIterator(_realm);
  38. IteratorProtocol.AddEntriesFromIterable(map, iterator, adder);
  39. }
  40. return map;
  41. }
  42. }
  43. }