FinalizationRegistryPrototype.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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(_constructor, PropertyFlag.NonEnumerable), ["register"] = new(new ClrFunction(Engine, "register", Register, 2, PropertyFlag.Configurable), PropertyFlags), ["unregister"] = new(new ClrFunction(Engine, "unregister", Unregister, 1, PropertyFlag.Configurable), PropertyFlags), ["cleanupSome"] = new(new ClrFunction(Engine, "cleanupSome", CleanupSome, 0, PropertyFlag.Configurable), PropertyFlags),
  29. };
  30. SetProperties(properties);
  31. var symbols = new SymbolDictionary(1) { [GlobalSymbolRegistry.ToStringTag] = new("FinalizationRegistry", PropertyFlag.Configurable) };
  32. SetSymbols(symbols);
  33. }
  34. /// <summary>
  35. /// https://tc39.es/ecma262/#sec-finalization-registry.prototype.register
  36. /// </summary>
  37. private JsValue Register(JsValue thisObject, JsValue[] arguments)
  38. {
  39. var finalizationRegistry = AssertFinalizationRegistryInstance(thisObject);
  40. var target = arguments.At(0);
  41. var heldValue = arguments.At(1);
  42. var unregisterToken = arguments.At(2);
  43. if (!target.CanBeHeldWeakly(_engine.GlobalSymbolRegistry))
  44. {
  45. ExceptionHelper.ThrowTypeError(_realm, "target must be an object or symbol");
  46. }
  47. if (SameValue(target, heldValue))
  48. {
  49. ExceptionHelper.ThrowTypeError(_realm, "target and holdings must not be same");
  50. }
  51. if (!unregisterToken.CanBeHeldWeakly(_engine.GlobalSymbolRegistry))
  52. {
  53. if (!unregisterToken.IsUndefined())
  54. {
  55. ExceptionHelper.ThrowTypeError(_realm, unregisterToken + " must be an object");
  56. }
  57. }
  58. var cell = new Cell(target, heldValue, unregisterToken);
  59. finalizationRegistry.AddCell(cell);
  60. return Undefined;
  61. }
  62. /// <summary>
  63. /// https://tc39.es/ecma262/#sec-finalization-registry.prototype.unregister
  64. /// </summary>
  65. private JsValue Unregister(JsValue thisObject, JsValue[] arguments)
  66. {
  67. var finalizationRegistry = AssertFinalizationRegistryInstance(thisObject);
  68. var unregisterToken = arguments.At(0);
  69. if (!unregisterToken.CanBeHeldWeakly(_engine.GlobalSymbolRegistry))
  70. {
  71. ExceptionHelper.ThrowTypeError(_realm, unregisterToken + " must be an object or symbol");
  72. }
  73. return finalizationRegistry.Remove(unregisterToken);
  74. }
  75. private JsValue CleanupSome(JsValue thisObject, JsValue[] arguments)
  76. {
  77. var finalizationRegistry = AssertFinalizationRegistryInstance(thisObject);
  78. var callback = arguments.At(0);
  79. if (!callback.IsUndefined() && callback is not ICallable)
  80. {
  81. ExceptionHelper.ThrowTypeError(_realm, callback + " must be callable");
  82. }
  83. FinalizationRegistryInstance.CleanupFinalizationRegistry(callback as ICallable);
  84. return Undefined;
  85. }
  86. private FinalizationRegistryInstance AssertFinalizationRegistryInstance(JsValue thisObject)
  87. {
  88. if (thisObject is FinalizationRegistryInstance finalizationRegistryInstance)
  89. {
  90. return finalizationRegistryInstance;
  91. }
  92. ExceptionHelper.ThrowTypeError(_realm, "object must be a FinalizationRegistry");
  93. return default;
  94. }
  95. }