FinalizationRegistryPrototype.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Jint.Collections;
  2. using Jint.Native.Object;
  3. using Jint.Native.Symbol;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Descriptors;
  6. using Jint.Runtime.Interop;
  7. namespace Jint.Native.FinalizationRegistry;
  8. /// <summary>
  9. /// https://tc39.es/ecma262/#sec-properties-of-the-finalization-registry-prototype-object
  10. /// </summary>
  11. internal sealed class FinalizationRegistryPrototype : Prototype
  12. {
  13. private readonly FinalizationRegistryConstructor _constructor;
  14. public FinalizationRegistryPrototype(
  15. Engine engine,
  16. Realm realm,
  17. FinalizationRegistryConstructor constructor,
  18. ObjectPrototype objectPrototype) : base(engine, realm)
  19. {
  20. _constructor = constructor;
  21. _prototype = objectPrototype;
  22. }
  23. protected override void Initialize()
  24. {
  25. const PropertyFlag PropertyFlags = PropertyFlag.NonEnumerable;
  26. var properties = new PropertyDictionary(4, checkExistingKeys: false)
  27. {
  28. [KnownKeys.Constructor] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable),
  29. ["register"] = new PropertyDescriptor(new ClrFunction(Engine, "register", Register, 2, PropertyFlag.Configurable), PropertyFlags),
  30. ["unregister"] = new PropertyDescriptor(new ClrFunction(Engine, "unregister", Unregister, 1, PropertyFlag.Configurable), PropertyFlags),
  31. };
  32. SetProperties(properties);
  33. var symbols = new SymbolDictionary(1) { [GlobalSymbolRegistry.ToStringTag] = new("FinalizationRegistry", PropertyFlag.Configurable) };
  34. SetSymbols(symbols);
  35. }
  36. /// <summary>
  37. /// https://tc39.es/ecma262/#sec-finalization-registry.prototype.register
  38. /// </summary>
  39. private JsValue Register(JsValue thisObject, JsCallArguments arguments)
  40. {
  41. var finalizationRegistry = AssertFinalizationRegistryInstance(thisObject);
  42. var target = arguments.At(0);
  43. var heldValue = arguments.At(1);
  44. var unregisterToken = arguments.At(2);
  45. if (!target.CanBeHeldWeakly(_engine.GlobalSymbolRegistry))
  46. {
  47. ExceptionHelper.ThrowTypeError(_realm, "target must be an object or symbol");
  48. }
  49. if (SameValue(target, heldValue))
  50. {
  51. ExceptionHelper.ThrowTypeError(_realm, "target and holdings must not be same");
  52. }
  53. if (!unregisterToken.CanBeHeldWeakly(_engine.GlobalSymbolRegistry))
  54. {
  55. if (!unregisterToken.IsUndefined())
  56. {
  57. ExceptionHelper.ThrowTypeError(_realm, unregisterToken + " must be an object");
  58. }
  59. }
  60. var cell = new Cell(target, heldValue, unregisterToken);
  61. finalizationRegistry.AddCell(cell);
  62. return Undefined;
  63. }
  64. /// <summary>
  65. /// https://tc39.es/ecma262/#sec-finalization-registry.prototype.unregister
  66. /// </summary>
  67. private JsValue Unregister(JsValue thisObject, JsCallArguments arguments)
  68. {
  69. var finalizationRegistry = AssertFinalizationRegistryInstance(thisObject);
  70. var unregisterToken = arguments.At(0);
  71. if (!unregisterToken.CanBeHeldWeakly(_engine.GlobalSymbolRegistry))
  72. {
  73. ExceptionHelper.ThrowTypeError(_realm, unregisterToken + " must be an object or symbol");
  74. }
  75. return finalizationRegistry.Remove(unregisterToken);
  76. }
  77. private FinalizationRegistryInstance AssertFinalizationRegistryInstance(JsValue thisObject)
  78. {
  79. if (thisObject is FinalizationRegistryInstance finalizationRegistryInstance)
  80. {
  81. return finalizationRegistryInstance;
  82. }
  83. ExceptionHelper.ThrowTypeError(_realm, "object must be a FinalizationRegistry");
  84. return default;
  85. }
  86. }