WeakMapConstructor.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. public sealed class WeakMapConstructor : FunctionInstance, IConstructor
  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 JsValue Call(JsValue thisObject, JsValue[] arguments)
  25. {
  26. ExceptionHelper.ThrowTypeError(_realm, "Constructor WeakMap requires 'new'");
  27. return null;
  28. }
  29. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  30. {
  31. if (newTarget.IsUndefined())
  32. {
  33. ExceptionHelper.ThrowTypeError(_realm);
  34. }
  35. var map = OrdinaryCreateFromConstructor(
  36. newTarget,
  37. static intrinsics => intrinsics.WeakMap.PrototypeObject,
  38. static (engine, realm, _) => new WeakMapInstance(engine));
  39. if (arguments.Length > 0 && !arguments[0].IsNullOrUndefined())
  40. {
  41. var adder = map.Get("set");
  42. var iterator = arguments.At(0).GetIterator(_realm);
  43. IteratorProtocol.AddEntriesFromIterable(map, iterator, adder);
  44. }
  45. return map;
  46. }
  47. }
  48. }