FinalizationRegistryConstructor.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using Jint.Native.Function;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. namespace Jint.Native.FinalizationRegistry;
  6. /// <summary>
  7. /// https://tc39.es/ecma262/#sec-finalization-registry-constructor
  8. /// </summary>
  9. internal sealed class FinalizationRegistryConstructor : Constructor
  10. {
  11. private static readonly JsString _functionName = new("FinalizationRegistry");
  12. public FinalizationRegistryConstructor(
  13. Engine engine,
  14. Realm realm,
  15. FunctionConstructor functionConstructor,
  16. ObjectPrototype objectPrototype) : base(engine, realm, _functionName)
  17. {
  18. PrototypeObject = new FinalizationRegistryPrototype(engine, realm, this, objectPrototype);
  19. _prototype = functionConstructor.PrototypeObject;
  20. _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
  21. _length = new PropertyDescriptor(JsNumber.PositiveOne, PropertyFlag.Configurable);
  22. }
  23. public FinalizationRegistryPrototype PrototypeObject { get; }
  24. protected internal override JsValue Call(JsValue thisObject, JsCallArguments arguments)
  25. {
  26. return Construct(arguments, thisObject);
  27. }
  28. public override ObjectInstance Construct(JsCallArguments arguments, JsValue newTarget)
  29. {
  30. if (newTarget.IsUndefined())
  31. {
  32. Throw.TypeError(_realm);
  33. }
  34. var cleanupCallback = arguments.At(0);
  35. if (cleanupCallback is not ICallable callable)
  36. {
  37. Throw.TypeError(_realm, "cleanup must be callable");
  38. return null;
  39. }
  40. var finalizationRegistry = OrdinaryCreateFromConstructor(
  41. newTarget,
  42. static intrinsics => intrinsics.FinalizationRegistry.PrototypeObject,
  43. (engine, realm, state) => new FinalizationRegistryInstance(engine, realm, state!),
  44. callable
  45. );
  46. return finalizationRegistry;
  47. }
  48. }