FinalizationRegistry.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/FinalizationRegistry.h>
  8. namespace JS {
  9. FinalizationRegistry* FinalizationRegistry::create(GlobalObject& global_object, FunctionObject& cleanup_callback)
  10. {
  11. return global_object.heap().allocate<FinalizationRegistry>(global_object, cleanup_callback, *global_object.finalization_registry_prototype());
  12. }
  13. FinalizationRegistry::FinalizationRegistry(FunctionObject& cleanup_callback, Object& prototype)
  14. : Object(prototype)
  15. , WeakContainer(heap())
  16. , m_cleanup_callback(&cleanup_callback)
  17. {
  18. }
  19. FinalizationRegistry::~FinalizationRegistry()
  20. {
  21. }
  22. void FinalizationRegistry::add_finalization_record(Cell& target, Value held_value, Object* unregister_token)
  23. {
  24. VERIFY(!held_value.is_empty());
  25. m_records.append({ &target, held_value, unregister_token });
  26. }
  27. bool FinalizationRegistry::remove_by_token(Object& unregister_token)
  28. {
  29. auto removed = false;
  30. for (auto it = m_records.begin(); it != m_records.end(); ++it) {
  31. if (it->unregister_token == &unregister_token) {
  32. it.remove(m_records);
  33. removed = true;
  34. }
  35. }
  36. return removed;
  37. }
  38. void FinalizationRegistry::remove_dead_cells(Badge<Heap>)
  39. {
  40. auto any_cells_were_removed = false;
  41. for (auto& record : m_records) {
  42. if (!record.target || record.target->state() == Cell::State::Live)
  43. continue;
  44. record.target = nullptr;
  45. any_cells_were_removed = true;
  46. break;
  47. }
  48. if (any_cells_were_removed)
  49. vm().enqueue_finalization_registry_cleanup_job(*this);
  50. }
  51. // 9.13 CleanupFinalizationRegistry ( finalizationRegistry ), https://tc39.es/ecma262/#sec-cleanup-finalization-registry
  52. ThrowCompletionOr<void> FinalizationRegistry::cleanup(FunctionObject* callback)
  53. {
  54. // 1. Assert: finalizationRegistry has [[Cells]] and [[CleanupCallback]] internal slots.
  55. // Note: Ensured by type.
  56. // 2. Let callback be finalizationRegistry.[[CleanupCallback]].
  57. auto cleanup_callback = callback ?: m_cleanup_callback;
  58. // 3. While finalizationRegistry.[[Cells]] contains a Record cell such that cell.[[WeakRefTarget]] is empty, an implementation may perform the following steps:
  59. for (auto it = m_records.begin(); it != m_records.end(); ++it) {
  60. // a. Choose any such cell.
  61. if (it->target != nullptr)
  62. continue;
  63. auto cell = *it;
  64. // b. Remove cell from finalizationRegistry.[[Cells]].
  65. it.remove(m_records);
  66. // c. Perform ? HostCallJobCallback(callback, undefined, « cell.[[HeldValue]] »).
  67. (void)TRY(call(global_object(), *cleanup_callback, js_undefined(), cell.held_value));
  68. }
  69. // 4. Return NormalCompletion(empty).
  70. return {};
  71. }
  72. void FinalizationRegistry::visit_edges(Cell::Visitor& visitor)
  73. {
  74. Base::visit_edges(visitor);
  75. visitor.visit(m_cleanup_callback);
  76. for (auto& record : m_records) {
  77. visitor.visit(record.held_value);
  78. visitor.visit(record.unregister_token);
  79. }
  80. }
  81. }