FinalizationRegistry.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2021-2022, 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(Realm& realm, JobCallback cleanup_callback, Object& prototype)
  10. : Object(ConstructWithPrototypeTag::Tag, prototype)
  11. , WeakContainer(heap())
  12. , m_realm(realm)
  13. , m_cleanup_callback(move(cleanup_callback))
  14. {
  15. }
  16. void FinalizationRegistry::add_finalization_record(Cell& target, Value held_value, Cell* unregister_token)
  17. {
  18. VERIFY(!held_value.is_empty());
  19. m_records.append({ &target, held_value, unregister_token });
  20. }
  21. // Extracted from FinalizationRegistry.prototype.unregister ( unregisterToken )
  22. bool FinalizationRegistry::remove_by_token(Cell& unregister_token)
  23. {
  24. // 4. Let removed be false.
  25. auto removed = false;
  26. // 5. For each Record { [[WeakRefTarget]], [[HeldValue]], [[UnregisterToken]] } cell of finalizationRegistry.[[Cells]], do
  27. for (auto it = m_records.begin(); it != m_records.end(); ++it) {
  28. // a. If cell.[[UnregisterToken]] is not empty and SameValue(cell.[[UnregisterToken]], unregisterToken) is true, then
  29. if (it->unregister_token == &unregister_token) {
  30. // i. Remove cell from finalizationRegistry.[[Cells]].
  31. it.remove(m_records);
  32. // ii. Set removed to true.
  33. removed = true;
  34. }
  35. }
  36. // 6. Return removed.
  37. return removed;
  38. }
  39. void FinalizationRegistry::remove_dead_cells(Badge<Heap>)
  40. {
  41. auto any_cells_were_removed = false;
  42. for (auto& record : m_records) {
  43. if (!record.target || record.target->state() == Cell::State::Live)
  44. continue;
  45. record.target = nullptr;
  46. any_cells_were_removed = true;
  47. break;
  48. }
  49. if (any_cells_were_removed)
  50. vm().host_enqueue_finalization_registry_cleanup_job(*this);
  51. }
  52. // 9.13 CleanupFinalizationRegistry ( finalizationRegistry ), https://tc39.es/ecma262/#sec-cleanup-finalization-registry
  53. ThrowCompletionOr<void> FinalizationRegistry::cleanup(Optional<JobCallback> callback)
  54. {
  55. auto& vm = this->vm();
  56. // 1. Assert: finalizationRegistry has [[Cells]] and [[CleanupCallback]] internal slots.
  57. // Note: Ensured by type.
  58. // 2. Let callback be finalizationRegistry.[[CleanupCallback]].
  59. auto& cleanup_callback = callback.has_value() ? callback.value() : m_cleanup_callback;
  60. // 3. While finalizationRegistry.[[Cells]] contains a Record cell such that cell.[[WeakRefTarget]] is empty, an implementation may perform the following steps:
  61. for (auto it = m_records.begin(); it != m_records.end(); ++it) {
  62. // a. Choose any such cell.
  63. if (it->target != nullptr)
  64. continue;
  65. // b. Remove cell from finalizationRegistry.[[Cells]].
  66. MarkedVector<Value> arguments(vm.heap());
  67. arguments.append(it->held_value);
  68. it.remove(m_records);
  69. // c. Perform ? HostCallJobCallback(callback, undefined, « cell.[[HeldValue]] »).
  70. TRY(vm.host_call_job_callback(cleanup_callback, js_undefined(), move(arguments)));
  71. }
  72. // 4. Return unused.
  73. return {};
  74. }
  75. void FinalizationRegistry::visit_edges(Cell::Visitor& visitor)
  76. {
  77. Base::visit_edges(visitor);
  78. visitor.visit(m_realm);
  79. for (auto& record : m_records) {
  80. visitor.visit(record.held_value);
  81. visitor.visit(record.unregister_token);
  82. }
  83. }
  84. }