FinalizationRegistry.cpp 3.5 KB

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