FinalizationRegistryPrototype.cs 4.2 KB

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