Skip to content

Commit

Permalink
deps: V8: revert de4c0042cbe6 from upstream V8
Browse files Browse the repository at this point in the history
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 <ulan@chromium.org>
    Reviewed-by: Ross McIlroy <rmcilroy@chromium.org>
    Commit-Queue: Ross McIlroy <rmcilroy@chromium.org>
    Auto-Submit: Shu-yu Guo <syg@chromium.org>
    Cr-Commit-Position: refs/heads/master@{#67492}

Refs: v8/v8@de4c004

PR-URL: #34356
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
  • Loading branch information
addaleax authored and MylesBorins committed Jul 16, 2020
1 parent 447b1e8 commit 2c9fd6e
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 1 deletion.
56 changes: 56 additions & 0 deletions deps/v8/include/v8.h
Expand Up @@ -5945,6 +5945,37 @@ class V8_EXPORT RegExp : public Object {
static void CheckCast(Value* obj);
};

/**
* An instance of the built-in FinalizationRegistry constructor.
*
* The C++ name is FinalizationGroup for backwards compatibility. This API is
* experimental and deprecated.
*/
class V8_EXPORT FinalizationGroup : public Object {
public:
/**
* Runs the cleanup callback of the given FinalizationRegistry.
*
* V8 will inform the embedder that there are finalizer callbacks be
* called through HostCleanupFinalizationGroupCallback.
*
* HostCleanupFinalizationGroupCallback should schedule a task to
* call FinalizationGroup::Cleanup() at some point in the
* future. It's the embedders responsiblity to make this call at a
* time which does not interrupt synchronous ECMAScript code
* execution.
*
* If the result is Nothing<bool> then an exception has
* occurred. Otherwise the result is |true| if the cleanup callback
* was called successfully. The result is never |false|.
*/
V8_DEPRECATED(
"FinalizationGroup cleanup is automatic if "
"HostCleanupFinalizationGroupCallback is not set")
static V8_WARN_UNUSED_RESULT Maybe<bool> Cleanup(
Local<FinalizationGroup> finalization_group);
};

/**
* A JavaScript value that wraps a C++ void*. This type of value is mainly used
* to associate C++ data structures with JavaScript objects.
Expand Down Expand Up @@ -7197,6 +7228,20 @@ typedef void (*AddCrashKeyCallback)(CrashKeyId id, const std::string& value);
typedef void (*BeforeCallEnteredCallback)(Isolate*);
typedef void (*CallCompletedCallback)(Isolate*);

/**
* HostCleanupFinalizationGroupCallback is called when we require the
* embedder to enqueue a task that would call
* FinalizationGroup::Cleanup().
*
* The FinalizationGroup is the one for which the embedder needs to
* call FinalizationGroup::Cleanup() on.
*
* The context provided is the one in which the FinalizationGroup was
* created in.
*/
typedef void (*HostCleanupFinalizationGroupCallback)(
Local<Context> context, Local<FinalizationGroup> fg);

/**
* HostImportModuleDynamicallyCallback is called when we require the
* embedder to load a module. This is used as part of the dynamic
Expand Down Expand Up @@ -8536,6 +8581,17 @@ class V8_EXPORT Isolate {
void SetAbortOnUncaughtExceptionCallback(
AbortOnUncaughtExceptionCallback callback);

/**
* This specifies the callback to be called when FinalizationRegistries
* are ready to be cleaned up and require FinalizationGroup::Cleanup()
* to be called in a future task.
*/
V8_DEPRECATED(
"FinalizationRegistry cleanup is automatic if "
"HostCleanupFinalizationGroupCallback is not set")
void SetHostCleanupFinalizationGroupCallback(
HostCleanupFinalizationGroupCallback callback);

/**
* This specifies the callback called by the upcoming dynamic
* import() language feature to load modules.
Expand Down
1 change: 1 addition & 0 deletions deps/v8/src/api/api-inl.h
Expand Up @@ -86,6 +86,7 @@ MAKE_TO_LOCAL(ToLocal, JSArrayBufferView, ArrayBufferView)
MAKE_TO_LOCAL(ToLocal, JSDataView, DataView)
MAKE_TO_LOCAL(ToLocal, JSTypedArray, TypedArray)
MAKE_TO_LOCAL(ToLocalShared, JSArrayBuffer, SharedArrayBuffer)
MAKE_TO_LOCAL(ToLocal, JSFinalizationRegistry, FinalizationGroup)

TYPED_ARRAYS(MAKE_TO_LOCAL_TYPED_ARRAY)

Expand Down
27 changes: 27 additions & 0 deletions deps/v8/src/api/api.cc
Expand Up @@ -8298,6 +8298,33 @@ void Isolate::SetAbortOnUncaughtExceptionCallback(
isolate->SetAbortOnUncaughtExceptionCallback(callback);
}

void Isolate::SetHostCleanupFinalizationGroupCallback(
HostCleanupFinalizationGroupCallback callback) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
isolate->SetHostCleanupFinalizationGroupCallback(callback);
}

Maybe<bool> FinalizationGroup::Cleanup(
Local<FinalizationGroup> finalization_group) {
i::Handle<i::JSFinalizationRegistry> fr =
Utils::OpenHandle(*finalization_group);
i::Isolate* isolate = fr->native_context().GetIsolate();
i::Handle<i::Context> i_context(fr->native_context(), isolate);
Local<Context> context = Utils::ToLocal(i_context);
ENTER_V8(isolate, context, FinalizationGroup, Cleanup, Nothing<bool>(),
i::HandleScope);
i::Handle<i::Object> callback(fr->cleanup(), isolate);
i::Handle<i::Object> argv[] = {callback};
fr->set_scheduled_for_cleanup(false);
has_pending_exception =
i::Execution::CallBuiltin(isolate,
isolate->finalization_registry_cleanup_some(),
fr, arraysize(argv), argv)
.is_null();
RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
return Just(true);
}

void Isolate::SetHostImportModuleDynamicallyCallback(
HostImportModuleDynamicallyCallback callback) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
Expand Down
3 changes: 3 additions & 0 deletions deps/v8/src/api/api.h
Expand Up @@ -94,6 +94,7 @@ class RegisteredExtension {
V(Data, Object) \
V(RegExp, JSRegExp) \
V(Object, JSReceiver) \
V(FinalizationGroup, JSFinalizationRegistry) \
V(Array, JSArray) \
V(Map, JSMap) \
V(Set, JSSet) \
Expand Down Expand Up @@ -206,6 +207,8 @@ class Utils {
v8::internal::Handle<v8::internal::JSTypedArray> obj);
static inline Local<BigUint64Array> ToLocalBigUint64Array(
v8::internal::Handle<v8::internal::JSTypedArray> obj);
static inline Local<FinalizationGroup> ToLocal(
v8::internal::Handle<v8::internal::JSFinalizationRegistry> obj);

static inline Local<SharedArrayBuffer> ToLocalShared(
v8::internal::Handle<v8::internal::JSArrayBuffer> obj);
Expand Down
15 changes: 15 additions & 0 deletions deps/v8/src/execution/isolate.cc
Expand Up @@ -3952,6 +3952,21 @@ MaybeHandle<JSPromise> Isolate::RunHostImportModuleDynamicallyCallback(

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

void Isolate::SetHostCleanupFinalizationGroupCallback(
HostCleanupFinalizationGroupCallback callback) {
host_cleanup_finalization_group_callback_ = callback;
}

void Isolate::RunHostCleanupFinalizationGroupCallback(
Handle<JSFinalizationRegistry> fr) {
if (host_cleanup_finalization_group_callback_ != nullptr) {
v8::Local<v8::Context> api_context =
v8::Utils::ToLocal(handle(Context::cast(fr->native_context()), this));
host_cleanup_finalization_group_callback_(api_context,
v8::Utils::ToLocal(fr));
}
}

void Isolate::SetHostImportModuleDynamicallyCallback(
HostImportModuleDynamicallyCallback callback) {
host_import_module_dynamically_callback_ = callback;
Expand Down
10 changes: 10 additions & 0 deletions deps/v8/src/execution/isolate.h
Expand Up @@ -1416,6 +1416,14 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory {
bool IsInAnyContext(Object object, uint32_t index);

void ClearKeptObjects();
void SetHostCleanupFinalizationGroupCallback(
HostCleanupFinalizationGroupCallback callback);
HostCleanupFinalizationGroupCallback
host_cleanup_finalization_group_callback() const {
return host_cleanup_finalization_group_callback_;
}
void RunHostCleanupFinalizationGroupCallback(
Handle<JSFinalizationRegistry> fr);

void SetHostImportModuleDynamicallyCallback(
HostImportModuleDynamicallyCallback callback);
Expand Down Expand Up @@ -1672,6 +1680,8 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory {
v8::Isolate::AtomicsWaitCallback atomics_wait_callback_ = nullptr;
void* atomics_wait_callback_data_ = nullptr;
PromiseHook promise_hook_ = nullptr;
HostCleanupFinalizationGroupCallback
host_cleanup_finalization_group_callback_ = nullptr;
HostImportModuleDynamicallyCallback host_import_module_dynamically_callback_ =
nullptr;
HostInitializeImportMetaObjectCallback
Expand Down
1 change: 1 addition & 0 deletions deps/v8/src/heap/finalization-registry-cleanup-task.cc
Expand Up @@ -37,6 +37,7 @@ void FinalizationRegistryCleanupTask::SlowAssertNoActiveJavaScript() {

void FinalizationRegistryCleanupTask::RunInternal() {
Isolate* isolate = heap_->isolate();
DCHECK(!isolate->host_cleanup_finalization_group_callback());
SlowAssertNoActiveJavaScript();

TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8",
Expand Down
12 changes: 12 additions & 0 deletions deps/v8/src/heap/heap.cc
Expand Up @@ -1201,6 +1201,16 @@ void Heap::GarbageCollectionEpilogue() {
TRACE_GC(tracer(), GCTracer::Scope::HEAP_EPILOGUE_REDUCE_NEW_SPACE);
ReduceNewSpaceSize();
}

if (FLAG_harmony_weak_refs &&
isolate()->host_cleanup_finalization_group_callback()) {
HandleScope handle_scope(isolate());
Handle<JSFinalizationRegistry> finalization_registry;
while (
DequeueDirtyJSFinalizationRegistry().ToHandle(&finalization_registry)) {
isolate()->RunHostCleanupFinalizationGroupCallback(finalization_registry);
}
}
}

class GCCallbacksScope {
Expand Down Expand Up @@ -6208,6 +6218,7 @@ void Heap::SetInterpreterEntryTrampolineForProfiling(Code code) {
}

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

void Heap::RemoveDirtyFinalizationRegistriesOnContext(NativeContext context) {
if (!FLAG_harmony_weak_refs) return;
if (isolate()->host_cleanup_finalization_group_callback()) return;

DisallowHeapAllocation no_gc;

Expand Down
4 changes: 3 additions & 1 deletion deps/v8/src/heap/mark-compact.cc
Expand Up @@ -2537,7 +2537,9 @@ void MarkCompactCollector::ClearJSWeakRefs() {
RecordSlot(weak_cell, slot, HeapObject::cast(*slot));
}
}
heap()->PostFinalizationRegistryCleanupTaskIfNeeded();
if (!isolate()->host_cleanup_finalization_group_callback()) {
heap()->PostFinalizationRegistryCleanupTaskIfNeeded();
}
}

void MarkCompactCollector::AbortWeakObjects() {
Expand Down
1 change: 1 addition & 0 deletions deps/v8/src/logging/counters.h
Expand Up @@ -772,6 +772,7 @@ class RuntimeCallTimer final {
V(Int8Array_New) \
V(Isolate_DateTimeConfigurationChangeNotification) \
V(Isolate_LocaleConfigurationChangeNotification) \
V(FinalizationGroup_Cleanup) \
V(JSON_Parse) \
V(JSON_Stringify) \
V(Map_AsArray) \
Expand Down

0 comments on commit 2c9fd6e

Please sign in to comment.