FinalizationRegistryConstructor.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 : FunctionInstance, IConstructor
  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, JsValue[] arguments)
  25. {
  26. return Construct(arguments, thisObject);
  27. }
  28. ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget) => Construct(arguments, newTarget);
  29. private ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  30. {
  31. if (newTarget.IsUndefined())
  32. {
  33. ExceptionHelper.ThrowTypeError(_realm);
  34. }
  35. var cleanupCallback = arguments.At(0);
  36. if (cleanupCallback is not ICallable callable)
  37. {
  38. ExceptionHelper.ThrowTypeError(_realm, "cleanup must be callable");
  39. return null;
  40. }
  41. var finalizationRegistry = OrdinaryCreateFromConstructor(
  42. newTarget,
  43. static intrinsics => intrinsics.FinalizationRegistry.PrototypeObject,
  44. (engine, realm, state) => new FinalizationRegistryInstance(engine, realm, state!),
  45. callable
  46. );
  47. return finalizationRegistry;
  48. }
  49. }