FinalizationRegistry.cpp 3.0 KB

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