revert_api_delete_deprecated_attachcppheap_and_detachcppheap.patch 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Charles Kerr <[email protected]>
  3. Date: Thu, 6 Mar 2025 14:31:19 -0600
  4. Subject: Revert "[api] Delete deprecated AttachCppHeap and DetachCppHeap"
  5. Restore this API because Node.js needs it.
  6. This patch can be removed after an upstream fix lands in Node.js,
  7. e.g. in https://github.com/nodejs/node-v8/tree/canary
  8. diff --git a/include/v8-isolate.h b/include/v8-isolate.h
  9. index 97f1030dd2ca47ca4b58ac64e2e11e615bc46130..24ef6b5e0af63179e557b9896134838e112c59db 100644
  10. --- a/include/v8-isolate.h
  11. +++ b/include/v8-isolate.h
  12. @@ -1172,6 +1172,28 @@ class V8_EXPORT Isolate {
  13. */
  14. void SetEmbedderRootsHandler(EmbedderRootsHandler* handler);
  15. + /**
  16. + * Attaches a managed C++ heap as an extension to the JavaScript heap. The
  17. + * embedder maintains ownership of the CppHeap. At most one C++ heap can be
  18. + * attached to V8.
  19. + *
  20. + * Multi-threaded use requires the use of v8::Locker/v8::Unlocker, see
  21. + * CppHeap.
  22. + *
  23. + * If a CppHeap is set via CreateParams, then this call is a noop.
  24. + */
  25. + V8_DEPRECATED("Set the heap on Isolate creation using CreateParams instead.")
  26. + void AttachCppHeap(CppHeap*);
  27. +
  28. + /**
  29. + * Detaches a managed C++ heap if one was attached using `AttachCppHeap()`.
  30. + *
  31. + * If a CppHeap is set via CreateParams, then this call is a noop.
  32. + */
  33. + V8_DEPRECATED(
  34. + "The CppHeap gets detached automatically during Isolate tear down.")
  35. + void DetachCppHeap();
  36. +
  37. using ReleaseCppHeapCallback = void (*)(std::unique_ptr<CppHeap>);
  38. /**
  39. @@ -1219,7 +1241,6 @@ class V8_EXPORT Isolate {
  40. class V8_DEPRECATED("AtomicsWaitWakeHandle is unused and will be removed.")
  41. #endif
  42. V8_EXPORT AtomicsWaitWakeHandle {
  43. -
  44. public:
  45. /**
  46. * Stop this `Atomics.wait()` call and call the |AtomicsWaitCallback|
  47. diff --git a/src/api/api.cc b/src/api/api.cc
  48. index 64044e9cf44d401c249787feafb651688ee0d9f9..1677e54b188b6a1699370d8cff37d2acf2933f38 100644
  49. --- a/src/api/api.cc
  50. +++ b/src/api/api.cc
  51. @@ -9876,6 +9876,16 @@ void Isolate::SetEmbedderRootsHandler(EmbedderRootsHandler* handler) {
  52. i_isolate->heap()->SetEmbedderRootsHandler(handler);
  53. }
  54. +void Isolate::AttachCppHeap(CppHeap* cpp_heap) {
  55. + i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(this);
  56. + i_isolate->heap()->AttachCppHeap(cpp_heap);
  57. +}
  58. +
  59. +void Isolate::DetachCppHeap() {
  60. + i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(this);
  61. + i_isolate->heap()->DetachCppHeap();
  62. +}
  63. +
  64. CppHeap* Isolate::GetCppHeap() const {
  65. const i::Isolate* i_isolate = reinterpret_cast<const i::Isolate*>(this);
  66. return i_isolate->heap()->cpp_heap();
  67. diff --git a/src/heap/cppgc-js/cpp-heap.cc b/src/heap/cppgc-js/cpp-heap.cc
  68. index e033791ca1cdeba4a304e69b922d4169a22f9caa..706f81f7bbc1b5a7a1b73afe018b0b2c0184d9ef 100644
  69. --- a/src/heap/cppgc-js/cpp-heap.cc
  70. +++ b/src/heap/cppgc-js/cpp-heap.cc
  71. @@ -513,6 +513,11 @@ CppHeap::CppHeap(
  72. }
  73. CppHeap::~CppHeap() {
  74. + if (isolate_) {
  75. + // TODO(ahaas): Delete this code once `v8::Isolate::DetachCppHeap` has been
  76. + // deleted.
  77. + isolate_->heap()->DetachCppHeap();
  78. + }
  79. Terminate();
  80. }
  81. diff --git a/src/heap/heap.cc b/src/heap/heap.cc
  82. index da9d8810b307e94f01238e56532a0ff93f1ff325..252a1b354110764c6351119d41a4adddca0c2913 100644
  83. --- a/src/heap/heap.cc
  84. +++ b/src/heap/heap.cc
  85. @@ -6056,6 +6056,21 @@ void Heap::AttachCppHeap(v8::CppHeap* cpp_heap) {
  86. cpp_heap_ = cpp_heap;
  87. }
  88. +void Heap::DetachCppHeap() {
  89. + // The API function should be a noop in case a CppHeap was passed on Isolate
  90. + // creation.
  91. + if (owning_cpp_heap_) {
  92. + return;
  93. + }
  94. +
  95. + // The CppHeap may have been detached already.
  96. + if (!cpp_heap_) return;
  97. +
  98. + CppHeap::From(cpp_heap_)->StartDetachingIsolate();
  99. + CppHeap::From(cpp_heap_)->DetachIsolate();
  100. + cpp_heap_ = nullptr;
  101. +}
  102. +
  103. std::optional<StackState> Heap::overridden_stack_state() const {
  104. if (!embedder_stack_state_origin_) return {};
  105. return embedder_stack_state_;
  106. diff --git a/src/heap/heap.h b/src/heap/heap.h
  107. index 570cb682903cbead5f8f80573290b13ab1d81183..bccdb6c1bdb8fbbc6cc5aee0e54105f210ca2ab9 100644
  108. --- a/src/heap/heap.h
  109. +++ b/src/heap/heap.h
  110. @@ -1104,6 +1104,9 @@ class Heap final {
  111. // Unified heap (C++) support. ===============================================
  112. // ===========================================================================
  113. + V8_EXPORT_PRIVATE void AttachCppHeap(v8::CppHeap* cpp_heap);
  114. + V8_EXPORT_PRIVATE void DetachCppHeap();
  115. +
  116. v8::CppHeap* cpp_heap() const { return cpp_heap_; }
  117. std::optional<StackState> overridden_stack_state() const;
  118. @@ -1645,8 +1648,6 @@ class Heap final {
  119. private:
  120. class AllocationTrackerForDebugging;
  121. - void AttachCppHeap(v8::CppHeap* cpp_heap);
  122. -
  123. using ExternalStringTableUpdaterCallback =
  124. Tagged<String> (*)(Heap* heap, FullObjectSlot pointer);