From 81b6e3de040c9675b2fad6e3b7d53989cb0e262e Mon Sep 17 00:00:00 2001 From: Andrey Kosyakov Date: Tue, 16 Apr 2024 23:40:36 +0000 Subject: [PATCH] Get WrapperTypeInfo via ScriptWrappable as opposed to using a dedicated internal field for that. Bug: 328117814 Change-Id: I01f9aff3ad8a41fafbd2655d23f076a0f76fdc57 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5455405 Reviewed-by: Nate Chapin Commit-Queue: Andrey Kosyakov Cr-Commit-Position: refs/heads/main@{#1288405} --- .../modules/v8/v8_context_snapshot_impl.cc | 24 +++++++++++++++---- .../platform/bindings/wrapper_type_info.cc | 13 +++++----- .../platform/bindings/wrapper_type_info.h | 3 --- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/third_party/blink/renderer/bindings/modules/v8/v8_context_snapshot_impl.cc b/third_party/blink/renderer/bindings/modules/v8/v8_context_snapshot_impl.cc index c923c0ab75cd16..0e5881b79ddc9f 100644 --- a/third_party/blink/renderer/bindings/modules/v8/v8_context_snapshot_impl.cc +++ b/third_party/blink/renderer/bindings/modules/v8/v8_context_snapshot_impl.cc @@ -271,6 +271,22 @@ void DeserializeInternalFieldCallback(v8::Local object, } } +namespace { +// We only care for WrapperTypeInfo and do not supply an actual instance of +// the document. Since we need a script wrappable to get type info now, this +// class is a minimal implementation of ScriptWrappable that returns correct +// type info for HTMLDocument. +class DummyHTMLDocumentForSnapshot : public ScriptWrappable { + public: + DummyHTMLDocumentForSnapshot() = default; + + private: + const WrapperTypeInfo* GetWrapperTypeInfo() const override { + return V8HTMLDocument::GetWrapperTypeInfo(); + } +}; +} // namespace + void TakeSnapshotForWorld(v8::SnapshotCreator* snapshot_creator, const DOMWrapperWorld& world) { v8::Isolate* isolate = snapshot_creator->GetIsolate(); @@ -300,11 +316,9 @@ void TakeSnapshotForWorld(v8::SnapshotCreator* snapshot_creator, v8::Local document_wrapper = CreatePlatformObject( isolate, context, world, document_wrapper_type_info); - int indices[] = {kV8DOMWrapperObjectIndex, kV8DOMWrapperTypeIndex}; - void* values[] = {nullptr, - const_cast(document_wrapper_type_info)}; - document_wrapper->SetAlignedPointerInInternalFields(std::size(indices), - indices, values); + V8DOMWrapper::SetNativeInfo( + isolate, document_wrapper, document_wrapper_type_info, + MakeGarbageCollected()); V8PrivateProperty::GetWindowDocumentCachedAccessor(isolate).Set( context->Global(), document_wrapper); diff --git a/third_party/blink/renderer/platform/bindings/wrapper_type_info.cc b/third_party/blink/renderer/platform/bindings/wrapper_type_info.cc index e823d598033a54..f618f00697f1fc 100644 --- a/third_party/blink/renderer/platform/bindings/wrapper_type_info.cc +++ b/third_party/blink/renderer/platform/bindings/wrapper_type_info.cc @@ -57,13 +57,14 @@ v8::Local WrapperTypeInfo::GetV8ClassTemplate( return v8_template; } -const WrapperTypeInfo* ToWrapperTypeInfo( - const v8::TracedReference& wrapper) { - return GetInternalField(wrapper); -} - const WrapperTypeInfo* ToWrapperTypeInfo(v8::Local wrapper) { - return GetInternalField(wrapper); + const auto* wrappable = ToScriptWrappable(wrapper->GetIsolate(), wrapper); + const WrapperTypeInfo* type_info = + wrappable ? wrappable->GetWrapperTypeInfo() : nullptr; + DCHECK_EQ( + type_info, + (GetInternalField(wrapper))); + return type_info; } } // namespace blink diff --git a/third_party/blink/renderer/platform/bindings/wrapper_type_info.h b/third_party/blink/renderer/platform/bindings/wrapper_type_info.h index 6cd34f0a2ace5f..a153137318feca 100644 --- a/third_party/blink/renderer/platform/bindings/wrapper_type_info.h +++ b/third_party/blink/renderer/platform/bindings/wrapper_type_info.h @@ -206,9 +206,6 @@ inline ScriptWrappable* ToScriptWrappable(v8::Isolate* isolate, wrapper); } -PLATFORM_EXPORT const WrapperTypeInfo* ToWrapperTypeInfo( - const v8::TracedReference& wrapper); - PLATFORM_EXPORT const WrapperTypeInfo* ToWrapperTypeInfo( v8::Local wrapper);