Skip to content

Commit 2c9fd6e

Browse files
addaleaxMylesBorins
authored andcommitted
deps: V8: revert de4c0042cbe6 from upstream V8
Original commit message: [weakrefs] Remove deprecated FinalizationGroup V8 API Bug: v8:8179 Change-Id: I16170a197028beb35309b15613004b29a956896c Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2171696 Reviewed-by: Ulan Degenbaev <[email protected]> Reviewed-by: Ross McIlroy <[email protected]> Commit-Queue: Ross McIlroy <[email protected]> Auto-Submit: Shu-yu Guo <[email protected]> Cr-Commit-Position: refs/heads/master@{#67492} Refs: v8/v8@de4c004 PR-URL: #34356 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: Myles Borins <[email protected]>
1 parent 447b1e8 commit 2c9fd6e

File tree

10 files changed

+129
-1
lines changed

10 files changed

+129
-1
lines changed

deps/v8/include/v8.h

+56
Original file line numberDiff line numberDiff line change
@@ -5945,6 +5945,37 @@ class V8_EXPORT RegExp : public Object {
59455945
static void CheckCast(Value* obj);
59465946
};
59475947

5948+
/**
5949+
* An instance of the built-in FinalizationRegistry constructor.
5950+
*
5951+
* The C++ name is FinalizationGroup for backwards compatibility. This API is
5952+
* experimental and deprecated.
5953+
*/
5954+
class V8_EXPORT FinalizationGroup : public Object {
5955+
public:
5956+
/**
5957+
* Runs the cleanup callback of the given FinalizationRegistry.
5958+
*
5959+
* V8 will inform the embedder that there are finalizer callbacks be
5960+
* called through HostCleanupFinalizationGroupCallback.
5961+
*
5962+
* HostCleanupFinalizationGroupCallback should schedule a task to
5963+
* call FinalizationGroup::Cleanup() at some point in the
5964+
* future. It's the embedders responsiblity to make this call at a
5965+
* time which does not interrupt synchronous ECMAScript code
5966+
* execution.
5967+
*
5968+
* If the result is Nothing<bool> then an exception has
5969+
* occurred. Otherwise the result is |true| if the cleanup callback
5970+
* was called successfully. The result is never |false|.
5971+
*/
5972+
V8_DEPRECATED(
5973+
"FinalizationGroup cleanup is automatic if "
5974+
"HostCleanupFinalizationGroupCallback is not set")
5975+
static V8_WARN_UNUSED_RESULT Maybe<bool> Cleanup(
5976+
Local<FinalizationGroup> finalization_group);
5977+
};
5978+
59485979
/**
59495980
* A JavaScript value that wraps a C++ void*. This type of value is mainly used
59505981
* to associate C++ data structures with JavaScript objects.
@@ -7197,6 +7228,20 @@ typedef void (*AddCrashKeyCallback)(CrashKeyId id, const std::string& value);
71977228
typedef void (*BeforeCallEnteredCallback)(Isolate*);
71987229
typedef void (*CallCompletedCallback)(Isolate*);
71997230

7231+
/**
7232+
* HostCleanupFinalizationGroupCallback is called when we require the
7233+
* embedder to enqueue a task that would call
7234+
* FinalizationGroup::Cleanup().
7235+
*
7236+
* The FinalizationGroup is the one for which the embedder needs to
7237+
* call FinalizationGroup::Cleanup() on.
7238+
*
7239+
* The context provided is the one in which the FinalizationGroup was
7240+
* created in.
7241+
*/
7242+
typedef void (*HostCleanupFinalizationGroupCallback)(
7243+
Local<Context> context, Local<FinalizationGroup> fg);
7244+
72007245
/**
72017246
* HostImportModuleDynamicallyCallback is called when we require the
72027247
* embedder to load a module. This is used as part of the dynamic
@@ -8536,6 +8581,17 @@ class V8_EXPORT Isolate {
85368581
void SetAbortOnUncaughtExceptionCallback(
85378582
AbortOnUncaughtExceptionCallback callback);
85388583

8584+
/**
8585+
* This specifies the callback to be called when FinalizationRegistries
8586+
* are ready to be cleaned up and require FinalizationGroup::Cleanup()
8587+
* to be called in a future task.
8588+
*/
8589+
V8_DEPRECATED(
8590+
"FinalizationRegistry cleanup is automatic if "
8591+
"HostCleanupFinalizationGroupCallback is not set")
8592+
void SetHostCleanupFinalizationGroupCallback(
8593+
HostCleanupFinalizationGroupCallback callback);
8594+
85398595
/**
85408596
* This specifies the callback called by the upcoming dynamic
85418597
* import() language feature to load modules.

deps/v8/src/api/api-inl.h

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ MAKE_TO_LOCAL(ToLocal, JSArrayBufferView, ArrayBufferView)
8686
MAKE_TO_LOCAL(ToLocal, JSDataView, DataView)
8787
MAKE_TO_LOCAL(ToLocal, JSTypedArray, TypedArray)
8888
MAKE_TO_LOCAL(ToLocalShared, JSArrayBuffer, SharedArrayBuffer)
89+
MAKE_TO_LOCAL(ToLocal, JSFinalizationRegistry, FinalizationGroup)
8990

9091
TYPED_ARRAYS(MAKE_TO_LOCAL_TYPED_ARRAY)
9192

deps/v8/src/api/api.cc

+27
Original file line numberDiff line numberDiff line change
@@ -8298,6 +8298,33 @@ void Isolate::SetAbortOnUncaughtExceptionCallback(
82988298
isolate->SetAbortOnUncaughtExceptionCallback(callback);
82998299
}
83008300

8301+
void Isolate::SetHostCleanupFinalizationGroupCallback(
8302+
HostCleanupFinalizationGroupCallback callback) {
8303+
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8304+
isolate->SetHostCleanupFinalizationGroupCallback(callback);
8305+
}
8306+
8307+
Maybe<bool> FinalizationGroup::Cleanup(
8308+
Local<FinalizationGroup> finalization_group) {
8309+
i::Handle<i::JSFinalizationRegistry> fr =
8310+
Utils::OpenHandle(*finalization_group);
8311+
i::Isolate* isolate = fr->native_context().GetIsolate();
8312+
i::Handle<i::Context> i_context(fr->native_context(), isolate);
8313+
Local<Context> context = Utils::ToLocal(i_context);
8314+
ENTER_V8(isolate, context, FinalizationGroup, Cleanup, Nothing<bool>(),
8315+
i::HandleScope);
8316+
i::Handle<i::Object> callback(fr->cleanup(), isolate);
8317+
i::Handle<i::Object> argv[] = {callback};
8318+
fr->set_scheduled_for_cleanup(false);
8319+
has_pending_exception =
8320+
i::Execution::CallBuiltin(isolate,
8321+
isolate->finalization_registry_cleanup_some(),
8322+
fr, arraysize(argv), argv)
8323+
.is_null();
8324+
RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
8325+
return Just(true);
8326+
}
8327+
83018328
void Isolate::SetHostImportModuleDynamicallyCallback(
83028329
HostImportModuleDynamicallyCallback callback) {
83038330
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);

deps/v8/src/api/api.h

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class RegisteredExtension {
9494
V(Data, Object) \
9595
V(RegExp, JSRegExp) \
9696
V(Object, JSReceiver) \
97+
V(FinalizationGroup, JSFinalizationRegistry) \
9798
V(Array, JSArray) \
9899
V(Map, JSMap) \
99100
V(Set, JSSet) \
@@ -206,6 +207,8 @@ class Utils {
206207
v8::internal::Handle<v8::internal::JSTypedArray> obj);
207208
static inline Local<BigUint64Array> ToLocalBigUint64Array(
208209
v8::internal::Handle<v8::internal::JSTypedArray> obj);
210+
static inline Local<FinalizationGroup> ToLocal(
211+
v8::internal::Handle<v8::internal::JSFinalizationRegistry> obj);
209212

210213
static inline Local<SharedArrayBuffer> ToLocalShared(
211214
v8::internal::Handle<v8::internal::JSArrayBuffer> obj);

deps/v8/src/execution/isolate.cc

+15
Original file line numberDiff line numberDiff line change
@@ -3952,6 +3952,21 @@ MaybeHandle<JSPromise> Isolate::RunHostImportModuleDynamicallyCallback(
39523952

39533953
void Isolate::ClearKeptObjects() { heap()->ClearKeptObjects(); }
39543954

3955+
void Isolate::SetHostCleanupFinalizationGroupCallback(
3956+
HostCleanupFinalizationGroupCallback callback) {
3957+
host_cleanup_finalization_group_callback_ = callback;
3958+
}
3959+
3960+
void Isolate::RunHostCleanupFinalizationGroupCallback(
3961+
Handle<JSFinalizationRegistry> fr) {
3962+
if (host_cleanup_finalization_group_callback_ != nullptr) {
3963+
v8::Local<v8::Context> api_context =
3964+
v8::Utils::ToLocal(handle(Context::cast(fr->native_context()), this));
3965+
host_cleanup_finalization_group_callback_(api_context,
3966+
v8::Utils::ToLocal(fr));
3967+
}
3968+
}
3969+
39553970
void Isolate::SetHostImportModuleDynamicallyCallback(
39563971
HostImportModuleDynamicallyCallback callback) {
39573972
host_import_module_dynamically_callback_ = callback;

deps/v8/src/execution/isolate.h

+10
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,14 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory {
14161416
bool IsInAnyContext(Object object, uint32_t index);
14171417

14181418
void ClearKeptObjects();
1419+
void SetHostCleanupFinalizationGroupCallback(
1420+
HostCleanupFinalizationGroupCallback callback);
1421+
HostCleanupFinalizationGroupCallback
1422+
host_cleanup_finalization_group_callback() const {
1423+
return host_cleanup_finalization_group_callback_;
1424+
}
1425+
void RunHostCleanupFinalizationGroupCallback(
1426+
Handle<JSFinalizationRegistry> fr);
14191427

14201428
void SetHostImportModuleDynamicallyCallback(
14211429
HostImportModuleDynamicallyCallback callback);
@@ -1672,6 +1680,8 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory {
16721680
v8::Isolate::AtomicsWaitCallback atomics_wait_callback_ = nullptr;
16731681
void* atomics_wait_callback_data_ = nullptr;
16741682
PromiseHook promise_hook_ = nullptr;
1683+
HostCleanupFinalizationGroupCallback
1684+
host_cleanup_finalization_group_callback_ = nullptr;
16751685
HostImportModuleDynamicallyCallback host_import_module_dynamically_callback_ =
16761686
nullptr;
16771687
HostInitializeImportMetaObjectCallback

deps/v8/src/heap/finalization-registry-cleanup-task.cc

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void FinalizationRegistryCleanupTask::SlowAssertNoActiveJavaScript() {
3737

3838
void FinalizationRegistryCleanupTask::RunInternal() {
3939
Isolate* isolate = heap_->isolate();
40+
DCHECK(!isolate->host_cleanup_finalization_group_callback());
4041
SlowAssertNoActiveJavaScript();
4142

4243
TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8",

deps/v8/src/heap/heap.cc

+12
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,16 @@ void Heap::GarbageCollectionEpilogue() {
12011201
TRACE_GC(tracer(), GCTracer::Scope::HEAP_EPILOGUE_REDUCE_NEW_SPACE);
12021202
ReduceNewSpaceSize();
12031203
}
1204+
1205+
if (FLAG_harmony_weak_refs &&
1206+
isolate()->host_cleanup_finalization_group_callback()) {
1207+
HandleScope handle_scope(isolate());
1208+
Handle<JSFinalizationRegistry> finalization_registry;
1209+
while (
1210+
DequeueDirtyJSFinalizationRegistry().ToHandle(&finalization_registry)) {
1211+
isolate()->RunHostCleanupFinalizationGroupCallback(finalization_registry);
1212+
}
1213+
}
12041214
}
12051215

12061216
class GCCallbacksScope {
@@ -6208,6 +6218,7 @@ void Heap::SetInterpreterEntryTrampolineForProfiling(Code code) {
62086218
}
62096219

62106220
void Heap::PostFinalizationRegistryCleanupTaskIfNeeded() {
6221+
DCHECK(!isolate()->host_cleanup_finalization_group_callback());
62116222
// Only one cleanup task is posted at a time.
62126223
if (!HasDirtyJSFinalizationRegistries() ||
62136224
is_finalization_registry_cleanup_task_posted_) {
@@ -6267,6 +6278,7 @@ MaybeHandle<JSFinalizationRegistry> Heap::DequeueDirtyJSFinalizationRegistry() {
62676278

62686279
void Heap::RemoveDirtyFinalizationRegistriesOnContext(NativeContext context) {
62696280
if (!FLAG_harmony_weak_refs) return;
6281+
if (isolate()->host_cleanup_finalization_group_callback()) return;
62706282

62716283
DisallowHeapAllocation no_gc;
62726284

deps/v8/src/heap/mark-compact.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -2537,7 +2537,9 @@ void MarkCompactCollector::ClearJSWeakRefs() {
25372537
RecordSlot(weak_cell, slot, HeapObject::cast(*slot));
25382538
}
25392539
}
2540-
heap()->PostFinalizationRegistryCleanupTaskIfNeeded();
2540+
if (!isolate()->host_cleanup_finalization_group_callback()) {
2541+
heap()->PostFinalizationRegistryCleanupTaskIfNeeded();
2542+
}
25412543
}
25422544

25432545
void MarkCompactCollector::AbortWeakObjects() {

deps/v8/src/logging/counters.h

+1
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,7 @@ class RuntimeCallTimer final {
772772
V(Int8Array_New) \
773773
V(Isolate_DateTimeConfigurationChangeNotification) \
774774
V(Isolate_LocaleConfigurationChangeNotification) \
775+
V(FinalizationGroup_Cleanup) \
775776
V(JSON_Parse) \
776777
V(JSON_Stringify) \
777778
V(Map_AsArray) \

0 commit comments

Comments
 (0)