FinalizationRegistry.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. auto& global_object = this->global_object();
  50. // 1. Assert: finalizationRegistry has [[Cells]] and [[CleanupCallback]] internal slots.
  51. // Note: Ensured by type.
  52. // 2. Let callback be finalizationRegistry.[[CleanupCallback]].
  53. auto& cleanup_callback = callback.has_value() ? callback.value() : m_cleanup_callback;
  54. // 3. While finalizationRegistry.[[Cells]] contains a Record cell such that cell.[[WeakRefTarget]] is empty, an implementation may perform the following steps:
  55. for (auto it = m_records.begin(); it != m_records.end(); ++it) {
  56. // a. Choose any such cell.
  57. if (it->target != nullptr)
  58. continue;
  59. // b. Remove cell from finalizationRegistry.[[Cells]].
  60. MarkedVector<Value> arguments(vm.heap());
  61. arguments.append(it->held_value);
  62. it.remove(m_records);
  63. // c. Perform ? HostCallJobCallback(callback, undefined, « cell.[[HeldValue]] »).
  64. TRY(vm.host_call_job_callback(global_object, cleanup_callback, js_undefined(), move(arguments)));
  65. }
  66. // 4. Return unused.
  67. return {};
  68. }
  69. void FinalizationRegistry::visit_edges(Cell::Visitor& visitor)
  70. {
  71. Base::visit_edges(visitor);
  72. for (auto& record : m_records) {
  73. visitor.visit(record.held_value);
  74. visitor.visit(record.unregister_token);
  75. }
  76. }
  77. }