FinalizationRegistry.cpp 2.9 KB

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